Source code: com/prolifics/jni/Loader.java
1 /* @(#)Loader.java 77.5 00/04/25 17:57:54" */
2
3 /*************************************************/
4 /* Copyright (c) 1998 - 1999 */
5 /* by */
6 /* JYACC, Inc., New York NY USA */
7 /* and contributors. */
8 /* Use of this program is governed by the */
9 /* JYACC Public License Version 1.0, a copy of */
10 /* which can be obtained at */
11 /* http://www.possl.org/jyacc-license.html */
12 /*************************************************/
13
14 package com.prolifics.jni;
15
16 import java.util.*;
17 import java.util.zip.*;
18 import java.io.*;
19
20 /**
21 Custom loader, to allow classes to be reloaded after exiting editor.
22 */
23 public class Loader extends ClassLoader
24 {
25 private String psep;
26 private Vector paths = new Vector();
27 private Hashtable moreClasses = new Hashtable();
28 boolean usePrlloader = false;
29 Loader prlLoader = null;
30
31 Loader()
32 {
33 Properties p = System.getProperties();
34 psep = p.getProperty("path.separator");
35
36 String path = p.getProperty("java.class.path");
37 addPath (path);
38 }
39
40 void addPath (String path)
41 {
42 int from = 0;
43 int length = path.length();
44
45 while (from < length)
46 {
47 int to = path.indexOf(psep, from);
48 if (to == -1)
49 to = length;
50 String dir = path.substring(from, to);
51 if (dir.length() == 0)
52 dir = ".";
53 try {
54 paths.addElement(new DirLoader(dir));
55 } catch (Throwable e1) {
56 try {
57 paths.addElement(new JarLoader(dir));
58 } catch (Throwable e2) {
59 }
60 }
61 from = to + 1;
62 }
63 }
64
65 void setPrlLoader (Loader loader)
66 {
67 usePrlloader = true;
68 prlLoader = loader;
69 }
70
71 static byte[] getClassData(InputStream in)
72 {
73 Vector v = new Vector();
74 int c;
75
76 try {
77 while ((c = in.read()) != -1)
78 v.addElement(new Byte((byte)c));
79 } catch (Throwable e) {
80 } finally {
81 try {
82 in.close();
83 } catch (Throwable e) {
84 }
85 }
86 byte[] b = new byte[v.size()];
87 for (c = 0; c < v.size(); ++c)
88 b[c] = ((Byte)v.elementAt(c)).byteValue();
89 return b;
90 }
91
92 interface ClassData
93 {
94 byte[] getClassData(String name);
95 void close() throws IOException;
96 }
97
98 static class DirLoader extends File implements ClassData
99 {
100 public DirLoader(String dir) throws IOException
101 {
102 super(dir);
103 if (!isDirectory())
104 throw new IOException(dir);
105 }
106
107 public byte[] getClassData(String name)
108 {
109 try {
110 return Loader.getClassData
111 (new FileInputStream
112 (getPath() + separator + name));
113 } catch (Throwable e) {
114 return null;
115 }
116 }
117
118 public void close() { }
119 }
120
121 static class JarLoader extends ZipFile implements ClassData
122 {
123 public JarLoader(String jar) throws IOException
124 {
125 super(jar);
126 }
127
128 public byte[] getClassData(String name)
129 {
130 ZipEntry entry = getEntry(name);
131 if (entry != null)
132 {
133 try {
134 return Loader.getClassData(
135 getInputStream(entry));
136 } catch (Throwable e) {
137 }
138 }
139 return null;
140 }
141 }
142
143 byte[] getClass(String name)
144 {
145 name = name.replace('.', '/') + ".class";
146
147 int i;
148 for (i = 0; i < paths.size(); ++i)
149 {
150 byte[] data = ((ClassData)paths.elementAt(i)).
151 getClassData(name);
152 if (data != null)
153 return data;
154 }
155 return null;
156 }
157
158 public synchronized Class loadClass(String name, boolean resolve)
159 throws ClassNotFoundException
160 {
161 Class c;
162 byte[] data;
163
164 if ((c = findLoadedClass(name)) != null ||
165 (c = (Class)moreClasses.get(name)) != null)
166 {
167 return c;
168 }
169
170 if (name.startsWith(".") ||
171 name.startsWith("java.") ||
172 name.startsWith("javax.") ||
173 name.startsWith("com.sun.") ||
174 name.startsWith("com.ibm.ejs.") ||
175 name.startsWith("org.omg.")
176 )
177 {
178 c = findSystemClass(name);
179 }
180
181 else if (usePrlloader && name.startsWith("com.prolifics.jni."))
182 {
183 c = (prlLoader == null)
184 ? findSystemClass(name)
185 : prlLoader.loadClass(name, resolve);
186 }
187
188 else if ((data = getClass(name)) == null)
189 {
190 c = findSystemClass(name);
191 }
192
193 else
194 {
195 try {
196 c = defineClass(name, data, 0, data.length);
197 if (resolve)
198 resolveClass(c);
199
200 } catch (Throwable e) {
201 throw new ClassNotFoundException(name);
202 }
203 }
204
205 if (c != null)
206 moreClasses.put(name, c);
207 return c;
208 }
209
210 public synchronized void discard()
211 {
212 for (int i = 0; i < paths.size(); ++i)
213 {
214 try {
215 ((ClassData)paths.elementAt(i)).close();
216 } catch (IOException e) {
217 }
218 }
219 paths.removeAllElements();
220 }
221 }