Author: andy
Date: Tue Jun 24 14:39:05 2014
New Revision: 1605097

URL: http://svn.apache.org/r1605097
Log:
JENA-725 : QueryExecution extends AutoCloseable
JENA-727 : Add QueryExecution.isClosed

Modified:
    jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/QueryExecution.java
    
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/QueryExecutionBase.java
    
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/ResultSetCheckCondition.java
    
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
    jena/trunk/jena-sdb/pom.xml
    
jena/trunk/jena-sdb/src/test/java/com/hp/hpl/jena/sdb/test/junit/QueryTestSDB.java

Modified: 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/QueryExecution.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/QueryExecution.java?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/QueryExecution.java 
(original)
+++ jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/query/QueryExecution.java 
Tue Jun 24 14:39:05 2014
@@ -28,7 +28,7 @@ import com.hp.hpl.jena.sparql.util.Conte
 /** A interface for a single execution of a query. */
 
 
-public interface QueryExecution
+public interface QueryExecution extends AutoCloseable 
 {
     /** Set the initial association of variables and values.
      * May not be supported by all QueryExecution implementations.
@@ -143,23 +143,31 @@ public interface QueryExecution
        public void abort();
        
     /** Close the query execution and stop query evaluation as soon as 
convenient.
+     *  QueryExecution objects, and a {@linkplain ResultSet} from {@linkplain 
#execSelect},
+     *  can not be used once the QueryExecution is closed.  
+     *  Model results from {@linkplain #execConstruct} and {@linkplain 
#execDescribe}
+     *  are stil valid.
      *  It is important to close query execution objects in order to release
      *  resources such as working memory and to stop the query execution.
      *  Some storage subsystems require explicit ends of operations and this
      *  operation will cause those to be called where necessary.
      *  No operations on the query execution or any associated
      *  result set are permitted after this call.
-     *  This method should not be called in parallel with other methods on the
-     *  QueryExecution object.
      */
-       public void close();
+    @Override
+    public void close();
        
-       /** Set a timeout on the query execution.
+    /**
+     * Answer whether this QueryExecution object has been closed or not.
+     * @return boolean 
+     */
+    public boolean isClosed();
+
+    /** Set a timeout on the query execution.
         * Processing will be aborted after the timeout (which starts when the 
approprate exec call is made).
         * Not all query execution systems support timeouts.
         * A timeout of less than zero means no timeout.
         */
-       
        public void setTimeout(long timeout, TimeUnit timeoutUnits) ;
        
        /** Set time, in milliseconds 

Modified: 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/QueryExecutionBase.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/QueryExecutionBase.java?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/QueryExecutionBase.java
 (original)
+++ 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/QueryExecutionBase.java
 Tue Jun 24 14:39:05 2014
@@ -67,6 +67,7 @@ public class QueryExecutionBase implemen
     
     // Set if QueryIterator.cancel has been called 
     private volatile boolean    isCancelled = false ;
+    private boolean closed ;
     private volatile TimeoutCallback expectedCallback = null ;    
     private TimeoutCallback timeout1Callback = null ;
     private TimeoutCallback timeout2Callback = null ;
@@ -76,8 +77,7 @@ public class QueryExecutionBase implemen
     private static final long   TIMEOUT_INF = -2 ;
     private long                timeout1 = TIMEOUT_UNSET ;
     private long                timeout2 = TIMEOUT_UNSET ;
-    private final AlarmClock    alarmClock = AlarmClock.get() ;  
-
+    private final AlarmClock    alarmClock = AlarmClock.get() ;
     public QueryExecutionBase(Query query, 
                               Dataset dataset,
                               Context context,
@@ -137,6 +137,7 @@ public class QueryExecutionBase implemen
     @Override
     public void close()
     {
+        closed = true ;
         if ( queryIterator != null )
             queryIterator.close() ;
         if ( plan != null )
@@ -148,6 +149,16 @@ public class QueryExecutionBase implemen
     }
 
     @Override
+    public boolean isClosed() {
+        return closed ;
+    }
+
+    private void checkNotClosed() {
+        if ( closed )
+            throw new QueryExecException("HTTP QueryExecution has been 
closed") ;
+    }
+
+    @Override
     public void abort()
     {
         synchronized(lockTimeout)
@@ -166,12 +177,14 @@ public class QueryExecutionBase implemen
     @Override
     public ResultSet execSelect()
     {
+        checkNotClosed() ;
         if ( ! query.isSelectType() )
             throw new QueryExecException("Attempt to have ResultSet from a 
"+labelForQuery(query)+" query") ; 
-        return execResultSet() ;
+        ResultSet rs = execResultSet() ;
+        return new ResultSetCheckCondition(rs, this) ;
     }
 
-    // Construct
+     // Construct
     @Override
     public Model execConstruct()
     {
@@ -192,6 +205,7 @@ public class QueryExecutionBase implemen
     @Override
     public Model execConstruct(Model model)
     {
+        checkNotClosed() ;
         try
         {
             Iterator<Triple> it = execConstructTriples();
@@ -217,6 +231,7 @@ public class QueryExecutionBase implemen
     @Override
     public Iterator<Triple> execConstructTriples()
     {
+        checkNotClosed() ;
         if ( ! query.isConstructType() )
             throw new QueryExecException("Attempt to get a CONSTRUCT model 
from a "+labelForQuery(query)+" query") ;
         // This causes there to be no PROJECT around the pattern.
@@ -237,6 +252,7 @@ public class QueryExecutionBase implemen
     @Override
     public Model execDescribe(Model model)
     {
+        checkNotClosed() ;
         if ( ! query.isDescribeType() )
             throw new QueryExecException("Attempt to get a DESCRIBE result 
from a "+labelForQuery(query)+" query") ; 
         //Was: query.setQueryResultStar(true) ; but why?
@@ -318,6 +334,7 @@ public class QueryExecutionBase implemen
     @Override
     public boolean execAsk()
     {
+        checkNotClosed() ;
         if ( ! query.isAskType() )
             throw new QueryExecException("Attempt to have boolean from a 
"+labelForQuery(query)+" query") ; 
 

Modified: 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/ResultSetCheckCondition.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/ResultSetCheckCondition.java?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/ResultSetCheckCondition.java
 (original)
+++ 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/ResultSetCheckCondition.java
 Tue Jun 24 14:39:05 2014
@@ -46,10 +46,10 @@ public class ResultSetCheckCondition imp
     }
 
     // Feel free to replace with a lambda expression for Java8!
-    private static Condition checkQExec(QueryExecution qExec) {
+    private static Condition checkQExec(final QueryExecution qExec) {
         return new Condition() {
             @Override
-            public boolean check() { return true ; } // ! qExec.isClosed()  
+            public boolean check() { return ! qExec.isClosed() ; }  
         } ;
     }
 
@@ -103,7 +103,7 @@ public class ResultSetCheckCondition imp
     
     private final void check() {
         if ( ! condition.check()  ) {
-            throw new ARQException("ResultSet no longer valid") ;    
+            throw new ARQException("ResultSet no longer valid (QueryExecution 
has been closed)") ;    
         }
     }
 

Modified: 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
 (original)
+++ 
jena/trunk/jena-arq/src/main/java/com/hp/hpl/jena/sparql/engine/http/QueryEngineHTTP.java
 Tue Jun 24 14:39:05 2014
@@ -39,6 +39,7 @@ import com.hp.hpl.jena.graph.Triple ;
 import com.hp.hpl.jena.query.* ;
 import com.hp.hpl.jena.rdf.model.Model ;
 import com.hp.hpl.jena.sparql.ARQException ;
+import com.hp.hpl.jena.sparql.engine.ResultSetCheckCondition ;
 import com.hp.hpl.jena.sparql.graph.GraphFactory ;
 import com.hp.hpl.jena.sparql.resultset.CSVInput ;
 import com.hp.hpl.jena.sparql.resultset.JSONInput ;
@@ -68,7 +69,7 @@ public class QueryEngineHTTP implements 
     private List<String> namedGraphURIs = new ArrayList<>();
     private HttpAuthenticator authenticator;
 
-    private boolean finished = false;
+    private boolean closed = false;
 
     // Timeouts
     private long connectTimeout = -1;
@@ -333,6 +334,13 @@ public class QueryEngineHTTP implements 
 
     @Override
     public ResultSet execSelect() {
+        checkNotClosed() ;
+        ResultSet rs = execResultSetInner() ;
+        return new ResultSetCheckCondition(rs, this) ;
+    }
+    
+   private ResultSet execResultSetInner() {
+        
         HttpQuery httpQuery = makeHttpQuery();
         httpQuery.setAccept(selectContentType);
         InputStream in = httpQuery.exec();
@@ -400,6 +408,7 @@ public class QueryEngineHTTP implements 
     }
 
     private Model execModel(Model model) {
+        checkNotClosed() ;
         HttpQuery httpQuery = makeHttpQuery();
         httpQuery.setAccept(modelContentType);
         InputStream in = httpQuery.exec();
@@ -426,6 +435,7 @@ public class QueryEngineHTTP implements 
     }
 
     private Iterator<Triple> execTriples() {
+        checkNotClosed() ;
         HttpQuery httpQuery = makeHttpQuery();
         httpQuery.setAccept(modelContentType);
         InputStream in = httpQuery.exec();
@@ -452,6 +462,7 @@ public class QueryEngineHTTP implements 
 
     @Override
     public boolean execAsk() {
+        checkNotClosed() ;
         HttpQuery httpQuery = makeHttpQuery();
         httpQuery.setAccept(askContentType);
         InputStream in = httpQuery.exec();
@@ -489,6 +500,11 @@ public class QueryEngineHTTP implements 
         }
     }
 
+    private void checkNotClosed() {
+        if ( closed )
+            throw new QueryExecException("HTTP QueryExecution has been 
closed") ;
+    }
+
     @Override
     public Context getContext() {
         return context;
@@ -570,7 +586,7 @@ public class QueryEngineHTTP implements 
     }
 
     private HttpQuery makeHttpQuery() {
-        if (finished)
+        if (closed)
             throw new ARQException("HTTP execution already closed");
 
         HttpQuery httpQuery = new HttpQuery(service);
@@ -635,7 +651,7 @@ public class QueryEngineHTTP implements 
      * Cancel query evaluation
      */
     public void cancel() {
-        finished = true;
+        closed = true;
     }
 
     @Override
@@ -649,7 +665,7 @@ public class QueryEngineHTTP implements 
 
     @Override
     public void close() {
-        finished = true;
+        closed = true ;
         if (retainedConnection != null) {
             try {
                 retainedConnection.close();
@@ -670,7 +686,8 @@ public class QueryEngineHTTP implements 
         }
     }
 
-    // public boolean isActive() { return false ; }
+    @Override
+    public boolean isClosed() { return closed ; }
 
     @Override
     public String toString() {

Modified: jena/trunk/jena-sdb/pom.xml
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-sdb/pom.xml?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- jena/trunk/jena-sdb/pom.xml (original)
+++ jena/trunk/jena-sdb/pom.xml Tue Jun 24 14:39:05 2014
@@ -115,11 +115,6 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-compiler-plugin</artifactId>
-        <configuration>
-          <encoding>UTF-8</encoding>
-          <source>1.6</source>
-          <target>1.6</target>
-        </configuration>
       </plugin>
 
       <plugin>
@@ -170,29 +165,6 @@
         </configuration>
       </plugin>
 
-      <!--
-      <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
-        <artifactId>maven-assembly-plugin</artifactId>
-       <configuration>
-         <tarLongFileMode>gnu</tarLongFileMode>    
-       </configuration>
-        <executions>
-          <execution>
-            <id>create-distribution</id>
-            <phase>package</phase>
-            <goals><goal>single</goal></goals>
-           <configuration>
-             <descriptors>
-               <descriptor>assembly.xml</descriptor>
-             </descriptors>
-             <tarLongFileFormat>gnu</tarLongFileFormat>
-           </configuration>
-          </execution>
-        </executions>
-      </plugin>
-      -->
-
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-resources-plugin</artifactId>

Modified: 
jena/trunk/jena-sdb/src/test/java/com/hp/hpl/jena/sdb/test/junit/QueryTestSDB.java
URL: 
http://svn.apache.org/viewvc/jena/trunk/jena-sdb/src/test/java/com/hp/hpl/jena/sdb/test/junit/QueryTestSDB.java?rev=1605097&r1=1605096&r2=1605097&view=diff
==============================================================================
--- 
jena/trunk/jena-sdb/src/test/java/com/hp/hpl/jena/sdb/test/junit/QueryTestSDB.java
 (original)
+++ 
jena/trunk/jena-sdb/src/test/java/com/hp/hpl/jena/sdb/test/junit/QueryTestSDB.java
 Tue Jun 24 14:39:05 2014
@@ -189,9 +189,7 @@ public class QueryTestSDB extends EarlTe
 
         QueryEngineFactory f2 = QueryEngineSDB.getFactory() ;
         ds = DatasetStore.create(store) ;
-        QueryExecution qExec2 = new QueryExecutionBase(query, ds, null, f2) ;
-        
-        try {
+        try (QueryExecution qExec2 = new QueryExecutionBase(query, ds, null, 
f2) ) {
             SDBConnection.logSQLExceptions = true ;
             rs = qExec2.execSelect() ;
             


Reply via email to