summaryrefslogtreecommitdiff
path: root/java/src/Ice/PluginManagerI.java
blob: 56d9ea989d4ced6721ac36ceb229bf1edbe9dcc6 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2006 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 extends LocalObjectImpl implements PluginManager
{
    private static String _kindOfObject = "plugin";

    public synchronized 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.
	//
	java.util.ArrayList initializedPlugins = new java.util.ArrayList();
	try
	{
	    java.util.Iterator i = _initOrder.iterator();
	    while(i.hasNext())
	    {
		Plugin p = (Plugin)i.next();
		p.initialize();
		initializedPlugins.add(p);
	    }
	}
	catch(RuntimeException ex)
	{
	    //
	    // Destroy the plugins that have been successfully initialized, in the
	    // reverse order.
	    //
	    java.util.ListIterator i = initializedPlugins.listIterator(initializedPlugins.size());
	    while(i.hasPrevious())
	    {
		Plugin p = (Plugin)i.previous();
		try
		{
		    p.destroy();
		}
		catch(RuntimeException e)
		{
		    // Ignore.
		}
	    }
	    throw ex;
	}

	_initialized = true;
    }

    public synchronized Plugin
    getPlugin(String name)
    {
	if(_communicator == null)
	{
	    throw new CommunicatorDestroyedException();
	}

        Plugin p = (Plugin)_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)
	{
	    java.util.Iterator i = _plugins.values().iterator();
	    while(i.hasNext())
	    {
		Plugin p = (Plugin)i.next();
		p.destroy();
	    }

	    _logger = null;
	    _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=entry_point [args]
        //
	// If the Ice.PluginLoadOrder property is defined, load the
	// specified plugins in the specified order, then load any
	// remaining plugins.
	//
        final String prefix = "Ice.Plugin.";
	Properties properties = _communicator.getProperties();
        java.util.Map plugins = properties.getPropertiesForPrefix(prefix);

	final String loadOrder = properties.getProperty("Ice.PluginLoadOrder");
	if(loadOrder.length() > 0)
	{
	    String[] names = loadOrder.split("[, \t\n]+");
	    for(int i = 0; i < names.length; ++i)
	    {
		if(_plugins.containsKey(names[i]))
		{
		    PluginInitializationException ex = new PluginInitializationException();
		    ex.reason = "plugin `" + names[i] + "' already loaded";
		    throw ex;
		}

		final String key = "Ice.Plugin." + names[i];
		if(plugins.containsKey(key))
		{
		    final String value = (String)plugins.get(key);
		    loadPlugin(names[i], value, cmdArgs, false);
		    plugins.remove(key);
		}
		else
		{
		    PluginInitializationException ex = new PluginInitializationException();
		    ex.reason = "plugin `" + names[i] + "' not defined";
		    throw ex;
		}
	    }
	}

	//
	// Load any remaining plugins that weren't specified in PluginLoadOrder.
	//
        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();
            loadPlugin(name, value, cmdArgs, false);
        }

	//
	// Check for a Logger Plugin
	//
	String loggerStr = properties.getProperty("Ice.LoggerPlugin");
	if(loggerStr.length() != 0)
	{
	    loadPlugin("Logger", loggerStr, cmdArgs, true);
	}

	//
	// 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, StringSeqHolder cmdArgs, boolean isLogger)
    {
	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;
        LoggerFactory loggerFactory = null;
        try
        {
            Class c = Class.forName(className);
            java.lang.Object obj = c.newInstance();
            try
            {
	        if(isLogger)
		{
                    loggerFactory = (LoggerFactory)obj;
		}
		else
		{
                    pluginFactory = (PluginFactory)obj;
		}
            }
            catch(ClassCastException ex)
            {
                PluginInitializationException e = new PluginInitializationException();
                e.reason = "class " + className + " does not implement " + 
			   (isLogger ? "Ice.LoggerFactory" : "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.
        //
	if(isLogger)
	{
            try
            {
                _logger = loggerFactory.create(_communicator, args);
            }
            catch(Throwable ex)
            {
                PluginInitializationException e = new PluginInitializationException();
                e.reason = "exception in factory " + className;
                e.initCause(ex);
                throw e;
            }

	    if(_logger == null)
	    {
                PluginInitializationException e = new PluginInitializationException();
                e.reason = "failure in factory " + className;
                throw e;
	    }
	}
	else
	{
            Plugin plugin = null;
            try
            {
                plugin = pluginFactory.create(_communicator, name, args);
            }
            catch(PluginInitializationException ex)
            {
                throw ex;
            }
            catch(Throwable ex)
            {
                PluginInitializationException e = new PluginInitializationException();
                e.reason = "exception in factory " + className;
                e.initCause(ex);
                throw e;
            }

	    if(plugin == null)
	    {
                PluginInitializationException e = new PluginInitializationException();
                e.reason = "failure in factory " + className;
                throw e;
	    }

            _plugins.put(name, plugin);
	    _initOrder.add(plugin);
	}
    }

    public Logger
    getLogger()
    {
        return _logger;
    }

    private Communicator _communicator;
    private java.util.HashMap _plugins = new java.util.HashMap();
    private java.util.ArrayList _initOrder = new java.util.ArrayList();
    private Logger _logger = null;
    private boolean _initialized;
}