01: /*
02: * @(#)ClassPath.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package pnuts.lib;
10:
11: import java.io.*;
12: import java.net.*;
13: import java.util.*;
14:
15: class ClassPath {
16:
17: private URL[] urls;
18: private Vector loaders = new Vector();
19: private Hashtable lmap = new Hashtable(); // {URL, loader}
20: private URLStreamHandler jarHandler;
21:
22: public ClassPath(URL[] urls) {
23: this (urls, null);
24: }
25:
26: public ClassPath(URL[] urls, URLStreamHandlerFactory factory) {
27: this .urls = urls;
28: if (factory != null) {
29: jarHandler = factory.createURLStreamHandler("jar");
30: }
31: }
32:
33: public URL[] getURLs() {
34: return urls;
35: }
36:
37: public Resource getResource(String name) {
38: for (int i = 0; i < urls.length; i++) {
39: Loader loader = (Loader) lmap.get(urls[i]);
40: try {
41: if (loader == null) {
42: lmap.put(urls[i], loader = getLoader(urls[i]));
43: }
44: Resource res = loader.getResource(name);
45: if (res != null) {
46: return res;
47: }
48: } catch (IOException e) {
49: /* ignore */
50: }
51: }
52: return null;
53: }
54:
55: /*
56: * Returns the Loader for the specified base URL.
57: */
58: private Loader getLoader(URL url) throws IOException {
59: String file = url.getFile();
60: if (file != null && file.endsWith("/")) {
61: if ("file".equals(url.getProtocol())) {
62: return new FileLoader(url);
63: } else {
64: throw new RuntimeException("not supported");
65: }
66: } else {
67: return new JarLoader(url/*, jarHandler*/);
68: }
69: }
70: }
|