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
|
// **********************************************************************
//
// Copyright (c) 2003-2011 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(Plugin p : _initOrder)
{
p.initialize();
initializedPlugins.add(p);
}
}
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(java.util.Map.Entry<String, Plugin> p : _plugins.entrySet())
{
names.add(p.getKey());
}
return names.toArray(new String[0]);
}
public synchronized Plugin
getPlugin(String name)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
Plugin p = _plugins.get(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(_plugins.containsKey(name))
{
AlreadyRegisteredException ex = new AlreadyRegisteredException();
ex.id = name;
ex.kindOfObject = _kindOfObject;
throw ex;
}
_plugins.put(name, plugin);
}
public synchronized void
destroy()
{
if(_communicator != null)
{
if(_initialized)
{
for(java.util.Map.Entry<String, Plugin> p : _plugins.entrySet())
{
try
{
p.getValue().destroy();
}
catch(RuntimeException ex)
{
Ice.Util.getProcessLogger().warning("unexpected exception raised by plug-in `" + p.getKey() +
"' destruction:\n" + ex.toString());
}
}
}
_communicator = null;
}
}
public
PluginManagerI(Communicator communicator)
{
_communicator = communicator;
_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(_plugins.containsKey(name))
{
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);
//
// Separate the entry point from the arguments.
//
String className;
String[] args;
int pos = pluginSpec.indexOf(' ');
if(pos == -1)
{
pos = pluginSpec.indexOf('\t');
}
if(pos == -1)
{
pos = pluginSpec.indexOf('\n');
}
if(pos == -1)
{
className = pluginSpec;
args = new String[0];
}
else
{
className = pluginSpec.substring(0, pos);
args = pluginSpec.substring(pos).trim().split("[ \t\n]+", pos);
}
//
// 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 = IceInternal.Util.getInstance(_communicator).findClass(className);
if(c == null)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "class " + className + " not found";
throw e;
}
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);
}
_plugins.put(name, plugin);
_initOrder.add(plugin);
}
private Communicator _communicator;
private java.util.Map<String, Plugin> _plugins = new java.util.HashMap<String, Plugin>();
private java.util.List<Plugin> _initOrder = new java.util.ArrayList<Plugin>();
private boolean _initialized;
}
|