KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > standard > StandardPluginLifecycleHandler


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.standard;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.security.AccessController JavaDoc;
23 import java.security.PrivilegedAction JavaDoc;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.java.plugin.Plugin;
28 import org.java.plugin.PluginLifecycleException;
29 import org.java.plugin.PluginManager;
30 import org.java.plugin.registry.PluginDescriptor;
31 import org.java.plugin.util.ExtendedProperties;
32
33 /**
34  * Standard implementation of plug-in life cycle handler.
35  * <p>
36  * <b>Configuration parameters</b>
37  * </p>
38  * <p>
39  * This life cycle handler implementation supports following configuration
40  * parameters:
41  * <dl>
42  * <dt>probeParentLoaderLast</dt>
43  * <dd>If <code>true</code>, plug-in classloader will try loading classes from
44  * system (boot) classpath <b>after</b> trying to load them from plug-in
45  * classpath. Otherwise system classpath will be used <b>first</b>. Default
46  * value is <code>false</code> that corresponds to standard delegation model
47  * for classloaders hierarchy.</dd>
48  * </dl>
49  * </p>
50  * @version $Id: StandardPluginLifecycleHandler.java,v 1.5 2006/08/26 15:14:08 ddimon Exp $
51  */

52 public class StandardPluginLifecycleHandler extends PluginLifecycleHandler {
53     private final Log log = LogFactory.getLog(getClass());
54     private boolean probeParentLoaderLast;
55
56     /**
57      * Creates standard implementation of plug-in class loader.
58      * @see org.java.plugin.standard.PluginLifecycleHandler#createPluginClassLoader(
59      * org.java.plugin.registry.PluginDescriptor)
60      */

61     protected org.java.plugin.PluginClassLoader createPluginClassLoader(
62             final PluginDescriptor descr) {
63         /*StandardPluginClassLoader result = new StandardPluginClassLoader(
64                 getPluginManager(), descr, getClass().getClassLoader());*/

65         StandardPluginClassLoader result =
66             (StandardPluginClassLoader) AccessController.doPrivileged(
67                     new PrivilegedAction JavaDoc() {
68             public Object JavaDoc run() {
69                 return new StandardPluginClassLoader(getPluginManager(), descr,
70                         StandardPluginLifecycleHandler.this.getClass()
71                         .getClassLoader());
72             }
73         });
74         result.setProbeParentLoaderLast(probeParentLoaderLast);
75         return result;
76     }
77
78     /**
79      * Creates instance of plug-in class calling it's default (no-arguments)
80      * constructor. Class look-up is done with
81      * {@link PluginManager#getPluginClassLoader(PluginDescriptor) plug-in's class loader}.
82      * @see org.java.plugin.standard.PluginLifecycleHandler#createPluginInstance(
83      * org.java.plugin.registry.PluginDescriptor)
84      */

85     protected Plugin createPluginInstance(final PluginDescriptor descr)
86             throws PluginLifecycleException {
87         String JavaDoc className = descr.getPluginClassName();
88         Class JavaDoc pluginClass;
89         try {
90             pluginClass =
91                 getPluginManager().getPluginClassLoader(descr).loadClass(
92                         className);
93         } catch (ClassNotFoundException JavaDoc cnfe) {
94             throw new PluginLifecycleException(
95                     StandardObjectFactory.PACKAGE_NAME,
96                     "pluginClassNotFound", className, cnfe); //$NON-NLS-1$
97
}
98         try {
99             try {
100                 return (Plugin) pluginClass.newInstance();
101             } catch (Exception JavaDoc e) {
102                 // ignore before 1.0 release
103
log.warn("attempt to instantiate Plugin class for plug-in " //$NON-NLS-1$
104
+ descr.getUniqueId() + " with no-arguments" //$NON-NLS-1$
105
+ " constructor failed, fall-back to the deprecated" //$NON-NLS-1$
106
+ " two-arguments constructor", e); //$NON-NLS-1$
107
return (Plugin) pluginClass.getConstructor(
108                         new Class JavaDoc[] {PluginManager.class,
109                                 PluginDescriptor.class}).newInstance(
110                                         new Object JavaDoc[] {getPluginManager(),
111                                                 descr});
112             }
113         } catch (InvocationTargetException JavaDoc ite) {
114             Throwable JavaDoc t = ite.getTargetException();
115             throw new PluginLifecycleException(
116                     StandardObjectFactory.PACKAGE_NAME,
117                     "pluginClassInstantiationFailed", descr.getId(), //$NON-NLS-1$
118
(t != null) ? t : ite);
119         } catch (Exception JavaDoc e) {
120             throw new PluginLifecycleException(
121                     StandardObjectFactory.PACKAGE_NAME,
122                     "pluginClassInstantiationFailed", descr.getId(), e); //$NON-NLS-1$
123
}
124     }
125     
126     /**
127      * This method does nothing in this implementation.
128      * @see org.java.plugin.standard.PluginLifecycleHandler#beforePluginStart(
129      * org.java.plugin.Plugin)
130      */

131     protected void beforePluginStart(final Plugin plugin) {
132         // no-op
133
}
134
135     /**
136      * This method does nothing in this implementation.
137      * @see org.java.plugin.standard.PluginLifecycleHandler#afterPluginStop(
138      * org.java.plugin.Plugin)
139      */

140     protected void afterPluginStop(final Plugin plugin) {
141         // no-op
142
}
143
144     /**
145      * This method does nothing in this implementation.
146      * @see org.java.plugin.standard.PluginLifecycleHandler#dispose()
147      */

148     protected void dispose() {
149         // no-op
150
}
151
152     /**
153      * @see org.java.plugin.standard.PluginLifecycleHandler#configure(
154      * ExtendedProperties)
155      */

156     public void configure(ExtendedProperties config) {
157         probeParentLoaderLast = "true".equalsIgnoreCase( //$NON-NLS-1$
158
config.getProperty("probeParentLoaderLast", "false")); //$NON-NLS-1$ //$NON-NLS-2$
159
log.debug("probeParentLoaderLast parameter value is " //$NON-NLS-1$
160
+ probeParentLoaderLast);
161     }
162 }
163
Popular Tags