1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package Ice;
public final class PluginManagerI implements PluginManager
{
private static String _kindOfObject = "plugin";
public synchronized void
initializePlugins()
{
if(_initialized)
{
InitializationException ex = new InitializationException();
ex.reason = "plug-ins already initialized";
throw ex;
}
//
// Invoke initialize() on the plug-ins, in the order they were loaded.
//
java.util.List<Plugin> initializedPlugins = new java.util.ArrayList<Plugin>();
try
{
for(PluginInfo p : _plugins)
{
p.plugin.initialize();
initializedPlugins.add(p.plugin);
}
}
catch(RuntimeException ex)
{
//
// Destroy the plug-ins that have been successfully initialized, in the
// reverse order.
//
java.util.ListIterator<Plugin> i = initializedPlugins.listIterator(initializedPlugins.size());
while(i.hasPrevious())
{
Plugin p = i.previous();
try
{
p.destroy();
}
catch(RuntimeException e)
{
// Ignore.
}
}
throw ex;
}
_initialized = true;
}
public synchronized String[]
getPlugins()
{
java.util.ArrayList<String> names = new java.util.ArrayList<String>();
for(PluginInfo p : _plugins)
{
names.add(p.name);
}
return names.toArray(new String[0]);
}
public synchronized Plugin
getPlugin(String name)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
Plugin p = findPlugin(name);
if(p != null)
{
return p;
}
NotRegisteredException ex = new NotRegisteredException();
ex.id = name;
ex.kindOfObject = _kindOfObject;
throw ex;
}
public synchronized void
addPlugin(String name, Plugin plugin)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
if(findPlugin(name) != null)
{
AlreadyRegisteredException ex = new AlreadyRegisteredException();
ex.id = name;
ex.kindOfObject = _kindOfObject;
throw ex;
}
PluginInfo info = new PluginInfo();
info.name = name;
info.plugin = plugin;
_plugins.add(info);
}
public synchronized void
destroy()
{
if(_communicator != null)
{
if(_initialized)
{
java.util.ListIterator<PluginInfo> i = _plugins.listIterator(_plugins.size());
while(i.hasPrevious())
{
PluginInfo p = i.previous();
try
{
p.plugin.destroy();
}
catch(RuntimeException ex)
{
Ice.Util.getProcessLogger().warning("unexpected exception raised by plug-in `" +
p.name + "' destruction:\n" + ex.toString());
}
}
}
_communicator = null;
}
_plugins.clear();
if(_classLoaders != null)
{
_classLoaders.clear();
}
}
public
PluginManagerI(Communicator communicator, IceInternal.Instance instance)
{
_communicator = communicator;
_instance = instance;
_initialized = false;
}
public void
loadPlugins(StringSeqHolder cmdArgs)
{
assert(_communicator != null);
//
// Load and initialize the plug-ins defined in the property set
// with the prefix "Ice.Plugin.". These properties should
// have the following format:
//
// Ice.Plugin.name[.<language>]=entry_point [args]
//
// If the Ice.PluginLoadOrder property is defined, load the
// specified plug-ins in the specified order, then load any
// remaining plug-ins.
//
final String prefix = "Ice.Plugin.";
Properties properties = _communicator.getProperties();
java.util.Map<String, String> plugins = properties.getPropertiesForPrefix(prefix);
final String[] loadOrder = properties.getPropertyAsList("Ice.PluginLoadOrder");
for(String name : loadOrder)
{
if(findPlugin(name) != null)
{
PluginInitializationException ex = new PluginInitializationException();
ex.reason = "plug-in `" + name + "' already loaded";
throw ex;
}
String key = "Ice.Plugin." + name + ".java";
boolean hasKey = plugins.containsKey(key);
if(hasKey)
{
plugins.remove("Ice.Plugin." + name);
}
else
{
key = "Ice.Plugin." + name;
hasKey = plugins.containsKey(key);
}
if(hasKey)
{
final String value = plugins.get(key);
loadPlugin(name, value, cmdArgs);
plugins.remove(key);
}
else
{
PluginInitializationException ex = new PluginInitializationException();
ex.reason = "plug-in `" + name + "' not defined";
throw ex;
}
}
//
// Load any remaining plug-ins that weren't specified in PluginLoadOrder.
//
while(!plugins.isEmpty())
{
java.util.Iterator<java.util.Map.Entry<String, String> > p = plugins.entrySet().iterator();
java.util.Map.Entry<String, String> entry = p.next();
String name = entry.getKey().substring(prefix.length());
int dotPos = name.lastIndexOf('.');
if(dotPos != -1)
{
String suffix = name.substring(dotPos + 1);
if(suffix.equals("cpp") || suffix.equals("clr"))
{
//
// Ignored
//
p.remove();
}
else if(suffix.equals("java"))
{
name = name.substring(0, dotPos);
loadPlugin(name, entry.getValue(), cmdArgs);
p.remove();
//
// Don't want to load this one if it's there!
//
plugins.remove("Ice.Plugin." + name);
}
else
{
//
// Name is just a regular name that happens to contain a dot
//
dotPos = -1;
}
}
if(dotPos == -1)
{
//
// Is there a .java entry?
//
String value = entry.getValue();
p.remove();
String javaValue = plugins.remove("Ice.Plugin." + name + ".java");
if(javaValue != null)
{
value = javaValue;
}
loadPlugin(name, value, cmdArgs);
}
}
}
private void
loadPlugin(String name, String pluginSpec, StringSeqHolder cmdArgs)
{
assert(_communicator != null);
//
// We support the following formats:
//
// <class-name> [args]
// <jar-file>:<class-name> [args]
// <class-dir>:<class-name> [args]
// "<path with spaces>":<class-name> [args]
// "<path with spaces>:<class-name>" [args]
//
String[] args;
try
{
args = IceUtilInternal.Options.split(pluginSpec);
}
catch(IceUtilInternal.Options.BadQuote ex)
{
throw new PluginInitializationException("invalid arguments for plug-in `" + name + "':\n" +
ex.getMessage());
}
assert(args.length > 0);
final String entryPoint = args[0];
final boolean isWindows = System.getProperty("os.name").startsWith("Windows");
boolean absolutePath = false;
//
// Find first ':' that isn't part of the file path.
//
int pos = entryPoint.indexOf(':');
if(isWindows)
{
final String driveLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(pos == 1 && entryPoint.length() > 2 && driveLetters.indexOf(entryPoint.charAt(0)) != -1 &&
(entryPoint.charAt(2) == '\\' || entryPoint.charAt(2) == '/'))
{
absolutePath = true;
pos = entryPoint.indexOf(':', pos + 1);
}
if(!absolutePath)
{
absolutePath = entryPoint.startsWith("\\\\");
}
}
else
{
absolutePath = entryPoint.startsWith("/");
}
if((pos == -1 && absolutePath) || (pos != -1 && entryPoint.length() <= pos + 1))
{
//
// Class name is missing.
//
throw new PluginInitializationException("invalid entry point for plug-in `" + name + "':\n" + entryPoint);
}
//
// Extract the JAR file or subdirectory, if any.
//
String classDir = null; // Path name of JAR file or subdirectory.
String className;
if(pos == -1)
{
className = entryPoint;
}
else
{
classDir = entryPoint.substring(0, pos).trim();
className = entryPoint.substring(pos + 1).trim();
}
//
// Shift the arguments.
//
String[] tmp = new String[args.length - 1];
System.arraycopy(args, 1, tmp, 0, args.length - 1);
args = tmp;
//
// Convert command-line options into properties. First we
// convert the options from the plug-in configuration, then
// we convert the options from the application command-line.
//
Properties properties = _communicator.getProperties();
args = properties.parseCommandLineOptions(name, args);
cmdArgs.value = properties.parseCommandLineOptions(name, cmdArgs.value);
//
// Instantiate the class.
//
PluginFactory pluginFactory = null;
try
{
Class<?> c = null;
//
// Use a class loader if the user specified a JAR file or class directory.
//
if(classDir != null)
{
try
{
if(!absolutePath)
{
classDir = new java.io.File(System.getProperty("user.dir") + java.io.File.separator +
classDir).getCanonicalPath();
}
if(!classDir.endsWith(java.io.File.separator) && !classDir.toLowerCase().endsWith(".jar"))
{
classDir += java.io.File.separator;
}
//
// Reuse an existing class loader if we have already loaded a plug-in with
// the same value for classDir, otherwise create a new one.
//
ClassLoader cl = null;
if(_classLoaders == null)
{
_classLoaders = new java.util.HashMap<String, ClassLoader>();
}
else
{
cl = _classLoaders.get(classDir);
}
if(cl == null)
{
final java.net.URL[] url = new java.net.URL[] { new java.net.URL("file:///" + classDir) };
//
// Use the custom class loader (if any) as the parent.
//
if(_instance.initializationData().classLoader != null)
{
cl = new java.net.URLClassLoader(url, _instance.initializationData().classLoader);
}
else
{
cl = new java.net.URLClassLoader(url);
}
_classLoaders.put(classDir, cl);
}
c = cl.loadClass(className);
}
catch(java.net.MalformedURLException ex)
{
throw new PluginInitializationException("invalid entry point format `" + pluginSpec + "'", ex);
}
catch(java.io.IOException ex)
{
throw new PluginInitializationException("invalid path in entry point `" + pluginSpec + "'", ex);
}
catch(java.lang.ClassNotFoundException ex)
{
// Ignored
}
}
else
{
c = IceInternal.Util.getInstance(_communicator).findClass(className);
}
if(c == null)
{
throw new PluginInitializationException("class " + className + " not found");
}
java.lang.Object obj = c.newInstance();
try
{
pluginFactory = (PluginFactory)obj;
}
catch(ClassCastException ex)
{
throw new PluginInitializationException("class " + className + " does not implement Ice.PluginFactory",
ex);
}
}
catch(IllegalAccessException ex)
{
throw new PluginInitializationException("unable to access default constructor in class " + className, ex);
}
catch(InstantiationException ex)
{
throw new PluginInitializationException("unable to instantiate class " + className, ex);
}
//
// Invoke the factory.
//
Plugin plugin = null;
try
{
plugin = pluginFactory.create(_communicator, name, args);
}
catch(PluginInitializationException ex)
{
throw ex;
}
catch(Throwable ex)
{
throw new PluginInitializationException("exception in factory " + className, ex);
}
if(plugin == null)
{
throw new PluginInitializationException("failure in factory " + className);
}
PluginInfo info = new PluginInfo();
info.name = name;
info.plugin = plugin;
_plugins.add(info);
}
private Plugin
findPlugin(String name)
{
for(PluginInfo p : _plugins)
{
if(name.equals(p.name))
{
return p.plugin;
}
}
return null;
}
static class PluginInfo
{
String name;
Plugin plugin;
}
private Communicator _communicator;
private IceInternal.Instance _instance;
private java.util.List<PluginInfo> _plugins = new java.util.ArrayList<PluginInfo>();
private boolean _initialized;
private java.util.Map<String, ClassLoader> _classLoaders;
}
|