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.MalformedURLException;
007: import java.net.URL;
008: import java.util.Properties;
009: import java.util.zip.ZipEntry;
010: import java.util.zip.ZipFile;
011:
012: public class JavuPlugJarClassLoader extends ClassLoader {
013: protected final JavuPlugClassLoader node;
014: protected ZipFile file;
015: protected String fileName;
016:
017: protected JavuPlugJarClassLoader(String fileName,
018: JavuPlugClassLoader node) throws IOException {
019: super (node);
020: this .node = node;
021: this .fileName = fileName;
022: file = new ZipFile(fileName);
023: }
024:
025: public String getFileName() {
026: return fileName;
027: }
028:
029: public synchronized void closeJar() {
030: if (file == null)
031: return;
032: try {
033: file.close();
034: } catch (IOException e) {
035: }
036: file = null;
037: }
038:
039: protected synchronized Class jarFindClass(String name, String path) {
040: Class c = findLoadedClass(name);
041: if (c != null)
042: return c;
043:
044: if (file == null)
045: return null;
046:
047: try {
048: ZipEntry e = file.getEntry(path);
049: if (e != null) {
050: InputStream is = file.getInputStream(e);
051: int len = (int) e.getSize();
052: byte[] data = new byte[len];
053: int r = 0;
054: int off = 0;
055: while (r != -1 && len > 0) {
056: r = is.read(data, off, len);
057: off += r;
058: len -= r;
059: }
060: if (len == 0) {
061: c = defineClass(name, data, 0, data.length);
062: return c;
063: }
064: }
065: } catch (IOException e1) {
066: }
067: return null;
068: }
069:
070: protected Class loadClass(String name, boolean resolve)
071: throws ClassNotFoundException {
072: synchronized (this ) {
073: Class c = findLoadedClass(name);
074: if (c != null)
075: return c;
076: }
077: return node.loadClass(name, resolve);
078: }
079:
080: //////////////////////////////////////////////////////
081:
082: public URL findResource(String name) {
083: if (name.startsWith("/"))
084: name = name.substring(1);
085:
086: ZipEntry e = getResourceEntry(name);
087:
088: if (e != null) {
089:
090: try {
091: return new URL("javuplug:/" + node.getUrlPrefix() + ":"
092: + fileName.replace(File.separatorChar, '/')
093: + "!/" + name);
094: } catch (MalformedURLException e1) {
095: }
096: }
097: return null;
098: }
099:
100: public URL getResource(String name) {
101: URL u = node.getParent().getResource(name);
102: if (u != null)
103: return u;
104:
105: u = findResource(name);
106: if (u != null)
107: return u;
108:
109: return node.getResource(name);
110: }
111:
112: public synchronized InputStream getResourceAsStream(String name) {
113: ZipEntry e = getResourceEntry(name);
114: if (e != null) {
115: try {
116: return file.getInputStream(e);
117: } catch (IOException e1) {
118: }
119: }
120: return null;
121: }
122:
123: //////////////////////////////////////////////////////
124:
125: protected synchronized ZipEntry getResourceEntry(String name) {
126: if (file == null)
127: return null;
128: if (name.startsWith("/"))
129: name = name.substring(1);
130: return file.getEntry(name);
131: }
132:
133: //////////////////////////////////////////////////////
134:
135: public Class getPluginClass(String key) {
136: InputStream is = getResourceAsStream(key + ".properties");
137: Properties p = new Properties();
138: try {
139: p.load(is);
140: is.close();
141: String cn = p.getProperty(key);
142: if (cn != null) {
143: return loadClass(cn.trim(), true);
144: }
145: } catch (Exception e) {
146: }
147: return null;
148: }
149:
150: public Object createPlugin(String key, Class requiredClass) {
151: Class c = getPluginClass(key);
152: Object o = null;
153: try {
154: if (c != null)
155: o = c.newInstance();
156: if (requiredClass != null && !requiredClass.isInstance(o)) {
157: o = null;
158: }
159: } catch (Exception e) {
160: }
161: return o;
162: }
163: }
|