author | alexsch |
Tue, 17 Nov 2015 19:09:37 +0400 | |
changeset 34401 | 5c29353d768b |
parent 27574 | 2e8afdf5c6fb |
child 42806 | 35843e3d5ef1 |
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) 2003, 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.transform; |
|
27 |
||
28 |
import java.io.File; |
|
12458 | 29 |
import java.lang.reflect.Method; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
30 |
import java.lang.reflect.Modifier; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
31 |
import java.security.AccessController; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
32 |
import java.security.PrivilegedAction; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
33 |
import java.util.Iterator; |
12458 | 34 |
import java.util.Properties; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
35 |
import java.util.ServiceConfigurationError; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
36 |
import java.util.ServiceLoader; |
12005 | 37 |
|
38 |
/** |
|
39 |
* <p>Implements pluggable Datatypes.</p> |
|
40 |
* |
|
41 |
* <p>This class is duplicated for each JAXP subpackage so keep it in |
|
42 |
* sync. It is package private for secure class loading.</p> |
|
43 |
* |
|
44 |
* @author Santiago.PericasGeertsen@sun.com |
|
12458 | 45 |
* @author Huizhe.Wang@oracle.com |
12005 | 46 |
*/ |
47 |
class FactoryFinder { |
|
16953 | 48 |
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xalan.internal."; |
12005 | 49 |
|
50 |
/** |
|
51 |
* Internal debug flag. |
|
52 |
*/ |
|
53 |
private static boolean debug = false; |
|
54 |
||
55 |
/** |
|
27574 | 56 |
* Cache for properties in java.home/conf/jaxp.properties |
12005 | 57 |
*/ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
58 |
private final static Properties cacheProps = new Properties(); |
12005 | 59 |
|
60 |
/** |
|
27574 | 61 |
* Flag indicating if properties from java.home/conf/jaxp.properties |
12005 | 62 |
* have been cached. |
63 |
*/ |
|
12458 | 64 |
static volatile boolean firstTime = true; |
12005 | 65 |
|
66 |
/** |
|
67 |
* Security support class use to check access control before |
|
68 |
* getting certain system resources. |
|
69 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
70 |
private final static SecuritySupport ss = new SecuritySupport(); |
12005 | 71 |
|
72 |
// Define system property "jaxp.debug" to get output |
|
73 |
static { |
|
74 |
// Use try/catch block to support applets, which throws |
|
75 |
// SecurityException out of this code. |
|
76 |
try { |
|
77 |
String val = ss.getSystemProperty("jaxp.debug"); |
|
78 |
// Allow simply setting the prop to turn on debug |
|
79 |
debug = val != null && !"false".equals(val); |
|
80 |
} |
|
81 |
catch (SecurityException se) { |
|
82 |
debug = false; |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
private static void dPrint(String msg) { |
|
87 |
if (debug) { |
|
88 |
System.err.println("JAXP: " + msg); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
/** |
|
93 |
* Attempt to load a class using the class loader supplied. If that fails |
|
94 |
* and fall back is enabled, the current (i.e. bootstrap) class loader is |
|
95 |
* tried. |
|
96 |
* |
|
97 |
* If the class loader supplied is <code>null</code>, first try using the |
|
98 |
* context class loader followed by the current (i.e. bootstrap) class |
|
99 |
* loader. |
|
12458 | 100 |
* |
101 |
* Use bootstrap classLoader if cl = null and useBSClsLoader is true |
|
12005 | 102 |
*/ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
103 |
static private Class<?> getProviderClass(String className, ClassLoader cl, |
12458 | 104 |
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException |
12005 | 105 |
{ |
106 |
try { |
|
107 |
if (cl == null) { |
|
12458 | 108 |
if (useBSClsLoader) { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
109 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
12458 | 110 |
} else { |
111 |
cl = ss.getContextClassLoader(); |
|
112 |
if (cl == null) { |
|
113 |
throw new ClassNotFoundException(); |
|
114 |
} |
|
115 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
116 |
return Class.forName(className, false, cl); |
12458 | 117 |
} |
12005 | 118 |
} |
119 |
} |
|
120 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
121 |
return Class.forName(className, false, cl); |
12005 | 122 |
} |
123 |
} |
|
124 |
catch (ClassNotFoundException e1) { |
|
125 |
if (doFallback) { |
|
126 |
// 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
|
127 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
12005 | 128 |
} |
129 |
else { |
|
130 |
throw e1; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Create an instance of a class. Delegates to method |
|
137 |
* <code>getProviderClass()</code> in order to load the class. |
|
138 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
139 |
* @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
|
140 |
* instantiate. |
12458 | 141 |
* |
142 |
* @param className Name of the concrete class corresponding to the |
|
143 |
* service provider |
|
144 |
* |
|
145 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code> |
|
146 |
* current <code>Thread</code>'s context classLoader is used to load the factory class. |
|
147 |
* |
|
148 |
* @param doFallback True if the current ClassLoader should be tried as |
|
149 |
* a fallback if the class is not found using cl |
|
150 |
* |
|
151 |
* @param useServicesMechanism True use services mechanism |
|
152 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
153 |
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
|
154 |
boolean doFallback, boolean useServicesMechanism) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
155 |
throws TransformerFactoryConfigurationError |
12458 | 156 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
157 |
assert type != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
158 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
159 |
boolean useBSClsLoader = false; |
16953 | 160 |
// make sure we have access to restricted packages |
161 |
if (System.getSecurityManager() != null) { |
|
162 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
163 |
cl = null; |
|
164 |
useBSClsLoader = true; |
|
165 |
} |
|
166 |
} |
|
167 |
||
12005 | 168 |
try { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
169 |
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
170 |
if (!type.isAssignableFrom(providerClass)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
171 |
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
|
172 |
} |
12458 | 173 |
Object instance = null; |
174 |
if (!useServicesMechanism) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
175 |
instance = newInstanceNoServiceLoader(type, providerClass); |
12458 | 176 |
} |
177 |
if (instance == null) { |
|
178 |
instance = providerClass.newInstance(); |
|
179 |
} |
|
12005 | 180 |
if (debug) { // Extra check to avoid computing cl strings |
181 |
dPrint("created new instance of " + providerClass + |
|
182 |
" using ClassLoader: " + cl); |
|
183 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
184 |
return type.cast(instance); |
12005 | 185 |
} |
186 |
catch (ClassNotFoundException x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
187 |
throw new TransformerFactoryConfigurationError(x, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
188 |
"Provider " + className + " not found"); |
12005 | 189 |
} |
190 |
catch (Exception x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
191 |
throw new TransformerFactoryConfigurationError(x, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
192 |
"Provider " + className + " could not be instantiated: " + x); |
12005 | 193 |
} |
194 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
195 |
|
12458 | 196 |
/** |
197 |
* Try to construct using newTransformerFactoryNoServiceLoader |
|
198 |
* method if available. |
|
199 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
200 |
private static <T> T newInstanceNoServiceLoader(Class<T> type, Class<?> providerClass) { |
12458 | 201 |
// Retain maximum compatibility if no security manager. |
202 |
if (System.getSecurityManager() == null) { |
|
203 |
return null; |
|
204 |
} |
|
205 |
try { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
206 |
final Method creationMethod = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
207 |
providerClass.getDeclaredMethod( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
208 |
"newTransformerFactoryNoServiceLoader" |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
209 |
); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
210 |
final int modifiers = creationMethod.getModifiers(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
211 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
212 |
// Do not call the method if it's not public static. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
213 |
if (!Modifier.isPublic(modifiers) || !Modifier.isStatic(modifiers)) { |
12458 | 214 |
return null; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
215 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
216 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
217 |
// Only call the method if it's declared to return an instance of |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
218 |
// TransformerFactory |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
219 |
final Class<?> returnType = creationMethod.getReturnType(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
220 |
if (type.isAssignableFrom(returnType)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
221 |
final Object result = creationMethod.invoke(null, (Object[])null); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
222 |
return type.cast(result); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
223 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
224 |
// This should not happen, as |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
225 |
// TransformerFactoryImpl.newTransformerFactoryNoServiceLoader is |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
226 |
// declared to return TransformerFactory. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
227 |
throw new ClassCastException(returnType + " cannot be cast to " + type); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
228 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
229 |
} catch (ClassCastException e) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
230 |
throw new TransformerFactoryConfigurationError(e, e.getMessage()); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
231 |
} catch (NoSuchMethodException exc) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
232 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
233 |
} catch (Exception exc) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
234 |
return null; |
12458 | 235 |
} |
236 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
237 |
|
12005 | 238 |
/** |
239 |
* Finds the implementation Class object in the specified order. Main |
|
240 |
* entry point. |
|
241 |
* @return Class object of factory, never null |
|
242 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
243 |
* @param type Base class / Service interface of the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
244 |
* factory to find. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
245 |
* |
12005 | 246 |
* @param fallbackClassName Implementation class name, if nothing else |
247 |
* is found. Use null to mean no fallback. |
|
248 |
* |
|
249 |
* Package private so this code can be shared. |
|
250 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
251 |
static <T> T find(Class<T> type, String fallbackClassName) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
252 |
throws TransformerFactoryConfigurationError |
12005 | 253 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
254 |
assert type != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
255 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
256 |
final String factoryId = type.getName(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
257 |
|
12005 | 258 |
dPrint("find factoryId =" + factoryId); |
259 |
// Use the system property first |
|
260 |
try { |
|
261 |
String systemProp = ss.getSystemProperty(factoryId); |
|
262 |
if (systemProp != null) { |
|
263 |
dPrint("found system property, value=" + systemProp); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
264 |
return newInstance(type, systemProp, null, true, true); |
12005 | 265 |
} |
266 |
} |
|
267 |
catch (SecurityException se) { |
|
268 |
if (debug) se.printStackTrace(); |
|
269 |
} |
|
270 |
||
27574 | 271 |
// try to read from $java.home/conf/jaxp.properties |
12005 | 272 |
try { |
273 |
if (firstTime) { |
|
274 |
synchronized (cacheProps) { |
|
275 |
if (firstTime) { |
|
276 |
String configFile = ss.getSystemProperty("java.home") + File.separator + |
|
27574 | 277 |
"conf" + File.separator + "jaxp.properties"; |
12005 | 278 |
File f = new File(configFile); |
279 |
firstTime = false; |
|
280 |
if (ss.doesFileExist(f)) { |
|
281 |
dPrint("Read properties file "+f); |
|
282 |
cacheProps.load(ss.getFileInputStream(f)); |
|
283 |
} |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
287 |
final String factoryClassName = cacheProps.getProperty(factoryId); |
12005 | 288 |
|
289 |
if (factoryClassName != null) { |
|
27574 | 290 |
dPrint("found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName); |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
291 |
return newInstance(type, factoryClassName, null, true, true); |
12005 | 292 |
} |
293 |
} |
|
294 |
catch (Exception ex) { |
|
295 |
if (debug) ex.printStackTrace(); |
|
296 |
} |
|
297 |
||
298 |
// Try Jar Service Provider Mechanism |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
299 |
T provider = findServiceProvider(type); |
12005 | 300 |
if (provider != null) { |
301 |
return provider; |
|
302 |
} |
|
303 |
if (fallbackClassName == null) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
304 |
throw new TransformerFactoryConfigurationError(null, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
305 |
"Provider for " + factoryId + " cannot be found"); |
12005 | 306 |
} |
307 |
||
308 |
dPrint("loaded from fallback value: " + fallbackClassName); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
309 |
return newInstance(type, fallbackClassName, null, true, true); |
12005 | 310 |
} |
311 |
||
312 |
/* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
313 |
* Try to find provider using the ServiceLoader. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
314 |
* |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
315 |
* @param type Base class / Service interface of the factory to find. |
12005 | 316 |
* |
317 |
* @return instance of provider class if found or null |
|
318 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
319 |
private static <T> T findServiceProvider(final Class<T> type) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
320 |
throws TransformerFactoryConfigurationError |
12005 | 321 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
322 |
try { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
323 |
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
324 |
public T run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
325 |
final ServiceLoader<T> serviceLoader = ServiceLoader.load(type); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
326 |
final Iterator<T> iterator = serviceLoader.iterator(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
327 |
if (iterator.hasNext()) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
328 |
return iterator.next(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
329 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
330 |
return null; |
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 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
333 |
}); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
334 |
} catch(ServiceConfigurationError e) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
335 |
// 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
|
336 |
// FactoryConfigurationError - so we need to wrap the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
337 |
// ServiceConfigurationError in a RuntimeException. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
338 |
// The alternative would be to modify the logic in |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
339 |
// FactoryConfigurationError to allow setting a |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
340 |
// Throwable as the cause, but that could cause |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
341 |
// compatibility issues down the road. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
342 |
final RuntimeException x = new RuntimeException( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
343 |
"Provider for " + type + " cannot be created", e); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
344 |
final TransformerFactoryConfigurationError error = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
345 |
new TransformerFactoryConfigurationError(x, x.getMessage()); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
346 |
throw error; |
12005 | 347 |
} |
348 |
} |
|
349 |
} |