KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > WebClassLoader


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, 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.web;
23
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import javax.management.ObjectName JavaDoc;
27
28 import org.jboss.mx.loading.RepositoryClassLoader;
29 import org.jboss.mx.loading.LoaderRepositoryClassLoader;
30
31 /** A simple subclass of URLClassLoader that is used in conjunction with the
32 the WebService mbean to allow dynamic loading of resources and classes from
33 deployed ears, ejb jars and wars. A WebClassLoader is associated with a
34 Container and must have an UnifiedClassLoader as its parent. It overrides the
35 getURLs() method to return a different set of URLs for remote loading than
36 what is used for local loading.
37 <p>
38 WebClassLoader has two methods meant to be overriden by subclasses: getKey()
39 and getBytes(). The latter is a no-op in this implementation and should be
40 overriden by subclasses with bytecode generation ability, such as the
41 classloader used by the iiop module.
42 <p>
43 WebClassLoader subclasses must have a constructor with the same signature
44 as the WebClassLoader constructor.
45
46 @see #getUrls()
47 @see #setWebURLs(URL[])
48
49 @author <a HREF="mailto:Scott.Stark@jboss.org">Scott Stark</a>.
50 @author Sacha Labourey <sacha.labourey@cogito-info.ch>
51 @author Vladimir Blagojevic <vladimir@xisnext.2y.net>
52 @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
53 @version $Revision: 37459 $
54 */

55 public class WebClassLoader extends LoaderRepositoryClassLoader
56 {
57     /** This WebClassLoader is associated with this container. */
58     private ObjectName JavaDoc containerName;
59
60     /** The URLs returned by the getURLs() method override */
61    private URL JavaDoc[] webURLs;
62
63    private String JavaDoc codebaseString;
64
65    /** Creates new WebClassLoader.
66        Subclasses must have a constructor with the same signature. */

67    public WebClassLoader(ObjectName JavaDoc containerName, RepositoryClassLoader parent)
68    {
69       super(parent, parent.getLoaderRepository());
70       this.containerName = containerName;
71    }
72
73     /** Gets a string key used as the key into the WebServer's loaderMap. */
74     public String JavaDoc getKey()
75     {
76         String JavaDoc className = getClass().getName();
77         int dot = className.lastIndexOf('.');
78         if( dot >= 0 )
79             className = className.substring(dot+1);
80         String JavaDoc key = className + '[' + hashCode() + ']';
81         return key;
82     }
83
84     /** Gets the JMX ObjectName of the WebClassLoader's container. */
85     public ObjectName JavaDoc getContainer()
86     {
87         return containerName;
88     }
89
90     /** Returns the single URL for my parent, an UnifiedClassLoader. */
91     public URL JavaDoc getURL()
92     {
93         return ((RepositoryClassLoader) getParent()).getURL();
94     }
95
96     /** Get the list of URLs that should be used as the RMI annotated codebase.
97      This is the URLs previously set via setWebURLs. If setWebURLs has not
98      been invoked or was passed in a null value, the super class value of
99      getURLs() is used.
100     @return the local web URLs if not null, else the value of super.getURLs()
101      */

102     public URL JavaDoc[] getURLs()
103     {
104         URL JavaDoc[] urls = webURLs;
105         if( urls == null )
106             urls = super.getURLs();
107         return urls;
108     }
109     /** Access the URLClassLoader.getURLs() value.
110      @return the URLs used for local class and resource loading
111      */

112     public URL JavaDoc[] getLocalURLs()
113     {
114         return super.getURLs();
115     }
116
117     /** Set the URLs that should be returned from this classes getURLs() override.
118      @param webURLs, the set of URL codebases to be used for remote class loading.
119      */

120     public void setWebURLs(URL JavaDoc[] webURLs)
121     {
122         this.webURLs = webURLs;
123       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
124       for (int i = 0; i < webURLs.length; i++)
125       {
126          sb.append(webURLs[i].toString());
127          if (i < webURLs.length - 1)
128          {
129             sb.append(" ");
130          }
131       }
132       codebaseString = sb.toString();
133     }
134
135    public String JavaDoc getCodebaseString()
136    {
137       return codebaseString;
138    }
139
140     /** Gets the bytecodes for a given class.
141      * This implementation always returns null, indicating that it is unable
142      * to get bytecodes for any class. Should be overridden by subclasses
143      * with bytecode generation capability (such as the classloader used by
144      * the iiop module, which generates IIOP stubs on the fly).
145      *
146      @param cls a <code>Class</code>
147      @return a byte array with the bytecodes for class <code>cls</code>, or
148      * null if this classloader is unable to return such byte array.
149      */

150     public byte[] getBytes(Class JavaDoc clz)
151     {
152         return null; // this classloader is unable to return bytecodes
153
}
154
155 }
156
Popular Tags