View Javadoc

1   package org.apache.maven.shared.runtime;
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.io.IOException;
23  import java.net.URL;
24  import java.net.URLClassLoader;
25  import java.util.Enumeration;
26  
27  /**
28   * A parent- or child-delegating class loader for use by tests.
29   * 
30   * @author <a href="mailto:markh@apache.org">Mark Hobson</a>
31   * @version $Id: DelegatingClassLoader.java 831910 2009-11-02 15:05:33Z markh $
32   */
33  public class DelegatingClassLoader extends URLClassLoader
34  {
35      // fields -----------------------------------------------------------------
36  
37      private final boolean childDelegating;
38  
39      // constructors -----------------------------------------------------------
40  
41      public DelegatingClassLoader( URL[] urls )
42      {
43          super( urls );
44  
45          childDelegating = false;
46      }
47  
48      public DelegatingClassLoader( URL[] urls, ClassLoader parent )
49      {
50          this( urls, parent, false );
51      }
52  
53      public DelegatingClassLoader( URL[] urls, ClassLoader parent, boolean childDelegating )
54      {
55          super( urls, parent );
56  
57          this.childDelegating = childDelegating;
58      }
59  
60      // ClassLoader methods ----------------------------------------------------
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public Class<?> loadClass( String name ) throws ClassNotFoundException
67      {
68          Class<?> klass;
69  
70          if ( childDelegating )
71          {
72              klass = findLoadedClass( name );
73  
74              if ( klass == null )
75              {
76                  try
77                  {
78                      klass = findClass( name );
79                  }
80                  catch ( ClassNotFoundException exception )
81                  {
82                      if ( getParent() != null )
83                      {
84                          klass = getParent().loadClass( name );
85                      }
86                  }
87              }
88          }
89          else
90          {
91              klass = super.loadClass( name );
92          }
93  
94          return klass;
95      }
96  
97      /**
98       * {@inheritDoc}
99       */
100     @Override
101     public URL getResource( String name )
102     {
103         URL url;
104 
105         if ( childDelegating )
106         {
107             url = findResource( name );
108 
109             if ( url == null && getParent() != null )
110             {
111                 url = getParent().getResource( name );
112             }
113         }
114         else
115         {
116             url = super.getResource( name );
117         }
118 
119         return url;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public Enumeration<URL> getResources( String name ) throws IOException
127     {
128         Enumeration<URL> urls;
129 
130         if ( childDelegating )
131         {
132             urls = findResources( name );
133 
134             if ( getParent() != null )
135             {
136                 Enumeration<URL> parentURLs = getParent().getResources( name );
137 
138                 urls = new CompositeEnumeration<URL>( urls, parentURLs );
139             }
140         }
141         else
142         {
143             urls = super.getResources( name );
144         }
145 
146         return urls;
147     }
148 }