Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/validation/SchemaFactoryFinder.java
40948 views
1
/*
2
* Copyright (c) 2003, 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.validation;
27
28
import com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory;
29
import java.io.File;
30
import java.lang.reflect.InvocationTargetException;
31
import java.lang.reflect.Method;
32
import java.lang.reflect.Modifier;
33
import java.security.AccessControlContext;
34
import java.security.AccessController;
35
import java.security.PrivilegedAction;
36
import java.util.Properties;
37
import java.util.ServiceConfigurationError;
38
import java.util.ServiceLoader;
39
import java.util.function.Supplier;
40
import jdk.xml.internal.SecuritySupport;
41
42
/**
43
* Implementation of {@link SchemaFactory#newInstance(String)}.
44
*
45
* @author Kohsuke Kawaguchi
46
* @since 1.5
47
*/
48
class SchemaFactoryFinder {
49
50
/** debug support code. */
51
private static boolean debug = false;
52
53
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
54
/**
55
* <p>Cache properties for performance.</p>
56
*/
57
private static final Properties cacheProps = new Properties();
58
59
/**
60
* <p>First time requires initialization overhead.</p>
61
*/
62
private static volatile boolean firstTime = true;
63
64
static {
65
// Use try/catch block to support applets
66
try {
67
debug = SecuritySupport.getSystemProperty("jaxp.debug") != null;
68
} catch (Exception unused) {
69
debug = false;
70
}
71
}
72
73
/**
74
* <p>Conditional debug printing.</p>
75
*
76
* @param msgGen Supplier function that returns debug message
77
*/
78
private static void debugPrintln(Supplier<String> msgGen) {
79
if (debug) {
80
System.err.println("JAXP: " + msgGen.get());
81
}
82
}
83
84
/**
85
* <p><code>ClassLoader</code> to use to find <code>SchemaFactory</code>.</p>
86
*/
87
private final ClassLoader classLoader;
88
89
/**
90
* <p>Constructor that specifies <code>ClassLoader</code> to use
91
* to find <code>SchemaFactory</code>.</p>
92
*
93
* @param loader
94
* to be used to load resource, {@link SchemaFactory}, and
95
* {@link SchemaFactoryLoader} implementations during
96
* the resolution process.
97
* If this parameter is null, the default system class loader
98
* will be used.
99
*/
100
public SchemaFactoryFinder(ClassLoader loader) {
101
this.classLoader = loader;
102
if( debug ) {
103
debugDisplayClassLoader();
104
}
105
}
106
107
private void debugDisplayClassLoader() {
108
try {
109
if( classLoader == SecuritySupport.getContextClassLoader() ) {
110
debugPrintln(()->"using thread context class loader ("+classLoader+") for search");
111
return;
112
}
113
} catch( Throwable unused ) {
114
// getContextClassLoader() undefined in JDK1.1
115
}
116
117
if( classLoader==ClassLoader.getSystemClassLoader() ) {
118
debugPrintln(()->"using system class loader ("+classLoader+") for search");
119
return;
120
}
121
122
debugPrintln(()->"using class loader ("+classLoader+") for search");
123
}
124
125
/**
126
* <p>Creates a new {@link SchemaFactory} object for the specified
127
* schema language.</p>
128
*
129
* @param schemaLanguage
130
* See {@link SchemaFactory Schema Language} table in <code>SchemaFactory</code>
131
* for the list of available schema languages.
132
*
133
* @return <code>null</code> if the callee fails to create one.
134
*
135
* @throws NullPointerException
136
* If the <code>schemaLanguage</code> parameter is null.
137
* @throws SchemaFactoryConfigurationError
138
* If a configuration error is encountered.
139
*/
140
public SchemaFactory newFactory(String schemaLanguage) {
141
if(schemaLanguage==null) {
142
throw new NullPointerException();
143
}
144
SchemaFactory f = _newFactory(schemaLanguage);
145
if (f != null) {
146
debugPrintln(()->"factory '" + f.getClass().getName() + "' was found for " + schemaLanguage);
147
} else {
148
debugPrintln(()->"unable to find a factory for " + schemaLanguage);
149
}
150
return f;
151
}
152
153
/**
154
* <p>Lookup a <code>SchemaFactory</code> for the given <code>schemaLanguage</code>.</p>
155
*
156
* @param schemaLanguage Schema language to lookup <code>SchemaFactory</code> for.
157
*
158
* @return <code>SchemaFactory</code> for the given <code>schemaLanguage</code>.
159
*/
160
private SchemaFactory _newFactory(String schemaLanguage) {
161
SchemaFactory sf;
162
163
String propertyName = SERVICE_CLASS.getName() + ":" + schemaLanguage;
164
165
// system property look up
166
try {
167
debugPrintln(()->"Looking up system property '"+propertyName+"'" );
168
String r = SecuritySupport.getSystemProperty(propertyName);
169
if(r!=null) {
170
debugPrintln(()->"The value is '"+r+"'");
171
sf = createInstance(r);
172
if(sf!=null) return sf;
173
} else
174
debugPrintln(()->"The property is undefined.");
175
} catch( Throwable t ) {
176
if( debug ) {
177
debugPrintln(()->"failed to look up system property '"+propertyName+"'" );
178
t.printStackTrace();
179
}
180
}
181
182
String javah = SecuritySupport.getSystemProperty( "java.home" );
183
String configFile = javah + File.separator +
184
"conf" + File.separator + "jaxp.properties";
185
186
187
// try to read from $java.home/conf/jaxp.properties
188
try {
189
if(firstTime){
190
synchronized(cacheProps){
191
if(firstTime){
192
File f=new File( configFile );
193
firstTime = false;
194
if(SecuritySupport.doesFileExist(f)){
195
debugPrintln(()->"Read properties file " + f);
196
cacheProps.load(SecuritySupport.getFileInputStream(f));
197
}
198
}
199
}
200
}
201
final String factoryClassName = cacheProps.getProperty(propertyName);
202
debugPrintln(()->"found " + factoryClassName + " in $java.home/conf/jaxp.properties");
203
204
if (factoryClassName != null) {
205
sf = createInstance(factoryClassName);
206
if(sf != null){
207
return sf;
208
}
209
}
210
} catch (Exception ex) {
211
if (debug) {
212
ex.printStackTrace();
213
}
214
}
215
216
// Try with ServiceLoader
217
final SchemaFactory factoryImpl = findServiceProvider(schemaLanguage);
218
219
// The following assertion should always be true.
220
// Uncomment it, recompile, and run with -ea in case of doubts:
221
// assert factoryImpl == null || factoryImpl.isSchemaLanguageSupported(schemaLanguage);
222
223
if (factoryImpl != null) {
224
return factoryImpl;
225
}
226
227
// platform default
228
if(schemaLanguage.equals("http://www.w3.org/2001/XMLSchema")) {
229
debugPrintln(()->"attempting to use the platform default XML Schema validator");
230
return new XMLSchemaFactory();
231
}
232
233
debugPrintln(()->"all things were tried, but none was found. bailing out.");
234
return null;
235
}
236
237
/** <p>Create class using appropriate ClassLoader.</p>
238
*
239
* @param className Name of class to create.
240
* @return Created class or <code>null</code>.
241
*/
242
@SuppressWarnings("removal")
243
private Class<?> createClass(String className) {
244
Class<?> clazz;
245
// make sure we have access to restricted packages
246
boolean internal = false;
247
if (System.getSecurityManager() != null) {
248
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
249
internal = true;
250
}
251
}
252
253
try {
254
if (classLoader != null && !internal) {
255
clazz = Class.forName(className, false, classLoader);
256
} else {
257
clazz = Class.forName(className);
258
}
259
} catch (Throwable t) {
260
if(debug) {
261
t.printStackTrace();
262
}
263
return null;
264
}
265
266
return clazz;
267
}
268
269
/**
270
* <p>Creates an instance of the specified and returns it.</p>
271
*
272
* @param className
273
* fully qualified class name to be instantiated.
274
*
275
* @return null
276
* if it fails. Error messages will be printed by this method.
277
*/
278
SchemaFactory createInstance(String className) {
279
SchemaFactory schemaFactory = null;
280
281
debugPrintln(()->"createInstance(" + className + ")");
282
283
// get Class from className
284
Class<?> clazz = createClass(className);
285
if (clazz == null) {
286
debugPrintln(()->"failed to getClass(" + className + ")");
287
return null;
288
}
289
debugPrintln(()->"loaded " + className + " from " + which(clazz));
290
291
// instantiate Class as a SchemaFactory
292
try {
293
if (!SchemaFactory.class.isAssignableFrom(clazz)) {
294
throw new ClassCastException(clazz.getName()
295
+ " cannot be cast to " + SchemaFactory.class);
296
}
297
schemaFactory = (SchemaFactory) clazz.getConstructor().newInstance();
298
} catch (ClassCastException | IllegalAccessException | IllegalArgumentException |
299
InstantiationException | InvocationTargetException | NoSuchMethodException |
300
SecurityException ex) {
301
debugPrintln(()->"could not instantiate " + clazz.getName());
302
if (debug) {
303
ex.printStackTrace();
304
}
305
return null;
306
}
307
308
return schemaFactory;
309
}
310
311
// Call isSchemaLanguageSupported with initial context.
312
@SuppressWarnings("removal")
313
private boolean isSchemaLanguageSupportedBy(final SchemaFactory factory,
314
final String schemaLanguage,
315
AccessControlContext acc) {
316
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
317
public Boolean run() {
318
return factory.isSchemaLanguageSupported(schemaLanguage);
319
}
320
}, acc);
321
}
322
323
/**
324
* Finds a service provider subclass of SchemaFactory that supports the
325
* given schema language using the ServiceLoader.
326
*
327
* @param schemaLanguage The schema language for which we seek a factory.
328
* @return A SchemaFactory supporting the specified schema language, or null
329
* if none is found.
330
* @throws SchemaFactoryConfigurationError if a configuration error is found.
331
*/
332
@SuppressWarnings("removal")
333
private SchemaFactory findServiceProvider(final String schemaLanguage) {
334
assert schemaLanguage != null;
335
// store current context.
336
final AccessControlContext acc = AccessController.getContext();
337
try {
338
return AccessController.doPrivileged(new PrivilegedAction<SchemaFactory>() {
339
public SchemaFactory run() {
340
final ServiceLoader<SchemaFactory> loader =
341
ServiceLoader.load(SERVICE_CLASS);
342
for (SchemaFactory factory : loader) {
343
// restore initial context to call
344
// factory.isSchemaLanguageSupported
345
if (isSchemaLanguageSupportedBy(factory, schemaLanguage, acc)) {
346
return factory;
347
}
348
}
349
return null; // no factory found.
350
}
351
});
352
} catch (ServiceConfigurationError error) {
353
throw new SchemaFactoryConfigurationError(
354
"Provider for " + SERVICE_CLASS + " cannot be created", error);
355
}
356
}
357
358
private static final Class<SchemaFactory> SERVICE_CLASS = SchemaFactory.class;
359
360
361
// Used for debugging purposes
362
private static String which( Class<?> clazz ) {
363
return SecuritySupport.getClassSource(clazz);
364
}
365
}
366
367