1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package javax.xml.ws.spi;
21
22 import java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.lang.reflect.InvocationTargetException;
28 import java.lang.reflect.Method;
29 import java.security.PrivilegedAction;
30 import java.util.Properties;
31
32
33
34
35
36
37
38
39
40
41
42 class FactoryFinder {
43
44
45
46 private static final boolean debug = false;
47
48 private static void debugPrintln(String msg) {
49 if (debug) {
50 System.err.println("Factory Finder:" + msg);
51 }
52 }
53
54
55
56
57
58
59
60
61
62 private static ClassLoader findClassLoader()
63 throws ConfigurationError {
64
65
66
67 ClassLoader cl = (ClassLoader)
68 doPrivileged( new PrivilegedAction() {
69 public Object run() {
70
71 Method m = null;
72
73 try {
74
75 m = Thread.class.getMethod("getContextClassLoader", (Class []) null);
76 } catch (NoSuchMethodException e) {
77
78 debugPrintln("assuming JDK 1.1");
79 return FactoryFinder.class.getClassLoader();
80 }
81
82 try {
83 return (ClassLoader) m.invoke(Thread.currentThread(), (Object []) null);
84 } catch (IllegalAccessException e) {
85
86 throw new ConfigurationError("Unexpected IllegalAccessException",
87 e);
88 } catch (InvocationTargetException e) {
89
90 throw new ConfigurationError("Unexpected InvocationTargetException",
91 e);
92 }
93 }
94 }
95 );
96 return cl;
97
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 private static Object newInstance(String className,
113 ClassLoader classLoader)
114 throws ConfigurationError {
115
116 final ClassLoader iClassLoader = classLoader;
117 final String iClassName = className;
118
119
120
121
122 Object obj =
123 doPrivileged( new PrivilegedAction() {
124 public Object run() {
125 try {
126 if (iClassLoader != null) {
127 try {
128 return iClassLoader.loadClass(iClassName).newInstance();
129 } catch (ClassNotFoundException x) {
130
131 }
132 }
133 return Class.forName(iClassName).newInstance();
134 } catch (ClassNotFoundException x) {
135 throw new ConfigurationError(
136 "Provider " + iClassName + " not found", x);
137 } catch (Exception x) {
138 throw new ConfigurationError(
139 "Provider " + iClassName + " could not be instantiated: " + x,
140 x);
141 }
142 }
143 });
144 return obj;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159 static Object find(String factoryId, String fallbackClassName)
160 throws ConfigurationError {
161
162 final String iFactoryId = factoryId;
163 final String iFallbackClassName = fallbackClassName;
164
165 Object obj =
166 doPrivileged( new PrivilegedAction() {
167 public Object run() {
168 debugPrintln("debug is on");
169
170 ClassLoader classLoader = findClassLoader();
171
172
173 try {
174 String systemProp =
175 System.getProperty(iFactoryId);
176 if (systemProp != null) {
177 debugPrintln("found system property " + systemProp);
178 return newInstance(systemProp, classLoader);
179 }
180 } catch (SecurityException se) {
181 }
182
183
184 try {
185 String javah = System.getProperty("java.home");
186 String configFile = javah + File.separator +
187 "lib" + File.separator + "jaxrpc.properties";
188 File f = new File(configFile);
189 if (f.exists()) {
190 Properties props = new Properties();
191 props.load(new FileInputStream(f));
192 String factoryClassName = props.getProperty(iFactoryId);
193 debugPrintln("found java.home property " + factoryClassName);
194 return newInstance(factoryClassName, classLoader);
195 }
196 } catch (Exception ex) {
197 if (debug) ex.printStackTrace();
198 }
199
200 String serviceId = "META-INF/services/" + iFactoryId;
201
202 try {
203 InputStream is = null;
204 if (classLoader == null) {
205 is = ClassLoader.getSystemResourceAsStream(serviceId);
206 } else {
207 is = classLoader.getResourceAsStream(serviceId);
208 }
209
210 if (is != null) {
211 debugPrintln("found " + serviceId);
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 BufferedReader rd;
230 try {
231 rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
232 } catch (java.io.UnsupportedEncodingException e) {
233 rd = new BufferedReader(new InputStreamReader(is));
234 }
235
236 String factoryClassName = rd.readLine();
237 rd.close();
238
239 if (factoryClassName != null &&
240 ! "".equals(factoryClassName)) {
241 debugPrintln("loaded from services: " + factoryClassName);
242 return newInstance(factoryClassName, classLoader);
243 }
244 }
245 } catch (Exception ex) {
246 if (debug) ex.printStackTrace();
247 }
248
249 if (iFallbackClassName == null) {
250 throw new ConfigurationError(
251 "Provider for " + iFactoryId + " cannot be found", null);
252 }
253
254 debugPrintln("loaded from fallback value: " + iFallbackClassName);
255 return newInstance(iFallbackClassName, classLoader);
256 }
257 });
258 return obj;
259 }
260
261 private static Object doPrivileged(PrivilegedAction action) {
262 SecurityManager sm = System.getSecurityManager();
263 if (sm == null) {
264 return(action.run());
265 } else {
266 return java.security.AccessController.doPrivileged(action);
267 }
268 }
269
270 static class ConfigurationError extends Error {
271
272
273
274 private Exception exception;
275
276
277
278
279
280
281
282
283 ConfigurationError(String msg, Exception x) {
284 super(msg);
285 this.exception = x;
286 }
287
288 Exception getException() {
289 return exception;
290 }
291 }
292 }