KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > util > ResourceFinder


1 package org.openejb.util;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.InputStream JavaDoc;
7 import java.net.JarURLConnection JavaDoc;
8 import java.net.MalformedURLException JavaDoc;
9 import java.net.URL JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Properties JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.jar.JarEntry JavaDoc;
18 import java.util.jar.JarFile JavaDoc;
19
20 public class ResourceFinder {
21
22     private final String JavaDoc path;
23     private final ClassLoader JavaDoc classLoader;
24
25     public ResourceFinder(String JavaDoc path) {
26         this(path, Thread.currentThread().getContextClassLoader());
27     }
28
29     public ResourceFinder(String JavaDoc path, ClassLoader JavaDoc classLoader) {
30         this.path = path;
31         this.classLoader = classLoader;
32     }
33
34
35     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
36
//
37
// Find String
38
//
39
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
40

41     public String JavaDoc findString(String JavaDoc key) throws IOException JavaDoc {
42         String JavaDoc uri = path + key;
43
44         URL JavaDoc resource = classLoader.getResource(uri);
45         if (resource == null) {
46             throw new IOException JavaDoc("Could not find command in : " + uri);
47         }
48
49         return readContents(resource);
50     }
51
52     public List JavaDoc findAllStrings(String JavaDoc key) throws IOException JavaDoc {
53         String JavaDoc uri = path + key;
54
55         List JavaDoc strings = new ArrayList JavaDoc();
56
57         Enumeration JavaDoc resources = classLoader.getResources(uri);
58         while (resources.hasMoreElements()) {
59             URL JavaDoc url = (URL JavaDoc) resources.nextElement();
60             String JavaDoc string = readContents(url);
61             strings.add(string);
62         }
63         return strings;
64     }
65
66     public List JavaDoc findAvailableStrings(String JavaDoc key) throws IOException JavaDoc {
67         String JavaDoc uri = path + key;
68
69         List JavaDoc strings = new ArrayList JavaDoc();
70
71         Enumeration JavaDoc resources = classLoader.getResources(uri);
72         while (resources.hasMoreElements()) {
73             try {
74                 URL JavaDoc url = (URL JavaDoc) resources.nextElement();
75                 String JavaDoc string = readContents(url);
76                 strings.add(string);
77             } catch (Exception JavaDoc notAvailable) {
78             }
79         }
80         return strings;
81     }
82
83     public Map JavaDoc mapAllStrings(String JavaDoc key) throws IOException JavaDoc {
84         Map JavaDoc strings = new HashMap JavaDoc();
85         Map JavaDoc resourcesMap = getResourcesMap(key);
86         for (Iterator JavaDoc iterator = resourcesMap.entrySet().iterator(); iterator.hasNext();) {
87             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
88             String JavaDoc name = (String JavaDoc) entry.getKey();
89             URL JavaDoc url = (URL JavaDoc) entry.getValue();
90             String JavaDoc value = readContents(url);
91             strings.put(name,value);
92         }
93         return strings;
94     }
95
96     public Map JavaDoc mapAvailableStrings(String JavaDoc key) throws IOException JavaDoc {
97         Map JavaDoc strings = new HashMap JavaDoc();
98         Map JavaDoc resourcesMap = getResourcesMap(key);
99         for (Iterator JavaDoc iterator = resourcesMap.entrySet().iterator(); iterator.hasNext();) {
100             try {
101                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
102                 String JavaDoc name = (String JavaDoc) entry.getKey();
103                 URL JavaDoc url = (URL JavaDoc) entry.getValue();
104                 String JavaDoc value = readContents(url);
105                 strings.put(name,value);
106             } catch (Exception JavaDoc notAvailable) {
107             }
108         }
109         return strings;
110     }
111
112
113     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
114
//
115
// Find Class
116
//
117
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
118

119     public Class JavaDoc findClass(String JavaDoc key) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
120         String JavaDoc className = findString(key);
121         Class JavaDoc clazz = classLoader.loadClass(className);
122         return clazz;
123     }
124
125     public List JavaDoc findAllClasses(String JavaDoc key) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
126         List JavaDoc classes = new ArrayList JavaDoc();
127         List JavaDoc strings = findAllStrings(key);
128         for (int i = 0; i < strings.size(); i++) {
129             String JavaDoc className = (String JavaDoc) strings.get(i);
130             Class JavaDoc clazz = classLoader.loadClass(className);
131             classes.add(clazz);
132         }
133         return classes;
134     }
135
136     public List JavaDoc findAvailableClasses(String JavaDoc key) throws IOException JavaDoc {
137         List JavaDoc classes = new ArrayList JavaDoc();
138         List JavaDoc strings = findAvailableStrings(key);
139         for (int i = 0; i < strings.size(); i++) {
140             String JavaDoc className = (String JavaDoc) strings.get(i);
141             try {
142                 Class JavaDoc clazz = classLoader.loadClass(className);
143                 classes.add(clazz);
144             } catch (Exception JavaDoc notAvailable) {
145             }
146         }
147         return classes;
148     }
149
150     public Map JavaDoc mapAllClasses(String JavaDoc key) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
151         Map JavaDoc classes = new HashMap JavaDoc();
152         Map JavaDoc map = mapAllStrings(key);
153         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
154             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
155             String JavaDoc string = (String JavaDoc) entry.getKey();
156             String JavaDoc className = (String JavaDoc) entry.getValue();
157             Class JavaDoc clazz = classLoader.loadClass(className);
158             classes.put(string, clazz);
159         }
160         return classes;
161     }
162
163     public Map JavaDoc mapAvailableClasses(String JavaDoc key) throws IOException JavaDoc {
164         Map JavaDoc classes = new HashMap JavaDoc();
165         Map JavaDoc map = mapAvailableStrings(key);
166         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
167             try {
168                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
169                 String JavaDoc string = (String JavaDoc) entry.getKey();
170                 String JavaDoc className = (String JavaDoc) entry.getValue();
171                 Class JavaDoc clazz = classLoader.loadClass(className);
172                 classes.put(string, clazz);
173             } catch (Exception JavaDoc notAvailable) {
174             }
175         }
176         return classes;
177     }
178
179     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
180
//
181
// Find Implementation
182
//
183
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
184

