author | duke |
Wed, 05 Jul 2017 18:58:01 +0200 | |
changeset 17815 | b72ae39e1329 |
parent 17264 | 3aff554ad461 |
child 20965 | 913493fcadde |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
2 |
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
12005 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package javax.xml.stream; |
|
27 |
||
28 |
import java.io.File; |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
29 |
import java.security.AccessController; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
30 |
import java.security.PrivilegedAction; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
31 |
import java.util.Iterator; |
12005 | 32 |
import java.util.Properties; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
33 |
import java.util.ServiceConfigurationError; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
34 |
import java.util.ServiceLoader; |
12005 | 35 |
|
36 |
/** |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
37 |
* <p>Implements pluggable streams.</p> |
12005 | 38 |
* |
39 |
* <p>This class is duplicated for each JAXP subpackage so keep it in |
|
40 |
* sync. It is package private for secure class loading.</p> |
|
41 |
* |
|
42 |
* @author Santiago.PericasGeertsen@sun.com |
|
43 |
*/ |
|
44 |
class FactoryFinder { |
|
16953 | 45 |
// Check we have access to package. |
46 |
private static final String DEFAULT_PACKAGE = "com.sun.xml.internal."; |
|
12005 | 47 |
|
48 |
/** |
|
49 |
* Internal debug flag. |
|
50 |
*/ |
|
51 |
private static boolean debug = false; |
|
52 |
||
53 |
/** |
|
54 |
* Cache for properties in java.home/lib/jaxp.properties |
|
55 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
56 |
final private static Properties cacheProps = new Properties(); |
12005 | 57 |
|
58 |
/** |
|
59 |
* Flag indicating if properties from java.home/lib/jaxp.properties |
|
60 |
* have been cached. |
|
61 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
62 |
private static volatile boolean firstTime = true; |
12005 | 63 |
|
64 |
/** |
|
65 |
* Security support class use to check access control before |
|
66 |
* getting certain system resources. |
|
67 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
68 |
final private static SecuritySupport ss = new SecuritySupport(); |
12005 | 69 |
|
70 |
// Define system property "jaxp.debug" to get output |
|
71 |
static { |
|
72 |
// Use try/catch block to support applets, which throws |
|
73 |
// SecurityException out of this code. |
|
74 |
try { |
|
75 |
String val = ss.getSystemProperty("jaxp.debug"); |
|
76 |
// Allow simply setting the prop to turn on debug |
|
77 |
debug = val != null && !"false".equals(val); |
|
78 |
} |
|
79 |
catch (SecurityException se) { |
|
80 |
debug = false; |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
private static void dPrint(String msg) { |
|
85 |
if (debug) { |
|
86 |
System.err.println("JAXP: " + msg); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Attempt to load a class using the class loader supplied. If that fails |
|
92 |
* and fall back is enabled, the current (i.e. bootstrap) class loader is |
|
93 |
* tried. |
|
94 |
* |
|
95 |
* If the class loader supplied is <code>null</code>, first try using the |
|
96 |
* context class loader followed by the current (i.e. bootstrap) class |
|
97 |
* loader. |
|
16953 | 98 |
* |
99 |
* Use bootstrap classLoader if cl = null and useBSClsLoader is true |
|
12005 | 100 |
*/ |
101 |
static private Class getProviderClass(String className, ClassLoader cl, |
|
16953 | 102 |
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException |
12005 | 103 |
{ |
104 |
try { |
|
105 |
if (cl == null) { |
|
16953 | 106 |
if (useBSClsLoader) { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
107 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
16953 | 108 |
} else { |
109 |
cl = ss.getContextClassLoader(); |
|
110 |
if (cl == null) { |
|
111 |
throw new ClassNotFoundException(); |
|
112 |
} |
|
113 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
114 |
return Class.forName(className, false, cl); |
16953 | 115 |
} |
12005 | 116 |
} |
117 |
} |
|
118 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
119 |
return Class.forName(className, false, cl); |
12005 | 120 |
} |
121 |
} |
|
122 |
catch (ClassNotFoundException e1) { |
|
123 |
if (doFallback) { |
|
124 |
// Use current class loader - should always be bootstrap CL |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
125 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
12005 | 126 |
} |
127 |
else { |
|
128 |
throw e1; |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Create an instance of a class. Delegates to method |
|
135 |
* <code>getProviderClass()</code> in order to load the class. |
|
136 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
137 |
* @param type Base class / Service interface of the factory to |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
138 |
* instantiate. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
139 |
* |
12005 | 140 |
* @param className Name of the concrete class corresponding to the |
141 |
* service provider |
|
142 |
* |
|
16953 | 143 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code> |
144 |
* current <code>Thread</code>'s context classLoader is used to load the factory class. |
|
12005 | 145 |
* |
146 |
* @param doFallback True if the current ClassLoader should be tried as |
|
147 |
* a fallback if the class is not found using cl |
|
148 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
149 |
static <T> T newInstance(Class<T> type, String className, ClassLoader cl, boolean doFallback) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
150 |
throws FactoryConfigurationError |
12005 | 151 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
152 |
return newInstance(type, className, cl, doFallback, false); |
16953 | 153 |
} |
154 |
||
155 |
/** |
|
156 |
* Create an instance of a class. Delegates to method |
|
157 |
* <code>getProviderClass()</code> in order to load the class. |
|
158 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
159 |
* @param type Base class / Service interface of the factory to |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
160 |
* instantiate. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
161 |
* |
16953 | 162 |
* @param className Name of the concrete class corresponding to the |
163 |
* service provider |
|
164 |
* |
|
165 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code> |
|
166 |
* current <code>Thread</code>'s context classLoader is used to load the factory class. |
|
167 |
* |
|
168 |
* @param doFallback True if the current ClassLoader should be tried as |
|
169 |
* a fallback if the class is not found using cl |
|
170 |
* |
|
171 |
* @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter |
|
172 |
* is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader. |
|
173 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
174 |
static <T> T newInstance(Class<T> type, String className, ClassLoader cl, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
175 |
boolean doFallback, boolean useBSClsLoader) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
176 |
throws FactoryConfigurationError |
16953 | 177 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
178 |
assert type != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
179 |
|
16953 | 180 |
// make sure we have access to restricted packages |
181 |
if (System.getSecurityManager() != null) { |
|
182 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
183 |
cl = null; |
|
184 |
useBSClsLoader = true; |
|
185 |
} |
|
186 |
} |
|
187 |
||
12005 | 188 |
try { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
189 |
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
190 |
if (!type.isAssignableFrom(providerClass)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
191 |
throw new ClassCastException(className + " cannot be cast to " + type.getName()); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
192 |
} |
12005 | 193 |
Object instance = providerClass.newInstance(); |
194 |
if (debug) { // Extra check to avoid computing cl strings |
|
195 |
dPrint("created new instance of " + providerClass + |
|
196 |
" using ClassLoader: " + cl); |
|
197 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
198 |
return type.cast(instance); |
12005 | 199 |
} |
200 |
catch (ClassNotFoundException x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
201 |
throw new FactoryConfigurationError( |
12005 | 202 |
"Provider " + className + " not found", x); |
203 |
} |
|
204 |
catch (Exception x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
205 |
throw new FactoryConfigurationError( |
12005 | 206 |
"Provider " + className + " could not be instantiated: " + x, |
207 |
x); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/** |
|
212 |
* Finds the implementation Class object in the specified order. |
|
213 |
* |
|
214 |
* @return Class object of factory, never null |
|
215 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
216 |
* @param type Base class / Service interface of the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
217 |
* factory to find. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
218 |
* |
12005 | 219 |
* @param fallbackClassName Implementation class name, if nothing else |
220 |
* is found. Use null to mean no fallback. |
|
221 |
* |
|
222 |
* Package private so this code can be shared. |
|
223 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
224 |
static <T> T find(Class<T> type, String fallbackClassName) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
225 |
throws FactoryConfigurationError |
12005 | 226 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
227 |
return find(type, type.getName(), null, fallbackClassName); |
12005 | 228 |
} |
229 |
||
230 |
/** |
|
231 |
* Finds the implementation Class object in the specified order. Main |
|
232 |
* entry point. |
|
233 |
* @return Class object of factory, never null |
|
234 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
235 |
* @param type Base class / Service interface of the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
236 |
* factory to find. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
237 |
* |
12005 | 238 |
* @param factoryId Name of the factory to find, same as |
239 |
* a property name |
|
240 |
* |
|
241 |
* @param cl ClassLoader to be used to load the class, null means to use |
|
242 |
* the bootstrap ClassLoader |
|
243 |
* |
|
244 |
* @param fallbackClassName Implementation class name, if nothing else |
|
245 |
* is found. Use null to mean no fallback. |
|
246 |
* |
|
247 |
* Package private so this code can be shared. |
|
248 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
249 |
static <T> T find(Class<T> type, String factoryId, ClassLoader cl, String fallbackClassName) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
250 |
throws FactoryConfigurationError |
12005 | 251 |
{ |
252 |
dPrint("find factoryId =" + factoryId); |
|
253 |
||
254 |
// Use the system property first |
|
255 |
try { |
|
256 |
String systemProp = ss.getSystemProperty(factoryId); |
|
257 |
if (systemProp != null) { |
|
258 |
dPrint("found system property, value=" + systemProp); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
259 |
// There's a bug here - because 'cl' is ignored. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
260 |
// This will be handled separately. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
261 |
return newInstance(type, systemProp, null, true); |
12005 | 262 |
} |
263 |
} |
|
264 |
catch (SecurityException se) { |
|
265 |
if (debug) se.printStackTrace(); |
|
266 |
} |
|
267 |
||
268 |
// Try read $java.home/lib/stax.properties followed by |
|
269 |
// $java.home/lib/jaxp.properties if former not present |
|
270 |
String configFile = null; |
|
271 |
try { |
|
272 |
if (firstTime) { |
|
273 |
synchronized (cacheProps) { |
|
274 |
if (firstTime) { |
|
275 |
configFile = ss.getSystemProperty("java.home") + File.separator + |
|
276 |
"lib" + File.separator + "stax.properties"; |
|
277 |
File f = new File(configFile); |
|
278 |
firstTime = false; |
|
279 |
if (ss.doesFileExist(f)) { |
|
280 |
dPrint("Read properties file "+f); |
|
281 |
cacheProps.load(ss.getFileInputStream(f)); |
|
282 |
} |
|
283 |
else { |
|
284 |
configFile = ss.getSystemProperty("java.home") + File.separator + |
|
285 |
"lib" + File.separator + "jaxp.properties"; |
|
286 |
f = new File(configFile); |
|
287 |
if (ss.doesFileExist(f)) { |
|
288 |
dPrint("Read properties file "+f); |
|
289 |
cacheProps.load(ss.getFileInputStream(f)); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
290 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
291 |
} |
12005 | 292 |
} |
293 |
} |
|
294 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
295 |
final String factoryClassName = cacheProps.getProperty(factoryId); |
12005 | 296 |
|
297 |
if (factoryClassName != null) { |
|
298 |
dPrint("found in " + configFile + " value=" + factoryClassName); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
299 |
// There's a bug here - because 'cl' is ignored. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
300 |
// This will be handled separately. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
301 |
return newInstance(type, factoryClassName, null, true); |
12005 | 302 |
} |
303 |
} |
|
304 |
catch (Exception ex) { |
|
305 |
if (debug) ex.printStackTrace(); |
|
306 |
} |
|
307 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
308 |
if (type.getName().equals(factoryId)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
309 |
// Try Jar Service Provider Mechanism |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
310 |
final T provider = findServiceProvider(type); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
311 |
if (provider != null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
312 |
return provider; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
313 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
314 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
315 |
// We're in the case where a 'custom' factoryId was provided, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
316 |
// and in every case where that happens, we expect that |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
317 |
// fallbackClassName will be null. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
318 |
assert fallbackClassName == null; |
12005 | 319 |
} |
320 |
if (fallbackClassName == null) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
321 |
throw new FactoryConfigurationError( |
12005 | 322 |
"Provider for " + factoryId + " cannot be found", null); |
323 |
} |
|
324 |
||
325 |
dPrint("loaded from fallback value: " + fallbackClassName); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
326 |
return newInstance(type, fallbackClassName, cl, true); |
12005 | 327 |
} |
328 |
||
329 |
/* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
330 |
* Try to find provider using the ServiceLoader API |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
331 |
* |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
332 |
* @param type Base class / Service interface of the factory to find. |
12005 | 333 |
* |
334 |
* @return instance of provider class if found or null |
|
335 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
336 |
private static <T> T findServiceProvider(final Class<T> type) { |
12005 | 337 |
try { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
338 |
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
339 |
@Override |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
340 |
public T run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
341 |
final ServiceLoader<T> serviceLoader = ServiceLoader.load(type); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
342 |
final Iterator<T> iterator = serviceLoader.iterator(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
343 |
if (iterator.hasNext()) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
344 |
return iterator.next(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
345 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
346 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
347 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
348 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
349 |
}); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
350 |
} catch(ServiceConfigurationError e) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
351 |
// It is not possible to wrap an error directly in |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
352 |
// FactoryConfigurationError - so we need to wrap the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
353 |
// ServiceConfigurationError in a RuntimeException. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
354 |
// The alternative would be to modify the logic in |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
355 |
// FactoryConfigurationError to allow setting a |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
356 |
// Throwable as the cause, but that could cause |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
357 |
// compatibility issues down the road. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
358 |
final RuntimeException x = new RuntimeException( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
359 |
"Provider for " + type + " cannot be created", e); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
360 |
final FactoryConfigurationError error = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
361 |
new FactoryConfigurationError(x, x.getMessage()); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
362 |
throw error; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
363 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
364 |
} |
12005 | 365 |
|
366 |
} |