-
Notifications
You must be signed in to change notification settings - Fork 68
/
JarClassLoader.java
53 lines (44 loc) · 1.55 KB
/
JarClassLoader.java
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
package cn.trinea.java.dynamic.load.host.loader;
import java.net.URL;
import java.net.URLClassLoader;
/**
* custom jar class loader, so common lib will be load only once
*
* @author <a href="http://www.trinea.cn/" target="_blank">Trinea</a>
*
*/
public class JarClassLoader extends URLClassLoader {
static String PREFIX_COMMON_CLASS = "cn.trinea.java.dynamic.load.common";
public JarClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
}
// common lib class loader to load common class
private static ClassLoader COMMON_LIB_LOADER;
public static ClassLoader getCommonLibLoader() {
if (COMMON_LIB_LOADER == null) {
synchronized (JarClassLoader.class) {
if (COMMON_LIB_LOADER == null) {
COMMON_LIB_LOADER = JarClassLoader.class.getClassLoader();
}
}
}
return COMMON_LIB_LOADER;
}
private boolean isLibClass(String className) {
return (className != null && className.startsWith(PREFIX_COMMON_CLASS));
}
@Override
protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException {
if (isLibClass(className)) {
try {
Class<?> clazz = JarClassLoader.getCommonLibLoader().loadClass(className);
if (clazz != null) {
return clazz;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return super.loadClass(className, resolve);
}
}