185     public Class JavaDoc findImplementation(Class JavaDoc interfase) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
186         String JavaDoc className = findString(interfase.getName());
187         Class JavaDoc impl = classLoader.loadClass(className);
188         if (!interfase.isAssignableFrom(impl)) {
189             throw new ClassCastException JavaDoc("Class not of type: " + interfase.getName());
190         }
191         return impl;
192     }
193
194     public List JavaDoc findAllImplementations(Class JavaDoc interfase) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
195         List JavaDoc implementations = new ArrayList JavaDoc();
196         List JavaDoc strings = findAllStrings(interfase.getName());
197         for (int i = 0; i < strings.size(); i++) {
198             String JavaDoc className = (String JavaDoc) strings.get(i);
199             Class JavaDoc impl = classLoader.loadClass(className);
200             if (!interfase.isAssignableFrom(impl)) {
201                 throw new ClassCastException JavaDoc("Class not of type: " + interfase.getName());
202             }
203             implementations.add(impl);
204         }
205         return implementations;
206     }
207
208     public List JavaDoc findAvailableImplementations(Class JavaDoc interfase) throws IOException JavaDoc {
209         List JavaDoc implementations = new ArrayList JavaDoc();
210         List JavaDoc strings = findAvailableStrings(interfase.getName());
211         for (int i = 0; i < strings.size(); i++) {
212             String JavaDoc className = (String JavaDoc) strings.get(i);
213             try {
214                 Class JavaDoc impl = classLoader.loadClass(className);
215                 if (interfase.isAssignableFrom(impl)) {
216                     implementations.add(impl);
217                 }
218             } catch (Exception JavaDoc notAvailable) {
219             }
220         }
221         return implementations;
222     }
223
224     public Map JavaDoc mapAllImplementations(Class JavaDoc interfase) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
225         Map JavaDoc implementations = new HashMap JavaDoc();
226         Map JavaDoc map = mapAllStrings(interfase.getName());
227         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
228             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
229             String JavaDoc string = (String JavaDoc) entry.getKey();
230             String JavaDoc className = (String JavaDoc) entry.getValue();
231             Class JavaDoc impl = classLoader.loadClass(className);
232             if (!interfase.isAssignableFrom(impl)) {
233                 throw new ClassCastException JavaDoc("Class not of type: " + interfase.getName());
234             }
235             implementations.put(string, impl);
236         }
237         return implementations;
238     }
239
240     public Map JavaDoc mapAvailableImplementations(Class JavaDoc interfase) throws IOException JavaDoc {
241         Map JavaDoc implementations = new HashMap JavaDoc();
242         Map JavaDoc map = mapAvailableStrings(interfase.getName());
243         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
244             try {
245                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
246                 String JavaDoc string = (String JavaDoc) entry.getKey();
247                 String JavaDoc className = (String JavaDoc) entry.getValue();
248                 Class JavaDoc impl = classLoader.loadClass(className);
249                 if (interfase.isAssignableFrom(impl)) {
250                     implementations.put(string, impl);
251                 }
252             } catch (Exception JavaDoc notAvailable) {
253             }
254         }
255         return implementations;
256     }
257
258
259     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
260
//
261
// Find Properties
262
//
263
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
264

