diff --git a/src/main/java/javax/enterprise/concurrent/ManagedExecutors.java b/src/main/java/javax/enterprise/concurrent/ManagedExecutors.java index 72cf3f5..ff55dd8 100644 --- a/src/main/java/javax/enterprise/concurrent/ManagedExecutors.java +++ b/src/main/java/javax/enterprise/concurrent/ManagedExecutors.java @@ -1,6 +1,6 @@ package javax.enterprise.concurrent; -import java.util.Locale; +import java.util.HashMap; import java.util.Map; import java.util.concurrent.Callable; @@ -14,65 +14,80 @@ public static boolean isCurrentThreadShutdown() { return thread instanceof ManageableThread && ((ManageableThread)thread).isShutdown(); } - public static Callable managedTask(Callable task, ManagedTaskListener taskListener) { + public static Callable managedTask(Callable task, ManagedTaskListener taskListener) throws IllegalArgumentException { return new ManagedCallable(task, taskListener, null); } - public static Callable managedTask(Callable task, Map executionProperties, ManagedTaskListener taskListener) { + public static Callable managedTask(Callable task, Map executionProperties, ManagedTaskListener taskListener) throws IllegalArgumentException { return new ManagedCallable(task, taskListener, executionProperties); } - static class ManagedCallable implements Callable, ManagedTask { + static class ManagedCallable extends AbstractManagedTask implements Callable { private final Callable task; - private final ManagedTaskListener taskListener; - private final Map executionProperties; ManagedCallable(final Callable task, final ManagedTaskListener taskListener, final Map executionProperties) { + super(task, taskListener, executionProperties); this.task = task; - this.taskListener = taskListener; - this.executionProperties = executionProperties; } public V call() throws Exception { return task.call(); } - - public Map getExecutionProperties() { - return executionProperties; - } - - public ManagedTaskListener getManagedTaskListener() { - return taskListener; - } } - public static Runnable managedTask(Runnable task, ManagedTaskListener taskListener) { + public static Runnable managedTask(Runnable task, ManagedTaskListener taskListener) throws IllegalArgumentException { return new ManagedRunnable(task, taskListener, null); } - public static Runnable managedTask(Runnable task, Map executionProperties, ManagedTaskListener taskListener) { + public static Runnable managedTask(Runnable task, Map executionProperties, ManagedTaskListener taskListener) throws IllegalArgumentException { return new ManagedRunnable(task, taskListener, executionProperties); } - static class ManagedRunnable implements Runnable, ManagedTask { + static class ManagedRunnable extends AbstractManagedTask implements Runnable { private final Runnable task; - private final ManagedTaskListener taskListener; - private final Map executionProperties; ManagedRunnable(final Runnable task, final ManagedTaskListener taskListener, final Map executionProperties) { + super(task, taskListener, executionProperties); this.task = task; - this.taskListener = taskListener; - this.executionProperties = executionProperties; } public void run() { task.run(); } + } + + abstract static class AbstractManagedTask implements ManagedTask { + private final ManagedTaskListener taskListener; + private Map executionProperties = null; + + AbstractManagedTask(Object task, ManagedTaskListener taskListener, Map executionProperties) throws IllegalArgumentException { + if (task == null) { + throw new IllegalArgumentException("null task"); + } + final ManagedTask managedTask = task instanceof ManagedTask ? (ManagedTask) task : null; + if (taskListener != null) { + this.taskListener = taskListener; + } else { + this.taskListener = managedTask != null ? managedTask.getManagedTaskListener() : null; + } + if (managedTask != null && managedTask.getExecutionProperties() != null) { + this.executionProperties = new HashMap(managedTask.getExecutionProperties()); + } + if (executionProperties != null) { + if (this.executionProperties == null) { + this.executionProperties = new HashMap(executionProperties); + } else { + this.executionProperties.putAll(executionProperties); + } + } + } + @Override public Map getExecutionProperties() { return executionProperties; } + @Override public ManagedTaskListener getManagedTaskListener() { return taskListener; } diff --git a/src/main/java/javax/enterprise/concurrent/ManagedTaskListener.java b/src/main/java/javax/enterprise/concurrent/ManagedTaskListener.java index 420adb1..287ba79 100644 --- a/src/main/java/javax/enterprise/concurrent/ManagedTaskListener.java +++ b/src/main/java/javax/enterprise/concurrent/ManagedTaskListener.java @@ -5,11 +5,11 @@ /** */ public interface ManagedTaskListener { - void taskAborted(Future future, ManagedExecutorService executor, Throwable exception); + void taskAborted(Future future, ManagedExecutorService executor, Object task, Throwable exception); - void taskDone(Future future, ManagedExecutorService executor, Throwable exception); + void taskDone(Future future, ManagedExecutorService executor, Object task, Throwable exception); - void taskStarting(Future future, ManagedExecutorService executor); + void taskStarting(Future future, ManagedExecutorService executor, Object task); - void taskSubmitted(Future future, ManagedExecutorService executor); + void taskSubmitted(Future future, ManagedExecutorService executor, Object task); }