1
|
|
package abbot.util;
|
2
|
|
|
3
|
|
import java.io.File;
|
4
|
|
import java.net.*;
|
5
|
|
import java.util.StringTokenizer;
|
6
|
|
import java.util.ArrayList;
|
7
|
|
|
8
|
|
import abbot.Platform;
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
public class PathClassLoader extends java.net.URLClassLoader {
|
16
|
|
|
17
|
|
private String classPath;
|
18
|
|
private static final Factory factory = new Factory();
|
19
|
|
|
20
|
|
|
21
|
0
|
public PathClassLoader(String path) {
|
22
|
0
|
this(path, null);
|
23
|
|
}
|
24
|
|
|
25
|
|
|
26
|
253
|
public PathClassLoader(String path, ClassLoader parent){
|
27
|
253
|
super(getURLs(path != null ? path
|
28
|
|
: System.getProperty("java.class.path"), ":;"),
|
29
|
|
parent, factory);
|
30
|
253
|
this.classPath = path != null
|
31
|
|
? path : System.getProperty("java.class.path");
|
32
|
|
}
|
33
|
|
|
34
|
16
|
public String getClassPath() { return classPath; }
|
35
|
|
|
36
|
1476
|
protected synchronized Class loadClass(String name, boolean resolve)
|
37
|
|
throws ClassNotFoundException {
|
38
|
1476
|
int i = name.lastIndexOf('.');
|
39
|
1476
|
if(i != -1) {
|
40
|
1476
|
SecurityManager sm = System.getSecurityManager();
|
41
|
1476
|
if(sm != null)
|
42
|
992
|
sm.checkPackageAccess(name.substring(0, i));
|
43
|
|
}
|
44
|
1476
|
return super.loadClass(name, resolve);
|
45
|
|
}
|
46
|
|
|
47
|
|
|
48
|
253
|
static URL[] getURLs(String p, String separators) {
|
49
|
253
|
String s = p != null
|
50
|
|
? p : System.getProperty("java.class.path");
|
51
|
253
|
File files[] = s != null ? convertPathToFiles(s, separators) : new File[0];
|
52
|
253
|
URL[] urls = new URL[files.length];
|
53
|
253
|
for (int i=0;i < urls.length;i++) {
|
54
|
2310
|
try {
|
55
|
2310
|
urls[i] = files[i].toURL();
|
56
|
|
}
|
57
|
|
catch(MalformedURLException e) {
|
58
|
0
|
throw new RuntimeException(e.getMessage());
|
59
|
|
}
|
60
|
|
}
|
61
|
253
|
return urls;
|
62
|
|
}
|
63
|
|
|
64
|
|
|
65
|
8
|
public static String[] convertPathToFilenames(String path) {
|
66
|
8
|
return convertPathToFilenames(path, ":;");
|
67
|
|
}
|
68
|
|
|
69
|
|
|
70
|
253
|
public static File[] convertPathToFiles(String path, String seps) {
|
71
|
253
|
String[] names = convertPathToFilenames(path, ":;");
|
72
|
253
|
ArrayList files = new ArrayList();
|
73
|
253
|
for (int i=0;i < names.length;i++) {
|
74
|
2310
|
files.add(new File(names[i]));
|
75
|
|
}
|
76
|
253
|
return (File[])files.toArray(new File[files.size()]);
|
77
|
|
}
|
78
|
|
|
79
|
263
|
static String[] convertPathToFilenames(String path, String seps) {
|
80
|
263
|
if (path == null)
|
81
|
4
|
path = "";
|
82
|
263
|
boolean fixDrives = Platform.isWindows() && seps.indexOf(":") != -1;
|
83
|
263
|
StringTokenizer st = new StringTokenizer(path, seps);
|
84
|
263
|
ArrayList names = new ArrayList();
|
85
|
263
|
while (st.hasMoreTokens()) {
|
86
|
2321
|
String fp = st.nextToken();
|
87
|
|
|
88
|
2321
|
if (fixDrives && fp.length() == 1 && st.hasMoreTokens()) {
|
89
|
2234
|
char ch = fp.charAt(0);
|
90
|
2234
|
if ((ch >= 'a' && ch <= 'z')
|
91
|
|
|| (ch >= 'A' && ch <= 'Z')) {
|
92
|
2232
|
fp += ":" + st.nextToken();
|
93
|
|
}
|
94
|
|
}
|
95
|
2321
|
names.add(fp);
|
96
|
|
}
|
97
|
263
|
return (String[])names.toArray(new String[names.size()]);
|
98
|
|
}
|
99
|
|
|
100
|
|
|
101
|
|
private static class Factory implements URLStreamHandlerFactory {
|
102
|
|
private static final String PREFIX = "sun.net.www.protocol";
|
103
|
11
|
private Factory() { }
|
104
|
253
|
public URLStreamHandler createURLStreamHandler(String protocol) {
|
105
|
253
|
String name = PREFIX + "." + protocol + ".Handler";
|
106
|
253
|
try {
|
107
|
253
|
Class c = Class.forName(name);
|
108
|
253
|
return (URLStreamHandler)c.newInstance();
|
109
|
|
}
|
110
|
|
catch(ClassNotFoundException e) {
|
111
|
0
|
e.printStackTrace();
|
112
|
|
}
|
113
|
|
catch(InstantiationException e) {
|
114
|
0
|
e.printStackTrace();
|
115
|
|
}
|
116
|
|
catch(IllegalAccessException e) {
|
117
|
0
|
e.printStackTrace();
|
118
|
|
}
|
119
|
0
|
throw new Error("could not load "
|
120
|
|
+ protocol + "system protocol handler");
|
121
|
|
}
|
122
|
|
}
|
123
|
|
|
124
|
510
|
public String toString() {
|
125
|
510
|
return super.toString() + " (classpath=" + classPath + ")";
|
126
|
|
}
|
127
|
|
}
|
128
|
|
|