1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.booter;
20
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.util.HashSet;
24 import java.util.Set;
25
26
27
28
29 public class IsolatedClassLoader extends URLClassLoader {
30 private final ClassLoader parent = ClassLoader.getSystemClassLoader();
31
32 private final Set<URL> urls = new HashSet<>();
33
34 private final String roleName;
35
36 private boolean childDelegation = true;
37
38 private static final URL[] EMPTY_URL_ARRAY = new URL[0];
39
40 public IsolatedClassLoader(ClassLoader parent, boolean childDelegation, String roleName) {
41 super(EMPTY_URL_ARRAY, parent);
42
43 this.childDelegation = childDelegation;
44
45 this.roleName = roleName;
46 }
47
48
49
50
51
52 @Override
53 @Deprecated
54 public void addURL(URL url) {
55
56
57 if (!urls.contains(url)) {
58 super.addURL(url);
59 urls.add(url);
60 }
61 }
62
63 @Override
64 public synchronized Class loadClass(String name) throws ClassNotFoundException {
65 if (childDelegation) {
66 Class<?> c = findLoadedClass(name);
67
68 if (c == null) {
69 try {
70 c = findClass(name);
71 } catch (ClassNotFoundException e) {
72 if (parent == null) {
73 throw e;
74 } else {
75 c = parent.loadClass(name);
76 }
77 }
78 }
79
80 return c;
81 } else {
82 return super.loadClass(name);
83 }
84 }
85
86 @Override
87 public String toString() {
88 return "IsolatedClassLoader{roleName='" + roleName + "'}";
89 }
90 }