Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IsolatedClassLoader |
|
| 3.3333333333333335;3.333 |
1 | package org.apache.maven.surefire.booter; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * @noinspection CustomClassloader | |
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 | 0 | private boolean childDelegation = true; |
38 | ||
39 | 0 | private static final URL[] EMPTY_URL_ARRAY = new URL[0]; |
40 | ||
41 | public IsolatedClassLoader( ClassLoader parent, boolean childDelegation ) | |
42 | { | |
43 | 0 | super( EMPTY_URL_ARRAY, parent ); |
44 | ||
45 | 0 | this.childDelegation = childDelegation; |
46 | 0 | } |
47 | ||
48 | public void addURL( URL url ) | |
49 | { | |
50 | // avoid duplicates | |
51 | 0 | if ( !urls.contains( url ) ) |
52 | { | |
53 | 0 | super.addURL( url ); |
54 | 0 | urls.add( url ); |
55 | } | |
56 | 0 | } |
57 | ||
58 | public synchronized Class loadClass( String name ) | |
59 | throws ClassNotFoundException | |
60 | { | |
61 | Class c; | |
62 | ||
63 | 0 | if ( childDelegation ) |
64 | { | |
65 | 0 | c = findLoadedClass( name ); |
66 | ||
67 | 0 | ClassNotFoundException ex = null; |
68 | ||
69 | 0 | if ( c == null ) |
70 | { | |
71 | try | |
72 | { | |
73 | 0 | c = findClass( name ); |
74 | } | |
75 | 0 | catch ( ClassNotFoundException e ) |
76 | { | |
77 | 0 | ex = e; |
78 | ||
79 | 0 | if ( parent != null ) |
80 | { | |
81 | 0 | c = parent.loadClass( name ); |
82 | } | |
83 | 0 | } |
84 | } | |
85 | ||
86 | 0 | if ( c == null ) |
87 | { | |
88 | 0 | throw ex; |
89 | } | |
90 | 0 | } |
91 | else | |
92 | { | |
93 | 0 | c = super.loadClass( name ); |
94 | } | |
95 | ||
96 | 0 | return c; |
97 | } | |
98 | } |