File: PluginManager.java

package info (click to toggle)
libjpf-java 1.5.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,280 kB
  • ctags: 2,079
  • sloc: java: 13,449; xml: 337; sh: 48; makefile: 10
file content (462 lines) | stat: -rw-r--r-- 16,353 bytes parent folder | download | duplicates (4)
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
450
451
452
453
454
455
456
457
458
459
460
461
462
/*****************************************************************************
 * Java Plug-in Framework (JPF)
 * Copyright (C) 2004-2007 Dmitry Olshansky
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *****************************************************************************/
package org.java.plugin;

import java.net.URL;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Map;

import org.java.plugin.registry.Identity;
import org.java.plugin.registry.PluginDescriptor;
import org.java.plugin.registry.PluginRegistry;

/**
 * JPF "runtime" class - the entry point to the framework API. It is expected
 * that only one instance of this class will be created per application (other
 * scenarios may be possible but not tested).
 * <p>
 * Usage example. Somewhere in the beginning of your application:
 * 
 * <pre>
 * manager = factory.createManager();
 * manager.publishPlugins(getLocations(dir));
 * </pre>
 * 
 * Later on, before accessing plug-in:
 * 
 * <pre>
 * manager.activatePlugin(pluginId);
 * </pre>
 * 
 * @see org.java.plugin.ObjectFactory#createManager()
 * 
 * @version $Id: PluginManager.java,v 1.5 2007/04/07 12:42:14 ddimon Exp $
 */
public abstract class PluginManager {
    /**
     * JPF version number.
     */
    //NB: don't forget to update version number with new release
    public static final String VERSION = "1.5.1"; //$NON-NLS-1$
    
    /**
     * JPF version system property name.
     */
    public static final String VERSION_PROPERTY =
        "org.java.plugin.jpf-version"; //$NON-NLS-1$
    
