author | thartmann |
Fri, 24 Aug 2018 08:17:23 +0200 | |
changeset 51514 | 1e332d63bd96 |
parent 48412 | d4412e380f6b |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
44378
06aa1b671d17
8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents:
42806
diff
changeset
|
2 |
* Copyright (c) 2005, 2017, 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; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
35 |
import java.util.function.Supplier; |
47312 | 36 |
import jdk.xml.internal.SecuritySupport; |
12005 | 37 |
|
38 |
/** |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
39 |
* <p>Implements pluggable streams.</p> |
12005 | 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 |
* |
|
48412 | 44 |
* @author Santiago PericasGeertsen |
12005 | 45 |
*/ |
46 |
class FactoryFinder { |
|
16953 | 47 |
// Check we have access to package. |
48 |
private static final String DEFAULT_PACKAGE = "com.sun.xml.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 |
final private 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 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
64 |
private static volatile boolean firstTime = true; |
12005 | 65 |
|
66 |
// Define system property "jaxp.debug" to get output |
|
67 |
static { |
|
68 |
// Use try/catch block to support applets, which throws |
|
69 |
// SecurityException out of this code. |
|
70 |
try { |
|
47312 | 71 |
String val = SecuritySupport.getSystemProperty("jaxp.debug"); |
12005 | 72 |
// Allow simply setting the prop to turn on debug |
73 |
debug = val != null && !"false".equals(val); |
|
74 |
} |
|
75 |
catch (SecurityException se) { |
|
76 |
debug = false; |
|
77 |
} |
|
78 |
} |
|
79 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
80 |
private static void dPrint(Supplier<String> msgGen) { |
12005 | 81 |
if (debug) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
82 |
System.err.println("JAXP: " + msgGen.get()); |
12005 | 83 |
} |
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Attempt to load a class using the class loader supplied. If that fails |
|
88 |
* and fall back is enabled, the current (i.e. bootstrap) class loader is |
|
89 |
* tried. |
|
90 |
* |
|
91 |
* If the class loader supplied is <code>null</code>, first try using the |
|
92 |
* context class loader followed by the current (i.e. bootstrap) class |
|
93 |
* loader. |
|
16953 | 94 |
* |
95 |
* Use bootstrap classLoader if cl = null and useBSClsLoader is true |
|
12005 | 96 |
*/ |
47359
e1a6c0168741
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents:
47312
diff
changeset
|
97 |
static private Class<?> getProviderClass(String className, ClassLoader cl, |
16953 | 98 |
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException |
12005 | 99 |
{ |
100 |
try { |
|
101 |
if (cl == null) { |
|
16953 | 102 |
if (useBSClsLoader) { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
103 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
16953 | 104 |
} else { |
47312 | 105 |
cl = SecuritySupport.getContextClassLoader(); |
16953 | 106 |
if (cl == null) { |
107 |
throw new ClassNotFoundException(); |
|
108 |
} |
|
109 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
110 |
return Class.forName(className, false, cl); |
16953 | 111 |
} |
12005 | 112 |
} |
113 |
} |
|
114 |
else { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
115 |
return Class.forName(className, false, cl); |
12005 | 116 |
} |
117 |
} |
|
118 |
catch (ClassNotFoundException e1) { |
|
119 |
if (doFallback) { |
|
120 |
// 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
|
121 |
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
12005 | 122 |
} |
123 |
else { |
|
124 |
throw e1; |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
||
129 |
/** |
|
130 |
* Create an instance of a class. Delegates to method |
|
131 |
* <code>getProviderClass()</code> in order to load the class. |
|
132 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
133 |
* @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
|
134 |
* instantiate. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
135 |
* |
12005 | 136 |
* @param className Name of the concrete class corresponding to the |
137 |
* service provider |
|
138 |
* |
|
16953 | 139 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code> |
140 |
* current <code>Thread</code>'s context classLoader is used to load the factory class. |
|
12005 | 141 |
* |
142 |
* @param doFallback True if the current ClassLoader should be tried as |
|
143 |
* a fallback if the class is not found using cl |
|
144 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
145 |
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
|
146 |
throws FactoryConfigurationError |
12005 | 147 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
148 |
return newInstance(type, className, cl, doFallback, false); |
16953 | 149 |
} |
150 |
||
151 |
/** |
|
152 |
* Create an instance of a class. Delegates to method |
|
153 |
* <code>getProviderClass()</code> in order to load the class. |
|
154 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
155 |
* @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
|
156 |
* instantiate. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
157 |
* |
16953 | 158 |
* @param className Name of the concrete class corresponding to the |
159 |
* service provider |
|
160 |
* |
|
161 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code> |
|
162 |
* current <code>Thread</code>'s context classLoader is used to load the factory class. |
|
163 |
* |
|
164 |
* @param doFallback True if the current ClassLoader should be tried as |
|
165 |
* a fallback if the class is not found using cl |
|
166 |
* |
|
167 |
* @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter |
|
168 |
* is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader. |
|
169 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
170 |
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
|
171 |
boolean doFallback, boolean useBSClsLoader) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
172 |
throws FactoryConfigurationError |
16953 | 173 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
174 |
assert type != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
175 |
|
16953 | 176 |
// make sure we have access to restricted packages |
177 |
if (System.getSecurityManager() != null) { |
|
178 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
179 |
cl = null; |
|
180 |
useBSClsLoader = true; |
|
181 |
} |
|
182 |
} |
|
183 |
||
12005 | 184 |
try { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
185 |
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
186 |
if (!type.isAssignableFrom(providerClass)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
187 |
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
|
188 |
} |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
44378
diff
changeset
|
189 |
Object instance = providerClass.getConstructor().newInstance(); |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
190 |
final ClassLoader clD = cl; |
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
191 |
dPrint(()->"created new instance of " + providerClass + |
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
192 |
" using ClassLoader: " + clD); |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
193 |
return type.cast(instance); |
12005 | 194 |
} |
195 |
catch (ClassNotFoundException x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
196 |
throw new FactoryConfigurationError( |
12005 | 197 |
"Provider " + className + " not found", x); |
198 |
} |
|
199 |
catch (Exception x) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
200 |
throw new FactoryConfigurationError( |
12005 | 201 |
"Provider " + className + " could not be instantiated: " + x, |
202 |
x); |
|
203 |
} |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* Finds the implementation Class object in the specified order. |
|
208 |
* |
|
209 |
* @return Class object of factory, never null |
|
210 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
211 |
* @param type Base class / Service interface of the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
212 |
* factory to find. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
213 |
* |
12005 | 214 |
* @param fallbackClassName Implementation class name, if nothing else |
215 |
* is found. Use null to mean no fallback. |
|
216 |
* |
|
217 |
* Package private so this code can be shared. |
|
218 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
219 |
static <T> T find(Class<T> type, String fallbackClassName) |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
220 |
throws FactoryConfigurationError |
12005 | 221 |
{ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
222 |
return find(type, type.getName(), null, fallbackClassName); |
12005 | 223 |
} |
224 |
||
225 |
/** |
|
226 |
* Finds the implementation Class object in the specified order. Main |
|
227 |
* entry point. |
|
228 |
* @return Class object of factory, never null |
|
229 |
* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
230 |
* @param type Base class / Service interface of the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
231 |
* factory to find. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
232 |
* |
12005 | 233 |
* @param factoryId Name of the factory to find, same as |
234 |
* a property name |
|
235 |
* |
|
236 |
* @param cl ClassLoader to be used to load the class, null means to use |
|
237 |
* the bootstrap ClassLoader |
|
238 |
* |
|
239 |
* @param fallbackClassName Implementation class name, if nothing else |
|
240 |
* is found. Use null to mean no fallback. |
|
241 |
* |
|
242 |
* Package private so this code can be shared. |
|
243 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
244 |
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
|
245 |
throws FactoryConfigurationError |
12005 | 246 |
{ |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
247 |
dPrint(()->"find factoryId =" + factoryId); |
12005 | 248 |
|
249 |
// Use the system property first |
|
250 |
try { |
|
20965 | 251 |
|
252 |
final String systemProp; |
|
253 |
if (type.getName().equals(factoryId)) { |
|
47312 | 254 |
systemProp = SecuritySupport.getSystemProperty(factoryId); |
20965 | 255 |
} else { |
256 |
systemProp = System.getProperty(factoryId); |
|
257 |
} |
|
12005 | 258 |
if (systemProp != null) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
259 |
dPrint(()->"found system property, value=" + systemProp); |
21997
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
260 |
return newInstance(type, systemProp, cl, true); |
12005 | 261 |
} |
262 |
} |
|
263 |
catch (SecurityException se) { |
|
20965 | 264 |
throw new FactoryConfigurationError( |
265 |
"Failed to read factoryId '" + factoryId + "'", se); |
|
12005 | 266 |
} |
267 |
||
44378
06aa1b671d17
8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents:
42806
diff
changeset
|
268 |
// Try read $java.home/conf/stax.properties followed by |
27574 | 269 |
// $java.home/conf/jaxp.properties if former not present |
12005 | 270 |
String configFile = null; |
271 |
try { |
|
272 |
if (firstTime) { |
|
273 |
synchronized (cacheProps) { |
|
274 |
if (firstTime) { |
|
47312 | 275 |
configFile = SecuritySupport.getSystemProperty("java.home") + File.separator + |
44378
06aa1b671d17
8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents:
42806
diff
changeset
|
276 |
"conf" + File.separator + "stax.properties"; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
277 |
final File fStax = new File(configFile); |
12005 | 278 |
firstTime = false; |
47312 | 279 |
if (SecuritySupport.doesFileExist(fStax)) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
280 |
dPrint(()->"Read properties file "+fStax); |
47312 | 281 |
cacheProps.load(SecuritySupport.getFileInputStream(fStax)); |
12005 | 282 |
} |
283 |
else { |
|
47312 | 284 |
configFile = SecuritySupport.getSystemProperty("java.home") + File.separator + |
27574 | 285 |
"conf" + File.separator + "jaxp.properties"; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
286 |
final File fJaxp = new File(configFile); |
47312 | 287 |
if (SecuritySupport.doesFileExist(fJaxp)) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
288 |
dPrint(()->"Read properties file "+fJaxp); |
47312 | 289 |
cacheProps.load(SecuritySupport.getFileInputStream(fJaxp)); |
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) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
298 |
final String foundIn = configFile; |
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
299 |
dPrint(()->"found in " + foundIn + " value=" + factoryClassName); |
21997
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
300 |
return newInstance(type, factoryClassName, cl, true); |
12005 | 301 |
} |
302 |
} |
|
303 |
catch (Exception ex) { |
|
304 |
if (debug) ex.printStackTrace(); |
|
305 |
} |
|
306 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
307 |
if (type.getName().equals(factoryId)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
308 |
// Try Jar Service Provider Mechanism |
21997
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
309 |
final T provider = findServiceProvider(type, cl); |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
310 |
if (provider != null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
311 |
return provider; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
312 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
313 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
314 |
// 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
|
315 |
// 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
|
316 |
// fallbackClassName will be null. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
317 |
assert fallbackClassName == null; |
12005 | 318 |
} |
319 |
if (fallbackClassName == null) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
320 |
throw new FactoryConfigurationError( |
12005 | 321 |
"Provider for " + factoryId + " cannot be found", null); |
322 |
} |
|
323 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
27574
diff
changeset
|
324 |
dPrint(()->"loaded from fallback value: " + fallbackClassName); |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
325 |
return newInstance(type, fallbackClassName, cl, true); |
12005 | 326 |
} |
327 |
||
328 |
/* |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
329 |
* Try to find provider using the ServiceLoader API |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
330 |
* |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
331 |
* @param type Base class / Service interface of the factory to find. |
12005 | 332 |
* |
333 |
* @return instance of provider class if found or null |
|
334 |
*/ |
|
21997
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
335 |
private static <T> T findServiceProvider(final Class<T> type, final ClassLoader cl) { |
12005 | 336 |
try { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
337 |
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
338 |
@Override |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
339 |
public T run() { |
21997
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
340 |
final ServiceLoader<T> serviceLoader; |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
341 |
if (cl == null) { |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
342 |
//the current thread's context class loader |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
343 |
serviceLoader = ServiceLoader.load(type); |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
344 |
} else { |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
345 |
serviceLoader = ServiceLoader.load(type, cl); |
65d2e8e1d666
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents:
20965
diff
changeset
|
346 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
347 |
final Iterator<T> iterator = serviceLoader.iterator(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
348 |
if (iterator.hasNext()) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
349 |
return iterator.next(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
350 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
351 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
352 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
353 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
354 |
}); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
355 |
} catch(ServiceConfigurationError e) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
356 |
// 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
|
357 |
// FactoryConfigurationError - so we need to wrap the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
358 |
// ServiceConfigurationError in a RuntimeException. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
359 |
// The alternative would be to modify the logic in |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
360 |
// FactoryConfigurationError to allow setting a |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
361 |
// Throwable as the cause, but that could cause |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
362 |
// compatibility issues down the road. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
363 |
final RuntimeException x = new RuntimeException( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
364 |
"Provider for " + type + " cannot be created", e); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
365 |
final FactoryConfigurationError error = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
366 |
new FactoryConfigurationError(x, x.getMessage()); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
367 |
throw error; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
368 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
369 |
} |
12005 | 370 |
|
371 |
} |