11package javax .enterprise .concurrent ;
22
3- import java .util .Locale ;
3+ import java .util .HashMap ;
44import java .util .Map ;
55import java .util .concurrent .Callable ;
66
@@ -14,65 +14,80 @@ public static boolean isCurrentThreadShutdown() {
1414 return thread instanceof ManageableThread && ((ManageableThread )thread ).isShutdown ();
1515 }
1616
17- public static <V > Callable <V > managedTask (Callable <V > task , ManagedTaskListener taskListener ) {
17+ public static <V > Callable <V > managedTask (Callable <V > task , ManagedTaskListener taskListener ) throws IllegalArgumentException {
1818 return new ManagedCallable <V >(task , taskListener , null );
1919 }
2020
21- public static <V > Callable <V > managedTask (Callable <V > task , Map <String , String > executionProperties , ManagedTaskListener taskListener ) {
21+ public static <V > Callable <V > managedTask (Callable <V > task , Map <String , String > executionProperties , ManagedTaskListener taskListener ) throws IllegalArgumentException {
2222 return new ManagedCallable <V >(task , taskListener , executionProperties );
2323 }
2424
25- static class ManagedCallable <V > implements Callable <V >, ManagedTask {
25+ static class ManagedCallable <V > extends AbstractManagedTask implements Callable <V > {
2626 private final Callable <V > task ;
27- private final ManagedTaskListener taskListener ;
28- private final Map <String , String > executionProperties ;
2927
3028 ManagedCallable (final Callable <V > task , final ManagedTaskListener taskListener , final Map <String , String > executionProperties ) {
29+ super (task , taskListener , executionProperties );
3130 this .task = task ;
32- this .taskListener = taskListener ;
33- this .executionProperties = executionProperties ;
3431 }
3532
3633 public V call () throws Exception {
3734 return task .call ();
3835 }
39-
40- public Map <String , String > getExecutionProperties () {
41- return executionProperties ;
42- }
43-
44- public ManagedTaskListener getManagedTaskListener () {
45- return taskListener ;
46- }
4736 }
4837
49- public static Runnable managedTask (Runnable task , ManagedTaskListener taskListener ) {
38+ public static Runnable managedTask (Runnable task , ManagedTaskListener taskListener ) throws IllegalArgumentException {
5039 return new ManagedRunnable (task , taskListener , null );
5140 }
5241
53- public static Runnable managedTask (Runnable task , Map <String , String > executionProperties , ManagedTaskListener taskListener ) {
42+ public static Runnable managedTask (Runnable task , Map <String , String > executionProperties , ManagedTaskListener taskListener ) throws IllegalArgumentException {
5443 return new ManagedRunnable (task , taskListener , executionProperties );
5544 }
5645
57- static class ManagedRunnable implements Runnable , ManagedTask {
46+ static class ManagedRunnable extends AbstractManagedTask implements Runnable {
5847 private final Runnable task ;
59- private final ManagedTaskListener taskListener ;
60- private final Map <String , String > executionProperties ;
6148
6249 ManagedRunnable (final Runnable task , final ManagedTaskListener taskListener , final Map <String , String > executionProperties ) {
50+ super (task , taskListener , executionProperties );
6351 this .task = task ;
64- this .taskListener = taskListener ;
65- this .executionProperties = executionProperties ;
6652 }
6753
6854 public void run () {
6955 task .run ();
7056 }
57+ }
58+
59+ abstract static class AbstractManagedTask implements ManagedTask {
60+ private final ManagedTaskListener taskListener ;
61+ private Map <String , String > executionProperties = null ;
62+
63+ AbstractManagedTask (Object task , ManagedTaskListener taskListener , Map <String , String > executionProperties ) throws IllegalArgumentException {
64+ if (task == null ) {
65+ throw new IllegalArgumentException ("null task" );
66+ }
67+ final ManagedTask managedTask = task instanceof ManagedTask ? (ManagedTask ) task : null ;
68+ if (taskListener != null ) {
69+ this .taskListener = taskListener ;
70+ } else {
71+ this .taskListener = managedTask != null ? managedTask .getManagedTaskListener () : null ;
72+ }
73+ if (managedTask != null && managedTask .getExecutionProperties () != null ) {
74+ this .executionProperties = new HashMap <String , String >(managedTask .getExecutionProperties ());
75+ }
76+ if (executionProperties != null ) {
77+ if (this .executionProperties == null ) {
78+ this .executionProperties = new HashMap <String , String >(executionProperties );
79+ } else {
80+ this .executionProperties .putAll (executionProperties );
81+ }
82+ }
83+ }
7184
85+ @ Override
7286 public Map <String , String > getExecutionProperties () {
7387 return executionProperties ;
7488 }
7589
90+ @ Override
7691 public ManagedTaskListener getManagedTaskListener () {
7792 return taskListener ;
7893 }
0 commit comments