[jboss-cvs] JBossAS SVN: r91991 - projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Aug 4 21:13:02 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-08-04 21:13:02 -0400 (Tue, 04 Aug 2009)
New Revision: 91991

Added:
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappedExecutor.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ExecutionInterruptedException.java
Modified:
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappingExecutor.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/JBossExecutors.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/PrivilegedExecutor.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/SimpleQueueExecutor.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ThreadPool.java
   projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/WrappingExecutor.java
Log:
Clean up wrapping executors, fix interrupt handling a bit, other minor fixes

Added: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappedExecutor.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappedExecutor.java	                        (rev 0)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappedExecutor.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.threads;
+
+import java.util.concurrent.Executor;
+
+class DelegatingWrappedExecutor implements Executor {
+
+    private final WrappingExecutor delegate;
+    private final DirectExecutor taskWrapper;
+
+    DelegatingWrappedExecutor(final WrappingExecutor delegate, final DirectExecutor taskWrapper) {
+        this.delegate = delegate;
+        this.taskWrapper = taskWrapper;
+    }
+
+    public void execute(final Runnable command) {
+        delegate.execute(taskWrapper, command);
+    }
+}

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappingExecutor.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappingExecutor.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/DelegatingWrappingExecutor.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -25,19 +25,13 @@
 import java.util.concurrent.Executor;
 import java.util.concurrent.RejectedExecutionException;
 
