KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > launcher > XBootClassLoader


1 /*====================================================================
2
3 ObjectWeb Util Launcher Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library 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 library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Christophe Demarey.
23 Contributor(s): Romain Rouvoy.
24
25 --------------------------------------------------------------------
26 $Id: XBootClassLoader.java,v 1.1 2004/05/19 15:58:29 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.launcher;
30
31
32 import java.net.MalformedURLException JavaDoc ;
33 import java.net.URLClassLoader JavaDoc ;
34 import java.net.URL JavaDoc ;
35
36 import org.objectweb.util.trace.TraceSystem;
37
38
39 /**
40  * Specific URLClassLoader {@link URLClassLoader}.<BR>
41  * <p>
42  * This loader overwrite URLClassLoader dafult behaviour. <BR>
43  * It searches requested classes first in locally registered URLs and
44  * then in the parent class loader.
45  * </p>
46  *
47  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
48  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
49  * @version 0.2
50  */

51 public class XBootClassLoader
52      extends URLClassLoader JavaDoc
53 {
54     // ==================================================================
55
//
56
// Constructors
57
//
58
// ==================================================================
59

60     /**
61      * Default constructor.
62      */

63     public XBootClassLoader() {
64         super(new URL JavaDoc[0]);
65     }
66
67     /**
68      * Constructor with parent ClassLoader.
69      *
70      * @param parent - the parent classloader to use.
71      */

72     public XBootClassLoader(ClassLoader JavaDoc parent) {
73         super(new URL JavaDoc[0], parent);
74     }
75
76     // ==================================================================
77
//
78
// Internal methods
79
//
80
// ==================================================================
81

82     /**
83      * Loads the Class with the specified name.
84      *
85      * @param name The name of the Class to be loaded (must not be null).
86      * @param resolve if true then resolve the class.
87      *
88      * @return the requested Class object.
89      * @see Class
90      *
91      * @throws ClassNotFoundException if the requested class does not
92      * exist in the loader's classpath.
93      */

94     protected Class JavaDoc loadClass(String JavaDoc name, boolean resolve)
95         throws ClassNotFoundException JavaDoc
96     { // check if the class has already been loaded
97
Class JavaDoc c = findLoadedClass(name);
98         if (c == null)
99             try { // If not found, call findClass for finding the class
100
c = findClass(name);
101             } catch (ClassNotFoundException JavaDoc e) {
102                 return super.loadClass(name, resolve);
103             }
104         if (resolve)
105             resolveClass(c);
106         return c;
107     }
108
109     // ==================================================================
110
//
111
// Public methods
112
//
113
// ==================================================================
114

115     /**
116      * Searches and loads a Class in the classpath of this ClassLoader.
117      * @param name - The name of the class to be loaded (must not be null).
118      * @return The required Class object.
119      * @see Class
120      * @throws ClassNotFoundException if the requested class does not
121      * exist in the loader's classpath.
122      */

123     public Class JavaDoc findClass(String JavaDoc name)
124         throws ClassNotFoundException JavaDoc
125     {
126         return super.findClass(name);
127     }
128
129     /**
130      * Loads the Class with the specified name.
131      * @param name - The name of the class to be loaded (must not be null).
132      * @return the required Class object.
133      * @see Class
134      * @throws ClassNotFoundException if the requested class does not
135      * exist in the loader's classpath.
136      */

137     public Class JavaDoc loadClass(String JavaDoc name)
138         throws ClassNotFoundException JavaDoc
139     {
140         TraceSystem.get("classloader").debug("Loading class "+name);
141         return loadClass(name, false);
142     }
143     
144     /**
145      * Adds URL to the ClassLoader.
146      * @param url - A valid URL to load (must not be null).
147      */

148     public void addURL(URL JavaDoc url)
149     { // This class is overriden for making it public
150
TraceSystem.get("classloader").info("Registering URL "+url);
151         // Check if the archive exist
152
try {
153             url.openConnection().connect();
154         } catch (Exception JavaDoc ex) {
155             throw new LauncherException(ex);
156         }
157         if (!java.util.Arrays.asList(getURLs()).contains(url))
158             super.addURL(url);
159     }
160
161
162     /**
163      * Adds URL to the ClassLoader.
164      * @param url - a string representation of an URL (must not be null).
165      * @throws MalformedURLException the syntax of the url is invalid.
166      */

167     public void addURL(String JavaDoc url)
168         throws MalformedURLException JavaDoc
169     {
170         this.addURL(new URL JavaDoc(url));
171     }
172
173
174     /**
175      * Adds a set of URLs to the ClassLoader.
176      * @param urls - a string representation of a set of URL (must not be null).
177      * @throws MalformedURLException the syntax of an url is invalid.
178      */

179     public void addURL(String JavaDoc[] urls)
180         throws MalformedURLException JavaDoc
181     {
182         for (int i=0 ; i < urls.length ; i++)
183             this.addURL(urls[i]);
184     }
185
186     
187     /**
188      * Checks if to classloaders are equivalent.
189      * Two classloaders are equivalent if they reference the same URLs.
190      * @param obj the classloader to compare to the current one.
191      * @return true if classloaders are equivalent, false in other cases.
192      */

193     public boolean equals(Object JavaDoc obj)
194     {
195         if (obj instanceof XBootClassLoader) {
196             XBootClassLoader loader = (XBootClassLoader) obj;
197             return java.util.Arrays.equals(getURLs(), loader.getURLs());
198         }
199         return false ;
200     }
201 }
202
Popular Tags