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