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
|
// **********************************************************************
//
// 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.
//
// **********************************************************************
#include <IceUtil/Options.h>
#include <Ice/PluginManagerI.h>
#include <Ice/DynamicLibrary.h>
#include <Ice/Communicator.h>
#include <Ice/Properties.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Initialize.h>
#include <Ice/Instance.h>
#include <Ice/LocalException.h>
using namespace std;
using namespace Ice;
using namespace IceInternal;
const char * const Ice::PluginManagerI::_kindOfObject = "plugin";
typedef Ice::Plugin* (*PLUGIN_FACTORY)(const CommunicatorPtr&, const string&, const StringSeq&);
void
Ice::PluginManagerI::initializePlugins()
{
if(_initialized)
{
InitializationException ex(__FILE__, __LINE__);
ex.reason = "plug-ins already initialized";
throw ex;
}
//
// Invoke initialize() on the plug-ins, in the order they were loaded.
//
vector<PluginPtr> initializedPlugins;
try
{
for(PluginInfoList::iterator p = _plugins.begin(); p != _plugins.end(); ++p)
{
p->plugin->initialize();
initializedPlugins.push_back(p->plugin);
}
}
catch(...)
{
//
// Destroy the plug-ins that have been successfully initialized, in the
// reverse order.
//
for(vector<PluginPtr>::reverse_iterator p = initializedPlugins.rbegin(); p != initializedPlugins.rend(); ++p)
{
try
{
(*p)->destroy();
}
catch(...)
{
// Ignore.
}
}
throw;
}
_initialized = true;
}
StringSeq
Ice::PluginManagerI::getPlugins()
{
IceUtil::Mutex::Lock sync(*this);
StringSeq names;
for(PluginInfoList::iterator p = _plugins.begin(); p != _plugins.end(); ++p)
{
names.push_back(p->name);
}
return names;
}
PluginPtr
Ice::PluginManagerI::getPlugin(const string& name)
{
IceUtil::Mutex::Lock sync(*this);
if(!_communicator)
{
throw CommunicatorDestroyedException(__FILE__, __LINE__);
}
PluginPtr p = findPlugin(name);
if(p)
{
return p;
}
NotRegisteredException ex(__FILE__, __LINE__);
ex.kindOfObject = _kindOfObject;
ex.id = name;
throw ex;
}
void
Ice::PluginManagerI::addPlugin(const string& name, const PluginPtr& plugin)
{
IceUtil::Mutex::Lock sync(*this);
if(!_communicator)
{
throw CommunicatorDestroyedException(__FILE__, __LINE__);
}
if(findPlugin(name))
{
AlreadyRegisteredException ex(__FILE__, __LINE__);
ex.kindOfObject = _kindOfObject;
ex.id = name;
throw ex;
}
PluginInfo info;
info.name = name;
info.plugin = plugin;
_plugins.push_back(info);
}
void
Ice::PluginManagerI::destroy()
{
IceUtil::Mutex::Lock sync(*this);
if(_communicator)
{
if(_initialized)
{
//
// Destroy the plug-ins that have been successfully initialized, in the
// reverse order.
//
for(PluginInfoList::reverse_iterator p = _plugins.rbegin(); p != _plugins.rend(); ++p)
{
try
{
p->plugin->destroy();
}
catch(const std::exception& ex)
{
Warning out(getProcessLogger());
out << "unexpected exception raised by plug-in `" << p->name << "' destruction:\n" << ex.what();
}
catch(const std::string& str)
{
Warning out(getProcessLogger());
out << "unexpected exception raised by plug-in `" << p->name << "' destruction:\n" << str;
}
catch(const char* msg)
{
Warning out(getProcessLogger());
out << "unexpected exception raised by plug-in `" << p->name << "' destruction:\n" << msg;
}
catch(...)
{
Warning out(getProcessLogger());
out << "unexpected exception raised by plug-in `" << p->name << "' destruction";
}
}
}
_communicator = 0;
}
_plugins.clear();
_libraries = 0;
}
Ice::PluginManagerI::PluginManagerI(const CommunicatorPtr& communicator, const DynamicLibraryListPtr& libraries) :
_communicator(communicator),
_libraries(libraries),
_initialized(false)
{
}
void
Ice::PluginManagerI::loadPlugins(int& argc, char* argv[])
{
assert(_communicator);
StringSeq cmdArgs = argsToStringSeq(argc, argv);
//
// 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.
//
const string prefix = "Ice.Plugin.";
PropertiesPtr properties = _communicator->getProperties();
PropertyDict plugins = properties->getPropertiesForPrefix(prefix);
StringSeq loadOrder = properties->getPropertyAsList("Ice.PluginLoadOrder");
for(StringSeq::const_iterator p = loadOrder.begin(); p != loadOrder.end(); ++p)
{
string name = *p;
if(findPlugin(name))
{
PluginInitializationException ex(__FILE__, __LINE__);
ex.reason = "plug-in `" + name + "' already loaded";
throw ex;
}
PropertyDict::iterator r = plugins.find("Ice.Plugin." + name + ".cpp");
if(r == plugins.end())
{
r = plugins.find("Ice.Plugin." + name);
}
else
{
plugins.erase("Ice.Plugin." + name);
}
if(r != plugins.end())
{
loadPlugin(name, r->second, cmdArgs);
plugins.erase(r);
}
else
{
PluginInitializationException ex(__FILE__, __LINE__);
ex.reason = "plug-in `" + name + "' not defined";
throw ex;
}
}
//
// Load any remaining plug-ins that weren't specified in PluginLoadOrder.
//
while(!plugins.empty())
{
PropertyDict::iterator p = plugins.begin();
string name = p->first.substr(prefix.size());
size_t dotPos = name.find_last_of('.');
if(dotPos != string::npos)
{
string suffix = name.substr(dotPos + 1);
if(suffix == "java" || suffix == "clr")
{
//
// Ignored
//
plugins.erase(p);
}
else if(suffix == "cpp")
{
name = name.substr(0, dotPos);
loadPlugin(name, p->second, cmdArgs);
plugins.erase(p);
plugins.erase("Ice.Plugin." + name);
}
else
{
//
// Name is just a regular name that happens to contain a dot
//
dotPos = string::npos;
}
}
if(dotPos == string::npos)
{
//
// Is there a .cpp entry?
//
PropertyDict::iterator q = plugins.find("Ice.Plugin." + name + ".cpp");
if(q != plugins.end())
{
plugins.erase(p);
p = q;
}
loadPlugin(name, p->second, cmdArgs);
plugins.erase(p);
}
}
stringSeqToArgs(cmdArgs, argc, argv);
}
void
Ice::PluginManagerI::loadPlugin(const string& name, const string& pluginSpec, StringSeq& cmdArgs)
{
assert(_communicator);
//
// Split the entire property value into arguments. An entry point containing spaces
// must be enclosed in quotes.
//
StringSeq args;
try
{
args = IceUtilInternal::Options::split(pluginSpec);
}
catch(const IceUtilInternal::BadOptException& ex)
{
PluginInitializationException e(__FILE__, __LINE__);
e.reason = "invalid arguments for plug-in `" + name + "':\n" + ex.reason;
throw e;
}
assert(!args.empty());
//
// Shift the arguments.
//
const string entryPoint = args[0];
args.erase(args.begin());
//
// 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.
//
PropertiesPtr properties = _communicator->getProperties();
args = properties->parseCommandLineOptions(name, args);
cmdArgs = properties->parseCommandLineOptions(name, cmdArgs);
//
// Load the entry point symbol.
//
PluginPtr plugin;
DynamicLibraryPtr library = new DynamicLibrary(IceInternal::getInstance(_communicator)->initializationData().stringConverter);
DynamicLibrary::symbol_type sym = library->loadEntryPoint(entryPoint);
if(sym == 0)
{
ostringstream out;
string msg = library->getErrorMessage();
out << "unable to load entry point `" << entryPoint << "'";
if(!msg.empty())
{
out << ": " + msg;
}
PluginInitializationException ex(__FILE__, __LINE__);
ex.reason = out.str();
throw ex;
}
//
// Invoke the factory function. No exceptions can be raised
// by the factory function because it's declared extern "C".
//
PLUGIN_FACTORY factory = (PLUGIN_FACTORY)sym;
plugin = factory(_communicator, name, args);
if(!plugin)
{
PluginInitializationException e(__FILE__, __LINE__);
ostringstream out;
out << "failure in entry point `" << entryPoint << "'";
e.reason = out.str();
throw e;
}
PluginInfo info;
info.name = name;
info.plugin = plugin;
_plugins.push_back(info);
_libraries->add(library);
}
Ice::PluginPtr
Ice::PluginManagerI::findPlugin(const string& name) const
{
for(PluginInfoList::const_iterator p = _plugins.begin(); p != _plugins.end(); ++p)
{
if(name == p->name)
{
return p->plugin;
}
}
return 0;
}
void
IceInternal::loadPlugin(const Ice::CommunicatorPtr& communicator,
const string& name,
const string& pluginSpec,
Ice::StringSeq& cmdArgs)
{
PluginManagerIPtr pluginManager = PluginManagerIPtr::dynamicCast(getInstance(communicator)->pluginManager());
pluginManager->loadPlugin(name, pluginSpec, cmdArgs);
}
|