KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > common > resources > Resources


1 /*
2  * Copyright 2004 Clinton Begin
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 com.ibatis.common.resources;
17
18 import java.io.*;
19 import java.net.URL JavaDoc;
20 import java.net.URLConnection JavaDoc;
21 import java.util.Properties JavaDoc;
22
23 /**
24  * A class to simplify access to resources through the classloader.
25  */

26 public class Resources extends Object JavaDoc {
27
28   private static ClassLoader JavaDoc defaultClassLoader;
29
30   private Resources() {
31   }
32
33   /**
34    * Returns the default classloader (may be null).
35    *
36    * @return The default classloader
37    */

38   public static ClassLoader JavaDoc getDefaultClassLoader() {
39     return defaultClassLoader;
40   }
41
42   /**
43    * Sets the default classloader
44    *
45    * @param defaultClassLoader - the new default ClassLoader
46    */

47   public static void setDefaultClassLoader(ClassLoader JavaDoc defaultClassLoader) {
48     Resources.defaultClassLoader = defaultClassLoader;
49   }
50
51   /**
52    * Returns the URL of the resource on the classpath
53    *
54    * @param resource The resource to find
55    * @return The resource
56    * @throws IOException If the resource cannot be found or read
57    */

58   public static URL JavaDoc getResourceURL(String JavaDoc resource) throws IOException {
59     return getResourceURL(getClassLoader(), resource);
60   }
61
62   /**
63    * Returns the URL of the resource on the classpath
64    *
65    * @param loader The classloader used to load the resource
66    * @param resource The resource to find
67    * @return The resource
68    * @throws IOException If the resource cannot be found or read
69    */

70   public static URL JavaDoc getResourceURL(ClassLoader JavaDoc loader, String JavaDoc resource) throws IOException {
71     URL JavaDoc url = null;
72     if (loader != null) url = loader.getResource(resource);
73     if (url == null) url = ClassLoader.getSystemResource(resource);
74     if (url == null) throw new IOException("Could not find resource " + resource);
75     return url;
76   }
77
78   /**
79    * Returns a resource on the classpath as a Stream object
80    *
81    * @param resource The resource to find
82    * @return The resource
83    * @throws IOException If the resource cannot be found or read
84    */

85   public static InputStream getResourceAsStream(String JavaDoc resource) throws IOException {
86     return getResourceAsStream(getClassLoader(), resource);
87   }
88
89   /**
90    * Returns a resource on the classpath as a Stream object
91    *
92    * @param loader The classloader used to load the resource
93    * @param resource The resource to find
94    * @return The resource
95    * @throws IOException If the resource cannot be found or read
96    */

97   public static InputStream getResourceAsStream(ClassLoader JavaDoc loader, String JavaDoc resource) throws IOException {
98     InputStream in = null;
99     if (loader != null) in = loader.getResourceAsStream(resource);
100     if (in == null) in = ClassLoader.getSystemResourceAsStream(resource);
101     if (in == null) throw new IOException("Could not find resource " + resource);
102     return in;
103   }
104
105   /**
106    * Returns a resource on the classpath as a Properties object
107    *
108    * @param resource The resource to find
109    * @return The resource
110    * @throws IOException If the resource cannot be found or read
111    */

112   public static Properties JavaDoc getResourceAsProperties(String JavaDoc resource)
113       throws IOException {
114     Properties JavaDoc props = new Properties JavaDoc();
115     InputStream in = null;
116     String JavaDoc propfile = resource;
117     in = getResourceAsStream(propfile);
118     props.load(in);
119     in.close();
120     return props;
121   }
122
123   /**
124    * Returns a resource on the classpath as a Properties object
125    *
126    * @param loader The classloader used to load the resource
127    * @param resource The resource to find
128    * @return The resource
129    * @throws IOException If the resource cannot be found or read
130    */

131   public static Properties JavaDoc getResourceAsProperties(ClassLoader JavaDoc loader, String JavaDoc resource)
132       throws IOException {
133     Properties JavaDoc props = new Properties JavaDoc();
134     InputStream in = null;
135     String JavaDoc propfile = resource;
136     in = getResourceAsStream(loader, propfile);
137     props.load(in);
138     in.close();
139     return props;
140   }
141
142   /**
143    * Returns a resource on the classpath as a Reader object
144    *
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 Reader getResourceAsReader(String JavaDoc resource) throws IOException {
150     return new InputStreamReader(getResourceAsStream(resource));
151   }
152
153   /**
154    * Returns a resource on the classpath as a Reader object
155    *
156    * @param loader The classloader used to load the resource
157    * @param resource The resource to find
158    * @return The resource
159    * @throws IOException If the resource cannot be found or read
160    */

161   public static Reader getResourceAsReader(ClassLoader JavaDoc loader, String JavaDoc resource) throws IOException {
162     return new InputStreamReader(getResourceAsStream(loader, resource));
163   }
164
165   /**
166    * Returns a resource on the classpath as a File object
167    *
168    * @param resource The resource to find
169    * @return The resource
170    * @throws IOException If the resource cannot be found or read
171    */

172   public static File getResourceAsFile(String JavaDoc resource) throws IOException {
173     return new File(getResourceURL(resource).getFile());
174   }
175
176   /**
177    * Returns a resource on the classpath as a File object
178    *
179    * @param loader - the classloader used to load the resource
180    * @param resource - the resource to find
181    * @return The resource
182    * @throws IOException If the resource cannot be found or read
183    */

184   public static File getResourceAsFile(ClassLoader JavaDoc loader, String JavaDoc resource) throws IOException {
185     return new File(getResourceURL(loader, resource).getFile());
186   }
187
188   /**
189    * Gets a URL as an input stream
190    *
191    * @param urlString - the URL to get
192    * @return An input stream with the data from the URL
193    * @throws IOException If the resource cannot be found or read
194    */

195   public static InputStream getUrlAsStream(String JavaDoc urlString) throws IOException {
196     URL JavaDoc url = new URL JavaDoc(urlString);
197     URLConnection JavaDoc conn = url.openConnection();
198     return conn.getInputStream();
199   }
200
201   /**
202    * Gets a URL as a Reader
203    *
204    * @param urlString - the URL to get
205    * @return A Reader with the data from the URL
206    * @throws IOException If the resource cannot be found or read
207    */

208   public static Reader getUrlAsReader(String JavaDoc urlString) throws IOException {
209     return new InputStreamReader(getUrlAsStream(urlString));
210   }
211
212   /**
213    * Gets a URL as a Properties object
214    *
215    * @param urlString - the URL to get
216    * @return A Properties object with the data from the URL
217    * @throws IOException If the resource cannot be found or read
218    */

219   public static Properties JavaDoc getUrlAsProperties(String JavaDoc urlString) throws IOException {
220     Properties JavaDoc props = new Properties JavaDoc();
221     InputStream in = null;
222     String JavaDoc propfile = urlString;
223     in = getUrlAsStream(propfile);
224     props.load(in);
225     in.close();
226     return props;
227   }
228
229   /**
230    * Loads a class
231    *
232    * @param className - the class to load
233    * @return The loaded class
234    * @throws ClassNotFoundException If the class cannot be found (duh!)
235    */

236   public static Class JavaDoc classForName(String JavaDoc className) throws ClassNotFoundException JavaDoc {
237     Class JavaDoc clazz = null;
238     try {
239       clazz = getClassLoader().loadClass(className);
240     } catch (Exception JavaDoc e) {
241       // Ignore. Failsafe below.
242
}
243     if (clazz == null) {
244       clazz = Class.forName(className);
245     }
246     return clazz;
247   }
248
249   /**
250    * Creates an instance of a class
251    *
252    * @param className - the class to create
253    * @return An instance of the class
254    * @throws ClassNotFoundException If the class cannot be found (duh!)
255    * @throws InstantiationException If the class cannot be instantiaed
256    * @throws IllegalAccessException If the class is not public, or other access problems arise
257    */

258   public static Object JavaDoc instantiate(String JavaDoc className)
259       throws ClassNotFoundException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc {
260     return instantiate(classForName(className));
261   }
262
263   /**
264    * Creates an instance of a class
265    *
266    * @param clazz - the class to create
267    * @return An instance of the class
268    * @throws InstantiationException If the class cannot be instantiaed
269    * @throws IllegalAccessException If the class is not public, or other access problems arise
270    */

271   public static Object JavaDoc instantiate(Class JavaDoc clazz)
272       throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
273     return clazz.newInstance();
274   }
275
276   private static ClassLoader JavaDoc getClassLoader() {
277     if (defaultClassLoader != null) {
278       return defaultClassLoader;
279     } else {
280       return Thread.currentThread().getContextClassLoader();
281     }
282   }
283
284 }
285
Popular Tags