ac0f848f3130e6c718dac5cb96c07b3393045161
[jabaws.git] / engine / compbio / engine / local / _TrackingExecutor.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 package compbio.engine.local;\r
19 \r
20 import java.util.ArrayList;\r
21 import java.util.HashSet;\r
22 import java.util.List;\r
23 import java.util.Set;\r
24 import java.util.concurrent.AbstractExecutorService;\r
25 import java.util.concurrent.Callable;\r
26 import java.util.concurrent.ExecutorService;\r
27 import java.util.concurrent.Future;\r
28 import java.util.concurrent.TimeUnit;\r
29 \r
30 /**\r
31  * This executor extends standard Java ExecutorService by adding the method to\r
32  * obtain all Runnables which were running and did not complete upon executor\r
33  * termination. For this to work properly Runnables must propagate an\r
34  * Interruption exceptions, not swallow them, which a good Runnable should do\r
35  * anyway.\r
36  * \r
37  * TODO it may be better to persists task from different place\r
38  * \r
39  * @author pvtroshin\r
40  * @version 1.0 October 2009\r
41  * @deprecated\r
42  */\r
43 @Deprecated\r
44 class _TrackingExecutor extends AbstractExecutorService {\r
45 \r
46         private final ExecutorService executor;\r
47 \r
48         public _TrackingExecutor(ExecutorService executor) {\r
49                 this.executor = executor;\r
50         }\r
51 \r
52         private final Set<Runnable> cancelledRunnableTasksAtShutdown = new HashSet<Runnable>();\r
53         private final Set<Callable<?>> cancelledCallableTasksAtShutdown = new HashSet<Callable<?>>();\r
54 \r
55         public List getCancelledTasks() {\r
56                 if (!executor.isTerminated()) {\r
57                         throw new IllegalStateException(\r
58                                         "Executor must be terminated before running this method!");\r
59                 }\r
60                 ArrayList tasks = new ArrayList(cancelledCallableTasksAtShutdown);\r
61                 tasks.addAll(cancelledRunnableTasksAtShutdown);\r
62                 return tasks;\r
63         }\r
64 \r
65         @Override\r
66         public void execute(final Runnable runnable) {\r
67                 executor.execute(new Runnable() {\r
68                         @Override\r
69                         public void run() {\r
70                                 try {\r
71                                         runnable.run();\r
72                                 } finally {\r
73                                         if (isShutdown() && Thread.currentThread().isInterrupted()) {\r
74                                                 cancelledRunnableTasksAtShutdown.add(runnable);\r
75                                         }\r
76                                 }\r
77                         }\r
78                 });\r
79         }\r
80 \r
81         @Override\r
82         public <T> Future<T> submit(final Callable<T> task) {\r
83                 return executor.submit(new Callable<T>() {\r
84                         @Override\r
85                         public T call() throws Exception {\r
86                                 try {\r
87                                         return task.call();\r
88                                 } finally {\r
89                                         if (isShutdown() && Thread.currentThread().isInterrupted()) {\r
90                                                 cancelledCallableTasksAtShutdown.add(task);\r
91                                         }\r
92                                 }\r
93                         }\r
94                 });\r
95         }\r
96 \r
97         @Override\r
98         public boolean awaitTermination(long timeout, TimeUnit unit)\r
99                         throws InterruptedException {\r
100                 return executor.awaitTermination(timeout, unit);\r
101         }\r
102 \r
103         @Override\r
104         public boolean isShutdown() {\r
105                 return executor.isShutdown();\r
106         }\r
107 \r
108         @Override\r
109         public boolean isTerminated() {\r
110                 return executor.isTerminated();\r
111         }\r
112 \r
113         @Override\r
114         public void shutdown() {\r
115                 executor.shutdown();\r
116         }\r
117 \r
118         @Override\r
119         public List<Runnable> shutdownNow() {\r
120                 return executor.shutdownNow();\r
121         }\r
122 \r
123 }\r