[Mulgara-svn] r1912 - in trunk/src/jar: resolver/java/org/mulgara/resolver util/java/org/mulgara/util

alexhall at mulgara.org alexhall at mulgara.org
Fri Feb 5 20:51:09 UTC 2010


Author: alexhall
Date: 2010-02-05 12:51:08 -0800 (Fri, 05 Feb 2010)
New Revision: 1912

Added:
   trunk/src/jar/util/java/org/mulgara/util/ThreadUtil.java
Modified:
   trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
Log:
Add logic to the query resolution dispatch handlers to check for interrupts and halt query execution if interrupted.

Modified: trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java
===================================================================
--- trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java	2010-02-05 20:49:04 UTC (rev 1911)
+++ trunk/src/jar/resolver/java/org/mulgara/resolver/ConstraintOperations.java	2010-02-05 20:51:08 UTC (rev 1912)
@@ -48,6 +48,7 @@
 import org.mulgara.resolver.spi.QueryEvaluationContext;
 import org.mulgara.store.tuples.Tuples;
 import org.mulgara.util.NVPair;
+import org.mulgara.util.ThreadUtil;
 
 /**
  * Localized version of a global {@link Query}.
@@ -149,6 +150,8 @@
   public static Tuples resolveModelExpression(QueryEvaluationContext context, GraphExpression modelExpr,
                                       Constraint constraint) throws QueryException {
     try {
+      ThreadUtil.checkForInterrupt(QueryException.class);
+      
       if (logger.isDebugEnabled()) {
         logger.debug("Resolving " + constraint + " against GraphExpression[" + modelExpr.getClass() + "]");
       }
@@ -175,6 +178,8 @@
   public static Tuples resolveConstraintExpression(QueryEvaluationContext context,
       GraphExpression modelExpr, ConstraintExpression constraintExpr) throws QueryException {
     try {
+      ThreadUtil.checkForInterrupt(QueryException.class);
+      
       if (logger.isDebugEnabled()) {
         logger.debug("Resolving ConstraintExpression[" + constraintExpr.getClass() + "]");
       }

Added: trunk/src/jar/util/java/org/mulgara/util/ThreadUtil.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/ThreadUtil.java	                        (rev 0)
+++ trunk/src/jar/util/java/org/mulgara/util/ThreadUtil.java	2010-02-05 20:51:08 UTC (rev 1912)
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2010 Revelytix.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mulgara.util;
+
+/**
+ * Utility class for working with threads.
+ * @created 2010-02-02
+ * @author Alex Hall
+ * @copyright &copy; 2010 <a href="http://www.revelytix.com/">Revelytix</a>
+ * @license <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, v2.0</a>
+ */
+public class ThreadUtil {
+  
+  /** Default exception message to use when a thread was interrupted. */
+  private static final String DEFAULT_INTERRUPT_MSG = "Process was interrupted.";
+  
+  /**
+   * Checks whether the current thread has been interrupted, either locally via the
+   * {@link Thread#interrupt()} method or remotely by an interruptible RMI operation.
+   * If the thread was interrupted, then throw an exception.
+   * @param <E> The type of exception to throw if the thread was interrupted.
+   * @param exceptionClass The class used to instantiate the exception.
+   * @throws E If the thread has been interrupted.
+   */
+  public static <E extends Exception> void checkForInterrupt(Class<E> exceptionClass) throws E {
+    checkForInterrupt(exceptionClass, DEFAULT_INTERRUPT_MSG);
+  }
+
+  /**
+   * Checks whether the current thread has been interrupted, either locally via the
+   * {@link Thread#interrupt()} method or remotely by an interruptible RMI operation.
+   * If the thread was interrupted, then throw an exception with the given message.
+   * @param <E> The type of exception to throw if the thread was interrupted.
+   * @param exceptionClass The class used to instantiate the exception.
+   * @param msg The message to use when constructing the exception.
+   * @throws E If the thread has been interrupted.
+   */
+  public static <E extends Exception> void checkForInterrupt(Class<E> exceptionClass, String msg) throws E {
+    if (Thread.currentThread().isInterrupted() || Rmi.isInterrupted()) {
+      throw Reflect.newInstance(exceptionClass, msg);
+    }
+  }
+}




More information about the Mulgara-svn mailing list