View Javadoc
1   /*
2    *    Copyright 2009-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.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.URISyntaxException;
24  import java.net.URL;
25  import java.net.URLConnection;
26  import java.nio.charset.Charset;
27  import java.nio.file.Path;
28  import java.util.Properties;
29  
30  /**
31   * A class to simplify access to resources through the classloader.
32   *
33   * @author Clinton Begin
34   */
35  public class Resources {
36  
37    private static final ClassLoaderWrapper classLoaderWrapper = new ClassLoaderWrapper();
38  
39    /**
40     * Charset to use when calling getResourceAsReader. null means use the system default.
41     */
42    private static Charset charset;
43  
44    private Resources() {
45    }
46  
47    /**
48     * Returns the default classloader (may be null).
49     *
50     * @return The default classloader
51     */
52    public static ClassLoader getDefaultClassLoader() {
53      return classLoaderWrapper.defaultClassLoader;
54    }
55  
56    /**
57     * Sets the default classloader
58     *
59     * @param defaultClassLoader
60     *          - the new default ClassLoader
61     */
62    public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
63      classLoaderWrapper.defaultClassLoader = defaultClassLoader;
64    }
65  
66    /**
67     * Returns the URL of the resource on the classpath
68     *
69     * @param resource
70     *          The resource to find
71     *
72     * @return The resource
73     *
74     * @throws java.io.IOException
75     *           If the resource cannot be found or read
76     */
77    public static URL getResourceURL(String resource) throws IOException {
78      // issue #625
79      return getResourceURL(null, resource);
80    }
81  
82    /**
83     * Returns the URL of the resource on the classpath
84     *
85     * @param loader
86     *          The classloader used to fetch the resource
87     * @param resource
88     *          The resource to find
89     *
90     * @return The resource
91     *
92     * @throws java.io.IOException
93     *           If the resource cannot be found or read
94     */
95    public static URL getResourceURL(ClassLoader loader, String resource) throws IOException {
96      URL url = classLoaderWrapper.getResourceAsURL(resource, loader);
97      if (url == null) {
98        throw new IOException("Could not find resource " + resource);
99      }
100     return url;
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 IOException
232    *           Signals that an I/O exception has occurred.
233    * @throws URISyntaxException
234    *           the URI syntax exception
235    */
236   public static File getResourceAsFile(String resource) throws IOException, URISyntaxException {
237     return Path.of(getResourceURL(resource).toURI()).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 IOException
251    *           Signals that an I/O exception has occurred.
252    * @throws URISyntaxException
253    *           the URI syntax exception
254    */
255   public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException, URISyntaxException {
256     return Path.of(getResourceURL(loader, resource).toURI()).toFile();
257   }
258 
259   /**
260    * Gets a URL as an input stream
261    *
262    * @param urlString
263    *          - the URL to get
264    *
265    * @return An input stream with the data from the URL
266    *
267    * @throws java.io.IOException
268    *           If the resource cannot be found or read
269    */
270   public static InputStream getUrlAsStream(String urlString) throws IOException {
271     URL url = new URL(urlString);
272     URLConnection conn = url.openConnection();
273     return conn.getInputStream();
274   }
275 
276   /**
277    * Gets a URL as a Reader
278    *
279    * @param urlString
280    *          - the URL to get
281    *
282    * @return A Reader with the data from the URL
283    *
284    * @throws java.io.IOException
285    *           If the resource cannot be found or read
286    */
287   public static Reader getUrlAsReader(String urlString) throws IOException {
288     Reader reader;
289     if (charset == null) {
290       reader = new InputStreamReader(getUrlAsStream(urlString));
291     } else {
292       reader = new InputStreamReader(getUrlAsStream(urlString), charset);
293     }
294     return reader;
295   }
296 
297   /**
298    * Gets a URL as a Properties object
299    *
300    * @param urlString
301    *          - the URL to get
302    *
303    * @return A Properties object with the data from the URL
304    *
305    * @throws java.io.IOException
306    *           If the resource cannot be found or read
307    */
308   public static Properties getUrlAsProperties(String urlString) throws IOException {
309     Properties props = new Properties();
310     try (InputStream in = getUrlAsStream(urlString)) {
311       props.load(in);
312     }
313     return props;
314   }
315 
316   /**
317    * Loads a class
318    *
319    * @param className
320    *          - the class to fetch
321    *
322    * @return The loaded class
323    *
324    * @throws ClassNotFoundException
325    *           If the class cannot be found (duh!)
326    */
327   public static Class<?> classForName(String className) throws ClassNotFoundException {
328     return classLoaderWrapper.classForName(className);
329   }
330 
331   public static Charset getCharset() {
332     return charset;
333   }
334 
335   public static void setCharset(Charset charset) {
336     Resources.charset = charset;
337   }
338 
339 }