[Mulgara-svn] r1913 - trunk/tools/src/org/mulgara/tools

alexhall at mulgara.org alexhall at mulgara.org
Fri Feb 5 20:53:13 UTC 2010


Author: alexhall
Date: 2010-02-05 12:53:12 -0800 (Fri, 05 Feb 2010)
New Revision: 1913

Modified:
   trunk/tools/src/org/mulgara/tools/Sparql.java
Log:
Add a shutdown hook to cancel the query if it's still running when the user exits via Ctrl-C.

Modified: trunk/tools/src/org/mulgara/tools/Sparql.java
===================================================================
--- trunk/tools/src/org/mulgara/tools/Sparql.java	2010-02-05 20:51:08 UTC (rev 1912)
+++ trunk/tools/src/org/mulgara/tools/Sparql.java	2010-02-05 20:53:12 UTC (rev 1913)
@@ -38,34 +38,30 @@
 
   /** A URI for the default graph in Mulgara. This one is always empty. */
   private static final URI EMPTY_GRAPH = URI.create(Mulgara.NULL_GRAPH);
+  
+  private final Iterator<String> queryStrings;
+  private final ConnectionFactory factory;
+  private Connection connection = null;
+  
+  private Sparql(Iterator<String> queryStrings) {
+    this.queryStrings = queryStrings;
+    this.factory = new ConnectionFactory();
+  }
 
-  /**
-   * Run a set of SPARQL queries against a local server.
-   * @param args A list of filenames containing the queries to run.
-   * @throws Exception Any kind of exception is just displayed on stderr, without any handling.
-   */
-  public static void main(String[] args) throws Exception {
-    // set up basic logging
-    logConfig();
+  void runQueries() throws Exception {
+    connection = factory.newConnection(HOST);
 
-    // Connect to the server
-    ConnectionFactory factory = new ConnectionFactory();
-    Connection conn = factory.newConnection(HOST);
-
-    // get the list of queries
-    Iterator<String> queryStrings;
-    if (args.length == 0) queryStrings = getStdIn();
-    else queryStrings = getFiles(args);
-
     // iterate over all the query strings
     while (queryStrings.hasNext()) {
       // parse the string into a Query object
       SparqlInterpreter interpreter = new SparqlInterpreter();
       interpreter.setDefaultGraphUri(EMPTY_GRAPH);
-      Query query = interpreter.parseQuery(queryStrings.next());
+      String queryStr = queryStrings.next();
+      Query query = interpreter.parseQuery(queryStr);
 
+      System.out.println("Executing query:\n" + queryStr);
       // execute the query, and get back the answer
-      Answer a = conn.execute(query);
+      Answer a = connection.execute(query);
       // print the results
       System.out.println("Result: " + query.getResultMessage());
       printAnswer(a);
@@ -75,9 +71,49 @@
     }
 
     // clean up the server connection
-    conn.dispose();
+    connection.dispose();
+    connection = null;
   }
+  
+  void cancel() throws Exception {
+    if (connection != null) {
+      System.out.println("Canceling query...");
+      connection.cancel();
+      connection.dispose();
+    }
+  }
+  
+  /**
+   * Run a set of SPARQL queries against a local server.
+   * @param args A list of filenames containing the queries to run.
+   * @throws Exception Any kind of exception is just displayed on stderr, without any handling.
+   */
+  public static void main(String[] args) throws Exception {
+    // set up basic logging
+    logConfig();
 
+    // get the list of queries
+    Iterator<String> queryStrings;
+    if (args.length == 0) queryStrings = getStdIn();
+    else queryStrings = getFiles(args);
+    
+    final Sparql sparql = new Sparql(queryStrings);
+    
+    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
+      @Override
+      public void run() {
+        try {
+          sparql.cancel();
+        } catch (Exception e) {
+          System.err.println("Error canceling query");
+          e.printStackTrace();
+        }
+      }
+    }));
+    
+    sparql.runQueries();
+  }
+
   /**
    * Iterate through an Answer and print each of the lines to STDOUT.
    * @param a The answer to print.




More information about the Mulgara-svn mailing list