KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > classes > v1 > jdk2 > UrlClassLoader


1 /*
2  * @(#)UrlClassLoader.java
3  *
4  * Copyright (C) 2000,2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.util.classes.v1.jdk2;
28
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import java.util.Hashtable JavaDoc;
36
37 import net.sourceforge.groboutils.util.classes.v1.IUrlClassLoader;
38
39
40 /**
41  * A JDK 1.2+ URL Class Loader implementation.
42  * <P>
43  * New in version 1.0.0: This will attempt to load from the thread's
44  * context loader if the URL is bad.
45  *
46  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
47  * @version $Date: 2003/05/06 05:35:01 $
48  * @since November 17, 2000 (GroboUtils Alpha 0.9.0)
49  */

50 public class UrlClassLoader implements IUrlClassLoader
51 {
52     //----------------------------
53
// Public data
54

55     //----------------------------
56
// Private data
57

58     private Hashtable JavaDoc m_urlLoaders = new Hashtable JavaDoc();
59     
60     //----------------------------
61
// constructors
62

63     /**
64     * Default constructor
65     */

66     public UrlClassLoader()
67     {
68         // do nothing
69
}
70     
71     
72     //----------------------------
73
// Public methods
74

75     
76     /**
77      * Load the given class from the given URL. If the URL is <tt>null</tt>,
78      * then it is up to the classloader to figure out where to load it from.
79      * This should, in general, load the class from the default class loader.
80      *
81      * @param className the exact class name to load.
82      * @param url the URL from which the class is loaded. If this is
83      * <tt>null</tt>, then the returned class is implementation specific.
84      * @return the loaded Class instance, or <tt>null</tt> if the class could
85      * not be found or if the given URL is not valid.
86      */

87     public Class JavaDoc loadClass( String JavaDoc className, String JavaDoc url )
88     {
89         URL JavaDoc murl = convertUrl( url );
90         ClassLoader JavaDoc cl = getClassLoader( murl );
91         Class JavaDoc c = null;
92         
93         // Try 5 times to get around time-out issues.
94
for (int i = 0; i < 5 && c == null; ++i)
95         {
96             try
97             {
98                 c = cl.loadClass( className );
99             }
100             catch (ClassNotFoundException JavaDoc cnfe)
101             {
102                 // ignore
103
cnfe.printStackTrace();
104             }
105         }
106         return c;
107     }
108     
109     
110     /**
111      * Call to flush any cache stored in the interface. This allows for
112      * a class loader to cache results, and free up memory when it is
113      * not needed.
114      */

115     public void flush()
116     {
117         this.m_urlLoaders = new Hashtable JavaDoc();
118     }
119     
120     
121     
122     //----------------------------
123
// Protected methods
124

125     
126     /**
127      * Converts the given string to a fully qualified URL. If no
128      * scheme is given, then it is converted to a File scheme.
129      */

130     protected URL JavaDoc convertUrl( String JavaDoc url )
131     {
132         if (url == null)
133         {
134             return null;
135         }
136         
137         try
138         {
139             return new URL JavaDoc( url );
140         }
141         catch (IOException JavaDoc ioe)
142         {
143             // assume that the string is intended to be a file name.
144
try
145             {
146                 return new URL JavaDoc( "file:" + url );
147             }
148             catch (IOException JavaDoc e)
149             {
150                 return null;
151             }
152         }
153     }
154     
155     
156     /**
157      *
158      */

159     protected ClassLoader JavaDoc getClassLoader( URL JavaDoc url )
160     {
161         ClassLoader JavaDoc cl;
162         if (url == null)
163         {
164             cl = Thread.currentThread().getContextClassLoader();
165         }
166         else
167         {
168             cl = (URLClassLoader JavaDoc)this.m_urlLoaders.get( url );
169             if (cl == null)
170             {
171                 cl = new URLClassLoader JavaDoc( new URL JavaDoc[] { url } );
172                 this.m_urlLoaders.put( url, cl );
173             }
174         }
175         return cl;
176     }
177     
178     
179     //----------------------------
180
// Private methods
181
}
182  
183
Popular Tags