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