265     public Properties JavaDoc findProperties(String JavaDoc key) throws IOException JavaDoc {
266         String JavaDoc uri = path + key;
267
268         URL JavaDoc resource = classLoader.getResource(uri);
269         if (resource == null) {
270             throw new IOException JavaDoc("Could not find command in : " + uri);
271         }
272
273         return loadProperties(resource);
274     }
275
276     public List JavaDoc findAllProperties(String JavaDoc key) throws IOException JavaDoc {
277         String JavaDoc uri = path + key;
278
279         List JavaDoc properties = new ArrayList JavaDoc();
280
281         Enumeration JavaDoc resources = classLoader.getResources(uri);
282         while (resources.hasMoreElements()) {
283             URL JavaDoc url = (URL JavaDoc) resources.nextElement();
284             Properties JavaDoc props = loadProperties(url);
285             properties.add(props);
286         }
287         return properties;
288     }
289
290     public List JavaDoc findAvailableProperties(String JavaDoc key) throws IOException JavaDoc {
291         String JavaDoc uri = path + key;
292
293         List JavaDoc properties = new ArrayList JavaDoc();
294
295         Enumeration JavaDoc resources = classLoader.getResources(uri);
296         while (resources.hasMoreElements()) {
297             try {
298                 URL JavaDoc url = (URL JavaDoc) resources.nextElement();
299                 Properties JavaDoc props = loadProperties(url);
300                 properties.add(props);
301             } catch (Exception JavaDoc notAvailable) {
302             }
303         }
304         return properties;
305     }
306
307     public Map JavaDoc mapAllProperties(String JavaDoc key) throws IOException JavaDoc {
308         Map JavaDoc propertiesMap = new HashMap JavaDoc();
309         Map JavaDoc map = getResourcesMap(key);
310         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
311             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
312             String JavaDoc string = (String JavaDoc) entry.getKey();
313             URL JavaDoc url = (URL JavaDoc) entry.getValue();
314             Properties JavaDoc properties = loadProperties(url);
315             propertiesMap.put(string, properties);
316         }
317         return propertiesMap;
318     }
319
320     public Map JavaDoc mapAvailableProperties(String JavaDoc key) throws IOException JavaDoc {
321         Map JavaDoc propertiesMap = new HashMap JavaDoc();
322         Map JavaDoc map = getResourcesMap(key);
323         for (Iterator JavaDoc iterator = map.entrySet().iterator(); iterator.hasNext();) {
324             try {
325                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
326                 String JavaDoc string = (String JavaDoc) entry.getKey();
327                 URL JavaDoc url = (URL JavaDoc) entry.getValue();
328                 Properties JavaDoc properties = loadProperties(url);
329                 propertiesMap.put(string, properties);
330             } catch (Exception JavaDoc notAvailable) {
331             }
332         }
333         return propertiesMap;
334     }
335
336     // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
337
//
338
// Map Resources
339
//
340
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
341

