1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.plugins;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.log4j.spi.LoggerRepository;
27 import org.apache.log4j.spi.LoggerRepositoryEx;
28 import org.apache.log4j.spi.LoggerRepositoryEventListener;
29
30
31
32
33
34
35
36
37
38
39 public final class PluginRegistry {
40
41
42
43
44 private final Map pluginMap;
45
46
47
48 private final LoggerRepositoryEx loggerRepository;
49
50
51
52
53 private final RepositoryListener listener = new RepositoryListener();
54
55
56
57 private final List listenerList =
58 Collections.synchronizedList(new ArrayList());
59
60
61
62
63
64 public PluginRegistry(final LoggerRepositoryEx repository) {
65 super();
66 pluginMap = new HashMap();
67 this.loggerRepository = repository;
68 this.loggerRepository.addLoggerRepositoryEventListener(listener);
69 }
70
71
72
73
74
75 public LoggerRepositoryEx getLoggerRepository() {
76 return loggerRepository;
77 }
78
79
80
81
82
83
84
85
86
87
88 public boolean pluginNameExists(final String name) {
89 synchronized (pluginMap) {
90 return pluginMap.containsKey(name);
91 }
92 }
93
94
95
96
97
98
99
100
101
102 public void addPlugin(final Plugin plugin) {
103
104 synchronized (pluginMap) {
105 String name = plugin.getName();
106
107
108 plugin.setLoggerRepository(getLoggerRepository());
109
110 Plugin existingPlugin = (Plugin) pluginMap.get(name);
111 if (existingPlugin != null) {
112 existingPlugin.shutdown();
113 }
114
115
116 pluginMap.put(name, plugin);
117 firePluginStarted(plugin);
118 }
119 }
120
121
122
123
124
125
126
127 private void firePluginStarted(final Plugin plugin) {
128 PluginEvent e = null;
129 synchronized (listenerList) {
130 for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
131 PluginListener l = (PluginListener) iter.next();
132 if (e == null) {
133 e = new PluginEvent(plugin);
134 }
135 l.pluginStarted(e);
136 }
137 }
138 }
139
140
141
142
143
144
145
146 private void firePluginStopped(final Plugin plugin) {
147 PluginEvent e = null;
148 synchronized (listenerList) {
149 for (Iterator iter = listenerList.iterator(); iter.hasNext();) {
150 PluginListener l = (PluginListener) iter.next();
151 if (e == null) {
152 e = new PluginEvent(plugin);
153 }
154 l.pluginStopped(e);
155 }
156 }
157 }
158
159
160
161
162
163
164
165 public List getPlugins() {
166 synchronized (pluginMap) {
167 List pluginList = new ArrayList(pluginMap.size());
168 Iterator iter = pluginMap.values().iterator();
169
170 while (iter.hasNext()) {
171 pluginList.add(iter.next());
172 }
173 return pluginList;
174 }
175 }
176
177
178
179
180
181
182
183
184
185 public List getPlugins(final Class pluginClass) {
186 synchronized (pluginMap) {
187 List pluginList = new ArrayList(pluginMap.size());
188 Iterator iter = pluginMap.values().iterator();
189
190 while (iter.hasNext()) {
191 Object plugin = iter.next();
192
193 if (pluginClass.isInstance(plugin)) {
194 pluginList.add(plugin);
195 }
196 }
197 return pluginList;
198 }
199 }
200
201
202
203
204
205
206
207
208
209 public Plugin stopPlugin(final String pluginName) {
210 synchronized (pluginMap) {
211 Plugin plugin = (Plugin) pluginMap.get(pluginName);
212
213 if (plugin == null) {
214 return null;
215 }
216
217
218 plugin.shutdown();
219
220
221 pluginMap.remove(pluginName);
222 firePluginStopped(plugin);
223
224
225 return plugin;
226 }
227 }
228
229
230
231
232 public void stopAllPlugins() {
233 synchronized (pluginMap) {
234
235 loggerRepository.removeLoggerRepositoryEventListener(listener);
236
237 Iterator iter = pluginMap.values().iterator();
238
239 while (iter.hasNext()) {
240 Plugin plugin = (Plugin) iter.next();
241 plugin.shutdown();
242 firePluginStopped(plugin);
243 }
244 }
245 }
246
247
248
249
250
251
252
253
254 public void addPluginListener(final PluginListener l) {
255 listenerList.add(l);
256 }
257
258
259
260
261
262
263
264
265 public void removePluginListener(final PluginListener l) {
266 listenerList.remove(l);
267 }
268
269
270
271
272 private class RepositoryListener implements LoggerRepositoryEventListener {
273
274
275
276
277
278 public void configurationResetEvent(final LoggerRepository repository) {
279 PluginRegistry.this.stopAllPlugins();
280 }
281
282
283
284
285
286
287
288 public void configurationChangedEvent(
289 final LoggerRepository repository) {
290
291 }
292
293
294
295
296
297
298
299 public void shutdownEvent(final LoggerRepository repository) {
300 PluginRegistry.this.stopAllPlugins();
301 }
302 }
303 }