1 22 package org.jboss.system.deployers; 23 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.Set ; 27 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 31 import org.jboss.deployers.plugins.deployers.helpers.AbstractTopLevelClassLoaderDeployer; 32 import org.jboss.deployers.plugins.deployers.helpers.ClassPathVisitor; 33 import org.jboss.deployers.spi.DeploymentException; 34 import org.jboss.deployers.spi.deployer.DeploymentUnit; 35 import org.jboss.deployers.spi.structure.DeploymentContext; 36 import org.jboss.mx.loading.LoaderRepositoryFactory; 37 import org.jboss.mx.loading.RepositoryClassLoader; 38 import org.jboss.mx.loading.LoaderRepositoryFactory.LoaderRepositoryConfig; 39 import org.jboss.system.ServiceController; 40 import org.jboss.system.metadata.ServiceDeployment; 41 import org.jboss.virtual.VirtualFile; 42 43 52 public class ServiceClassLoaderDeployer extends AbstractTopLevelClassLoaderDeployer 53 { 54 55 private final ServiceController controller; 56 57 63 public ServiceClassLoaderDeployer(ServiceController controller) 64 { 65 if (controller == null) 66 throw new IllegalArgumentException ("Null controller"); 67 this.controller = controller; 68 } 69 70 public ClassLoader createTopLevelClassLoader(DeploymentContext context) throws Exception 71 { 72 MBeanServer server = controller.getMBeanServer(); 73 DeploymentUnit unit = context.getDeploymentUnit(); 74 75 VirtualFile root = context.getRoot(); 77 URL url = null; 78 try 79 { 80 if (root != null) 81 url = trimJARURL(root.toURL()); 82 } 83 catch (Exception ignored) 84 { 85 log.debug("Unable to get URL for " + context.getName() + " reason=" + ignored); 86 } 87 88 LoaderRepositoryConfig config = unit.getAttachment(LoaderRepositoryConfig.class); 90 if (config == null) 91 config = new LoaderRepositoryConfig(); 92 LoaderRepositoryFactory.createLoaderRepository(server, config); 93 94 Object [] args = { url, url, Boolean.TRUE }; 96 String [] sig = { "java.net.URL", "java.net.URL", "boolean" }; 97 RepositoryClassLoader ucl = (RepositoryClassLoader) server.invoke(config.repositoryName, "newClassLoader", args, sig); 98 99 try 101 { 102 ClassPathVisitor visitor = new ClassPathVisitor(); 103 context.visit(visitor); 104 Set <VirtualFile> classpath = visitor.getClassPath(); 105 for (VirtualFile path : classpath) 106 { 107 if (path != root) 109 ucl.addURL(trimJARURL(path.toURL())); 110 } 111 112 ObjectName uclName = ucl.getObjectName(); 114 if (server.isRegistered(uclName) == false) 115 server.registerMBean(ucl, uclName); 116 } 117 catch (Throwable t) 118 { 119 internalRemoveClassLoader(context, ucl); 120 throw DeploymentException.rethrowAsDeploymentException("Error creating classloader: " + context.getName(), t); 121 } 122 123 return ucl; 124 } 125 126 public void removeTopLevelClassLoader(DeploymentContext context) 127 { 128 RepositoryClassLoader ucl = (RepositoryClassLoader) context.getClassLoader(); 129 if (ucl == null) 130 return; 131 internalRemoveClassLoader(context, ucl); 132 } 133 134 140 private void internalRemoveClassLoader(DeploymentContext context, RepositoryClassLoader ucl) 141 { 142 MBeanServer server = controller.getMBeanServer(); 143 144 try 146 { 147 ObjectName uclName = ucl.getObjectName(); 148 if (server.isRegistered(uclName) == true ) 149 server.unregisterMBean(uclName); 150 } 151 catch (Throwable t) 152 { 153 log.warn("Error unregistering classloader mbean: " + ucl + " for " + context.getName(), t); 154 } 155 156 try 158 { 159 ucl.unregister(); 160 } 161 catch (Throwable t) 162 { 163 log.warn("Error unregistering ucl: " + ucl + " for " + context.getName(), t); 164 } 165 166 try 168 { 169 170 DeploymentUnit unit = context.getDeploymentUnit(); 171 ServiceDeployment deployment = unit.getAttachment(ServiceDeployment.class); 172 if (deployment != null) 173 { 174 LoaderRepositoryConfig config = unit.getAttachment(LoaderRepositoryConfig.class); 175 if (config != null) 176 LoaderRepositoryFactory.destroyLoaderRepository(server, config.repositoryName); 177 } 178 } 179 catch (Throwable t) 180 { 181 log.warn("Error removing classloader from repository: " + ucl + " for " + context.getName(), t); 182 } 183 } 184 185 private URL trimJARURL(URL url) throws MalformedURLException 187 { 188 String temp = url.toString(); 189 if (temp.startsWith("jar:") && temp.endsWith("!/")) 190 { 191 temp = temp.substring(4, temp.length()-2); 192 return new URL (temp); 193 } 194 return url; 195 } 196 } 197 | Popular Tags |