342     public Map JavaDoc getResourcesMap(String JavaDoc key) throws IOException JavaDoc {
343         String JavaDoc basePath = path + key;
344
345         if (!basePath.endsWith("/")){
346             basePath += "/";
347         }
348         
349         Map JavaDoc resources = new HashMap JavaDoc();
350         Enumeration JavaDoc urls = classLoader.getResources(basePath);
351
352         while (urls.hasMoreElements()) {
353             URL JavaDoc location = (URL JavaDoc) urls.nextElement();
354
355             try {
356                 if (location.getProtocol().equals("jar")) {
357
358                     readJarEntries(location, basePath, resources);
359
360                 } else if (location.getProtocol().equals("file")) {
361
362                     readDirectoryEntries(location, resources);
363
364                 }
365             } catch (Exception JavaDoc e) {
366             }
367         }
368
369         return resources;
370     }
371
372     private static void readDirectoryEntries(URL JavaDoc location, Map JavaDoc resources) throws MalformedURLException JavaDoc {
373         File JavaDoc dir = new File JavaDoc(location.getPath());
374         if (dir.isDirectory()) {
375             File JavaDoc[] files = dir.listFiles();
376             for (int i = 0; i < files.length; i++) {
377                 File JavaDoc file = files[i];
378                 if (!file.isDirectory()){
379                     String JavaDoc name = file.getName();
380                     URL JavaDoc url = file.toURL();
381                     resources.put(name, url);
382                 }
383             }
384         }
385     }
386
387     private static void readJarEntries(URL JavaDoc location, String JavaDoc basePath, Map JavaDoc resources) throws IOException JavaDoc {
388         JarURLConnection JavaDoc conn = (JarURLConnection JavaDoc) location.openConnection();
389         JarFile JavaDoc jarfile = conn.getJarFile();
390
391         Enumeration JavaDoc entries = jarfile.entries();
392         while (entries != null && entries.hasMoreElements()) {
393             JarEntry JavaDoc entry = (JarEntry JavaDoc) entries.nextElement();
394             String JavaDoc name = entry.getName();
395
396             if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) {
397                 continue;
398             }
399
400             name = name.substring(basePath.length());
401
402             if (name.indexOf("/") != -1) {
403                 continue;
404             }
405
406             URL JavaDoc resource = new URL JavaDoc(location, name);
407             resources.put(name, resource);
408         }
409     }
410
411     private Properties JavaDoc loadProperties(URL JavaDoc resource) throws IOException JavaDoc {
412         InputStream JavaDoc in = resource.openStream();
413
414         BufferedInputStream JavaDoc reader = null;
415         try {
416             reader = new BufferedInputStream JavaDoc(in);
417             Properties JavaDoc properties = new Properties JavaDoc();
418             properties.load(reader);
419
420             return properties;
421         } finally {
422             try {
423                 in.close();
424                 reader.close();
425             } catch (Exception JavaDoc e) {
426             }
427         }
428     }
429
430     private String JavaDoc readContents(URL JavaDoc resource) throws IOException JavaDoc {
431         InputStream JavaDoc in = resource.openStream();
432         BufferedInputStream JavaDoc reader = null;
433         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
434
435         try {
436             reader = new BufferedInputStream JavaDoc(in);
437
438             int b = reader.read();
439             while (b != -1) {
440                 sb.append((char) b);
441                 b = reader.read();
442             }
443
444             return sb.toString().trim();
445         } finally {
446             try {
447                 in.close();
448                 reader.close();
449             } catch (Exception JavaDoc e) {
450             }
451         }
452     }
453 }
Popular Tags