1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
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 // Contributed by ThoughtWorks Inc.
33
34 public class ResourceUtils {
35 //--------------------------------------------------------------------------
36 // Constants:
37 //--------------------------------------------------------------------------
38
39 //--------------------------------------------------------------------------
40 // Protected Variables:
41 //--------------------------------------------------------------------------
42
43 //--------------------------------------------------------------------------
44 // Private Variables:
45 //--------------------------------------------------------------------------
46
47 //--------------------------------------------------------------------------
48 // Constructors:
49 //--------------------------------------------------------------------------
50
51 //--------------------------------------------------------------------------
52 // Public Methods:
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 // Protected Methods:
117 //--------------------------------------------------------------------------
118
119 //--------------------------------------------------------------------------
120 // Private Methods:
121 //--------------------------------------------------------------------------
122
123 //--------------------------------------------------------------------------
124 // Nested Top-Level Classes or Interfaces:
125 //--------------------------------------------------------------------------
126
127 }
128
129
130
131
132
133