    static {
        try {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                public Object run() {
                    System.setProperty(VERSION_PROPERTY, VERSION);
                    return null;
                }
            });
        } catch (SecurityException se) {
            // ignore
        }
    }
    
    /**
     * Looks for plug-in manager instance for given object.
     * 
     * @param obj
     *            any object that may be managed by some plug-in manager
     * @return plug-in manager instance or <code>null</code> if given object
     *         doesn't belong to any plug-in (possibly it is part of "host"
     *         application) and thus doesn't managed by the Framework directly
     *         or indirectly.
     */
    public static PluginManager lookup(final Object obj) {
        if (obj == null) {
            return null;
        }
        ClassLoader clsLoader;
        if (obj instanceof Plugin) {
            return ((Plugin) obj).getManager();
        } else if (obj instanceof Class) {
            clsLoader = ((Class) obj).getClassLoader();
        } else if (obj instanceof ClassLoader) {
            clsLoader = (ClassLoader) obj;
        } else {
            clsLoader = obj.getClass().getClassLoader();
        }
        if (!(clsLoader instanceof PluginClassLoader)) {
            return lookup(clsLoader.getParent());
        }
        return ((PluginClassLoader) clsLoader).getPluginManager();
    }

    /**
     * @return registry, used by this manager
     */
    public abstract PluginRegistry getRegistry();

    /**
     * @return path resolver
     */
    public abstract PathResolver getPathResolver();

    /**
     * Registers plug-ins and their locations with this plug-in manager. You
     * should use this method to register new plug-ins to make them available
     * for activation with this manager instance (compare this to
     * {@link PluginRegistry#register(URL[])} method that just makes plug-in's
     * meta-data available for reading and doesn't "know" where are things
     * actually located).
     * <p>
     * Note that this method only load plug-ins to this manager but not activate
     * them. Call {@link #activatePlugin(String)} method to make plug-in
     * activated. It is recommended to do this immediately before first plug-in
     * use.
     * 
     * @param locations
     *            plug-in locations data
     * @return map where keys are manifest URL's and values are registered
     *         plug-ins or plug-in fragments, URL's for unprocessed manifests
     *         are not included
     * @throws JpfException
     *             if given plug-ins can't be registered or published (optional
     *             behavior)
     * 
     * @see org.java.plugin.registry.PluginDescriptor
     * @see org.java.plugin.registry.PluginFragment
     */
    public abstract Map<String, Identity> publishPlugins(
            final PluginLocation[] locations) throws JpfException;

    /**
     * Looks for plug-in with given ID and activates it if it is not activated
     * yet. Note that this method will never return <code>null</code>.
     * 
     * @param id
     *            plug-in ID
     * @return found plug-in
     * @throws PluginLifecycleException
     *             if plug-in can't be found or activated
     */
    public abstract Plugin getPlugin(final String id)
            throws PluginLifecycleException;

    /**
     * Activates plug-in with given ID if it is not activated yet. Actually this
     * makes plug-in "running" and calls {@link Plugin#doStart()} method. This
     * method will effectively activate all depending plug-ins. It is safe to
     * call this method more than once.
     * 
     * @param id
     *            plug-in ID
     * @throws PluginLifecycleException
     *             if plug-in can't be found or activated
     */
    public abstract void activatePlugin(final String id)
            throws PluginLifecycleException;

    /**
     * Looks for plug-in, given object belongs to.
     * 
     * @param obj
     *            any object that maybe belongs to some plug-in
     * @return plug-in or <code>null</code> if given object doesn't belong to
     *         any plug-in (possibly it is part of "host" application) and thus
     *         doesn't managed by the Framework directly or indirectly
     */
    public abstract Plugin getPluginFor(final Object obj);

    /**
     * @param descr
     *            plug-in descriptor
     * @return <code>true</code> if plug-in with given descriptor is activated
     */
    public abstract boolean isPluginActivated(final PluginDescriptor descr);

    /**
     * @param descr
     *            plug-in descriptor
     * @return <code>true</code> if plug-in disabled as it's activation fails
     */
    public abstract boolean isBadPlugin(final PluginDescriptor descr);

    /**
     * @param descr
     *            plug-in descriptor
     * @return <code>true</code> if plug-in is currently activating
     */
    public abstract boolean isPluginActivating(final PluginDescriptor descr);

    /**
     * Returns instance of plug-in's class loader and not tries to activate
     * plug-in. Use this method if you need to get access to plug-in resources
     * and don't want to cause plug-in activation.
     * 
     * @param descr
     *            plug-in descriptor
     * @return class loader instance for plug-in with given descriptor
     */
    public abstract PluginClassLoader getPluginClassLoader(
            final PluginDescriptor descr);

    /**
     * Shuts down the framework. <br>
     * Calling this method will deactivate all active plug-ins in order, reverse
     * to the order they was activated. It also releases all resources allocated
     * by this manager (class loaders, plug-in descriptors etc.). All disabled
     * plug-ins will be marked as "enabled", all registered event listeners will
     * be unregistered.
     */
    public abstract void shutdown();

    /**
     * Deactivates plug-in with given ID if it has been successfully activated
     * before. This method makes plug-in "not running" and calls
     * {@link Plugin#doStop()} method. Note that this method will effectively
     * deactivate all depending plug-ins.
     * 
     * @param id
     *            plug-in ID
     */
    public abstract void deactivatePlugin(final String id);

    /**
     * Disables plug-in (with dependencies) in this manager instance. Disabled
     * plug-in can't be activated although it may be valid and successfully
     * registered with plug-in registry. Before disabling, plug-in will be
     * deactivated if it was successfully activated.
     * <p>
     * Be careful with this method as it can effectively disable large set of
     * inter-depending plug-ins and your application may become unstable or even
     * disabled as whole.
     * 
     * @param descr
     *            descriptor of plug-in to be disabled
     * @return descriptors of plug-ins that was actually disabled
     */
    public abstract PluginDescriptor[] disablePlugin(
            final PluginDescriptor descr);

    /**
     * Enables plug-in (or plug-ins) in this manager instance. Don't miss this
     * with plug-in activation semantic. Enabled plug-in is simply ready to be
     * activated. By default all loaded plug-ins are enabled.
     * 
     * @param descr
     *            descriptor of plug-in to be enabled
     * @param includeDependings
     *            if <code>true</code>, depending plug-ins will be also
     *            enabled
     * @return descriptors of plug-ins that was actually enabled
     * @see #disablePlugin(PluginDescriptor)
     */
    public abstract PluginDescriptor[] enablePlugin(
            final PluginDescriptor descr, final boolean includeDependings);

    /**
     * @param descr
     *            plug-in descriptor
     * @return <code>true</code> if given plug-in is disabled in this manager
     */
    public abstract boolean isPluginEnabled(final PluginDescriptor descr);

    /**
     * Registers plug-in manager event listener. If given listener has been
     * registered before, this method will throw an
     * {@link IllegalArgumentException}.
     * 
     * @param listener
     *            new manager event listener
     */
    public abstract void registerListener(final EventListener listener);

    /**
     * Unregisters manager event listener. If given listener hasn't been
     * registered before, this method will throw an
     * {@link IllegalArgumentException}.
     * 
     * @param listener
     *            registered listener
     */
    public abstract void unregisterListener(final EventListener listener);

    // Delegating methods

    /**
     * Initializes given plug-in with this manager instance and given
     * descriptor.
     * 
     * @param plugin
     *            plug-in instance to be initialized
     * @param descr
     *            plug-in descriptor
     */
    protected final void initPlugin(final Plugin plugin,
            final PluginDescriptor descr) {
        plugin.setManager(this);
        plugin.setDescriptor(descr);
    }

    /**
     * Starts given plug-in. Simply forward call to {@link Plugin#doStart()}
     * method.
     * 
     * @param plugin
     *            plug-in to be started
     * @throws Exception
     *             if any error has occurred during plug-in start
     */
    protected final void startPlugin(final Plugin plugin) throws Exception {
        plugin.start();
    }

    /**
     * Stops given plug-in. Simply forward call to {@link Plugin#doStop()}
     * method.
     * 
     * @param plugin
     *            plug-in to be stopped
     * @throws Exception
     *             if any error has occurred during plug-in stop
     */
    protected final void stopPlugin(final Plugin plugin) throws Exception {
        plugin.stop();
    }

    /**
     * Forwards call to {@link PluginClassLoader#dispose()} method.
     * 
     * @param cl
     *            plug-in class loader to be disposed
     */
    protected final void disposeClassLoader(final PluginClassLoader cl) {
        cl.dispose();
    }

    /**
     * Forwards call to {@link PluginClassLoader#pluginsSetChanged()} method.
     * 
     * @param cl
     *            plug-in class loader to be notified
     */
    protected final void notifyClassLoader(final PluginClassLoader cl) {
        cl.pluginsSetChanged();
    }

    /**
     * Plug-ins life-cycle events callback interface.
     * 
     * @version $Id: PluginManager.java,v 1.5 2007/04/07 12:42:14 ddimon Exp $
     */
    public interface EventListener {
        /**
         * This method will be called by the manager just after plug-in has been
         * successfully activated.
         * 
         * @param plugin
         *            just activated plug-in
         */
        void pluginActivated(Plugin plugin);

        /**
         * This method will be called by the manager just before plug-in
         * deactivation.
         * 
         * @param plugin
         *            plug-in to be deactivated
         */
        void pluginDeactivated(Plugin plugin);

        /**
         * This method will be called by the manager just before plug-in
         * disabling.
         * 
         * @param descriptor
         *            descriptor of plug-in to be disabled
         */
        void pluginDisabled(PluginDescriptor descriptor);

        /**
         * This method will be called by the manager just after plug-in
         * enabling.
         * 
         * @param descriptor
         *            descriptor of enabled plug-in
         */
        void pluginEnabled(PluginDescriptor descriptor);
    }

    /**
     * An abstract adapter class for receiving plug-ins life-cycle events. The
     * methods in this class are empty. This class exists as convenience for
     * creating listener objects.
     * 
     * @version $Id: PluginManager.java,v 1.5 2007/04/07 12:42:14 ddimon Exp $
     */
    public abstract static class EventListenerAdapter implements EventListener {
        /**
         * @see org.java.plugin.PluginManager.EventListener#pluginActivated(
         *      org.java.plugin.Plugin)
         */
        public void pluginActivated(final Plugin plugin) {
            // no-op
        }

        /**
         * @see org.java.plugin.PluginManager.EventListener#pluginDeactivated(
         *      org.java.plugin.Plugin)
         */
        public void pluginDeactivated(final Plugin plugin) {
            // no-op
        }

        /**
         * @see org.java.plugin.PluginManager.EventListener#pluginDisabled(
         *      org.java.plugin.registry.PluginDescriptor)
         */
        public void pluginDisabled(final PluginDescriptor descriptor) {
            // no-op
        }

        /**
         * @see org.java.plugin.PluginManager.EventListener#pluginEnabled(
         *      org.java.plugin.registry.PluginDescriptor)
         */
        public void pluginEnabled(final PluginDescriptor descriptor) {
            // no-op
        }
    }

    /**
     * Simple callback interface to hold info about plug-in manifest and plug-in
     * data locations.
     * 
     * @version $Id: PluginManager.java,v 1.5 2007/04/07 12:42:14 ddimon Exp $
     */
    public static interface PluginLocation {
        /**
         * @return location of plug-in manifest
         */
        URL getManifestLocation();

        /**
         * @return location of plug-in context ("home")
         */
        URL getContextLocation();
    }
}