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
|
// **********************************************************************
//
// Copyright (c) 2002
// ZeroC, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
package Ice;
public final class PluginManagerI extends LocalObjectImpl implements PluginManager
{
public synchronized Plugin
getPlugin(String name)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
Plugin p = (Plugin)_plugins.get(name);
if(p != null)
{
return p;
}
throw new PluginNotFoundException();
}
public synchronized void
addPlugin(String name, Plugin plugin)
{
if(_communicator == null)
{
throw new CommunicatorDestroyedException();
}
if(_plugins.containsKey(name))
{
throw new PluginExistsException();
}
_plugins.put(name, plugin);
}
public synchronized void
destroy()
{
if(_communicator != null)
{
java.util.Iterator i = _plugins.entrySet().iterator();
while(i.hasNext())
{
Plugin p = (Plugin)i.next();
p.destroy();
}
_communicator = null;
}
}
public
PluginManagerI(Communicator communicator)
{
_communicator = communicator;
}
private 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=entry_point [args]
//
final String prefix = "Ice.Plugin.";
Ice.Properties properties = _communicator.getProperties();
java.util.Map plugins = properties.getPropertiesForPrefix(prefix);
java.util.Iterator p = plugins.entrySet().iterator();
while(p.hasNext())
{
java.util.Map.Entry entry = (java.util.Map.Entry)p.next();
String name = ((String)entry.getKey()).substring(prefix.length());
String value = (String)entry.getValue();
//
// Separate the entry point from the arguments.
//
String className;
String[] args;
int pos = value.indexOf(' ');
if(pos == -1)
{
pos = value.indexOf('\t');
}
if(pos == -1)
{
pos = value.indexOf('\n');
}
if(pos == -1)
{
className = value;
args = new String[0];
}
else
{
className = value.substring(0, pos);
args = value.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.
//
args = properties.parseCommandLineOptions(name, args);
cmdArgs.value = properties.parseCommandLineOptions(name, cmdArgs.value);
loadPlugin(name, className, args);
}
}
private void
loadPlugin(String name, String className, String[] args)
{
assert(_communicator != null);
//
// Instantiate the class.
//
PluginFactory factory = null;
try
{
Class c = Class.forName(className);
java.lang.Object obj = c.newInstance();
try
{
factory = (PluginFactory)obj;
}
catch(ClassCastException ex)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "class " + className + " does not implement Ice.PluginFactory";
e.initCause(ex);
throw e;
}
}
catch(ClassNotFoundException ex)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "class " + className + " not found";
e.initCause(ex);
throw e;
}
catch(IllegalAccessException ex)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "unable to access default constructor in class " + className;
e.initCause(ex);
throw e;
}
catch(InstantiationException ex)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "unable to instantiate class " + className;
e.initCause(ex);
throw e;
}
//
// Invoke the factory.
//
Plugin plugin = null;
try
{
plugin = factory.create(_communicator, name, args);
}
catch(Exception ex)
{
PluginInitializationException e = new PluginInitializationException();
e.reason = "exception in factory " + className;
e.initCause(ex);
throw e;
}
_plugins.put(name, plugin);
}
private Communicator _communicator;
private java.util.HashMap _plugins = new java.util.HashMap();
}
|