KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > loading > LoaderRepositoryDomain


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.mx.loading;
23
24 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
25
26 import org.jboss.classloading.spi.ClassLoadingDomain;
27 import org.jboss.classloading.spi.DomainClassLoader;
28 import org.jboss.classloading.spi.Translator;
29 import org.jboss.logging.Logger;
30 import org.jboss.mx.server.ServerConstants;
31
32 import java.net.URL JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 /**
38  * Port of legacy LoaderRepository to a ClassLoadingDomain base.
39  *
40  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 57108 $
43  */

44 public abstract class LoaderRepositoryDomain
45    implements ServerConstants, ClassLoadingDomain
46 {
47
48    // Attributes ----------------------------------------------------
49
protected static Vector JavaDoc loaders = new Vector JavaDoc();
50    protected static LoaderRepositoryDomain instance = null;
51    protected Translator translator = null;
52
53    /** The loaded classes cache, HashMap<String, Class>.
54     * Access synchronized via this.classes monitor.
55     */

56    private ConcurrentReaderHashMap classes = new ConcurrentReaderHashMap();
57
58
59    /**
60     * Native signature to class map
61     */

62    private static HashMap nativeClassBySignature;
63
64    // Static --------------------------------------------------------
65
private static final Logger log = Logger.getLogger(LoaderRepositoryDomain.class);
66
67    /**
68     * Construct the native class map
69     */

70    static
71    {
72       nativeClassBySignature = new HashMap();
73       nativeClassBySignature.put("boolean", boolean.class);
74       nativeClassBySignature.put("byte", byte.class);
75       nativeClassBySignature.put("char", char.class);
76       nativeClassBySignature.put("double", double.class);
77       nativeClassBySignature.put("float", float.class);
78       nativeClassBySignature.put("int", int.class);
79       nativeClassBySignature.put("long", long.class);
80       nativeClassBySignature.put("short", short.class);
81       nativeClassBySignature.put("void", void.class);
82
83       nativeClassBySignature.put("boolean[]", boolean[].class);
84       nativeClassBySignature.put("byte[]", byte[].class);
85       nativeClassBySignature.put("char[]", char[].class);
86       nativeClassBySignature.put("double[]", double[].class);
87       nativeClassBySignature.put("float[]", float[].class);
88       nativeClassBySignature.put("int[]", int[].class);
89       nativeClassBySignature.put("long[]", long[].class);
90       nativeClassBySignature.put("short[]", short[].class);
91    }
92
93    // Public --------------------------------------------------------
94
public Vector JavaDoc getLoaders()
95    {
96       return loaders;
97    }
98
99    public URL JavaDoc[] getURLs()
100    {
101       return null;
102    }
103
104    public Class JavaDoc getCachedClass(String JavaDoc classname)
105    {
106        return (Class JavaDoc)classes.get(classname);
107    }
108
109    public Translator getTranslator()
110    {
111       return translator;
112    }
113
114    public void setTranslator(Translator t)
115    {
116       translator = t;
117    }
118
119    /**
120     * Compare two loader repository, by default we do no special ordering
121     *
122     * @param lr the loader repository
123     * @return -1, 0, 1 depending upon the order
124     */

125    public int compare(LoaderRepositoryDomain lr)
126    {
127       if (lr == this)
128          return 0;
129       else
130          return 1;
131    }
132    
133    // BEGIN ClassLoadingDomain **************************************************************************************
134

135    /**
136     * Loads a class from the repository. This method attempts to load the class
137     * using all the classloader registered to the repository.
138     *
139     * @param className the class to load
140     * @return the found class
141     * @exception ClassNotFoundException when there is no such class
142     */

143    public abstract Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc;
144    /**
145     * Load a class from this domain
146     *
147     * @param name the class to load
148     * @param resolve whether to resolve the class
149     * @param classLoader the requesting classloader
150     * @return the class
151     * @throws ClassNotFoundException when the class is not found
152     */

153    public abstract Class JavaDoc loadClass(String JavaDoc name, boolean resolve, DomainClassLoader classLoader) throws ClassNotFoundException JavaDoc;
154    
155    // END ClassLoadingDomain ****************************************************************************************
156

157    /** Create DomainClassLoader and optionally add it to the repository
158     * @param url the URL to use for class loading
159     * @param addToRepository a flag indicating if the CL should be added to
160     * the repository
161     * @return the UCL instance
162     * @throws Exception
163     */

164    public abstract DomainClassLoader newClassLoader(final URL JavaDoc url, boolean addToRepository)
165       throws Exception JavaDoc;
166    /** Create DomainClassLoader and optionally add it to the repository
167     * @param url the URL to use for class loading
168     * @param origURL an orignal URL to use as the URL for the CL CodeSource.
169     * This is useful when the url is a local copy that is difficult to use for
170     * security policy writing.
171     * @param addToRepository a flag indicating if the CL should be added to
172     * the repository
173     * @return the CL instance
174     * @throws Exception
175     */

176    public abstract DomainClassLoader newClassLoader(final URL JavaDoc url, final URL JavaDoc origURL,
177       boolean addToRepository)
178       throws Exception JavaDoc;
179
180    /** Find a resource URL for the given name
181     *
182     * @param name the resource name
183     * @param cl the requesting class loader
184     * @return The resource URL if found, null otherwise
185     */

186    public abstract URL JavaDoc getResource(String JavaDoc name, DomainClassLoader cl);
187    /** Find all resource URLs for the given name. Since this typically
188     * entails an exhuastive search of the repository it can be a relatively
189     * slow operation.
190     *
191     * @param name the resource name
192     * @param cl the requesting class loader
193     * @param urls a list into which the located resource URLs will be placed
194     */

195    public abstract void getResources(String JavaDoc name, DomainClassLoader cl, List JavaDoc urls);
196
197    /** Add a class loader to the repository
198     */

199    public abstract void addClassLoader(DomainClassLoader cl);
200    /** Update the set of URLs known to be associated with a previously added
201     * class loader.
202     *
203     * @param cl
204     * @param url
205     */

206    public abstract boolean addClassLoaderURL(DomainClassLoader cl, URL JavaDoc url);
207    /** Remove a cladd loader from the repository.
208     * @param cl
209     */

210    public abstract void removeClassLoader(DomainClassLoader cl);
211
212    /**
213     * Return the class of a java native type
214     * @return the class, or null if className is not a native class name
215     */

216    public static final Class JavaDoc getNativeClassForName(String JavaDoc className)
217    {
218       // Check for native classes
219
return (Class JavaDoc)nativeClassBySignature.get(className);
220    }
221
222    /**
223     * Allow subclasses to override the ordering
224     *
225     * @param lr the loader repository
226     * @return -1, 0, 1 depending upon the order
227     */

228    protected int reverseCompare(LoaderRepositoryDomain lr)
229    {
230       return 0;
231    }
232
233    /**
234     * Add a class to the the cache
235     */

236    void cacheLoadedClass(String JavaDoc name, Class JavaDoc cls, DomainClassLoader cl)
237    {
238        synchronized( classes )
239        {
240           // Update the global cache
241
classes.put(name, cls);
242           if( log.isTraceEnabled() )
243           {
244              log.trace("cacheLoadedClass, classname: "+name+", class: "+cls
245                 +", cl: "+cl);
246           }
247
248            /**
249             * TODO: Adding this implementation is a hack for jmx 1.2 checkin.
250             * Had to add this because need getCachedClass() to work.
251             * However, this method does not add loaded classes to collection
252             * to be unloaded when called to remove classloader.
253             * Hopefully this will be a short term workaround.
254             * Contact telrod@e2technologies.net if you have questions. -TME
255             */

256        }
257    }
258
259    void clear()
260    {
261       classes.clear();
262    }
263 }
264
Popular Tags