001: /*
002: * Copyright 2004 Clinton Begin
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.ibatis.common.resources;
017:
018: import java.io.*;
019: import java.net.URL;
020: import java.net.URLConnection;
021: import java.nio.charset.Charset;
022: import java.util.Properties;
023:
024: /**
025: * A class to simplify access to resources through the classloader.
026: */
027: public class Resources extends Object {
028:
029: private static ClassLoader defaultClassLoader;
030:
031: /**
032: * Charset to use when calling getResourceAsReader.
033: * null means use the system default.
034: */
035: private static Charset charset;
036:
037: private Resources() {
038: }
039:
040: /**
041: * Returns the default classloader (may be null).
042: *
043: * @return The default classloader
044: */
045: public static ClassLoader getDefaultClassLoader() {
046: return defaultClassLoader;
047: }
048:
049: /**
050: * Sets the default classloader
051: *
052: * @param defaultClassLoader - the new default ClassLoader
053: */
054: public static void setDefaultClassLoader(
055: ClassLoader defaultClassLoader) {
056: Resources.defaultClassLoader = defaultClassLoader;
057: }
058:
059: /**
060: * Returns the URL of the resource on the classpath
061: *
062: * @param resource The resource to find
063: * @return The resource
064: * @throws IOException If the resource cannot be found or read
065: */
066: public static URL getResourceURL(String resource)
067: throws IOException {
068: return getResourceURL(getClassLoader(), resource);
069: }
070:
071: /**
072: * Returns the URL of the resource on the classpath
073: *
074: * @param loader The classloader used to load the resource
075: * @param resource The resource to find
076: * @return The resource
077: * @throws IOException If the resource cannot be found or read
078: */
079: public static URL getResourceURL(ClassLoader loader, String resource)
080: throws IOException {
081: URL url = null;
082: if (loader != null)
083: url = loader.getResource(resource);
084: if (url == null)
085: url = ClassLoader.getSystemResource(resource);
086: if (url == null)
087: throw new IOException("Could not find resource " + resource);
088: return url;
089: }
090:
091: /**
092: * Returns a resource on the classpath as a Stream object
093: *
094: * @param resource The resource to find
095: * @return The resource
096: * @throws IOException If the resource cannot be found or read
097: */
098: public static InputStream getResourceAsStream(String resource)
099: throws IOException {
100: return getResourceAsStream(getClassLoader(), resource);
101: }
102:
103: /**
104: * Returns a resource on the classpath as a Stream object
105: *
106: * @param loader The classloader used to load the resource
107: * @param resource The resource to find
108: * @return The resource
109: * @throws IOException If the resource cannot be found or read
110: */
111: public static InputStream getResourceAsStream(ClassLoader loader,
112: String resource) throws IOException {
113: InputStream in = null;
114: if (loader != null)
115: in = loader.getResourceAsStream(resource);
116: if (in == null)
117: in = ClassLoader.getSystemResourceAsStream(resource);
118: if (in == null)
119: throw new IOException("Could not find resource " + resource);
120: return in;
121: }
122:
123: /**
124: * Returns a resource on the classpath as a Properties object
125: *
126: * @param resource The resource to find
127: * @return The resource
128: * @throws IOException If the resource cannot be found or read
129: */
130: public static Properties getResourceAsProperties(String resource)
131: throws IOException {
132: Properties props = new Properties();
133: InputStream in = null;
134: String propfile = resource;
135: in = getResourceAsStream(propfile);
136: props.load(in);
137: in.close();
138: return props;
139: }
140:
141: /**
142: * Returns a resource on the classpath as a Properties object
143: *
144: * @param loader The classloader used to load the resource
145: * @param resource The resource to find
146: * @return The resource
147: * @throws IOException If the resource cannot be found or read
148: */
149: public static Properties getResourceAsProperties(
150: ClassLoader loader, String resource) throws IOException {
151: Properties props = new Properties();
152: InputStream in = null;
153: String propfile = resource;
154: in = getResourceAsStream(loader, propfile);
155: props.load(in);
156: in.close();
157: return props;
158: }
159:
160: /**
161: * Returns a resource on the classpath as a Reader object
162: *
163: * @param resource The resource to find
164: * @return The resource
165: * @throws IOException If the resource cannot be found or read
166: */
167: public static Reader getResourceAsReader(String resource)
168: throws IOException {
169: Reader reader;
170: if (charset == null) {
171: reader = new InputStreamReader(
172: getResourceAsStream(resource));
173: } else {
174: reader = new InputStreamReader(
175: getResourceAsStream(resource), charset);
176: }
177:
178: return reader;
179: }
180:
181: /**
182: * Returns a resource on the classpath as a Reader object
183: *
184: * @param loader The classloader used to load the resource
185: * @param resource The resource to find
186: * @return The resource
187: * @throws IOException If the resource cannot be found or read
188: */
189: public static Reader getResourceAsReader(ClassLoader loader,
190: String resource) throws IOException {
191: Reader reader;
192: if (charset == null) {
193: reader = new InputStreamReader(getResourceAsStream(loader,
194: resource));
195: } else {
196: reader = new InputStreamReader(getResourceAsStream(loader,
197: resource), charset);
198: }
199:
200: return reader;
201: }
202:
203: /**
204: * Returns a resource on the classpath as a File object
205: *
206: * @param resource The resource to find
207: * @return The resource
208: * @throws IOException If the resource cannot be found or read
209: */
210: public static File getResourceAsFile(String resource)
211: throws IOException {
212: return new File(getResourceURL(resource).getFile());
213: }
214:
215: /**
216: * Returns a resource on the classpath as a File object
217: *
218: * @param loader - the classloader used to load the resource
219: * @param resource - the resource to find
220: * @return The resource
221: * @throws IOException If the resource cannot be found or read
222: */
223: public static File getResourceAsFile(ClassLoader loader,
224: String resource) throws IOException {
225: return new File(getResourceURL(loader, resource).getFile());
226: }
227:
228: /**
229: * Gets a URL as an input stream
230: *
231: * @param urlString - the URL to get
232: * @return An input stream with the data from the URL
233: * @throws IOException If the resource cannot be found or read
234: */
235: public static InputStream getUrlAsStream(String urlString)
236: throws IOException {
237: URL url = new URL(urlString);
238: URLConnection conn = url.openConnection();
239: return conn.getInputStream();
240: }
241:
242: /**
243: * Gets a URL as a Reader
244: *
245: * @param urlString - the URL to get
246: * @return A Reader with the data from the URL
247: * @throws IOException If the resource cannot be found or read
248: */
249: public static Reader getUrlAsReader(String urlString)
250: throws IOException {
251: return new InputStreamReader(getUrlAsStream(urlString));
252: }
253:
254: /**
255: * Gets a URL as a Properties object
256: *
257: * @param urlString - the URL to get
258: * @return A Properties object with the data from the URL
259: * @throws IOException If the resource cannot be found or read
260: */
261: public static Properties getUrlAsProperties(String urlString)
262: throws IOException {
263: Properties props = new Properties();
264: InputStream in = null;
265: String propfile = urlString;
266: in = getUrlAsStream(propfile);
267: props.load(in);
268: in.close();
269: return props;
270: }
271:
272: /**
273: * Loads a class
274: *
275: * @param className - the class to load
276: * @return The loaded class
277: * @throws ClassNotFoundException If the class cannot be found (duh!)
278: */
279: public static Class classForName(String className)
280: throws ClassNotFoundException {
281: Class clazz = null;
282: try {
283: clazz = getClassLoader().loadClass(className);
284: } catch (Exception e) {
285: // Ignore. Failsafe below.
286: }
287: if (clazz == null) {
288: clazz = Class.forName(className);
289: }
290: return clazz;
291: }
292:
293: /**
294: * Creates an instance of a class
295: *
296: * @param className - the class to create
297: * @return An instance of the class
298: * @throws ClassNotFoundException If the class cannot be found (duh!)
299: * @throws InstantiationException If the class cannot be instantiaed
300: * @throws IllegalAccessException If the class is not public, or other access problems arise
301: */
302: public static Object instantiate(String className)
303: throws ClassNotFoundException, InstantiationException,
304: IllegalAccessException {
305: return instantiate(classForName(className));
306: }
307:
308: /**
309: * Creates an instance of a class
310: *
311: * @param clazz - the class to create
312: * @return An instance of the class
313: * @throws InstantiationException If the class cannot be instantiaed
314: * @throws IllegalAccessException If the class is not public, or other access problems arise
315: */
316: public static Object instantiate(Class clazz)
317: throws InstantiationException, IllegalAccessException {
318: return clazz.newInstance();
319: }
320:
321: private static ClassLoader getClassLoader() {
322: if (defaultClassLoader != null) {
323: return defaultClassLoader;
324: } else {
325: return Thread.currentThread().getContextClassLoader();
326: }
327: }
328:
329: public static Charset getCharset() {
330: return charset;
331: }
332:
333: /**
334: * Use this method to set the Charset to be used when
335: * calling the getResourceAsReader methods. This will
336: * allow iBATIS to function properly when the system default
337: * encoding doesn't deal well with unicode (IBATIS-340, IBATIS-349)
338: *
339: * @param charset
340: */
341: public static void setCharset(Charset charset) {
342: Resources.charset = charset;
343: }
344:
345: }
|