1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveutils.service.impl; |
16 | |
17 | import java.util.Iterator; |
18 | import java.util.List; |
19 | import java.util.SortedSet; |
20 | import java.util.TreeSet; |
21 | |
22 | import org.apache.commons.logging.Log; |
23 | import org.apache.commons.logging.LogFactory; |
24 | import org.apache.hivemind.ShutdownCoordinator; |
25 | import org.apache.hivemind.events.RegistryShutdownListener; |
26 | |
27 | public final class ShutdownCoordinatorImpl implements ShutdownCoordinator |
28 | { |
29 | static private final Log _logger = LogFactory.getLog(ShutdownCoordinatorImpl.class); |
30 | |
31 | public ShutdownCoordinatorImpl(int defaultPriority, List<ShutdownContribution> priorities) |
32 | { |
33 | _priorities = priorities; |
34 | _defaultPriority = defaultPriority; |
35 | _listeners = new TreeSet<ShutdownListener>(); |
36 | } |
37 | |
38 | public synchronized void addRegistryShutdownListener(RegistryShutdownListener l) |
39 | { |
40 | _logger.debug("addRegistryShutdownListener() " + l); |
41 | // Check this listener is not already registered |
42 | for (ShutdownListener listener: _listeners) |
43 | { |
44 | if (listener.getListener() == l) |
45 | { |
46 | _logger.debug("addRegistryShutdownListener() already registered"); |
47 | return; |
48 | } |
49 | } |
50 | // OK, find whether it has a contribution with priority |
51 | int priority = _defaultPriority; |
52 | for (ShutdownContribution contrib: _priorities) |
53 | { |
54 | if (contrib.getNameIsClass()) |
55 | { |
56 | if (l.getClass().getName().equals(contrib.getName())) |
57 | { |
58 | priority = contrib.getPriority(); |
59 | break; |
60 | } |
61 | } |
62 | else |
63 | { |
64 | if (l.toString().equals(contrib.getName())) |
65 | { |
66 | priority = contrib.getPriority(); |
67 | break; |
68 | } |
69 | } |
70 | } |
71 | _logger.debug("addRegistryShutdownListener() priority=" + priority); |
72 | |
73 | // Add it to the list of listeners |
74 | _listeners.add(new ShutdownListener(l, priority)); |
75 | } |
76 | |
77 | public synchronized void removeRegistryShutdownListener(RegistryShutdownListener l) |
78 | { |
79 | _logger.debug("removeRegistryShutdownListener() " + l); |
80 | Iterator<ShutdownListener> i = _listeners.iterator(); |
81 | while (i.hasNext()) |
82 | { |
83 | if (i.next().getListener() == l) |
84 | { |
85 | i.remove(); |
86 | break; |
87 | } |
88 | } |
89 | } |
90 | |
91 | public synchronized void shutdown() |
92 | { |
93 | _logger.debug("shutdown()"); |
94 | for (ShutdownListener listener: _listeners) |
95 | { |
96 | listener.getListener().registryDidShutdown(); |
97 | } |
98 | } |
99 | |
100 | static private class ShutdownListener implements Comparable<ShutdownListener> |
101 | { |
102 | public ShutdownListener(RegistryShutdownListener listener, int priority) |
103 | { |
104 | _listener = listener; |
105 | _priority = priority; |
106 | _order = ++_count; |
107 | } |
108 | |
109 | public RegistryShutdownListener getListener() |
110 | { |
111 | return _listener; |
112 | } |
113 | |
114 | public int compareTo(ShutdownListener that) |
115 | { |
116 | int diff = this._priority - that._priority; |
117 | if (diff != 0) |
118 | { |
119 | return diff; |
120 | } |
121 | else |
122 | { |
123 | return this._order - that._order; |
124 | } |
125 | } |
126 | |
127 | private final RegistryShutdownListener _listener; |
128 | private final int _priority; |
129 | private final int _order; |
130 | static private int _count = 0; |
131 | } |
132 | |
133 | private final int _defaultPriority; |
134 | private final List<ShutdownContribution> _priorities; |
135 | private final SortedSet<ShutdownListener> _listeners; |
136 | } |