1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.application;
21
22 import java.util.ArrayList;
23 import java.util.HashSet;
24
25 import com.cosylab.util.ObjectList;
26
27
28
29
30
31
32
33
34
35 public class DefaultPlugInManager implements PlugInManager
36 {
37 private String name= "DefaultPlugInManager";
38 private ApplicationPanel parent;
39 private boolean destroyed = false;
40 private ArrayList<PlugIn> plugins = new ArrayList<PlugIn>();
41 private HashSet<Class> cyclicPlugIn = new HashSet<Class>();
42 private ObjectList listeners = new ObjectList(PlugInListener.class);
43
44
45
46
47 public DefaultPlugInManager()
48 {
49 }
50
51
52
53
54
55
56 public ApplicationPanel getOwnerPanel()
57 {
58 return parent;
59 }
60
61
62
63
64
65
66 public final synchronized boolean isDestroyed()
67 {
68 return destroyed;
69 }
70
71
72
73
74
75
76
77
78
79
80
81 public synchronized final void destroy()
82 {
83 if (destroyed) {
84 return;
85 }
86
87 PlugIn[] pl = getPlugIns();
88
89 for (int i = 0; i < pl.length; i++) {
90 try {
91 if (pl[i] instanceof VisiblePlugIn) {
92 getOwnerPanel().removePlugInGUI((VisiblePlugIn)pl[i]);
93 }
94 } catch (Exception e) {
95
96 }
97
98 try {
99 pl[i].destroy();
100 } catch (Exception e) {
101
102 }
103 plugins.clear();
104 }
105
106 destroyed = true;
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public final synchronized void installPlugIn(Class plugType)
130 throws PlugInException
131 {
132 if (plugType == null) {
133 throw new NullPointerException("plugType");
134 }
135
136 if (isDestroyed()) {
137 return;
138 }
139
140 if (parent == null) {
141 throw new IllegalStateException("parent == null");
142 }
143
144
145 if (!PlugIn.class.isAssignableFrom(plugType)) {
146 throw new PlugInException(this,
147 "Class '" + plugType.getName() + "' does not implement '"
148 + PlugIn.class.getName() + "'.", plugType, null);
149 }
150
151
152 if (getPlugIn(plugType) != null) {
153 return;
154 }
155
156
157 if (cyclicPlugIn.contains(plugType)) {
158 throw new PlugInException(this,
159 "Cyclic plugin dependency: cannot instantiate without causing endless loop",
160 plugType, null);
161 }
162
163 cyclicPlugIn.add(plugType);
164
165
166 PlugIn plugIn = null;
167
168 try {
169 plugIn = (PlugIn)plugType.newInstance();
170 } catch (Exception e) {
171 throw new PlugInException(this,
172 "Could not invoke the plugin constructor.", plugType, e);
173 }
174
175
176 plugins.add(plugIn);
177
178
179 try {
180 plugIn.installPlugIn(this);
181 } catch (Exception e) {
182
183 plugins.remove(plugIn);
184 throw new PlugInException(this,
185 "Exception while installing the plugin", plugType, e);
186 } finally {
187
188 cyclicPlugIn.remove(plugType);
189 }
190
191
192 PlugInListener[] l = (PlugInListener[])listeners.toArray();
193 PlugInEvent event = new PlugInEvent(this, plugIn);
194
195 for (int i = 0; i < l.length; i++) {
196 try {
197 l[i].plugInInstalled(event);
198 } catch (Exception e) {
199 e.printStackTrace();
200 }
201 }
202 }
203
204
205
206
207
208
209
210
211 public synchronized PlugIn[] getPlugIns()
212 {
213 return plugins.toArray(new PlugIn[plugins.size()]);
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 public PlugIn getPlugIn(Class plugType)
231 {
232 if (plugType == null) {
233 throw new NullPointerException("plugType");
234 }
235
236 PlugIn[] array = getPlugIns();
237
238 for (int i = 0; i < array.length; i++) {
239 if (array[i].getClass().equals(plugType)) {
240 return array[i];
241 }
242 }
243
244 return null;
245 }
246
247
248
249
250
251
252
253
254 public void addPlugInListener(PlugInListener l)
255 {
256 listeners.add(l);
257 }
258
259
260
261
262
263
264
265
266 public void removePlugInListener(PlugInListener l)
267 {
268 listeners.remove(l);
269 }
270
271
272
273
274
275
276
277
278
279 public PlugInListener[] getPlugInListeners()
280 {
281 return (PlugInListener[])listeners.toArray();
282 }
283
284
285
286
287
288
289
290 public void removePlugIn(Class plugType)
291 {
292 if (plugType == null) {
293 throw new NullPointerException("plugType");
294 }
295
296 if (isDestroyed()) {
297 return;
298 }
299
300 if (parent == null) {
301 throw new IllegalStateException("parent == null");
302 }
303
304 PlugIn plugIn = getPlugIn(plugType);
305
306 if (plugIn == null) {
307 return;
308 }
309
310 plugins.remove(plugIn);
311
312
313 try {
314 if (plugIn instanceof VisiblePlugIn) {
315 getOwnerPanel().removePlugInGUI((VisiblePlugIn)plugIn);
316 }
317
318 plugIn.destroy();
319 } catch (Exception e) {
320
321 e.printStackTrace();
322 }
323
324
325 PlugInListener[] l = (PlugInListener[])listeners.toArray();
326 PlugInEvent event = new PlugInEvent(this, plugIn);
327
328 for (int i = 0; i < l.length; i++) {
329 try {
330 l[i].plugInRemoved(event);
331 } catch (Exception e) {
332 e.printStackTrace();
333 }
334 }
335 }
336
337
338
339
340
341
342 public void setOwnerPanel(ApplicationPanel panel)
343 {
344 this.parent = panel;
345 }
346
347
348
349
350 public String getName()
351 {
352 return name;
353 }
354
355
356
357 public PlugIn acquirePlugIn(Class plugType) throws PlugInException {
358 PlugIn pl= getPlugIn(plugType);
359 if (pl==null) installPlugIn(plugType);
360 return getPlugIn(plugType);
361 }
362
363 }
364
365