1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
package java.net;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureClassLoader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.aspectj.weaver.loadtime.Aj;
public class URLClassLoader extends SecureClassLoader {
public final static boolean debug = false;
private List path = new LinkedList();
private Aj agent;
public URLClassLoader() {
super();
}
public URLClassLoader(ClassLoader parent) {
super(parent);
}
public URLClassLoader(URL[] urls) throws IOException {
this(urls,null,null);
}
public URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory) throws IOException {
super(parent);
if (debug) System.err.println("> URLClassLoader.URLClassLoader() parent=" + parent);
for (int i = 0; i < urls.length; i++) {
Object pathElement;
URL url = urls[i];
if (debug) System.err.println("- URLClassLoader.URLClassLoader() url=" + url.getPath());
File file = new File(encode(url.getFile()));
if (debug) System.err.println("- URLClassLoader.URLClassLoader() file" + file);
if (file.isDirectory()) pathElement = file;
else if (file.exists() && file.getName().endsWith(".jar")) pathElement = new JarFile(file);
else throw new RuntimeException(file.getAbsolutePath().toString());
path.add(pathElement);
}
agent = new Aj();
if (debug) System.err.println("< URLClassLoader.URLClassLoader() path=" + path);
}
// public final static boolean debug = false;
//
// private List path = new LinkedList();
//// private com.bea.jvm.ClassPreProcessor agent;
// private Object agent;
// private Method preProcess;
// public JRockitClassLoader (URLClassLoader clone) throws Exception {
// /* Use extensions loader */
// super(clone.getParent());
//
// URL[] urls = clone.getURLs();
// for (int i = 0; i < urls.length; i++) {
// Object pathElement;
// URL url = urls[i];
// if (debug) System.err.println("JRockitClassLoader.JRockitClassLoader() url=" + url.getPath());
// File file = new File(encode(url.getFile()));
// if (debug) System.err.println("JRockitClassLoader.JRockitClassLoader() file" + file);
// if (file.isDirectory()) pathElement = file;
// else if (file.exists() && file.getName().endsWith(".jar")) pathElement = new JarFile(file);
// else throw new RuntimeException(file.getAbsolutePath().toString());
// path.add(pathElement);
// }
//
// Class agentClazz = Class.forName("org.aspectj.weaver.loadtime.JRockitAgent",false,this);
// Object obj = agentClazz.newInstance();
// if (debug) System.err.println("JRockitClassLoader.JRockitClassLoader() obj=" + obj);
// this.agent = obj;
// byte[] bytes = new byte[] {};
// Class[] parameterTypes = new Class[] { java.lang.ClassLoader.class, java.lang.String.class, bytes.getClass() };
// preProcess = agentClazz.getMethod("preProcess",parameterTypes);
// }
/* Get rid of escaped characters */
private String encode (String s) {
StringBuffer result = new StringBuffer();
int i = s.indexOf("%");
while (i != -1) {
result.append(s.substring(0,i));
String escaped = s.substring(i+1,i+3);
s = s.substring(i+3);
Integer value = Integer.valueOf(escaped,16);
result.append(new Character((char)value.intValue()));
i = s.indexOf("%");
}
result.append(s);
return result.toString();
}
protected Class findClass(String name) throws ClassNotFoundException {
if (debug) System.err.println("> URLClassLoader.findClass() name=" + name);
Class clazz = null;
try {
clazz = super.findClass(name);
}
catch (ClassNotFoundException ex) {
for (Iterator i = path.iterator(); clazz == null && i.hasNext();) {
byte[] classBytes = null;
try {
Object pathElement = i.next();
if (pathElement instanceof File) {
File dir = (File)pathElement;
String className = name.replace('.','/') + ".class";
File classFile = new File(dir,className);
if (debug) System.err.println("- URLClassLoader.findClass() classFile=" + classFile);
if (classFile.exists()) classBytes = loadClassFromFile(name,classFile);
}
else {
JarFile jar = (JarFile)pathElement;
String className = name.replace('.','/') + ".class";
ZipEntry entry = jar.getEntry(className);
if (entry != null) classBytes = loadBytesFromZipEntry(jar,entry);
}
if (classBytes != null) {
clazz = defineClass(name,classBytes);
}
}
catch (IOException ioException) {
ex.printStackTrace();
}
}
}
if (debug) System.err.println("< URLClassLoader.findClass() clazz=" + clazz);
if (clazz == null) throw new ClassNotFoundException(name);
return clazz;
}
protected URL findResource (String name) {
if (debug) System.err.println("> URLClassLoader.findResource() name=" + name);
URL url = null;
try {
Enumeration enu = findResources(name);
if (enu.hasMoreElements()) url = (URL)enu.nextElement();
}
catch (IOException ex) {
ex.printStackTrace();
}
if (debug) System.err.println("< URLClassLoader.findResource() url=" + url);
return url;
}
protected Enumeration findResources (String name) throws IOException {
if (debug) System.err.println("> URLClassLoader.findResources() name=" + name);
Vector urls = new Vector();
for (Iterator i = path.iterator(); i.hasNext();) {
Object pathElement = i.next();
if (pathElement instanceof File) {
File dir = (File)pathElement;
File resourceFile = new File(dir,name);
// if (debug) System.err.println("- URLClassLoader.findResources() file=" + resourceFile);
}
else {
JarFile jar = (JarFile)pathElement;
ZipEntry entry = jar.getEntry(name);
// if (debug) System.err.println("- URLClassLoader.findResources() entry=" + entry);
if (entry != null) {
if (debug) System.err.println("- URLClassLoader.findResources() jar=" + jar.getName());
final byte[] bytes = loadBytesFromZipEntry(jar,entry);
URLStreamHandler streamHandler = new URLStreamHandler() {
protected URLConnection openConnection(URL u) throws IOException {
URLConnection connection = new URLConnection(u) {
public void connect() throws IOException {
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}
};
return connection;
}
};
URL url = new URL("file",null,0,jar.getName(),streamHandler);
urls.add(url);
}
}
}
Enumeration enu = urls.elements();
if (debug) System.err.println("< URLClassLoader.findResources() enu=" + enu);
return enu;
}
private Class defineClass (String name, byte[] bytes) {
if (debug) System.err.println("> URLClassLoader.defineClass() name=" + name);
// try {
if (agent != null) bytes = agent.preProcess(name,bytes,this);
// }
// catch (IllegalAccessException iae) {
// iae.printStackTrace();
// throw new ClassFormatError(iae.getMessage());
// }
// catch (InvocationTargetException ite) {
// ite.printStackTrace();
// throw new ClassFormatError(ite.getTargetException().getMessage());
// }
if (debug) System.err.println("< URLClassLoader.defineClass() name=" + name);
return super.defineClass(name,bytes,0,bytes.length);
}
private byte[] loadClassFromFile (String name, File file) throws IOException {
if (debug) System.err.println("> URLClassLoader.loadClassFromFile() file=" + file);
byte[] bytes;
bytes = new byte[(int)file.length()];
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
bytes = readBytes(fis,bytes);
}
finally {
if (fis != null) fis.close();
}
if (debug) System.err.println("< URLClassLoader.loadClassFromFile() bytes=b[" + bytes.length + "]");
return bytes;
}
private byte[] loadBytesFromZipEntry (JarFile jar, ZipEntry entry) throws IOException {
if (debug) System.err.println("> URLClassLoader.loadBytesFromZipEntry() entry=" + entry);
byte[] bytes;
bytes = new byte[(int)entry.getSize()];
InputStream is = null;
try {
is = jar.getInputStream(entry);
bytes = readBytes(is,bytes);
}
finally {
if (is != null) is.close();
}
if (debug) System.err.println("< URLClassLoader.loadBytesFromZipEntry() bytes=b[" + bytes.length + "]");
return bytes;
}
private byte[] readBytes (InputStream is, byte[] bytes) throws IOException {
for (int offset = 0; offset < bytes.length;) {
int read = is.read(bytes,offset,bytes.length - offset);
offset += read;
}
return bytes;
}
}
|