View Javadoc
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      private final ClassLoader parent = ClassLoader.getSystemClassLoader();
34  
35      private final Set<URL> urls = new HashSet<URL>();
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       * @deprecated this method will use {@link java.io.File} instead of {@link URL} in the next
54       * major version.
55       */
56      @Deprecated
57      public void addURL( URL url )
58      {
59          // avoid duplicates
60          // todo avoid URL due to calling equals method may cause some overhead due to resolving host or file.
61          if ( !urls.contains( url ) )
62          {
63              super.addURL( url );
64              urls.add( url );
65          }
66      }
67  
68      public synchronized Class loadClass( String name )
69          throws ClassNotFoundException
70      {
71          if ( childDelegation )
72          {
73              Class<?> c = findLoadedClass( name );
74  
75              if ( c == null )
76              {
77                  try
78                  {
79                      c = findClass( name );
80                  }
81                  catch ( ClassNotFoundException e )
82                  {
83                      if ( parent == null )
84                      {
85                          throw e;
86                      }
87                      else
88                      {
89                          c = parent.loadClass( name );
90                      }
91                  }
92              }
93  
94              return c;
95          }
96          else
97          {
98              return super.loadClass( name );
99          }
100     }
101 
102     public String toString()
103     {
104         return "IsolatedClassLoader{roleName='" + roleName + "'}";
105     }
106 }