1 19 package org.java.plugin.standard; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.security.AccessController ; 23 import java.security.PrivilegedAction ; 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 52 public class StandardPluginLifecycleHandler extends PluginLifecycleHandler { 53 private final Log log = LogFactory.getLog(getClass()); 54 private boolean probeParentLoaderLast; 55 56 61 protected org.java.plugin.PluginClassLoader createPluginClassLoader( 62 final PluginDescriptor descr) { 63 65 StandardPluginClassLoader result = 66 (StandardPluginClassLoader) AccessController.doPrivileged( 67 new PrivilegedAction () { 68 public Object 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 85 protected Plugin createPluginInstance(final PluginDescriptor descr) 86 throws PluginLifecycleException { 87 String className = descr.getPluginClassName(); 88 Class pluginClass; 89 try { 90 pluginClass = 91 getPluginManager().getPluginClassLoader(descr).loadClass( 92 className); 93 } catch (ClassNotFoundException cnfe) { 94 throw new PluginLifecycleException( 95 StandardObjectFactory.PACKAGE_NAME, 96 "pluginClassNotFound", className, cnfe); } 98 try { 99 try { 100 return (Plugin) pluginClass.newInstance(); 101 } catch (Exception e) { 102 log.warn("attempt to instantiate Plugin class for plug-in " + descr.getUniqueId() + " with no-arguments" + " constructor failed, fall-back to the deprecated" + " two-arguments constructor", e); return (Plugin) pluginClass.getConstructor( 108 new Class [] {PluginManager.class, 109 PluginDescriptor.class}).newInstance( 110 new Object [] {getPluginManager(), 111 descr}); 112 } 113 } catch (InvocationTargetException ite) { 114 Throwable t = ite.getTargetException(); 115 throw new PluginLifecycleException( 116 StandardObjectFactory.PACKAGE_NAME, 117 "pluginClassInstantiationFailed", descr.getId(), (t != null) ? t : ite); 119 } catch (Exception e) { 120 throw new PluginLifecycleException( 121 StandardObjectFactory.PACKAGE_NAME, 122 "pluginClassInstantiationFailed", descr.getId(), e); } 124 } 125 126 131 protected void beforePluginStart(final Plugin plugin) { 132 } 134 135 140 protected void afterPluginStop(final Plugin plugin) { 141 } 143 144 148 protected void dispose() { 149 } 151 152 156 public void configure(ExtendedProperties config) { 157 probeParentLoaderLast = "true".equalsIgnoreCase( config.getProperty("probeParentLoaderLast", "false")); log.debug("probeParentLoaderLast parameter value is " + probeParentLoaderLast); 161 } 162 } 163 | Popular Tags |