1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.impl; |
16 |
| |
17 |
| import java.util.HashSet; |
18 |
| import java.util.Iterator; |
19 |
| import java.util.Set; |
20 |
| |
21 |
| import org.apache.commons.logging.Log; |
22 |
| import org.apache.commons.logging.LogFactory; |
23 |
| import org.apache.hivemind.ShutdownCoordinator; |
24 |
| import org.apache.hivemind.events.RegistryShutdownListener; |
25 |
| import org.apache.hivemind.util.EventListenerList; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public final class ShutdownCoordinatorImpl implements ShutdownCoordinator |
34 |
| { |
35 |
| private final Log _log; |
36 |
| |
37 |
| private Set alreadyShutdown; |
38 |
| |
39 |
131
| public ShutdownCoordinatorImpl()
|
40 |
| { |
41 |
131
| this(LogFactory.getLog(ShutdownCoordinatorImpl.class));
|
42 |
| } |
43 |
| |
44 |
163
| public ShutdownCoordinatorImpl(Log log)
|
45 |
| { |
46 |
163
| _log = log;
|
47 |
| } |
48 |
| |
49 |
| private EventListenerList _listenerList; |
50 |
| |
51 |
1705
| public synchronized void addRegistryShutdownListener(
|
52 |
| RegistryShutdownListener s) |
53 |
| { |
54 |
1705
| if (_listenerList == null)
|
55 |
140
| _listenerList = new EventListenerList();
|
56 |
| |
57 |
1705
| _listenerList.addListener(s);
|
58 |
| } |
59 |
| |
60 |
0
| public synchronized void removeRegistryShutdownListener(
|
61 |
| RegistryShutdownListener s) |
62 |
| { |
63 |
0
| if (_listenerList != null)
|
64 |
0
| _listenerList.removeListener(s);
|
65 |
| } |
66 |
| |
67 |
60
| public void shutdown()
|
68 |
| { |
69 |
60
| if (_listenerList == null)
|
70 |
20
| return;
|
71 |
| |
72 |
40
| Iterator i = _listenerList.getListeners();
|
73 |
| |
74 |
40
| _listenerList = null;
|
75 |
| |
76 |
40
| while (i.hasNext())
|
77 |
| { |
78 |
407
| RegistryShutdownListener s = (RegistryShutdownListener) i.next();
|
79 |
| |
80 |
407
| shutdown(s);
|
81 |
| } |
82 |
| |
83 |
40
| _listenerList = null;
|
84 |
| } |
85 |
| |
86 |
407
| private void shutdown(RegistryShutdownListener s)
|
87 |
| { |
88 |
407
| if (alreadyShutdown == null)
|
89 |
| { |
90 |
40
| alreadyShutdown = new HashSet();
|
91 |
| } |
92 |
407
| final Long id = new Long(System.identityHashCode(s));
|
93 |
407
| if (!alreadyShutdown.contains(id))
|
94 |
| { |
95 |
402
| try
|
96 |
| { |
97 |
402
| s.registryDidShutdown();
|
98 |
| } |
99 |
| catch (RuntimeException ex) |
100 |
| { |
101 |
1
| _log.error(ImplMessages.shutdownCoordinatorFailure(s, ex), ex);
|
102 |
| } |
103 |
| finally |
104 |
| { |
105 |
402
| alreadyShutdown.add(id);
|
106 |
| } |
107 |
| } |
108 |
| } |
109 |
| |
110 |
| } |