View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Loads classes from jar files added via {@link #addURL(URL)}.
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       * @deprecated this method will use {@link java.io.File} instead of {@link URL} in the next
50       * major version.
51       */
52      @Override
53      @Deprecated
54      public void addURL(URL url) {
55          // avoid duplicates
56          // todo avoid URL due to calling equals method may cause some overhead due to resolving host or file.
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  }