KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > tc6 > deployers > WarClassLoaderDeployer


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
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.web.tomcat.tc6.deployers;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.catalina.Loader;
30 import org.jboss.deployers.plugins.deployer.AbstractSimpleDeployer;
31 import org.jboss.deployers.spi.DeploymentException;
32 import org.jboss.deployers.spi.classloader.ClassLoaderFactory;
33 import org.jboss.deployers.spi.deployer.DeploymentUnit;
34 import org.jboss.deployers.spi.structure.DeploymentContext;
35 import org.jboss.metadata.WebMetaData;
36 import org.jboss.virtual.VirtualFile;
37 import org.jboss.web.tomcat.tc6.TomcatInjectionContainer;
38 import org.jboss.web.tomcat.tc6.WebAppLoader;
39 import org.jboss.web.tomcat.tc6.WebCtxLoader;
40
41 /**
42  * A class loader deployer for wars that handles installing a war first class
43  * loader unless the WebMetaData indicates otherwise.
44  * AbstractClassLoaderDeployer
45  *
46  * @author Scott.Stark@jboss.org
47  * @version $Revision:$
48  */

49 public class WarClassLoaderDeployer extends AbstractSimpleDeployer
50    implements ClassLoaderFactory
51 {
52    /** The parent class loader first model flag */
53    private boolean java2ClassLoadingCompliance = false;
54    private boolean useJBossWebLoader;
55    /** Package names that should be ignored for class loading */
56    private String JavaDoc[] filteredPackages;
57
58    public WarClassLoaderDeployer()
59    {
60       // Should be after the default class loading deployer to pickup parent cl
61
super.setRelativeOrder(CLASSLOADER_DEPLOYER - 1);
62    }
63
64    public boolean isJava2ClassLoadingCompliance()
65    {
66       return java2ClassLoadingCompliance;
67    }
68    public void setJava2ClassLoadingCompliance(boolean flag)
69    {
70       this.java2ClassLoadingCompliance = flag;
71    }
72
73    public String JavaDoc[] getFilteredPackages()
74    {
75       return filteredPackages;
76    }
77    public void setFilteredPackages(String JavaDoc[] pkgs)
78    {
79       this.filteredPackages = pkgs;
80    }
81
82    public boolean isUseJBossWebLoader()
83    {
84       return useJBossWebLoader;
85    }
86    public void setUseJBossWebLoader(boolean useJBossWebLoader)
87    {
88       this.useJBossWebLoader = useJBossWebLoader;
89    }
90
91    
92    @Override JavaDoc
93    public void deploy(DeploymentUnit unit) throws DeploymentException
94    {
95       WebMetaData metaData = unit.getAttachment(WebMetaData.class);
96       if( metaData != null )
97       {
98          unit.addAttachment(ClassLoaderFactory.class, this);
99       }
100    }
101
102    public ClassLoader JavaDoc createClassLoader(DeploymentContext context) throws Exception JavaDoc
103    {
104       ClassLoader JavaDoc loader = null;
105       try
106       {
107          DeploymentUnit unit = context.getDeploymentUnit();
108          WebMetaData metaData = unit.getAttachment(WebMetaData.class);
109          loader = createClassLoader(metaData, context);
110          if( loader != null )
111             context.setClassLoader(loader);
112       }
113       catch(Exception JavaDoc e)
114       {
115          log.warn("", e);
116       }
117       return loader;
118    }
119
120    public void removeClassLoader(DeploymentContext context) throws Exception JavaDoc
121    {
122       
123    }
124
125    /**
126     *
127     */

128    protected ClassLoader JavaDoc createClassLoader(WebMetaData metaData, DeploymentContext context)
129          throws Exception JavaDoc
130    {
131       ClassLoader JavaDoc loader = null;
132       if (metaData != null)
133       {
134          ClassLoader JavaDoc parent = context.getClassLoader();
135          if( parent == null )
136             parent = context.getTopLevel().getClassLoader();
137          if( parent == null )
138             parent = Thread.currentThread().getContextClassLoader();
139          Loader webLoader = getWebLoader(parent, metaData, context.getClassPath());
140          context.getDeploymentUnit().addAttachment(Loader.class, webLoader);
141          loader = webLoader.getClassLoader();
142       }
143       return loader;
144    }
145
146    /**
147     * Create the web app class loader.
148     *
149     * @param loader
150     * @param metaData
151     * @param classpath
152     * @return
153     * @throws MalformedURLException
154     */

155    protected Loader getWebLoader(ClassLoader JavaDoc loader, WebMetaData metaData,
156          List JavaDoc<VirtualFile> classpath)
157          throws Exception JavaDoc
158    {
159       Loader webLoader = null;
160       TomcatInjectionContainer injectionContainer = null;
161       if (useJBossWebLoader)
162       {
163          WebCtxLoader jbossLoader = new WebCtxLoader(loader, injectionContainer);
164          ArrayList JavaDoc<URL JavaDoc> cp = new ArrayList JavaDoc<URL JavaDoc>();
165          for(VirtualFile vf : classpath)
166          {
167             try
168             {
169                URL JavaDoc path = vf.toURL();
170                cp.add(path);
171             }
172             catch(Exception JavaDoc e)
173             {
174                log.debug("Ignoring path element: "+vf, e);
175             }
176          }
177          jbossLoader.setClasspath(cp);
178          webLoader = jbossLoader;
179       }
180       else
181       {
182          WebAppLoader jbossLoader = new WebAppLoader(loader, filteredPackages, injectionContainer);
183          jbossLoader.setDelegate(java2ClassLoadingCompliance);
184          jbossLoader.start();
185          webLoader = jbossLoader;
186       }
187       return webLoader;
188    }
189
190 }
191
Popular Tags