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 | 0 | private final ClassLoader parent = ClassLoader.getSystemClassLoader(); |
34 | |
|
35 | 0 | private final Set urls = new HashSet(); |
36 | |
|
37 | |
private final String roleName; |
38 | |
|
39 | 0 | private boolean childDelegation = true; |
40 | |
|
41 | 0 | private static final URL[] EMPTY_URL_ARRAY = new URL[0]; |
42 | |
|
43 | |
public IsolatedClassLoader( ClassLoader parent, boolean childDelegation, String roleName ) |
44 | |
{ |
45 | 0 | super( EMPTY_URL_ARRAY, parent ); |
46 | |
|
47 | 0 | this.childDelegation = childDelegation; |
48 | |
|
49 | 0 | this.roleName = roleName; |
50 | 0 | } |
51 | |
|
52 | |
public void addURL( URL url ) |
53 | |
{ |
54 | |
|
55 | 0 | if ( !urls.contains( url ) ) |
56 | |
{ |
57 | 0 | super.addURL( url ); |
58 | 0 | urls.add( url ); |
59 | |
} |
60 | 0 | } |
61 | |
|
62 | |
public synchronized Class loadClass( String name ) |
63 | |
throws ClassNotFoundException |
64 | |
{ |
65 | |
Class c; |
66 | |
|
67 | 0 | if ( childDelegation ) |
68 | |
{ |
69 | 0 | c = findLoadedClass( name ); |
70 | |
|
71 | 0 | ClassNotFoundException ex = null; |
72 | |
|
73 | 0 | if ( c == null ) |
74 | |
{ |
75 | |
try |
76 | |
{ |
77 | 0 | c = findClass( name ); |
78 | |
} |
79 | 0 | catch ( ClassNotFoundException e ) |
80 | |
{ |
81 | 0 | ex = e; |
82 | |
|
83 | 0 | if ( parent != null ) |
84 | |
{ |
85 | 0 | c = parent.loadClass( name ); |
86 | |
} |
87 | 0 | } |
88 | |
} |
89 | |
|
90 | 0 | if ( c == null ) |
91 | |
{ |
92 | 0 | throw ex; |
93 | |
} |
94 | 0 | } |
95 | |
else |
96 | |
{ |
97 | 0 | c = super.loadClass( name ); |
98 | |
} |
99 | |
|
100 | 0 | return c; |
101 | |
} |
102 | |
|
103 | |
public String toString() |
104 | |
{ |
105 | 0 | return "IsolatedClassLoader{" + |
106 | |
"roleName='" + roleName + '\'' + |
107 | |
'}'; |
108 | |
} |
109 | |
} |