1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.resource;
20
21 import java.io.IOException;
22
23 import java.net.URL;
24
25 import java.net.URLConnection;
26
27 /**
28 * Base class for resource loaders. Resource loaders can lookup resources
29 * as URLs from arbitrary locations, including JAR files.
30 *
31 */
32 public class ResourceLoader
33 {
34 /**
35 * Returns the shared resource loader that always returns null.
36 *
37 * @return null for any resource path
38 */
39 public static ResourceLoader getNullResourceLoader()
40 {
41 return _NULL_RESOURCE_LOADER;
42 }
43
44 /**
45 * Finds the resource with the given name. A resource is some data
46 * (images, audio, text, etc) that can be accessed by class code in a way
47 * that is independent of the location of the code.
48 *
49 * <p> The name of a resource is a '<tt>/</tt>'-separated path name that
50 * identifies the resource.
51 *
52 * <p> This method will first search the this resource loader for the
53 * resource. That failing, this method will invoke
54 * {@link #findResource(String)} to on the parent resource loader to
55 * find the resource. </p>
56 *
57 * @param name the resource name
58 *
59 * @return A <tt>URL</tt> object for reading the resource, or
60 * <tt>null</tt> if the resource could not be found or the invoker
61 * doesn't have adequate privileges to get the resource.
62 */
63 public URL getResource(
64 String name
65 ) throws IOException
66 {
67 URL url = findResource(name);
68
69 if (url == null && _parent != null)
70 {
71 url = _parent.getResource(name);
72 }
73
74 return url;
75 }
76
77 /**
78 * Finds the resource with the given name. Resource loader implementations
79 * should override this method to specify where to find resources. </p>
80 *
81 * @param name
82 * The resource name
83 *
84 * @return A <tt>URL</tt> object for reading the resource, or
85 * <tt>null</tt> if the resource could not be found
86 */
87 protected URL findResource(
88 String name
89 ) throws IOException
90 {
91 return null;
92 }
93
94 /**
95 * Returns the content type of this URL connection.
96 *
97 * @return the content type
98 */
99 protected String getContentType(
100 URLConnection conn)
101 {
102 return conn.getContentType();
103 }
104
105 /**
106 * Returns the parent resource loader, or null if this is a root
107 * resource loader.
108 *
109 * @return the parent resource loader
110 */
111 protected ResourceLoader getParent()
112 {
113 return _parent;
114 }
115
116 /**
117 * Constructs a new resource loader with specified parent resource loader.
118 *
119 * @param parent the parent resource loader
120 */
121 protected ResourceLoader(
122 ResourceLoader parent)
123 {
124 _parent = parent;
125 }
126
127 /**
128 * Constructs a new root resource loader.
129 */
130 protected ResourceLoader()
131 {
132 this(null);
133 }
134
135 private static final ResourceLoader _NULL_RESOURCE_LOADER =
136 new ResourceLoader();
137
138 private final ResourceLoader _parent;
139
140 }