1 /*
2 * Copyright 2009-2023 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.apache.ibatis.io;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.Reader;
23 import java.net.URL;
24 import java.net.URLConnection;
25 import java.nio.charset.Charset;
26 import java.util.Properties;
27
28 /**
29 * A class to simplify access to resources through the classloader.
30 *
31 * @author Clinton Begin
32 */
33 public class Resources {
34
35 private static final ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
36
37 /**
38 * Charset to use when calling getResourceAsReader. null means use the system default.
39 */
40 private static Charset charset;
41
42 private Resources() {
43 }
44
45 /**
46 * Returns the default classloader (may be null).
47 *
48 * @return The default classloader
49 */
50 public static ClassLoader getDefaultClassLoader() {
51 return classLoaderWrapper.defaultClassLoader;
52 }
53
54 /**
55 * Sets the default classloader
56 *
57 * @param defaultClassLoader
58 * - the new default ClassLoader
59 */
60 public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
61 classLoaderWrapper.defaultClassLoader = defaultClassLoader;
62 }
63
64 /**
65 * Returns the URL of the resource on the classpath
66 *
67 * @param resource
68 * The resource to find
69 *
70 * @return The resource
71 *
72 * @throws java.io.IOException
73 * If the resource cannot be found or read
74 */
75 public static URL getResourceURL(String resource) throws IOException {
76 // issue #625
77 return getResourceURL(null, resource);
78 }
79
80 /**
81 * Returns the URL of the resource on the classpath
82 *
83 * @param loader
84 * The classloader used to fetch the resource
85 * @param resource
86 * The resource to find
87 *
88 * @return The resource
89 *
90 * @throws java.io.IOException
91 * If the resource cannot be found or read
92 */
93 public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
94 URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
95 if (url == null) {
96 throw new IOException("Could not find resource " + resource);
97 }
98 return url;
99 }
100
101 /**
102 * Returns a resource on the classpath as a Stream object
103 *
104 * @param resource
105 * The resource to find
106 *
107 * @return The resource
108 *
109 * @throws java.io.IOException
110 * If the resource cannot be found or read
111 */
112 public static InputStream getResourceAsStream(String resource) throws IOException {
113 return getResourceAsStream(null, resource);
114 }
115
116 /**
117 * Returns a resource on the classpath as a Stream object
118 *
119 * @param loader
120 * The classloader used to fetch the resource
121 * @param resource
122 * The resource to find
123 *
124 * @return The resource
125 *
126 * @throws java.io.IOException
127 * If the resource cannot be found or read
128 */
129 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
130 InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
131 if (in == null) {
132 throw new IOException("Could not find resource " + resource);
133 }
134 return in;
135 }
136
137 /**
138 * Returns a resource on the classpath as a Properties object
139 *
140 * @param resource
141 * The resource to find
142 *
143 * @return The resource
144 *
145 * @throws java.io.IOException
146 * If the resource cannot be found or read
147 */
148 public static Properties getResourceAsProperties(String resource) throws IOException {
149 Properties props = new Properties();
150 try (InputStream in = getResourceAsStream(resource)) {
151 props.load(in);
152 }
153 return props;
154 }
155
156 /**
157 * Returns a resource on the classpath as a Properties object
158 *
159 * @param loader
160 * The classloader used to fetch the resource
161 * @param resource
162 * The resource to find
163 *
164 * @return The resource
165 *
166 * @throws java.io.IOException
167 * If the resource cannot be found or read
168 */
169 public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
170 Properties props = new Properties();
171 try (InputStream in = getResourceAsStream(loader, resource)) {
172 props.load(in);
173 }
174 return props;
175 }
176
177 /**
178 * Returns a resource on the classpath as a Reader object
179 *
180 * @param resource
181 * The resource to find
182 *
183 * @return The resource
184 *
185 * @throws java.io.IOException
186 * If the resource cannot be found or read
187 */
188 public static Reader getResourceAsReader(String resource) throws IOException {
189 Reader reader;
190 if (charset == null) {
191 reader = new InputStreamReader(getResourceAsStream(resource));
192 } else {
193 reader = new InputStreamReader(getResourceAsStream(resource), charset);
194 }
195 return reader;
196 }
197
198 /**
199 * Returns a resource on the classpath as a Reader object
200 *
201 * @param loader
202 * The classloader used to fetch the resource
203 * @param resource
204 * The resource to find
205 *
206 * @return The resource
207 *
208 * @throws java.io.IOException
209 * If the resource cannot be found or read
210 */
211 public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
212 Reader reader;
213 if (charset == null) {
214 reader = new InputStreamReader(getResourceAsStream(loader, resource));
215 } else {
216 reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
217 }
218 return reader;
219 }
220
221 /**
222 * Returns a resource on the classpath as a File object
223 *
224 * @param resource
225 * The resource to find
226 *
227 * @return The resource
228 *
229 * @throws java.io.IOException
230 * If the resource cannot be found or read
231 */
232 public static File getResourceAsFile(String resource) throws IOException {
233 return new File(getResourceURL(resource).getFile());
234 }
235
236 /**
237 * Returns a resource on the classpath as a File object
238 *
239 * @param loader
240 * - the classloader used to fetch the resource
241 * @param resource
242 * - the resource to find
243 *
244 * @return The resource
245 *
246 * @throws java.io.IOException
247 * If the resource cannot be found or read
248 */
249 public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
250 return new File(getResourceURL(loader, resource).getFile());
251 }
252
253 /**
254 * Gets a URL as an input stream
255 *
256 * @param urlString
257 * - the URL to get
258 *
259 * @return An input stream with the data from the URL
260 *
261 * @throws java.io.IOException
262 * If the resource cannot be found or read
263 */
264 public static InputStream getUrlAsStream(String urlString) throws IOException {
265 URL url = new URL(urlString);
266 URLConnection conn = url.openConnection();
267 return conn.getInputStream();
268 }
269
270 /**
271 * Gets a URL as a Reader
272 *
273 * @param urlString
274 * - the URL to get
275 *
276 * @return A Reader with the data from the URL
277 *
278 * @throws java.io.IOException
279 * If the resource cannot be found or read
280 */
281 public static Reader getUrlAsReader(String urlString) throws IOException {
282 Reader reader;
283 if (charset == null) {
284 reader = new InputStreamReader(getUrlAsStream(urlString));
285 } else {
286 reader = new InputStreamReader(getUrlAsStream(urlString), charset);
287 }
288 return reader;
289 }
290
291 /**
292 * Gets a URL as a Properties object
293 *
294 * @param urlString
295 * - the URL to get
296 *
297 * @return A Properties object with the data from the URL
298 *
299 * @throws java.io.IOException
300 * If the resource cannot be found or read
301 */
302 public static Properties getUrlAsProperties(String urlString) throws IOException {
303 Properties props = new Properties();
304 try (InputStream in = getUrlAsStream(urlString)) {
305 props.load(in);
306 }
307 return props;
308 }
309
310 /**
311 * Loads a class
312 *
313 * @param className
314 * - the class to fetch
315 *
316 * @return The loaded class
317 *
318 * @throws ClassNotFoundException
319 * If the class cannot be found (duh!)
320 */
321 public static Class<?> classForName(String className) throws ClassNotFoundException {
322 return classLoaderWrapper.classForName(className);
323 }
324
325 public static Charset getCharset() {
326 return charset;
327 }
328
329 public static void setCharset(Charset charset) {
330 Resources.charset = charset;
331 }
332
333 }