| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.ini4j.spi; |
| 17 | |
|
| 18 | |
import java.io.BufferedReader; |
| 19 | |
import java.io.InputStream; |
| 20 | |
import java.io.InputStreamReader; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
final class ServiceFinder |
| 29 | |
{ |
| 30 | |
private static final String SERVICES_PATH = "META-INF/services/"; |
| 31 | |
|
| 32 | |
private ServiceFinder() |
| 33 | 0 | { |
| 34 | 0 | } |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
static <T> T findService(Class<T> clazz) |
| 56 | |
{ |
| 57 | |
try |
| 58 | |
{ |
| 59 | |
|
| 60 | |
|
| 61 | 253 | return clazz.cast(findServiceClass(clazz).newInstance()); |
| 62 | |
} |
| 63 | 1 | catch (Exception x) |
| 64 | |
{ |
| 65 | 1 | throw (IllegalArgumentException) new IllegalArgumentException("Provider " + clazz.getName() + " could not be instantiated: " + x) |
| 66 | |
.initCause(x); |
| 67 | |
} |
| 68 | |
} |
| 69 | |
|
| 70 | |
@SuppressWarnings(Warnings.UNCHECKED) |
| 71 | |
static <T> Class<? extends T> findServiceClass(Class<T> clazz) throws IllegalArgumentException |
| 72 | |
{ |
| 73 | 254 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 74 | 254 | String serviceClassName = findServiceClassName(clazz.getName()); |
| 75 | 254 | Class<T> ret = clazz; |
| 76 | |
|
| 77 | 254 | if (serviceClassName != null) |
| 78 | |
{ |
| 79 | |
try |
| 80 | |
{ |
| 81 | 2 | ret = (Class<T>) ((classLoader == null) ? Class.forName(serviceClassName) : classLoader.loadClass(serviceClassName)); |
| 82 | |
} |
| 83 | 1 | catch (ClassNotFoundException x) |
| 84 | |
{ |
| 85 | 1 | throw (IllegalArgumentException) new IllegalArgumentException("Provider " + serviceClassName + " not found").initCause(x); |
| 86 | 1 | } |
| 87 | |
} |
| 88 | |
|
| 89 | 253 | return ret; |
| 90 | |
} |
| 91 | |
|
| 92 | |
static String findServiceClassName(String serviceId) throws IllegalArgumentException |
| 93 | |
{ |
| 94 | 259 | String serviceClassName = null; |
| 95 | |
|
| 96 | |
|
| 97 | |
try |
| 98 | |
{ |
| 99 | 259 | String systemProp = System.getProperty(serviceId); |
| 100 | |
|
| 101 | 259 | if (systemProp != null) |
| 102 | |
{ |
| 103 | 3 | serviceClassName = systemProp; |
| 104 | |
} |
| 105 | |
} |
| 106 | 0 | catch (SecurityException x) |
| 107 | |
{ |
| 108 | |
assert true; |
| 109 | 259 | } |
| 110 | |
|
| 111 | 259 | if (serviceClassName == null) |
| 112 | |
{ |
| 113 | 256 | serviceClassName = loadLine(SERVICES_PATH + serviceId); |
| 114 | |
} |
| 115 | |
|
| 116 | 259 | return serviceClassName; |
| 117 | |
} |
| 118 | |
|
| 119 | |
private static String loadLine(String servicePath) |
| 120 | |
{ |
| 121 | 256 | String ret = null; |
| 122 | |
|
| 123 | |
|
| 124 | |
try |
| 125 | |
{ |
| 126 | 256 | InputStream is = null; |
| 127 | 256 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 128 | |
|
| 129 | 256 | if (classLoader == null) |
| 130 | |
{ |
| 131 | 0 | is = ClassLoader.getSystemResourceAsStream(servicePath); |
| 132 | |
} |
| 133 | |
else |
| 134 | |
{ |
| 135 | 256 | is = classLoader.getResourceAsStream(servicePath); |
| 136 | |
} |
| 137 | |
|
| 138 | 256 | if (is != null) |
| 139 | |
{ |
| 140 | 3 | BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
| 141 | 3 | String line = rd.readLine(); |
| 142 | |
|
| 143 | 3 | rd.close(); |
| 144 | 3 | if (line != null) |
| 145 | |
{ |
| 146 | 2 | line = line.trim(); |
| 147 | 2 | if (line.length() != 0) |
| 148 | |
{ |
| 149 | 1 | ret = line.split("\\s|#")[0]; |
| 150 | |
} |
| 151 | |
} |
| 152 | |
} |
| 153 | |
} |
| 154 | 0 | catch (Exception x) |
| 155 | |
{ |
| 156 | |
assert true; |
| 157 | 256 | } |
| 158 | |
|
| 159 | 256 | return ret; |
| 160 | |
} |
| 161 | |
} |