Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/stream/FactoryFinder.java
40948 views
1
/*
2
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
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;
29
import java.security.AccessController;
30
import java.security.PrivilegedAction;
31
import java.util.Iterator;
32
import java.util.Properties;
33
import java.util.ServiceConfigurationError;
34
import java.util.ServiceLoader;
35
import java.util.function.Supplier;
36
import jdk.xml.internal.SecuritySupport;
37
38
/**
39
* <p>Implements pluggable streams.</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
45
*/
46
class FactoryFinder {
47
// Check we have access to package.
48
private static final String DEFAULT_PACKAGE = "com.sun.xml.internal.";
49
50
/**
51
* Internal debug flag.
52
*/
53
private static boolean debug = false;
54
55
/**
56
* Cache for properties in java.home/conf/jaxp.properties
57
*/
58
final private static Properties cacheProps = new Properties();
59
60
/**
61
* Flag indicating if properties from java.home/conf/jaxp.properties
62
* have been cached.
63
*/
64
private static volatile boolean firstTime = true;
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 {
71
String val = SecuritySupport.getSystemProperty("jaxp.debug");
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
80
private static void dPrint(Supplier<String> msgGen) {
81
if (debug) {
82
System.err.println("JAXP: " + msgGen.get());
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.
94
*
95
* Use bootstrap classLoader if cl = null and useBSClsLoader is true
96
*/
97
static private Class<?> getProviderClass(String className, ClassLoader cl,
98
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
99
{
100
try {
101
if (cl == null) {
102
if (useBSClsLoader) {
103
return Class.forName(className, false, FactoryFinder.class.getClassLoader());
104
} else {
105
cl = SecuritySupport.getContextClassLoader();
106
if (cl == null) {
107
throw new ClassNotFoundException();
108
}
109
else {
110
return Class.forName(className, false, cl);
111
}
112
}
113
}
114
else {
115
return Class.forName(className, false, cl);
116
}
117
}
118
catch (ClassNotFoundException e1) {
119
if (doFallback) {
120
// Use current class loader - should always be bootstrap CL
121
return Class.forName(className, false, FactoryFinder.class.getClassLoader());
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
*
133
* @param type Base class / Service interface of the factory to
134
* instantiate.
135
*
136
* @param className Name of the concrete class corresponding to the
137
* service provider
138
*
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.
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
*/
145
static <T> T newInstance(Class<T> type, String className, ClassLoader cl, boolean doFallback)
146
throws FactoryConfigurationError
147
{
148
return newInstance(type, className, cl, doFallback, false);
149
}
150
151
/**
152
* Create an instance of a class. Delegates to method
153
* <code>getProviderClass()</code> in order to load the class.
154
*
155
* @param type Base class / Service interface of the factory to
156
* instantiate.
157
*
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
*/
170
@SuppressWarnings("removal")
171
static <T> T newInstance(Class<T> type, String className, ClassLoader cl,
172
boolean doFallback, boolean useBSClsLoader)
173
throws FactoryConfigurationError
174
{
175
assert type != null;
176
177
// make sure we have access to restricted packages
178
if (System.getSecurityManager() != null) {
179
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
180
cl = null;
181
useBSClsLoader = true;
182
}
183
}
184
185
try {
186
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
187
if (!type.isAssignableFrom(providerClass)) {
188
throw new ClassCastException(className + " cannot be cast to " + type.getName());
189
}
190
Object instance = providerClass.getConstructor().newInstance();
191
final ClassLoader clD = cl;
192
dPrint(()->"created new instance of " + providerClass +
193
" using ClassLoader: " + clD);
194
return type.cast(instance);
195
}
196
catch (ClassNotFoundException x) {
197
throw new FactoryConfigurationError(
198
"Provider " + className + " not found", x);
199
}
200
catch (Exception x) {
201
throw new FactoryConfigurationError(
202
"Provider " + className + " could not be instantiated: " + x,
203
x);
204
}
205
}
206
207
/**
208
* Finds the implementation Class object in the specified order.
209
*
210
* @return Class object of factory, never null
211
*
212
* @param type Base class / Service interface of the
213
* factory to find.
214
*
215
* @param fallbackClassName Implementation class name, if nothing else
216
* is found. Use null to mean no fallback.
217
*
218
* Package private so this code can be shared.
219
*/
220
static <T> T find(Class<T> type, String fallbackClassName)
221
throws FactoryConfigurationError
222
{
223
return find(type, type.getName(), null, fallbackClassName);
224
}
225
226
/**
227
* Finds the implementation Class object in the specified order. Main
228
* entry point.
229
* @return Class object of factory, never null
230
*
231
* @param type Base class / Service interface of the
232
* factory to find.
233
*
234
* @param factoryId Name of the factory to find, same as
235
* a property name
236
*
237
* @param cl ClassLoader to be used to load the class, null means to use
238
* the bootstrap ClassLoader
239
*
240
* @param fallbackClassName Implementation class name, if nothing else
241
* is found. Use null to mean no fallback.
242
*
243
* Package private so this code can be shared.
244
*/
245
static <T> T find(Class<T> type, String factoryId, ClassLoader cl, String fallbackClassName)
246
throws FactoryConfigurationError
247
{
248
dPrint(()->"find factoryId =" + factoryId);
249
250
// Use the system property first
251
try {
252
253
final String systemProp;
254
if (type.getName().equals(factoryId)) {
255
systemProp = SecuritySupport.getSystemProperty(factoryId);
256
} else {
257
systemProp = System.getProperty(factoryId);
258
}
259
if (systemProp != null) {
260
dPrint(()->"found system property, value=" + systemProp);
261
return newInstance(type, systemProp, cl, true);
262
}
263
}
264
catch (SecurityException se) {
265
throw new FactoryConfigurationError(
266
"Failed to read factoryId '" + factoryId + "'", se);
267
}
268
269
// Try read $java.home/conf/stax.properties followed by
270
// $java.home/conf/jaxp.properties if former not present
271
String configFile = null;
272
try {
273
if (firstTime) {
274
synchronized (cacheProps) {
275
if (firstTime) {
276
configFile = SecuritySupport.getSystemProperty("java.home") + File.separator +
277
"conf" + File.separator + "stax.properties";
278
final File fStax = new File(configFile);
279
firstTime = false;
280
if (SecuritySupport.doesFileExist(fStax)) {
281
dPrint(()->"Read properties file "+fStax);
282
cacheProps.load(SecuritySupport.getFileInputStream(fStax));
283
}
284
else {
285
configFile = SecuritySupport.getSystemProperty("java.home") + File.separator +
286
"conf" + File.separator + "jaxp.properties";
287
final File fJaxp = new File(configFile);
288
if (SecuritySupport.doesFileExist(fJaxp)) {
289
dPrint(()->"Read properties file "+fJaxp);
290
cacheProps.load(SecuritySupport.getFileInputStream(fJaxp));
291
}
292
}
293
}
294
}
295
}
296
final String factoryClassName = cacheProps.getProperty(factoryId);
297
298
if (factoryClassName != null) {
299
final String foundIn = configFile;
300
dPrint(()->"found in " + foundIn + " value=" + factoryClassName);
301
return newInstance(type, factoryClassName, cl, true);
302
}
303
}
304
catch (Exception ex) {
305
if (debug) ex.printStackTrace();
306
}
307
308
if (type.getName().equals(factoryId)) {
309
// Try Jar Service Provider Mechanism
310
final T provider = findServiceProvider(type, cl);
311
if (provider != null) {
312
return provider;
313
}
314
} else {
315
// We're in the case where a 'custom' factoryId was provided,
316
// and in every case where that happens, we expect that
317
// fallbackClassName will be null.
318
assert fallbackClassName == null;
319
}
320
if (fallbackClassName == null) {
321
throw new FactoryConfigurationError(
322
"Provider for " + factoryId + " cannot be found", null);
323
}
324
325
dPrint(()->"loaded from fallback value: " + fallbackClassName);
326
return newInstance(type, fallbackClassName, cl, true);
327
}
328
329
/*
330
* Try to find provider using the ServiceLoader API
331
*
332
* @param type Base class / Service interface of the factory to find.
333
*
334
* @return instance of provider class if found or null
335
*/
336
@SuppressWarnings("removal")
337
private static <T> T findServiceProvider(final Class<T> type, final ClassLoader cl) {
338
try {
339
return AccessController.doPrivileged(new PrivilegedAction<T>() {
340
@Override
341
public T run() {
342
final ServiceLoader<T> serviceLoader;
343
if (cl == null) {
344
//the current thread's context class loader
345
serviceLoader = ServiceLoader.load(type);
346
} else {
347
serviceLoader = ServiceLoader.load(type, cl);
348
}
349
final Iterator<T> iterator = serviceLoader.iterator();
350
if (iterator.hasNext()) {
351
return iterator.next();
352
} else {
353
return null;
354
}
355
}
356
});
357
} catch(ServiceConfigurationError e) {
358
// It is not possible to wrap an error directly in
359
// FactoryConfigurationError - so we need to wrap the
360
// ServiceConfigurationError in a RuntimeException.
361
// The alternative would be to modify the logic in
362
// FactoryConfigurationError to allow setting a
363
// Throwable as the cause, but that could cause
364
// compatibility issues down the road.
365
final RuntimeException x = new RuntimeException(
366
"Provider for " + type + " cannot be created", e);
367
final FactoryConfigurationError error =
368
new FactoryConfigurationError(x, x.getMessage());
369
throw error;
370
}
371
}
372
373
}
374
375