-class DelegatingWrappingExecutor implements WrappingExecutor {
+class DelegatingWrappingExecutor extends ProtectedExecutor implements WrappingExecutor {
 
-    private final Executor delegate;
-
     DelegatingWrappingExecutor(final Executor delegate) {
-        this.delegate = delegate;
+        super(delegate);
     }
 
     public void execute(final DirectExecutor directExecutor, final Runnable task) throws RejectedExecutionException {
-        delegate.execute(JBossExecutors.executorTask(directExecutor, task));
+        execute(JBossExecutors.executorTask(directExecutor, task));
     }
-
-    public String toString() {
-        return String.format("%s -> %s", super.toString(), delegate);
-    }
 }

Added: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ExecutionInterruptedException.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ExecutionInterruptedException.java	                        (rev 0)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ExecutionInterruptedException.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -0,0 +1,72 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.threads;
+
+import java.util.concurrent.RejectedExecutionException;
+
+/**
+ * Thrown when a {@link java.util.concurrent.Executor#execute(Runnable)} submission is interrupted before the task could be accepted.  If
+ * this exception is thrown, the thread's {@code interrupted} flag will also be set.
+ */
+public class ExecutionInterruptedException extends RejectedExecutionException {
+
+    private static final long serialVersionUID = -8420751681898632287L;
+
+    /**
+     * Constructs a {@code ExecutionInterruptedException} with no detail message. The cause is not initialized, and may
+     * subsequently be initialized by a call to {@link #initCause(Throwable) initCause}.
+     */
+    public ExecutionInterruptedException() {
+    }
+
+    /**
+     * Constructs a {@code ExecutionInterruptedException} with the specified detail message. The cause is not initialized,
+     * and may subsequently be initialized by a call to {@link #initCause(Throwable) initCause}.
+     *
+     * @param msg the detail message
+     */
+    public ExecutionInterruptedException(final String msg) {
+        super(msg);
+    }
+
+    /**
+     * Constructs a {@code ExecutionInterruptedException} with the specified cause. The detail message is set to:
+     * <pre>(cause == null ? null : cause.toString())</pre>
+     * (which typically contains the class and detail message of {@code cause}).
+     *
+     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
+     */
+    public ExecutionInterruptedException(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * Constructs a {@code ExecutionInterruptedException} with the specified detail message and cause.
+     *
+     * @param msg the detail message
+     * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method)
+     */
+    public ExecutionInterruptedException(final String msg, final Throwable cause) {
+        super(msg, cause);
+    }
+}

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/JBossExecutors.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/JBossExecutors.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/JBossExecutors.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -132,6 +132,18 @@
     }
 
     /**
+     * Create a direct executor which runs with the privileges given by the supplied class' protection domain.
+     *
+     * @param delegate the executor to delegate to at the privileged level
+     * @param targetClass the target class whose protection domain should be used
+     * @return the new direct executor
+     * @throws SecurityException if there is a security manager installed and the caller lacks the {@code "getProtectionDomain"} {@link RuntimePermission}
+     */
+    public static DirectExecutor privilegedExecutor(final DirectExecutor delegate, final Class<?> targetClass) throws SecurityException {
+        return new PrivilegedExecutor(delegate, targetClass);
+    }
+
+    /**
      * Create a direct executor which runs with the privileges given by the current access control context.
      *
      * @param delegate the executor to delegate to at the privileged level
@@ -277,11 +289,7 @@
      * @return a wrapping executor
      */
     public static Executor executor(final WrappingExecutor delegate, final DirectExecutor taskWrapper) {
-        return new Executor() {
-            public void execute(final Runnable command) {
-                delegate.execute(taskWrapper, command);
-            }
-        };
+        return new DelegatingWrappedExecutor(delegate, taskWrapper);
     }
 
     /**

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/PrivilegedExecutor.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/PrivilegedExecutor.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/PrivilegedExecutor.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -25,6 +25,7 @@
 import java.security.AccessControlContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
 
 class PrivilegedExecutor implements DirectExecutor {
 
@@ -36,6 +37,11 @@
         this.context = context;
     }
 
+    PrivilegedExecutor(final DirectExecutor delegate, final Class<?> targetClass) throws SecurityException {
+        this.delegate = delegate;
+        context = new AccessControlContext(new ProtectionDomain[] { targetClass.getProtectionDomain() });
+    }
+
     public void execute(final Runnable command) {
         final SecurityManager sm = System.getSecurityManager();
         if (sm != null) {

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/SimpleQueueExecutor.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/SimpleQueueExecutor.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/SimpleQueueExecutor.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -158,7 +158,7 @@
             }
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
-            throw new RejectedExecutionException("Thread interrupted");
+            throw new ExecutionInterruptedException("Thread interrupted");
         }
     }
 

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ThreadPool.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ThreadPool.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/ThreadPool.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -62,7 +62,7 @@
         runningThreads = 0;
     }
 
-    public void executeBlocking(final Runnable runnable, final DirectExecutor taskExecutor) throws InterruptedException {
+    public void executeBlocking(final DirectExecutor taskExecutor, final Runnable runnable) throws InterruptedException {
         if (runnable == null) {
             throw new NullPointerException("runnable is null");
         }

Modified: projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/WrappingExecutor.java
===================================================================
--- projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/WrappingExecutor.java	2009-08-04 22:55:59 UTC (rev 91990)
+++ projects/jboss-threads/trunk/main/src/main/java/org/jboss/threads/WrappingExecutor.java	2009-08-05 01:13:02 UTC (rev 91991)
@@ -23,10 +23,27 @@
 package org.jboss.threads;
 
 import java.util.concurrent.RejectedExecutionException;
+import java.util.concurrent.Executor;
 
 /**
  * An executor which runs a task within the given direct executor.
  */
-public interface WrappingExecutor {
+public interface WrappingExecutor extends Executor {
+
+    /**
+     * Run the given task directly, without a wrapper.
+     *
+     * @param task the task to run
+     * @throws RejectedExecutionException if the execution was rejected for some reason
+     */
+    void execute(Runnable task) throws RejectedExecutionException;
+
+    /**
+     * Run the given task within the given wrapper.
+     *
+     * @param directExecutor the task wrapper
+     * @param task the task to run
+     * @throws RejectedExecutionException if the execution was rejected for some reason
+     */
     void execute(DirectExecutor directExecutor, Runnable task) throws RejectedExecutionException;
 }




More information about the jboss-cvs-commits mailing list