001: package abbot.util;
002:
003: import java.io.File;
004: import java.net.*;
005: import java.util.StringTokenizer;
006: import java.util.ArrayList;
007:
008: import abbot.Platform;
009:
010: /** Provide a class loader that loads from a custom path. Similar to
011: * sun.misc.Launcher$AppClassLoader (the usual application class loader),
012: * except that it doesn't do the security checks that AppClassLoader does.
013: * If path given is null, uses java.class.path.
014: */
015: public class PathClassLoader extends java.net.URLClassLoader {
016:
017: private String classPath;
018: private static final Factory factory = new Factory();
019:
020: /** Create a class loader that loads classes from the given path. */
021: public PathClassLoader(String path) {
022: this (path, null);
023: }
024:
025: /** Create a class loader that loads classes from the given path. */
026: public PathClassLoader(String path, ClassLoader parent) {
027: super (getURLs(path != null ? path : System
028: .getProperty("java.class.path"), ":;"), parent, factory);
029: this .classPath = path != null ? path : System
030: .getProperty("java.class.path");
031: }
032:
033: public String getClassPath() {
034: return classPath;
035: }
036:
037: protected synchronized Class loadClass(String name, boolean resolve)
038: throws ClassNotFoundException {
039: int i = name.lastIndexOf('.');
040: if (i != -1) {
041: SecurityManager sm = System.getSecurityManager();
042: if (sm != null)
043: sm.checkPackageAccess(name.substring(0, i));
044: }
045: return super .loadClass(name, resolve);
046: }
047:
048: /** Returns an array of URLs based on the given classpath string. */
049: static URL[] getURLs(String p, String separators) {
050: String s = p != null ? p : System
051: .getProperty("java.class.path");
052: File files[] = s != null ? convertPathToFiles(s, separators)
053: : new File[0];
054: URL[] urls = new URL[files.length];
055: for (int i = 0; i < urls.length; i++) {
056: try {
057: urls[i] = files[i].toURL();
058: } catch (MalformedURLException e) {
059: throw new RuntimeException(e.getMessage());
060: }
061: }
062: return urls;
063: }
064:
065: /** Returns an array of filenames (including path). */
066: public static String[] convertPathToFilenames(String path) {
067: return convertPathToFilenames(path, ":;");
068: }
069:
070: /** Convert the given path string into an array of File. */
071: public static File[] convertPathToFiles(String path, String seps) {
072: String[] names = convertPathToFilenames(path, ":;");
073: ArrayList files = new ArrayList();
074: for (int i = 0; i < names.length; i++) {
075: files.add(new File(names[i]));
076: }
077: return (File[]) files.toArray(new File[files.size()]);
078: }
079:
080: static String[] convertPathToFilenames(String path, String seps) {
081: if (path == null)
082: path = "";
083: boolean fixDrives = Platform.isWindows()
084: && seps.indexOf(":") != -1;
085: StringTokenizer st = new StringTokenizer(path, seps);
086: ArrayList names = new ArrayList();
087: while (st.hasMoreTokens()) {
088: String fp = st.nextToken();
089: // Fix up w32 absolute pathnames
090: if (fixDrives && fp.length() == 1 && st.hasMoreTokens()) {
091: char ch = fp.charAt(0);
092: if ((ch >= 'a' && ch <= 'z')
093: || (ch >= 'A' && ch <= 'Z')) {
094: fp += ":" + st.nextToken();
095: }
096: }
097: names.add(fp);
098: }
099: return (String[]) names.toArray(new String[names.size()]);
100: }
101:
102: /** Taken from sun.misc.Launcher. */
103: private static class Factory implements URLStreamHandlerFactory {
104: private static final String PREFIX = "sun.net.www.protocol";
105:
106: private Factory() {
107: }
108:
109: public URLStreamHandler createURLStreamHandler(String protocol) {
110: String name = PREFIX + "." + protocol + ".Handler";
111: try {
112: Class c = Class.forName(name);
113: return (URLStreamHandler) c.newInstance();
114: } catch (ClassNotFoundException e) {
115: e.printStackTrace();
116: } catch (InstantiationException e) {
117: e.printStackTrace();
118: } catch (IllegalAccessException e) {
119: e.printStackTrace();
120: }
121: throw new Error("could not load " + protocol
122: + "system protocol handler");
123: }
124: }
125:
126: public String toString() {
127: return super .toString() + " (classpath=" + classPath + ")";
128: }
129: }
|