KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > cli > CommandFinder


1 package org.openejb.cli;
2
3 import java.io.BufferedInputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.util.Collections JavaDoc;
7 import java.util.Enumeration JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 public class CommandFinder {
13     private String JavaDoc path;
14     private Map JavaDoc classMap = Collections.synchronizedMap(new HashMap JavaDoc());
15     
16     public CommandFinder(String JavaDoc path) {
17         this.path = path;
18     }
19     
20     public Properties JavaDoc doFindCommandProperies(String JavaDoc key) throws IOException JavaDoc {
21         String JavaDoc uri = path + key;
22         
23         // lets try the thread context class loader first
24
InputStream JavaDoc in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri);
25         if (in == null) {
26             in = CommandFinder.class.getClassLoader().getResourceAsStream(uri);
27             if (in == null) {
28                 throw new IOException JavaDoc("Could not find command in : " + uri);
29             }
30         }
31
32         // lets load the file
33
BufferedInputStream JavaDoc reader = null;
34         try {
35             reader = new BufferedInputStream JavaDoc(in);
36             Properties JavaDoc properties = new Properties JavaDoc();
37             properties.load(reader);
38             
39             return properties;
40         } finally {
41             try {
42                 reader.close();
43             } catch (Exception JavaDoc e) {
44             }
45         }
46     }
47     
48     public Enumeration JavaDoc doFindCommands() throws IOException JavaDoc {
49         return Thread.currentThread().getContextClassLoader().getResources(path);
50     }
51 }
Popular Tags