001: /*
002: * @(#)URLClassLoader.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.lib;
010:
011: import java.io.*;
012: import java.net.*;
013: import java.util.*;
014:
015: /**
016: * This class is a subset of java.net.URLClassLoader.
017: *
018: * @see pnuts.lib.ClassPath
019: */
020: public class URLClassLoader extends ClassLoader {
021: private ClassPath ucp;
022: private URL[] urls;
023: private ClassLoader parent;
024:
025: public URLClassLoader(URL[] urls) {
026: this (urls, null, null);
027: }
028:
029: public URLClassLoader(URL[] urls, URLStreamHandlerFactory factory) {
030: this (urls, null, factory);
031: }
032:
033: public URLClassLoader(URL[] urls, ClassLoader parent) {
034: this (urls, parent, null);
035: }
036:
037: public URLClassLoader(URL[] urls, ClassLoader parent,
038: URLStreamHandlerFactory factory) {
039: this .urls = urls;
040: this .ucp = new ClassPath(urls, factory);
041: this .parent = parent;
042: }
043:
044: protected synchronized Class loadClass(String name, boolean resolve)
045: throws ClassNotFoundException {
046: Class c = findLoadedClass(name);
047: if (c == null) {
048: try {
049: if (parent != null && resolve) {
050: c = parent.loadClass(name);
051: } else {
052: c = findSystemClass(name);
053: }
054: } catch (ClassNotFoundException e) {
055: c = findClass(name);
056: }
057: }
058: if (resolve) {
059: resolveClass(c);
060: }
061: return c;
062: }
063:
064: protected Class findClass(String name)
065: throws ClassNotFoundException {
066: String path = name.replace('.', '/').concat(".class");
067: Resource res = ucp.getResource(path);
068: if (res != null) {
069: try {
070: return defineClass(name, res);
071: } catch (IOException e) {
072: throw new ClassNotFoundException(name);
073: }
074: } else {
075: throw new ClassNotFoundException(name);
076: }
077: }
078:
079: private Class defineClass(String name, Resource res)
080: throws IOException {
081: byte[] b = res.getBytes();
082: return defineClass(name, b, 0, b.length);
083: }
084:
085: public URL getResource(String name) {
086: URL url = getSystemResource(name);
087: if (url == null) {
088: url = findResource(name);
089: }
090: return url;
091: }
092:
093: public URL findResource(String name) {
094: Resource res = ucp.getResource(name);
095: return res != null ? res.getURL() : null;
096: }
097:
098: public InputStream getResourceAsStream(String name) {
099: URL url = getResource(name);
100: try {
101: return url != null ? url.openStream() : null;
102: } catch (IOException e) {
103: return null;
104: }
105: }
106:
107: public URL[] getURLs() {
108: return ucp.getURLs();
109: }
110:
111: private static final String protocolPathProp = "java.protocol.handler.pkgs";
112:
113: static {
114: Properties p = new Properties(System.getProperties());
115: String s = p.getProperty(protocolPathProp);
116: if (s != null) {
117: p.put(protocolPathProp, s + "|pnuts.lib");
118: } else {
119: p.put(protocolPathProp, "pnuts.lib");
120: }
121: System.setProperties(p);
122: }
123: }
|