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
|
// **********************************************************************
//
// Copyright (c) 2003-2009 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.
//
// **********************************************************************
namespace Ice
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
public interface PluginFactory
{
Plugin create(Communicator communicator, string name, string[] args);
}
public sealed class PluginManagerI : PluginManager
{
private static string _kindOfObject = "plugin";
public void initializePlugins()
{
if(_initialized)
{
InitializationException ex = new InitializationException();
ex.reason = "plugins already initialized";
throw ex;
}
//
// Invoke initialize() on the plugins, in the order they were loaded.
//
ArrayList initializedPlugins = new ArrayList();
try
{
foreach(Plugin p in _initOrder)
{
p.initialize();
initializedPlugins.Add(p);
}
}
catch(Exception)
{
//
// Destroy the plugins that have been successfully initialized, in the
// reverse order.
//
initializedPlugins.Reverse();
foreach(Plugin p in initializedPlugins)
{
try
{
p.destroy();
}
catch(Exception)
{
// Ignore.
}
}
throw;
}
_initialized = true;
}
public Plugin getPlugin(string name)
{
lock(this)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
Plugin p = (Plugin)_plugins[name];
if(p != null)
{
return p;
}
NotRegisteredException ex = new NotRegisteredException();
ex.id = name;
ex.kindOfObject = _kindOfObject;
throw ex;
}
}
public void addPlugin(string name, Plugin plugin)
{
lock(this)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
if(_plugins.Contains(name))
{
AlreadyRegisteredException ex = new AlreadyRegisteredException();
ex.id = name;
ex.kindOfObject = _kindOfObject;
throw ex;
}
_plugins[name] = plugin;
}
}
public void destroy()
{
lock(this)
{
if(_communicator != null)
{
foreach(Plugin plugin in _plugins.Values)
{
plugin.destroy();
}
_communicator = null;
}
}
}
public PluginManagerI(Communicator communicator)
{
_communicator = communicator;
_plugins = new Hashtable();
_initOrder = new ArrayList();
_initialized = false;
}
public void loadPlugins(ref string[] cmdArgs)
{
Debug.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]
//
// The code below is different from the Java/C++ algorithm
// because C# must support full assembly names such as:
//
// Ice.Plugin.Logger=logger, Version=0.0.0.0, Culture=neutral:LoginPluginFactory
//
// If the Ice.PluginLoadOrder property is defined, load the
// specified plugins in the specified order, then load any
// remaining plugins.
//
string prefix = "Ice.Plugin.";
Properties properties = _communicator.getProperties();
Dictionary<string, string> plugins = properties.getPropertiesForPrefix(prefix);
string[] loadOrder = properties.getPropertyAsList("Ice.PluginLoadOrder");
for(int i = 0; i < loadOrder.Length; ++i)
{
if(loadOrder[i].Length == 0)
{
continue;
}
if(_plugins.Contains(loadOrder[i]))
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "plugin `" + loadOrder[i] + "' already loaded";
throw e;
}
string key = "Ice.Plugin." + loadOrder[i] + ".clr";
bool hasKey = plugins.ContainsKey(key);
if(hasKey)
{
plugins.Remove("Ice.Plugin." + loadOrder[i]);
}
else
{
key = "Ice.Plugin." + loadOrder[i];
hasKey = plugins.ContainsKey(key);
}
if(hasKey)
{
string value = plugins[key];
loadPlugin(loadOrder[i], value, ref cmdArgs);
plugins.Remove(key);
}
else
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "plugin `" + loadOrder[i] + "' not defined";
throw e;
}
}
//
// Load any remaining plugins that weren't specified in PluginLoadOrder.
//
while(plugins.Count > 0)
{
IEnumerator<KeyValuePair<string, string>> p = plugins.GetEnumerator();
p.MoveNext();
string key = p.Current.Key;
string val = p.Current.Value;
string name = key.Substring(prefix.Length);
int dotPos = name.LastIndexOf('.');
if(dotPos != -1)
{
string suffix = name.Substring(dotPos + 1);
if(suffix.Equals("cpp") || suffix.Equals("java"))
{
//
// Ignored
//
plugins.Remove(key);
}
else if(suffix.Equals("clr"))
{
name = name.Substring(0, dotPos);
loadPlugin(name, val, ref cmdArgs);
plugins.Remove(key);
plugins.Remove("Ice.Plugin." + name);
}
else
{
//
// Name is just a regular name that happens to contain a dot
//
dotPos = -1;
}
}
if(dotPos == -1)
{
plugins.Remove(key);
//
// Is there a .clr entry?
//
string clrKey = "Ice.Plugin." + name + ".clr";
if(plugins.ContainsKey(clrKey))
{
val = plugins[clrKey];
plugins.Remove(clrKey);
}
loadPlugin(name, val, ref cmdArgs);
}
}
//
// An application can set Ice.InitPlugins=0 if it wants to postpone
// initialization until after it has interacted directly with the
// plugins.
//
if(properties.getPropertyAsIntWithDefault("Ice.InitPlugins", 1) > 0)
{
initializePlugins();
}
}
private void loadPlugin(string name, string pluginSpec, ref string[] cmdArgs)
{
Debug.Assert(_communicator != null);
//
// Separate the entry point from the arguments. First
// look for the :, then for the next whitespace. This
// represents the end of the entry point.
//
// The remainder of the configuration line represents
// the arguments.
//
string entryPoint = pluginSpec;
string[] args = new string[0];
int start = pluginSpec.IndexOf(':');
if(start != -1)
{
//
// Skip drive letter, if any.
//
if(pluginSpec.Length > 3 &&
start == 1 &&
System.Char.IsLetter(pluginSpec[0]) &&
(pluginSpec[2] == '\\' || pluginSpec[2] == '/'))
{
start = pluginSpec.IndexOf(':', 3);
}
//
// Find the whitespace.
//
int pos = pluginSpec.IndexOf(' ', start);
if(pos == -1)
{
pos = pluginSpec.IndexOf('\t', start);
}
if(pos == -1)
{
pos = pluginSpec.IndexOf('\n', start);
}
if(pos != -1)
{
entryPoint = pluginSpec.Substring(0, pos);
char[] delims = { ' ', '\t', '\n' };
args = pluginSpec.Substring(pos).Trim().Split(delims, 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 = properties.parseCommandLineOptions(name, cmdArgs);
//
// Retrieve the assembly name and the type.
//
string err = "unable to load plugin '" + entryPoint + "': ";
int sepPos = entryPoint.IndexOf(':');
if (sepPos == -1)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = err + "invalid entry point format";
throw e;
}
System.Reflection.Assembly pluginAssembly = null;
string assemblyName = entryPoint.Substring(0, sepPos);
try
{
if (System.IO.File.Exists(assemblyName))
{
pluginAssembly = System.Reflection.Assembly.LoadFrom(assemblyName);
}
else
{
pluginAssembly = System.Reflection.Assembly.Load(assemblyName);
}
}
catch(System.Exception ex)
{
//
// IceSSL is not supported with Mono 1.2. We avoid throwing an exception in that case,
// so the same configuration can be used with Mono or Visual C#.
//
if(IceInternal.AssemblyUtil.runtime_ == IceInternal.AssemblyUtil.Runtime.Mono && name == "IceSSL")
{
if(!_sslWarnOnce)
{
_communicator.getLogger().warning(
"IceSSL plugin not loaded: IceSSL is not supported with Mono");
_sslWarnOnce = true;
}
return;
}
PluginInitializationException e = new PluginInitializationException();
e.reason = err + "unable to load assembly: '" + assemblyName + "': " + ex.ToString();
throw e;
}
//
// Instantiate the class.
//
PluginFactory pluginFactory = null;
string className = entryPoint.Substring(sepPos + 1);
System.Type c = pluginAssembly.GetType(className);
if(c == null)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = err + "GetType failed for '" + className + "'";
throw e;
}
try
{
pluginFactory = (PluginFactory)IceInternal.AssemblyUtil.createInstance(c);
if(pluginFactory == null)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = err + "Can't find constructor for '" + className + "'";
throw e;
}
}
catch(System.InvalidCastException ex)
{
PluginInitializationException e = new PluginInitializationException(ex);
e.reason = err + "InvalidCastException to Ice.PluginFactory";
throw e;
}
catch(System.UnauthorizedAccessException ex)
{
PluginInitializationException e = new PluginInitializationException(ex);
e.reason = err + "UnauthorizedAccessException: " + ex.ToString();
throw e;
}
catch(System.Exception ex)
{
PluginInitializationException e = new PluginInitializationException(ex);
e.reason = err + "System.Exception: " + ex.ToString();
throw e;
}
Plugin plugin = null;
try
{
plugin = pluginFactory.create(_communicator, name, args);
}
catch(PluginInitializationException ex)
{
ex.reason = err + ex.reason;
throw ex;
}
catch(System.Exception ex)
{
PluginInitializationException e = new PluginInitializationException(ex);
e.reason = err + "System.Exception in factory.create: " + ex.ToString();
throw e;
}
if(plugin == null)
{
PluginInitializationException ex = new PluginInitializationException();
ex.reason = err + "factory.create returned null plug-in";
throw ex;
}
_plugins[name] = plugin;
_initOrder.Add(plugin);
}
private Communicator _communicator;
private Hashtable _plugins;
private ArrayList _initOrder;
private bool _initialized;
private static bool _sslWarnOnce = false;
}
}
|