View Javadoc

1   package org.apache.maven.util;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.net.URL;
22  
23  /**
24   * A helper bean to load the given URI from the current threads class loader or
25   * the class loader that was used to load this class.
26   *
27   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
28   */
29  
30  public class ResourceBean
31  {
32      private ClassLoader classLoader;
33  
34      /**
35       * Attempts to load the given resource from the given name.
36       * The current thrad context class loader will be tried first, then the
37       * class loader used to load this class.
38       *
39       * If the classLoader property on this bean is set then that ClassLoader is
40       * used in preference to any other.
41       *
42       * @param name of the resource to load
43       * @return URL of the resource or null if could not be found
44       */
45      public URL findResource( String name )
46      {
47          URL answer = null;
48          if ( classLoader != null )
49          {
50              answer = classLoader.getResource( name );
51          }
52          // lets try the current threads class loader first
53          if ( answer == null )
54          {
55              ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
56              if ( contextClassLoader != null )
57              {
58                  answer = contextClassLoader.getResource( name );
59              }
60              if ( answer == null )
61              {
62                  answer = getClass().getClassLoader().getResource( name );
63              }
64          }
65          return answer;
66      }
67  
68      /**
69       * Returns the classLoader.
70       * @return ClassLoader
71       */
72      public ClassLoader getClassLoader()
73      {
74          return classLoader;
75      }
76  
77      /**
78       * Sets the classLoader.
79       * @param classLoader The classLoader to set
80       */
81      public void setClassLoader( ClassLoader classLoader )
82      {
83          this.classLoader = classLoader;
84      }
85  
86  }