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