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.1 $
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 try {
82 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
83 String line = reader.readLine();
84 if (line == null) {
85 throw new IOException("Empty file found for: " + uri);
86 }
87 line = line.trim();
88 Class answer = loadClass(line);
89 if (answer == null) {
90 throw new ClassNotFoundException("Could not find class: " + line);
91 }
92 return answer;
93 }
94 finally {
95 try {
96 in.close();
97 }
98 catch (Exception e) {
99
100 }
101 }
102 }
103
104 protected Class loadClass(String name) throws ClassNotFoundException {
105 try {
106 return Thread.currentThread().getContextClassLoader().loadClass(name);
107 }
108 catch (ClassNotFoundException e) {
109 return getClass().getClassLoader().loadClass(name);
110 }
111 }
112 }