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