author | egahlin |
Fri, 27 Sep 2019 13:01:55 +0200 | |
branch | JEP-349-branch |
changeset 58370 | 9b4841a568cb |
parent 48577 | 8dff65f1d611 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
2 |
* Copyright (c) 2003, 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.validation; |
|
27 |
||
48577 | 28 |
import com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory; |
12005 | 29 |
import java.io.File; |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
30 |
import java.lang.reflect.InvocationTargetException; |
12005 | 31 |
import java.lang.reflect.Method; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
32 |
import java.lang.reflect.Modifier; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
33 |
import java.security.AccessControlContext; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
34 |
import java.security.AccessController; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
35 |
import java.security.PrivilegedAction; |
12005 | 36 |
import java.util.Properties; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
37 |
import java.util.ServiceConfigurationError; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
38 |
import java.util.ServiceLoader; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
39 |
import java.util.function.Supplier; |
47312 | 40 |
import jdk.xml.internal.SecuritySupport; |
12005 | 41 |
|
42 |
/** |
|
43 |
* Implementation of {@link SchemaFactory#newInstance(String)}. |
|
44 |
* |
|
48412 | 45 |
* @author Kohsuke Kawaguchi |
12005 | 46 |
* @since 1.5 |
47 |
*/ |
|
48 |
class SchemaFactoryFinder { |
|
49 |
||
50 |
/** debug support code. */ |
|
51 |
private static boolean debug = false; |
|
47312 | 52 |
|
16953 | 53 |
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal"; |
12005 | 54 |
/** |
55 |
* <p>Cache properties for performance.</p> |
|
56 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
57 |
private static final Properties cacheProps = new Properties(); |
12005 | 58 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
59 |
/** |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
60 |
* <p>First time requires initialization overhead.</p> |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
61 |
*/ |
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 |
static { |
|
65 |
// Use try/catch block to support applets |
|
66 |
try { |
|
47312 | 67 |
debug = SecuritySupport.getSystemProperty("jaxp.debug") != null; |
15545 | 68 |
} catch (Exception unused) { |
12005 | 69 |
debug = false; |
70 |
} |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* <p>Conditional debug printing.</p> |
|
75 |
* |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
76 |
* @param msgGen Supplier function that returns debug message |
12005 | 77 |
*/ |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
78 |
private static void debugPrintln(Supplier<String> msgGen) { |
12005 | 79 |
if (debug) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
80 |
System.err.println("JAXP: " + msgGen.get()); |
12005 | 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 { |
|
47312 | 109 |
if( classLoader == SecuritySupport.getContextClassLoader() ) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
110 |
debugPrintln(()->"using thread context class loader ("+classLoader+") for search"); |
12005 | 111 |
return; |
112 |
} |
|
15545 | 113 |
} catch( Throwable unused ) { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
114 |
// getContextClassLoader() undefined in JDK1.1 |
12005 | 115 |
} |
116 |
||
117 |
if( classLoader==ClassLoader.getSystemClassLoader() ) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
118 |
debugPrintln(()->"using system class loader ("+classLoader+") for search"); |
12005 | 119 |
return; |
120 |
} |
|
121 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
122 |
debugPrintln(()->"using class loader ("+classLoader+") for search"); |
12005 | 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. |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
137 |
* @throws SchemaFactoryConfigurationError |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
138 |
* If a configuration error is encountered. |
12005 | 139 |
*/ |
140 |
public SchemaFactory newFactory(String schemaLanguage) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
141 |
if(schemaLanguage==null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
142 |
throw new NullPointerException(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
143 |
} |
12005 | 144 |
SchemaFactory f = _newFactory(schemaLanguage); |
145 |
if (f != null) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
146 |
debugPrintln(()->"factory '" + f.getClass().getName() + "' was found for " + schemaLanguage); |
12005 | 147 |
} else { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
148 |
debugPrintln(()->"unable to find a factory for " + schemaLanguage); |
12005 | 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 { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
167 |
debugPrintln(()->"Looking up system property '"+propertyName+"'" ); |
47312 | 168 |
String r = SecuritySupport.getSystemProperty(propertyName); |
12005 | 169 |
if(r!=null) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
170 |
debugPrintln(()->"The value is '"+r+"'"); |
48577 | 171 |
sf = createInstance(r); |
12005 | 172 |
if(sf!=null) return sf; |
173 |
} else |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
174 |
debugPrintln(()->"The property is undefined."); |
12005 | 175 |
} catch( Throwable t ) { |
176 |
if( debug ) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
177 |
debugPrintln(()->"failed to look up system property '"+propertyName+"'" ); |
12005 | 178 |
t.printStackTrace(); |
179 |
} |
|
180 |
} |
|
181 |
||
47312 | 182 |
String javah = SecuritySupport.getSystemProperty( "java.home" ); |
12005 | 183 |
String configFile = javah + File.separator + |
27574 | 184 |
"conf" + File.separator + "jaxp.properties"; |
12005 | 185 |
|
186 |
||
27574 | 187 |
// try to read from $java.home/conf/jaxp.properties |
12005 | 188 |
try { |
189 |
if(firstTime){ |
|
190 |
synchronized(cacheProps){ |
|
191 |
if(firstTime){ |
|
192 |
File f=new File( configFile ); |
|
193 |
firstTime = false; |
|
47312 | 194 |
if(SecuritySupport.doesFileExist(f)){ |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
195 |
debugPrintln(()->"Read properties file " + f); |
47312 | 196 |
cacheProps.load(SecuritySupport.getFileInputStream(f)); |
12005 | 197 |
} |
198 |
} |
|
199 |
} |
|
200 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
201 |
final String factoryClassName = cacheProps.getProperty(propertyName); |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
202 |
debugPrintln(()->"found " + factoryClassName + " in $java.home/conf/jaxp.properties"); |
12005 | 203 |
|
204 |
if (factoryClassName != null) { |
|
48577 | 205 |
sf = createInstance(factoryClassName); |
12005 | 206 |
if(sf != null){ |
207 |
return sf; |
|
208 |
} |
|
209 |
} |
|
210 |
} catch (Exception ex) { |
|
211 |
if (debug) { |
|
212 |
ex.printStackTrace(); |
|
213 |
} |
|
214 |
} |
|
215 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
216 |
// Try with ServiceLoader |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
217 |
final SchemaFactory factoryImpl = findServiceProvider(schemaLanguage); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
218 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
219 |
// The following assertion should always be true. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
220 |
// Uncomment it, recompile, and run with -ea in case of doubts: |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
221 |
// assert factoryImpl == null || factoryImpl.isSchemaLanguageSupported(schemaLanguage); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
222 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
223 |
if (factoryImpl != null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
224 |
return factoryImpl; |
12005 | 225 |
} |
226 |
||
227 |
// platform default |
|
228 |
if(schemaLanguage.equals("http://www.w3.org/2001/XMLSchema")) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
229 |
debugPrintln(()->"attempting to use the platform default XML Schema validator"); |
48577 | 230 |
return new XMLSchemaFactory(); |
12005 | 231 |
} |
232 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
233 |
debugPrintln(()->"all things were tried, but none was found. bailing out."); |
12005 | 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 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
242 |
private Class<?> createClass(String className) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
243 |
Class<?> clazz; |
16953 | 244 |
// make sure we have access to restricted packages |
245 |
boolean internal = false; |
|
246 |
if (System.getSecurityManager() != null) { |
|
247 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
248 |
internal = true; |
|
249 |
} |
|
250 |
} |
|
12005 | 251 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
252 |
try { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
253 |
if (classLoader != null && !internal) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
254 |
clazz = Class.forName(className, false, classLoader); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
255 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
256 |
clazz = Class.forName(className); |
12005 | 257 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
258 |
} catch (Throwable t) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
259 |
if(debug) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
260 |
t.printStackTrace(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
261 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
262 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
263 |
} |
12005 | 264 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
265 |
return clazz; |
12005 | 266 |
} |
267 |
||
268 |
/** |
|
269 |
* <p>Creates an instance of the specified and returns it.</p> |
|
270 |
* |
|
271 |
* @param className |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
272 |
* fully qualified class name to be instantiated. |
12005 | 273 |
* |
274 |
* @return null |
|
275 |
* if it fails. Error messages will be printed by this method. |
|
276 |
*/ |
|
48577 | 277 |
SchemaFactory createInstance(String className) { |
12005 | 278 |
SchemaFactory schemaFactory = null; |
279 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
280 |
debugPrintln(()->"createInstance(" + className + ")"); |
12005 | 281 |
|
282 |
// get Class from className |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
283 |
Class<?> clazz = createClass(className); |
12005 | 284 |
if (clazz == null) { |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
285 |
debugPrintln(()->"failed to getClass(" + className + ")"); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
286 |
return null; |
12005 | 287 |
} |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
288 |
debugPrintln(()->"loaded " + className + " from " + which(clazz)); |
12005 | 289 |
|
290 |
// instantiate Class as a SchemaFactory |
|
291 |
try { |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
292 |
if (!SchemaFactory.class.isAssignableFrom(clazz)) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
293 |
throw new ClassCastException(clazz.getName() |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
294 |
+ " cannot be cast to " + SchemaFactory.class); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
295 |
} |
48577 | 296 |
schemaFactory = (SchemaFactory) clazz.getConstructor().newInstance(); |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
297 |
} catch (ClassCastException | IllegalAccessException | IllegalArgumentException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
298 |
InstantiationException | InvocationTargetException | NoSuchMethodException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
299 |
SecurityException ex) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
300 |
debugPrintln(()->"could not instantiate " + clazz.getName()); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
301 |
if (debug) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
302 |
ex.printStackTrace(); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
303 |
} |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
304 |
return null; |
12005 | 305 |
} |
306 |
||
307 |
return schemaFactory; |
|
308 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
309 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
310 |
// Call isSchemaLanguageSupported with initial context. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
311 |
private boolean isSchemaLanguageSupportedBy(final SchemaFactory factory, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
312 |
final String schemaLanguage, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
313 |
AccessControlContext acc) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
314 |
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
315 |
public Boolean run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
316 |
return factory.isSchemaLanguageSupported(schemaLanguage); |
12005 | 317 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
318 |
}, acc); |
12005 | 319 |
} |
320 |
||
321 |
/** |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
322 |
* Finds a service provider subclass of SchemaFactory that supports the |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
323 |
* given schema language using the ServiceLoader. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
324 |
* |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
325 |
* @param schemaLanguage The schema language for which we seek a factory. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
326 |
* @return A SchemaFactory supporting the specified schema language, or null |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
327 |
* if none is found. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
328 |
* @throws SchemaFactoryConfigurationError if a configuration error is found. |
12005 | 329 |
*/ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
330 |
private SchemaFactory findServiceProvider(final String schemaLanguage) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
331 |
assert schemaLanguage != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
332 |
// store current context. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
333 |
final AccessControlContext acc = AccessController.getContext(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
334 |
try { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
335 |
return AccessController.doPrivileged(new PrivilegedAction<SchemaFactory>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
336 |
public SchemaFactory run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
337 |
final ServiceLoader<SchemaFactory> loader = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
338 |
ServiceLoader.load(SERVICE_CLASS); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
339 |
for (SchemaFactory factory : loader) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
340 |
// restore initial context to call |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
341 |
// factory.isSchemaLanguageSupported |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
342 |
if (isSchemaLanguageSupportedBy(factory, schemaLanguage, acc)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
343 |
return factory; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
344 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
345 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
346 |
return null; // no factory found. |
12005 | 347 |
} |
17264
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 |
} catch (ServiceConfigurationError error) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
350 |
throw new SchemaFactoryConfigurationError( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
351 |
"Provider for " + SERVICE_CLASS + " cannot be created", error); |
12005 | 352 |
} |
353 |
} |
|
354 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
355 |
private static final Class<SchemaFactory> SERVICE_CLASS = SchemaFactory.class; |
12005 | 356 |
|
357 |
||
31287
d6581a7c221f
8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents:
27574
diff
changeset
|
358 |
// Used for debugging purposes |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
359 |
private static String which( Class<?> clazz ) { |
47312 | 360 |
return SecuritySupport.getClassSource(clazz); |
12005 | 361 |
} |
362 |
} |