1 17 package org.apache.servicemix.jbi.framework; 18 19 import java.io.File ; 20 import java.net.MalformedURLException ; 21 import java.net.URL ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.jbi.JBIException; 26 import javax.jbi.component.Bootstrap; 27 import javax.jbi.component.Component; 28 import javax.jbi.management.DeploymentException; 29 import javax.jbi.management.InstallerMBean; 30 import javax.management.InstanceNotFoundException ; 31 import javax.management.MBeanRegistrationException ; 32 import javax.management.ObjectName ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.servicemix.jbi.container.JBIContainer; 37 import org.apache.xbean.classloader.DestroyableClassLoader; 38 import org.apache.xbean.classloader.JarFileClassLoader; 39 40 46 public class InstallerMBeanImpl implements InstallerMBean { 47 private static final Log log = LogFactory.getLog(InstallerMBeanImpl.class); 48 private InstallationContextImpl context; 49 private JBIContainer container; 50 private ObjectName objectName; 51 private ObjectName extensionMBeanName; 52 private Bootstrap bootstrap; 53 private boolean initialized; 54 55 62 public InstallerMBeanImpl(JBIContainer container, 63 InstallationContextImpl ic) throws DeploymentException { 64 this.container = container; 65 this.context = ic; 66 bootstrap = createBootstrap(); 67 initBootstrap(); 68 } 69 70 protected void initBootstrap() throws DeploymentException { 71 try { 72 if (!initialized) { 73 try { 76 if (extensionMBeanName != null && 77 container.getMBeanServer() != null && 78 container.getMBeanServer().isRegistered(extensionMBeanName)) { 79 container.getMBeanServer().unregisterMBean(extensionMBeanName); 80 } 81 } catch (InstanceNotFoundException e) { 82 } catch (MBeanRegistrationException e) { 84 } 86 bootstrap.init(this.context); 88 extensionMBeanName = bootstrap.getExtensionMBeanName(); 89 initialized = true; 90 } 91 } catch (JBIException e) { 92 log.error("Could not initialize bootstrap", e); 93 throw new DeploymentException(e); 94 } 95 } 96 97 protected void cleanUpBootstrap() throws DeploymentException { 98 try { 99 bootstrap.cleanUp(); 100 } catch (JBIException e) { 101 log.error("Could not initialize bootstrap", e); 102 throw new DeploymentException(e); 103 } finally { 104 initialized = false; 105 } 106 } 107 108 protected Bootstrap createBootstrap() throws DeploymentException { 109 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 110 org.apache.servicemix.jbi.deployment.Component descriptor = context.getDescriptor(); 111 try { 112 ClassLoader cl = buildClassLoader( 113 context.getInstallRootAsDir(), 114 descriptor.getBootstrapClassPath().getPathElements(), 115 descriptor.isBootstrapClassLoaderDelegationParentFirst(), 116 null); 117 Thread.currentThread().setContextClassLoader(cl); 118 Class bootstrapClass = cl.loadClass(descriptor.getBootstrapClassName()); 119 Bootstrap bootstrap = (Bootstrap) bootstrapClass.newInstance(); 120 return bootstrap; 121 } 122 catch (MalformedURLException e) { 123 log.error("Could not create class loader", e); 124 throw new DeploymentException(e); 125 } 126 catch (ClassNotFoundException e) { 127 log.error("Class not found: " + descriptor.getBootstrapClassName(), e); 128 throw new DeploymentException(e); 129 } 130 catch (InstantiationException e) { 131 log.error("Could not instantiate : " + descriptor.getBootstrapClassName(), e); 132 throw new DeploymentException(e); 133 } 134 catch (IllegalAccessException e) { 135 log.error("Illegal access on: " + descriptor.getBootstrapClassName(), e); 136 throw new DeploymentException(e); 137 } 138 finally { 139 Thread.currentThread().setContextClassLoader(oldCl); 140 } 141 } 142 143 148 public String getInstallRoot() { 149 return context.getInstallRoot(); 150 } 151 152 159 public ObjectName install() throws JBIException { 160 if (isInstalled()) { 161 throw new DeploymentException("Component is already installed"); 162 } 163 initBootstrap(); 164 bootstrap.onInstall(); 165 ObjectName result = null; 168 try { 169 result = activateComponent(); 170 ComponentMBeanImpl lcc = container.getComponent(context.getComponentName()); 171 lcc.persistRunningState(); 172 context.setInstall(false); 173 } finally { 174 cleanUpBootstrap(); 175 } 176 return result; 177 } 178 179 public ObjectName activateComponent() throws JBIException { 180 ObjectName result = null; 181 ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); 182 org.apache.servicemix.jbi.deployment.Component descriptor = context.getDescriptor(); 183 try { 184 ClassLoader cl = buildClassLoader( 185 context.getInstallRootAsDir(), 186 (String []) context.getClassPathElements().toArray(new String [0]), 187 descriptor.isComponentClassLoaderDelegationParentFirst(), 188 context.getSharedLibraries()); 189 Thread.currentThread().setContextClassLoader(cl); 190 Class componentClass = cl.loadClass(descriptor.getComponentClassName()); 191 Component component = (Component) componentClass.newInstance(); 192 result = container.activateComponent( 193 context.getInstallRootAsDir(), 194 component, 195 context.getComponentDescription(), 196 (ComponentContextImpl) context.getContext(), 197 context.isBinding(), 198 context.isEngine(), 199 context.getSharedLibraries()); 200 } 201 catch (MalformedURLException e) { 202 log.error("Could not create class loader", e); 203 throw new DeploymentException(e); 204 } 205 catch (NoClassDefFoundError e) { 206 log.error("Class not found: " + descriptor.getBootstrapClassName(), e); 207 throw new DeploymentException(e); 208 } 209 catch (ClassNotFoundException e) { 210 log.error("Class not found: " + descriptor.getBootstrapClassName(), e); 211 throw new DeploymentException(e); 212 } 213 catch (InstantiationException e) { 214 log.error("Could not instantiate : " + descriptor.getBootstrapClassName(), e); 215 throw new DeploymentException(e); 216 } 217 catch (IllegalAccessException e) { 218 log.error("Illegal access on: " + descriptor.getBootstrapClassName(), e); 219 throw new DeploymentException(e); 220 } 221 catch (JBIException e) { 222 log.error("Could not initialize : " + descriptor.getBootstrapClassName(), e); 223 throw new DeploymentException(e); 224 } 225 finally { 226 Thread.currentThread().setContextClassLoader(oldCl); 227 } 228 return result; 229 } 230 231 236 public boolean isInstalled() { 237 return !context.isInstall(); 238 } 239 240 245 public void uninstall() throws javax.jbi.JBIException { 246 if (!isInstalled()) { 249 throw new DeploymentException("Component is not installed"); 250 } 251 String componentName = context.getComponentName(); 252 try { 253 container.deactivateComponent(componentName); 254 bootstrap.onUninstall(); 255 context.setInstall(true); 256 } finally { 257 cleanUpBootstrap(); 258 259 if (bootstrap.getClass().getClassLoader() instanceof DestroyableClassLoader) { 262 ((DestroyableClassLoader) bootstrap.getClass().getClassLoader()).destroy(); 263 } 264 System.gc(); 265 container.getEnvironmentContext().removeComponentRootDirectory(componentName); 266 } 267 } 268 269 275 public ObjectName getInstallerConfigurationMBean() throws javax.jbi.JBIException { 276 return extensionMBeanName; 277 } 278 281 public ObjectName getObjectName() { 282 return objectName; 283 } 284 287 public void setObjectName(ObjectName objectName) { 288 this.objectName = objectName; 289 } 290 291 303 protected ClassLoader buildClassLoader( 304 File dir, 305 String [] classPathNames, 306 boolean parentFirst, 307 String [] list) throws MalformedURLException , DeploymentException { 308 309 ClassLoader [] parents; 311 312 if (list != null && list.length > 0) { 314 parents = new ClassLoader [list.length]; 315 for (int i = 0; i < parents.length; i++) { 316 org.apache.servicemix.jbi.framework.SharedLibrary sl = 317 container.getRegistry().getSharedLibrary(list[i]); 318 if (sl == null) { 319 throw new DeploymentException("Shared library " + list[i] + " is not installed"); 320 } 321 parents[i] = sl.getClassLoader(); 322 } 323 } else { 324 parents = new ClassLoader [] { getClass().getClassLoader() }; 325 } 326 327 List urls = new ArrayList (); 328 for (int i = 0; i < classPathNames.length; i++) { 329 File file = new File (dir, classPathNames[i]); 330 if (!file.exists()) { 331 log.warn("Unable to add File " + file 332 + " to class path as it doesn't exist: " 333 + file.getAbsolutePath()); 334 } 335 urls.add(file.toURL()); 336 } 337 338 ClassLoader cl = new JarFileClassLoader( 339 "Component ClassLoader", 340 (URL []) urls.toArray(new URL [urls.size()]), 341 parents, 342 !parentFirst, 343 new String [0], 344 new String [] { "java.", "javax." }); 345 if (log.isDebugEnabled()) { 346 log.debug("Component class loader: " + cl); 347 } 348 return cl; 349 } 350 351 } 352 | Popular Tags |