1 /*
2 * Copyright 2010-2025 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.migration.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.URI;
24 import java.net.URISyntaxException;
25 import java.net.URL;
26 import java.net.URLConnection;
27 import java.nio.charset.Charset;
28 import java.nio.file.Path;
29 import java.util.Properties;
30
31 /**
32 * A class to simplify access to resources through the classloader.
33 *
34 * @author Clinton Begin
35 */
36 public class Resources {
37
38 private static ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
39
40 /**
41 * Charset to use when calling getResourceAsReader. null means use the system default.
42 */
43 private static Charset charset;
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 URI 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 URI getResourceURI(String resource) throws IOException {
76 // issue #625
77 return getResourceURI(null, resource);
78 }
79
80 /**
81 * Returns the URI 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 URI getResourceURI(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 try {
99 return url.toURI();
100 } catch (URISyntaxException e) {
101 throw new IOException("Could not find resource " + resource);
102 }
103 }
104
105 /**
106 * Returns a resource on the classpath as a Stream object
107 *
108 * @param resource
109 * The resource to find
110 *
111 * @return The resource
112 *
113 * @throws java.io.IOException
114 * If the resource cannot be found or read
115 */
116 public static InputStream getResourceAsStream(String resource) throws IOException {
117 return getResourceAsStream(null, resource);
118 }
119
120 /**
121 * Returns a resource on the classpath as a Stream object
122 *
123 * @param loader
124 * The classloader used to fetch the resource
125 * @param resource
126 * The resource to find
127 *
128 * @return The resource
129 *
130 * @throws java.io.IOException
131 * If the resource cannot be found or read
132 */
133 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException {
134 InputStream in = classLoaderWrapper.getResourceAsStream(resource, loader);
135 if (in == null) {
136 throw new IOException("Could not find resource " + resource);
137 }
138 return in;
139 }
140
141 /**
142 * Returns a resource on the classpath as a Properties object
143 *
144 * @param resource
145 * The resource to find
146 *
147 * @return The resource
148 *
149 * @throws java.io.IOException
150 * If the resource cannot be found or read
151 */
152 public static Properties getResourceAsProperties(String resource) throws IOException {
153 Properties props = new Properties();
154 try (InputStream in = getResourceAsStream(resource)) {
155 props.load(in);
156 }
157 return props;
158 }
159
160 /**
161 * Returns a resource on the classpath as a Properties object
162 *
163 * @param loader
164 * The classloader used to fetch the resource
165 * @param resource
166 * The resource to find
167 *
168 * @return The resource
169 *
170 * @throws java.io.IOException
171 * If the resource cannot be found or read
172 */
173 public static Properties getResourceAsProperties(ClassLoader loader, String resource) throws IOException {
174 Properties props = new Properties();
175 try (InputStream in = getResourceAsStream(loader, resource)) {
176 props.load(in);
177 }
178 return props;
179 }
180
181 /**
182 * Returns a resource on the classpath as a Reader object
183 *
184 * @param resource
185 * The resource to find
186 *
187 * @return The resource
188 *
189 * @throws java.io.IOException
190 * If the resource cannot be found or read
191 */
192 public static Reader getResourceAsReader(String resource) throws IOException {
193 Reader reader;
194 if (charset == null) {
195 reader = new InputStreamReader(getResourceAsStream(resource));
196 } else {
197 reader = new InputStreamReader(getResourceAsStream(resource), charset);
198 }
199 return reader;
200 }
201
202 /**
203 * Returns a resource on the classpath as a Reader object
204 *
205 * @param loader
206 * The classloader used to fetch the resource
207 * @param resource
208 * The resource to find
209 *
210 * @return The resource
211 *
212 * @throws java.io.IOException
213 * If the resource cannot be found or read
214 */
215 public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException {
216 Reader reader;
217 if (charset == null) {
218 reader = new InputStreamReader(getResourceAsStream(loader, resource));
219 } else {
220 reader = new InputStreamReader(getResourceAsStream(loader, resource), charset);
221 }
222 return reader;
223 }
224
225 /**
226 * Returns a resource on the classpath as a File object
227 *
228 * @param resource
229 * The resource to find
230 *
231 * @return The resource
232 *
233 * @throws java.io.IOException
234 * If the resource cannot be found or read
235 */
236 public static File getResourceAsFile(String resource) throws IOException {
237 return Path.of(getResourceURI(resource)).toFile();
238 }
239
240 /**
241 * Returns a resource on the classpath as a File object
242 *
243 * @param loader
244 * - the classloader used to fetch the resource
245 * @param resource
246 * - the resource to find
247 *
248 * @return The resource
249 *
250 * @throws java.io.IOException
251 * If the resource cannot be found or read
252 */
253 public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException {
254 return Path.of(getResourceURI(loader, resource)).toFile();
255 }
256
257 /**
258 * Gets a URL as an input stream
259 *
260 * @param urlString
261 * - the URL to get
262 *
263 * @return An input stream with the data from the URL
264 *
265 * @throws java.io.IOException
266 * If the resource cannot be found or read
267 */
268 public static InputStream getUrlAsStream(String urlString) throws IOException {
269 URL url = new URL(urlString);
270 URLConnection conn = url.openConnection();
271 return conn.getInputStream();
272 }
273
274 /**
275 * Gets a URL as a Reader
276 *
277 * @param urlString
278 * - the URL to get
279 *
280 * @return A Reader with the data from the URL
281 *
282 * @throws java.io.IOException
283 * If the resource cannot be found or read
284 */
285 public static Reader getUrlAsReader(String urlString) throws IOException {
286 Reader reader;
287 if (charset == null) {
288 reader = new InputStreamReader(getUrlAsStream(urlString));
289 } else {
290 reader = new InputStreamReader(getUrlAsStream(urlString), charset);
291 }
292 return reader;
293 }
294
295 /**
296 * Gets a URL as a Properties object
297 *
298 * @param urlString
299 * - the URL to get
300 *
301 * @return A Properties object with the data from the URL
302 *
303 * @throws java.io.IOException
304 * If the resource cannot be found or read
305 */
306 public static Properties getUrlAsProperties(String urlString) throws IOException {
307 Properties props = new Properties();
308 try (InputStream in = getUrlAsStream(urlString)) {
309 props.load(in);
310 }
311 return props;
312 }
313
314 /**
315 * Loads a class
316 *
317 * @param className
318 * - the class to fetch
319 *
320 * @return The loaded class
321 *
322 * @throws ClassNotFoundException
323 * If the class cannot be found (duh!)
324 */
325 public static Class<?> classForName(String className) throws ClassNotFoundException {
326 return classLoaderWrapper.classForName(className);
327 }
328
329 public static Charset getCharset() {
330 return charset;
331 }
332
333 public static void setCharset(Charset charset) {
334 Resources.charset = charset;
335 }
336
337 }