1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.util;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 /***
28 * A helper class to discover protocols dynamically to ensure
29 * that the system is extensible and has minimum runtime dependencies
30 *
31 * @version $Revision: 1.2 $
32 */
33 public class FactoryFinder {
34 private String path;
35 private Map classes = new HashMap();
36
37 public FactoryFinder(String path) {
38 this.path = path;
39 }
40
41 /***
42 * Creates a new instance of the given key
43 *
44 * @param key is the key to add to the path to find a text file containing the factory name
45 * @return a newly created instance
46 */
47 public Object newInstance(String key) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
48 Class type = findClass(key);
49 return type.newInstance();
50 }
51
52 /***
53 * Loads the class for the given key
54 *
55 * @param key is the key to add to the path to find a text file containing the factory name
56 * @return the class for the given key
57 */
58 public Class findClass(String key) throws IOException, ClassNotFoundException {
59 Class answer = (Class) classes.get(key);
60 if (answer == null) {
61 answer = doFindClass(key);
62 classes.put(key, answer);
63 }
64 return answer;
65
66 }
67
68 private Class doFindClass(String key) throws IOException, ClassNotFoundException {
69 String uri = path + key;
70
71
72 InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(uri);
73 if (in == null) {
74 in = getClass().getClassLoader().getResourceAsStream(uri);
75 if (in == null) {
76 throw new IOException("Could not find class for resource: " + uri);
77 }
78 }
79
80
81 BufferedReader reader = null;
82 try {
83 reader = new BufferedReader(new InputStreamReader(in));
84 String line = reader.readLine();
85 if (line == null) {
86 throw new IOException("Empty file found for: " + uri);
87 }
88 line = line.trim();
89 Class answer = loadClass(line);
90 if (answer == null) {
91 throw new ClassNotFoundException("Could not find class: " + line);
92 }
93 return answer;
94 }
95 finally {
96 try {
97 reader.close();
98 }
99 catch (Exception e) {
100
101 }
102 }
103 }
104
105 protected Class loadClass(String name) throws ClassNotFoundException {
106 try {
107 return Thread.currentThread().getContextClassLoader().loadClass(name);
108 }
109 catch (ClassNotFoundException e) {
110 return getClass().getClassLoader().loadClass(name);
111 }
112 }
113 }