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