1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.util; |
18 |
|
|
19 |
|
import java.io.BufferedInputStream; |
20 |
|
import java.io.IOException; |
21 |
|
import java.io.InputStream; |
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Properties; |
26 |
|
import java.util.concurrent.ConcurrentHashMap; |
27 |
|
|
28 |
|
import org.apache.camel.spi.Injector; |
29 |
|
|
30 |
|
public class FactoryFinder { |
31 |
|
private final String path; |
32 |
474 |
private final ConcurrentHashMap classMap = new ConcurrentHashMap(); |
33 |
|
|
34 |
|
public FactoryFinder() { |
35 |
465 |
this("META-INF/services/org/apache/camel/"); |
36 |
465 |
} |
37 |
|
|
38 |
474 |
public FactoryFinder(String path) { |
39 |
474 |
this.path = path; |
40 |
474 |
} |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
public Object newInstance(String key) throws IllegalAccessException, InstantiationException, IOException, |
50 |
|
ClassNotFoundException { |
51 |
324 |
return newInstance(key, (String)null); |
52 |
|
} |
53 |
|
|
54 |
|
public Object newInstance(String key, String propertyPrefix) throws IllegalAccessException, |
55 |
|
InstantiationException, IOException, ClassNotFoundException { |
56 |
324 |
Class clazz = findClass(key, propertyPrefix); |
57 |
0 |
return clazz.newInstance(); |
58 |
|
} |
59 |
|
|
60 |
|
public Object newInstance(String key, Injector injector) throws IOException, ClassNotFoundException { |
61 |
0 |
return newInstance(key, injector, (String)null); |
62 |
|
} |
63 |
|
|
64 |
|
public Object newInstance(String key, Injector injector, String propertyPrefix) throws IOException, |
65 |
|
ClassNotFoundException { |
66 |
0 |
Class type = findClass(key, propertyPrefix); |
67 |
0 |
return injector.newInstance(type); |
68 |
|
} |
69 |
|
|
70 |
|
public <T> T newInstance(String key, Injector injector, Class<T> expectedType) throws IOException, |
71 |
|
ClassNotFoundException { |
72 |
0 |
return newInstance(key, injector, null, expectedType); |
73 |
|
} |
74 |
|
|
75 |
|
public <T> T newInstance(String key, Injector injector, String propertyPrefix, Class<T> expectedType) |
76 |
|
throws IOException, ClassNotFoundException { |
77 |
0 |
Class type = findClass(key, propertyPrefix); |
78 |
0 |
Object value = injector.newInstance(type); |
79 |
0 |
if (expectedType.isInstance(value)) { |
80 |
0 |
return expectedType.cast(value); |
81 |
|
} else { |
82 |
0 |
throw new ClassCastException("Not instanceof " + expectedType.getName() + " value: " + value); |
83 |
|
} |
84 |
|
} |
85 |
|
|
86 |
|
public <T> List<T> newInstances(String key, Injector injector, Class<T> type) throws IOException, |
87 |
|
ClassNotFoundException { |
88 |
141 |
List<Class> list = findClasses(key); |
89 |
0 |
List<T> answer = new ArrayList<T>(list.size()); |
90 |
0 |
answer.add(newInstance(key, injector, type)); |
91 |
0 |
return answer; |
92 |
|
} |
93 |
|
|
94 |
|
public Class findClass(String key) throws ClassNotFoundException, IOException { |
95 |
450 |
return findClass(key, null); |
96 |
|
} |
97 |
|
|
98 |
|
public Class findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException { |
99 |
915 |
if (propertyPrefix == null) { |
100 |
915 |
propertyPrefix = ""; |
101 |
|
} |
102 |
|
|
103 |
915 |
Class clazz = (Class)classMap.get(propertyPrefix + key); |
104 |
915 |
if (clazz == null) { |
105 |
498 |
clazz = newInstance(doFindFactoryProperies(key), propertyPrefix); |
106 |
33 |
classMap.put(propertyPrefix + key, clazz); |
107 |
|
} |
108 |
450 |
return clazz; |
109 |
|
} |
110 |
|
|
111 |
|
public List<Class> findClasses(String key) throws ClassNotFoundException, IOException { |
112 |
141 |
return findClasses(key, null); |
113 |
|
} |
114 |
|
|
115 |
|
public List<Class> findClasses(String key, String propertyPrefix) throws ClassNotFoundException, |
116 |
|
IOException { |
117 |
|
|
118 |
141 |
Class type = findClass(key, propertyPrefix); |
119 |
0 |
return Collections.singletonList(type); |
120 |
|
} |
121 |
|
|
122 |
|
private Class newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException, |
123 |
|
IOException { |
124 |
|
|
125 |
33 |
String className = properties.getProperty(propertyPrefix + "class"); |
126 |
33 |
if (className == null) { |
127 |
0 |
throw new IOException("Expected property is missing: " + propertyPrefix + "class"); |
128 |
|
} |
129 |
33 |
Class clazz = null; |
130 |
33 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
131 |
33 |
if (loader != null) { |
132 |
|
try { |
133 |
33 |
clazz = loader.loadClass(className); |
134 |
0 |
} catch (ClassNotFoundException e) { |
135 |
|
|
136 |
33 |
} |
137 |
|
} |
138 |
33 |
if (clazz == null) { |
139 |
0 |
clazz = FactoryFinder.class.getClassLoader().loadClass(className); |
140 |
|
} |
141 |
33 |
return clazz; |
142 |
|
} |
143 |
|
|
144 |
|
private Properties doFindFactoryProperies(String key) throws IOException { |
145 |
498 |
String uri = path + key; |
146 |
|
|
147 |
|
|
148 |
498 |
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
149 |
498 |
if (classLoader == null) { |
150 |
0 |
classLoader = getClass().getClassLoader(); |
151 |
|
} |
152 |
498 |
InputStream in = classLoader.getResourceAsStream(uri); |
153 |
498 |
if (in == null) { |
154 |
465 |
in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri); |
155 |
465 |
if (in == null) { |
156 |
465 |
throw new NoFactoryAvailableException(uri); |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
33 |
BufferedInputStream reader = null; |
162 |
|
try { |
163 |
33 |
reader = new BufferedInputStream(in); |
164 |
33 |
Properties properties = new Properties(); |
165 |
33 |
properties.load(reader); |
166 |
33 |
return properties; |
167 |
|
} finally { |
168 |
0 |
try { |
169 |
33 |
reader.close(); |
170 |
0 |
} catch (Exception ignore) { |
171 |
33 |
} |
172 |
33 |
} |
173 |
|
} |
174 |
|
} |