KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > deployers > ServiceClassLoaderDeployer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.system.deployers;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.MBeanServer JavaDoc;
29 import javax.management.ObjectName JavaDoc;
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 /**
44  * ServiceClassLoaderDeployer.<p>
45  *
46  * This deployer is responsible for creating classloaders for services of
47  * type {@link ServiceDeployment}.
48  *
49  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
50  * @version $Revision: 1.1 $
51  */

52 public class ServiceClassLoaderDeployer extends AbstractTopLevelClassLoaderDeployer
53 {
54    /** The service controller */
55    private final ServiceController controller;
56
57    /**
58     * Create a new ServiceDeployer.
59     *
60     * @param controller the service controller
61     * @throws IllegalArgumentException for a null controller
62     */

63    public ServiceClassLoaderDeployer(ServiceController controller)
64    {
65       if (controller == null)
66          throw new IllegalArgumentException JavaDoc("Null controller");
67       this.controller = controller;
68    }
69
70    public ClassLoader JavaDoc createTopLevelClassLoader(DeploymentContext context) throws Exception JavaDoc
71    {
72       MBeanServer JavaDoc server = controller.getMBeanServer();
73       DeploymentUnit unit = context.getDeploymentUnit();
74       
75       // Try to determine a root url
76
VirtualFile root = context.getRoot();
77       URL JavaDoc url = null;
78       try
79       {
80          if (root != null)
81             url = trimJARURL(root.toURL());
82       }
83       catch (Exception JavaDoc ignored)
84       {
85          log.debug("Unable to get URL for " + context.getName() + " reason=" + ignored);
86       }
87
88       // Check the loader repository config
89
LoaderRepositoryConfig config = unit.getAttachment(LoaderRepositoryConfig.class);
90       if (config == null)
91          config = new LoaderRepositoryConfig();
92       LoaderRepositoryFactory.createLoaderRepository(server, config);
93
94       // Create the classloader
95
Object JavaDoc[] args = { url, url, Boolean.TRUE };
96       String JavaDoc[] sig = { "java.net.URL", "java.net.URL", "boolean" };
97       RepositoryClassLoader ucl = (RepositoryClassLoader) server.invoke(config.repositoryName, "newClassLoader", args, sig);
98
99       // Add in the classpath
100
try
101       {
102          ClassPathVisitor visitor = new ClassPathVisitor();
103          context.visit(visitor);
104          Set JavaDoc<VirtualFile> classpath = visitor.getClassPath();
105          for (VirtualFile path : classpath)
106          {
107             // TODO Weed duplicates from other deployments
108
if (path != root)
109                ucl.addURL(trimJARURL(path.toURL()));
110          }
111
112          // Register the classloader with the MBeanServer
113
ObjectName JavaDoc uclName = ucl.getObjectName();
114          if (server.isRegistered(uclName) == false)
115             server.registerMBean(ucl, uclName);
116       }
117       catch (Throwable JavaDoc 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    /**
135     * Remove the classloader
136     *
137     * @param context the deployment context
138     * @param ucl the unified classloader
139     */

140    private void internalRemoveClassLoader(DeploymentContext context, RepositoryClassLoader ucl)
141    {
142       MBeanServer JavaDoc server = controller.getMBeanServer();
143
144       // Remove the classloader from the MBeanServer
145
try
146       {
147          ObjectName JavaDoc uclName = ucl.getObjectName();
148          if (server.isRegistered(uclName) == true )
149             server.unregisterMBean(uclName);
150       }
151       catch (Throwable JavaDoc t)
152       {
153          log.warn("Error unregistering classloader mbean: " + ucl + " for " + context.getName(), t);
154       }
155
156       // Remove the classloader from the loader repository
157
try
158       {
159          ucl.unregister();
160       }
161       catch (Throwable JavaDoc t)
162       {
163          log.warn("Error unregistering ucl: " + ucl + " for " + context.getName(), t);
164       }
165
166       // Remove the loader repository
167
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 JavaDoc t)
180       {
181          log.warn("Error removing classloader from repository: " + ucl + " for " + context.getName(), t);
182       }
183    }
184
185    // TODO figure out why this is necessary
186
private URL JavaDoc trimJARURL(URL JavaDoc url) throws MalformedURLException JavaDoc
187    {
188       String JavaDoc temp = url.toString();
189       if (temp.startsWith("jar:") && temp.endsWith("!/"))
190       {
191          temp = temp.substring(4, temp.length()-2);
192          return new URL JavaDoc(temp);
193       }
194       return url;
195    }
196 }
197
Popular Tags