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