1 package org.apache.maven.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }