1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.log4j.lf5.util;
18
19 import java.io.InputStream;
20 import java.net.URL;
21
22 /***
23 * ResourceUtils. Provide a set of convenience methods for working with
24 * Resources.
25 *
26 * @see org.apache.log4j.lf5.util.Resource
27 *
28 * @author Michael J. Sikorsky
29 * @author Robert Shaw
30 */
31
32
33
34 public class ResourceUtils {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 /***
56 * Get the InputStream for this resource. Note: to convert an InputStream
57 * into an InputReader, use: new InputStreamReader(InputStream).
58 *
59 * @param object The object to grab the Classloader from.
60 * This parameter is quite important from a
61 * visibility of resources standpoint as the
62 * hierarchy of Classloaders plays a role.
63 *
64 * @param resource The resource to load.
65 *
66 * @return If the Resource was found, the InputStream, otherwise null.
67 *
68 * @see Resource
69 * @see #getResourceAsURL(Object,Resource)
70 * @see InputStream
71 */
72 public static InputStream getResourceAsStream(Object object, Resource resource) {
73 ClassLoader loader = object.getClass().getClassLoader();
74
75 InputStream in = null;
76
77 if (loader != null) {
78 in = loader.getResourceAsStream(resource.getName());
79 } else {
80 in = ClassLoader.getSystemResourceAsStream(resource.getName());
81 }
82
83 return in;
84 }
85
86 /***
87 * Get the URL for this resource.
88 *
89 * @param object The object to grab the Classloader from.
90 * This parameter is quite important from a
91 * visibility of resources standpoint as the
92 * hierarchy of Classloaders plays a role.
93 *
94 * @param resource The resource to load.
95 *
96 * @return If the Resource was found, the URL, otherwise null.
97 *
98 * @see Resource
99 * @see #getResourceAsStream(Object,Resource)
100 */
101 public static URL getResourceAsURL(Object object, Resource resource) {
102 ClassLoader loader = object.getClass().getClassLoader();
103
104 URL url = null;
105
106 if (loader != null) {
107 url = loader.getResource(resource.getName());
108 } else {
109 url = ClassLoader.getSystemResource(resource.getName());
110 }
111
112 return (url);
113 }
114
115
116
117
118
119
120
121
122
123
124
125
126
127 }
128
129
130
131
132
133