1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.service.impl; |
16 |
| |
17 |
| import java.util.Iterator; |
18 |
| |
19 |
| import org.apache.commons.logging.Log; |
20 |
| import org.apache.commons.logging.LogFactory; |
21 |
| import org.apache.hivemind.service.ThreadCleanupListener; |
22 |
| import org.apache.hivemind.service.ThreadEventNotifier; |
23 |
| import org.apache.hivemind.util.Defense; |
24 |
| import org.apache.hivemind.util.EventListenerList; |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class ThreadEventNotifierImpl implements ThreadEventNotifier |
33 |
| { |
34 |
| private static final Log DEFAULT_LOG = LogFactory.getLog(ThreadEventNotifier.class); |
35 |
| |
36 |
| private final Log _log; |
37 |
| |
38 |
| private final ThreadLocal _storage = new ThreadLocal(); |
39 |
| |
40 |
30
| public ThreadEventNotifierImpl()
|
41 |
| { |
42 |
30
| this(DEFAULT_LOG);
|
43 |
| } |
44 |
| |
45 |
31
| public ThreadEventNotifierImpl(Log log)
|
46 |
| { |
47 |
31
| Defense.notNull(log, "log");
|
48 |
| |
49 |
31
| _log = log;
|
50 |
| } |
51 |
| |
52 |
17981
| public void addThreadCleanupListener(ThreadCleanupListener listener)
|
53 |
| { |
54 |
17960
| EventListenerList list = (EventListenerList) _storage.get();
|
55 |
| |
56 |
18010
| if (list == null)
|
57 |
| { |
58 |
12028
| list = new EventListenerList();
|
59 |
12029
| _storage.set(list);
|
60 |
| } |
61 |
| |
62 |
18009
| list.addListener(listener);
|
63 |
| } |
64 |
| |
65 |
1
| public void removeThreadCleanupListener(ThreadCleanupListener listener)
|
66 |
| { |
67 |
1
| EventListenerList list = (EventListenerList) _storage.get();
|
68 |
| |
69 |
1
| if (list != null)
|
70 |
1
| list.removeListener(listener);
|
71 |
| } |
72 |
| |
73 |
12012
| public void fireThreadCleanup()
|
74 |
| { |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
12012
| EventListenerList list = (EventListenerList) _storage.get();
|
80 |
| |
81 |
12012
| if (list == null)
|
82 |
2
| return;
|
83 |
| |
84 |
12010
| Iterator i = list.getListeners();
|
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
12010
| _storage.set(null);
|
90 |
| |
91 |
12010
| while (i.hasNext())
|
92 |
| { |
93 |
18010
| ThreadCleanupListener listener = (ThreadCleanupListener) i.next();
|
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
18010
| try
|
100 |
| { |
101 |
18010
| listener.threadDidCleanup();
|
102 |
| } |
103 |
| catch (RuntimeException ex) |
104 |
| { |
105 |
1
| _log.warn(ServiceMessages.threadCleanupException(ex), ex);
|
106 |
| } |
107 |
| } |
108 |
| } |
109 |
| |
110 |
| } |