001: /*
002: * @(#)UrlClassLoader.java
003: *
004: * Copyright (C) 2000,2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.classes.v1.jdk2;
028:
029: import java.net.URL;
030: import java.net.URLClassLoader;
031:
032: import java.io.File;
033: import java.io.IOException;
034:
035: import java.util.Hashtable;
036:
037: import net.sourceforge.groboutils.util.classes.v1.IUrlClassLoader;
038:
039: /**
040: * A JDK 1.2+ URL Class Loader implementation.
041: * <P>
042: * New in version 1.0.0: This will attempt to load from the thread's
043: * context loader if the URL is bad.
044: *
045: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
046: * @version $Date: 2003/05/06 05:35:01 $
047: * @since November 17, 2000 (GroboUtils Alpha 0.9.0)
048: */
049: public class UrlClassLoader implements IUrlClassLoader {
050: //----------------------------
051: // Public data
052:
053: //----------------------------
054: // Private data
055:
056: private Hashtable m_urlLoaders = new Hashtable();
057:
058: //----------------------------
059: // constructors
060:
061: /**
062: * Default constructor
063: */
064: public UrlClassLoader() {
065: // do nothing
066: }
067:
068: //----------------------------
069: // Public methods
070:
071: /**
072: * Load the given class from the given URL. If the URL is <tt>null</tt>,
073: * then it is up to the classloader to figure out where to load it from.
074: * This should, in general, load the class from the default class loader.
075: *
076: * @param className the exact class name to load.
077: * @param url the URL from which the class is loaded. If this is
078: * <tt>null</tt>, then the returned class is implementation specific.
079: * @return the loaded Class instance, or <tt>null</tt> if the class could
080: * not be found or if the given URL is not valid.
081: */
082: public Class loadClass(String className, String url) {
083: URL murl = convertUrl(url);
084: ClassLoader cl = getClassLoader(murl);
085: Class c = null;
086:
087: // Try 5 times to get around time-out issues.
088: for (int i = 0; i < 5 && c == null; ++i) {
089: try {
090: c = cl.loadClass(className);
091: } catch (ClassNotFoundException cnfe) {
092: // ignore
093: cnfe.printStackTrace();
094: }
095: }
096: return c;
097: }
098:
099: /**
100: * Call to flush any cache stored in the interface. This allows for
101: * a class loader to cache results, and free up memory when it is
102: * not needed.
103: */
104: public void flush() {
105: this .m_urlLoaders = new Hashtable();
106: }
107:
108: //----------------------------
109: // Protected methods
110:
111: /**
112: * Converts the given string to a fully qualified URL. If no
113: * scheme is given, then it is converted to a File scheme.
114: */
115: protected URL convertUrl(String url) {
116: if (url == null) {
117: return null;
118: }
119:
120: try {
121: return new URL(url);
122: } catch (IOException ioe) {
123: // assume that the string is intended to be a file name.
124: try {
125: return new URL("file:" + url);
126: } catch (IOException e) {
127: return null;
128: }
129: }
130: }
131:
132: /**
133: *
134: */
135: protected ClassLoader getClassLoader(URL url) {
136: ClassLoader cl;
137: if (url == null) {
138: cl = Thread.currentThread().getContextClassLoader();
139: } else {
140: cl = (URLClassLoader) this .m_urlLoaders.get(url);
141: if (cl == null) {
142: cl = new URLClassLoader(new URL[] { url });
143: this .m_urlLoaders.put(url, cl);
144: }
145: }
146: return cl;
147: }
148:
149: //----------------------------
150: // Private methods
151: }
|