001: package com.javujavu.javux.app;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.net.URL;
007: import java.util.Hashtable;
008: import java.util.Vector;
009:
010: import com.javujavu.javux.app.protocol.javuplug.JavuPlugURLConnection;
011:
012: public class JavuPlugClassLoader extends ClassLoader {
013: protected final Vector jars;
014: protected final Hashtable cache;
015: private final String urlPrefix;
016:
017: public JavuPlugClassLoader(ClassLoader parent) {
018: super (parent);
019:
020: jars = new Vector();
021: cache = new Hashtable();
022: urlPrefix = Integer.toString(hashCode(), 16);
023: }
024:
025: private String checkFile(String fileName) {
026: File f = new File(fileName);
027: if (f.isFile()) {
028: try {
029: return f.getCanonicalPath();
030: } catch (IOException e) {
031: }
032: }
033: return null;
034: }
035:
036: public synchronized JavuPlugJarClassLoader getJarLoader(
037: String fileName) {
038: fileName = checkFile(fileName);
039: if (fileName != null) {
040: for (int i = 0; i < jars.size(); i++) {
041: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
042: .elementAt(i);
043: if (fileName.equals(jar.fileName))
044: return jar;
045: }
046: }
047: return null;
048: }
049:
050: public synchronized JavuPlugJarClassLoader loadJar(String fileName) {
051: fileName = checkFile(fileName);
052: if (fileName == null)
053: return null;
054: JavuPlugJarClassLoader jar = getJarLoader(fileName);
055: if (jar == null) {
056: try {
057: jar = new JavuPlugJarClassLoader(fileName, this );
058: jars.addElement(jar);
059:
060: if (jars.size() == 1) {
061: JavuPlugURLConnection.addHandler(getUrlPrefix(),
062: this );
063: }
064: } catch (IOException e) {
065: }
066: }
067: return jar;
068: }
069:
070: protected String getUrlPrefix() {
071: return urlPrefix;
072: }
073:
074: public synchronized void unloadJar(String fileName) {
075: JavuPlugJarClassLoader jar = getJarLoader(fileName);
076: if (jar != null) {
077: jar.closeJar();
078: jars.removeElement(jar);
079: if (jars.size() == 0) {
080: JavuPlugURLConnection.removeHandler(getUrlPrefix());
081: }
082: cache.clear();
083: }
084: }
085:
086: public synchronized void unloadAllJars() {
087: for (int i = 0; i < jars.size(); i++) {
088: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
089: .elementAt(i);
090: jar.closeJar();
091: }
092: jars.removeAllElements();
093: JavuPlugURLConnection.removeHandler(getUrlPrefix());
094: cache.clear();
095: }
096:
097: protected Class findClass(String name)
098: throws ClassNotFoundException {
099: String path = name.replace('.', '/').concat(".class");
100:
101: for (int i = 0; i < jars.size(); i++) {
102: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
103: .elementAt(i);
104:
105: Class c = jar.jarFindClass(name, path);
106: if (c != null) {
107: cache.put(c.getName(), c);
108:
109: return c;
110: }
111: }
112: throw new ClassNotFoundException(name);
113: }
114:
115: protected synchronized Class loadClass(String name, boolean resolve)
116: throws ClassNotFoundException {
117: Class c = null;
118: c = (Class) cache.get(name);
119: if (c == null) {
120: try {
121: c = getParent().loadClass(name);
122: } catch (ClassNotFoundException e) {
123: }
124: }
125: if (c == null)
126: c = findClass(name);
127: if (resolve) {
128: resolveClass(c);
129: }
130: return c;
131: }
132:
133: public synchronized InputStream getResourceAsStream(String name) {
134: if (name.startsWith("/"))
135: name = name.substring(1);
136:
137: for (int i = 0; i < jars.size(); i++) {
138: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
139: .elementAt(i);
140: InputStream is = jar.getResourceAsStream(name);
141: if (is != null)
142: return is;
143: }
144: return null;
145: }
146:
147: public URL getResource(String name) {
148: URL u = getParent().getResource(name);
149: if (u != null)
150: return u;
151:
152: synchronized (this ) {
153: for (int i = 0; i < jars.size(); i++) {
154: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
155: .elementAt(i);
156: u = jar.findResource(name);
157: if (u != null)
158: return u;
159: }
160: return null;
161: }
162: }
163:
164: ///////////////////////////////////////////////////////////
165:
166: public JavuPlugJarClassLoader[] listResourceLoaders(String name) {
167: Vector v = new Vector();
168: synchronized (this ) {
169: for (int i = 0; i < jars.size(); i++) {
170: JavuPlugJarClassLoader jar = (JavuPlugJarClassLoader) jars
171: .elementAt(i);
172: if (jar.getResourceEntry(name) != null) {
173: v.addElement(jar);
174: }
175: }
176: }
177: JavuPlugJarClassLoader[] rr = new JavuPlugJarClassLoader[v
178: .size()];
179: v.copyInto(rr);
180: return rr;
181: }
182:
183: public JavuPlugJarClassLoader[] listPluginLoaders(String key) {
184: return listResourceLoaders(key + ".properties");
185: }
186: }
|