1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.sourceforge.acelogger.factory;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.TimeUnit;
23
24 import net.sourceforge.acelogger.constants.ExecutorManagerConstants;
25 import net.sourceforge.acelogger.execution.LogController;
26 import net.sourceforge.acelogger.execution.TerminationException;
27 import net.sourceforge.acelogger.execution.manager.ExecutorManager;
28
29
30
31
32
33
34
35
36 public final class ExecutorManagerFactory {
37
38
39
40
41 private static final ExecutorManagerFactory INSTANCE = new ExecutorManagerFactory();
42
43
44
45
46 private ExecutorManager defaultExecutorManager;
47
48
49
50
51 private final Map<String, ExecutorManager> registeredExecutors;
52
53
54
55
56 private boolean shutdown = false;
57
58
59
60
61
62
63 private ExecutorManagerFactory() {
64 registeredExecutors = new HashMap<String, ExecutorManager>();
65 defaultExecutorManager = ExecutorManagerConstants.EMPTY;
66 }
67
68
69
70
71
72
73
74
75
76
77
78 public static ExecutorManager getExecutorManager(String identifier) {
79 LogController.ensureInitialization();
80 ExecutorManager intendedExecutor = INSTANCE.registeredExecutors.get(identifier);
81 if (intendedExecutor == null) {
82 int lastDotIndex = identifier.lastIndexOf('.');
83 if (lastDotIndex > 0) {
84 intendedExecutor = getExecutorManager(identifier.substring(0, lastDotIndex));
85 } else {
86 intendedExecutor = INSTANCE.defaultExecutorManager;
87 }
88 }
89 return intendedExecutor;
90 }
91
92
93
94
95
96
97
98
99
100
101 public static ExecutorManager setDefaultExecutorManager(ExecutorManager manager) {
102 LogController.ensureInitialization();
103 ExecutorManager previous = INSTANCE.defaultExecutorManager;
104 if (manager != null) {
105 INSTANCE.defaultExecutorManager = manager;
106 }
107 return previous;
108 }
109
110
111
112
113
114
115
116
117 public static ExecutorManager getDefaultExecutorManager() {
118 LogController.ensureInitialization();
119 return INSTANCE.defaultExecutorManager;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 public static ExecutorManager registerExecutorManager(ExecutorManager manager) {
134 LogController.ensureInitialization();
135 ExecutorManager intendedExecutor = null;
136 if (manager != null) {
137 intendedExecutor = INSTANCE.registeredExecutors.put(manager.getIdentifier(), manager);
138 }
139 if (intendedExecutor == null) {
140 intendedExecutor = INSTANCE.defaultExecutorManager;
141 }
142 return intendedExecutor;
143 }
144
145
146
147
148
149
150
151 public static List<ExecutorManager> getRegisteredExecutorManagers() {
152 LogController.ensureInitialization();
153 List<ExecutorManager> allExecutorManagers = new ArrayList<ExecutorManager>(
154 INSTANCE.registeredExecutors.values());
155 return allExecutorManagers;
156 }
157
158
159
160
161
162
163
164
165 public static boolean isShutdown() {
166 LogController.ensureInitialization();
167 return INSTANCE.shutdown;
168 }
169
170
171
172
173
174
175
176
177
178 public static boolean doProperExecutorManagersShutdown() {
179 if (INSTANCE.shutdown) {
180 throw new IllegalStateException("");
181 } else {
182 LogController.ensureInitialization();
183 INSTANCE.shutdown = true;
184 boolean properShutdown = true;
185 List<ExecutorManager> allExecutorManagers = getRegisteredExecutorManagers();
186 for (ExecutorManager currentExecutorManager : allExecutorManagers) {
187 currentExecutorManager.orderProperShutdown();
188 properShutdown &= currentExecutorManager.isTerminated();
189 }
190 return properShutdown;
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 public static boolean awaitExecutorManagersShutdown(final long terminationTimeout)
208 throws TerminationException {
209 boolean allDone = false;
210 if (INSTANCE.shutdown) {
211 List<ExecutorManager> remainingExecutorManagers = getRegisteredExecutorManagers();
212 final CountDownLatch synchronizer =
213 new CountDownLatch(remainingExecutorManagers.size());
214 for (final ExecutorManager currentManager : remainingExecutorManagers) {
215 LogController.submitBackgroundTask(new Runnable() {
216
217
218
219 public void run() {
220 currentManager.awaitTermination(terminationTimeout);
221 synchronizer.countDown();
222 }
223 });
224 }
225 try {
226 allDone = synchronizer.await(terminationTimeout, TimeUnit.MILLISECONDS);
227 } catch (InterruptedException e) {
228 throw new TerminationException("Interrupted while waiting executor to finish", e);
229 }
230 } else {
231 throw new IllegalStateException(
232 "Can only await executor managers shutdown after calling"
233 + " doProperExecutorManagersShutdown");
234 }
235 return allDone;
236 }
237
238 }