| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
package org.apache.camel.util; |
| 19 |
|
|
| 20 |
|
import org.apache.camel.spi.Injector; |
| 21 |
|
|
| 22 |
|
import java.io.BufferedInputStream; |
| 23 |
|
import java.io.IOException; |
| 24 |
|
import java.io.InputStream; |
| 25 |
|
import java.util.Properties; |
| 26 |
|
import java.util.concurrent.ConcurrentHashMap; |
| 27 |
|
|
| 28 |
|
public class FactoryFinder { |
| 29 |
|
private final String path; |
| 30 |
45 |
private final ConcurrentHashMap classMap = new ConcurrentHashMap(); |
| 31 |
|
|
| 32 |
|
public FactoryFinder() { |
| 33 |
44 |
this("META-INF/services/org/apache/camel/"); |
| 34 |
44 |
} |
| 35 |
|
|
| 36 |
45 |
public FactoryFinder(String path) { |
| 37 |
45 |
this.path = path; |
| 38 |
45 |
} |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public Object newInstance(String key) |
| 48 |
|
throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException { |
| 49 |
44 |
return newInstance(key, (String) null); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
public Object newInstance(String key, String propertyPrefix) |
| 53 |
|
throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException { |
| 54 |
44 |
Class clazz = findClass(key, propertyPrefix); |
| 55 |
0 |
return clazz.newInstance(); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
public Object newInstance(String key, Injector injector) throws IOException, ClassNotFoundException { |
| 59 |
0 |
return newInstance(key, injector, null); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
public Object newInstance(String key, Injector injector, String propertyPrefix) throws IOException, ClassNotFoundException { |
| 63 |
0 |
Class type = findClass(key, propertyPrefix); |
| 64 |
0 |
return injector.newInstance(type); |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
public Class findClass(String key) throws ClassNotFoundException, IOException { |
| 68 |
67 |
return findClass(key, null); |
| 69 |
|
} |
| 70 |
|
|
| 71 |
|
public Class findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException { |
| 72 |
111 |
if (propertyPrefix == null) { |
| 73 |
111 |
propertyPrefix = ""; |
| 74 |
|
} |
| 75 |
|
|
| 76 |
111 |
Class clazz = (Class) classMap.get(propertyPrefix + key); |
| 77 |
111 |
if (clazz == null) { |
| 78 |
50 |
clazz = newInstance(doFindFactoryProperies(key), propertyPrefix); |
| 79 |
6 |
classMap.put(propertyPrefix + key, clazz); |
| 80 |
|
} |
| 81 |
67 |
return clazz; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
private Class newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException, IOException { |
| 85 |
|
|
| 86 |
6 |
String className = properties.getProperty(propertyPrefix + "class"); |
| 87 |
6 |
if (className == null) { |
| 88 |
0 |
throw new IOException("Expected property is missing: " + propertyPrefix + "class"); |
| 89 |
|
} |
| 90 |
6 |
Class clazz = null; |
| 91 |
6 |
ClassLoader loader = Thread.currentThread().getContextClassLoader(); |
| 92 |
6 |
if (loader != null) { |
| 93 |
|
try { |
| 94 |
6 |
clazz = loader.loadClass(className); |
| 95 |
|
} |
| 96 |
0 |
catch (ClassNotFoundException e) { |
| 97 |
|
|
| 98 |
6 |
} |
| 99 |
|
} |
| 100 |
6 |
if (clazz == null) { |
| 101 |
0 |
clazz = FactoryFinder.class.getClassLoader().loadClass(className); |
| 102 |
|
} |
| 103 |
6 |
return clazz; |
| 104 |
|
} |
| 105 |
|
|
| 106 |
|
private Properties doFindFactoryProperies(String key) throws IOException { |
| 107 |
50 |
String uri = path + key; |
| 108 |
|
|
| 109 |
|
|
| 110 |
50 |
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 111 |
50 |
if (classLoader == null) { |
| 112 |
0 |
classLoader = getClass().getClassLoader(); |
| 113 |
|
} |
| 114 |
50 |
InputStream in = classLoader.getResourceAsStream(uri); |
| 115 |
50 |
if (in == null) { |
| 116 |
44 |
in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri); |
| 117 |
44 |
if (in == null) { |
| 118 |
44 |
throw new NoFactoryAvailableException(uri); |
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
|
| 123 |
6 |
BufferedInputStream reader = null; |
| 124 |
|
try { |
| 125 |
6 |
reader = new BufferedInputStream(in); |
| 126 |
6 |
Properties properties = new Properties(); |
| 127 |
6 |
properties.load(reader); |
| 128 |
6 |
return properties; |
| 129 |
|
} |
| 130 |
|
finally { |
| 131 |
0 |
try { |
| 132 |
6 |
reader.close(); |
| 133 |
|
} |
| 134 |
0 |
catch (Exception e) { |
| 135 |
6 |
} |
| 136 |
6 |
} |
| 137 |
|
} |
| 138 |
|
} |