author | joehw |
Wed, 04 Oct 2017 10:54:18 -0700 | |
changeset 47312 | d4f959806fe9 |
parent 47216 | 71c04702a3d5 |
child 48412 | d4412e380f6b |
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 |
||
28 |
import java.io.File; |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
29 |
import java.lang.reflect.InvocationTargetException; |
12005 | 30 |
import java.lang.reflect.Method; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
31 |
import java.lang.reflect.Modifier; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
32 |
import java.security.AccessControlContext; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
33 |
import java.security.AccessController; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
34 |
import java.security.PrivilegedAction; |
12005 | 35 |
import java.util.Properties; |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
36 |
import java.util.ServiceConfigurationError; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
37 |
import java.util.ServiceLoader; |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
38 |
import java.util.function.Supplier; |
47312 | 39 |
import jdk.xml.internal.SecuritySupport; |
12005 | 40 |
|
41 |
/** |
|
42 |
* Implementation of {@link SchemaFactory#newInstance(String)}. |
|
43 |
* |
|
44 |
* @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a> |
|
45 |
* @since 1.5 |
|
46 |
*/ |
|
47 |
class SchemaFactoryFinder { |
|
48 |
||
49 |
/** debug support code. */ |
|
50 |
private static boolean debug = false; |
|
47312 | 51 |
|
16953 | 52 |
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal"; |
12005 | 53 |
/** |
54 |
* <p>Cache properties for performance.</p> |
|
55 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
56 |
private static final Properties cacheProps = new Properties(); |
12005 | 57 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
58 |
/** |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
59 |
* <p>First time requires initialization overhead.</p> |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
60 |
*/ |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
61 |
private static volatile boolean firstTime = true; |
12005 | 62 |
|
63 |
static { |
|
64 |
// Use try/catch block to support applets |
|
65 |
try { |
|
47312 | 66 |
debug = SecuritySupport.getSystemProperty("jaxp.debug") != null; |
15545 | 67 |
} catch (Exception unused) { |
12005 | 68 |
debug = false; |
69 |
} |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* <p>Conditional debug printing.</p> |
|
74 |
* |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
75 |
* @param msgGen Supplier function that returns debug message |
12005 | 76 |
*/ |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
77 |
private static void debugPrintln(Supplier<String> msgGen) { |
12005 | 78 |
if (debug) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
79 |
System.err.println("JAXP: " + msgGen.get()); |
12005 | 80 |
} |
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* <p><code>ClassLoader</code> to use to find <code>SchemaFactory</code>.</p> |
|
85 |
*/ |
|
86 |
private final ClassLoader classLoader; |
|
87 |
||
88 |
/** |
|
89 |
* <p>Constructor that specifies <code>ClassLoader</code> to use |
|
90 |
* to find <code>SchemaFactory</code>.</p> |
|
91 |
* |
|
92 |
* @param loader |
|
93 |
* to be used to load resource, {@link SchemaFactory}, and |
|
94 |
* {@link SchemaFactoryLoader} implementations during |
|
95 |
* the resolution process. |
|
96 |
* If this parameter is null, the default system class loader |
|
97 |
* will be used. |
|
98 |
*/ |
|
99 |
public SchemaFactoryFinder(ClassLoader loader) { |
|
100 |
this.classLoader = loader; |
|
101 |
if( debug ) { |
|
102 |
debugDisplayClassLoader(); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
private void debugDisplayClassLoader() { |
|
107 |
try { |
|
47312 | 108 |
if( classLoader == SecuritySupport.getContextClassLoader() ) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
109 |
debugPrintln(()->"using thread context class loader ("+classLoader+") for search"); |
12005 | 110 |
return; |
111 |
} |
|
15545 | 112 |
} catch( Throwable unused ) { |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
113 |
// getContextClassLoader() undefined in JDK1.1 |
12005 | 114 |
} |
115 |
||
116 |
if( classLoader==ClassLoader.getSystemClassLoader() ) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
117 |
debugPrintln(()->"using system class loader ("+classLoader+") for search"); |
12005 | 118 |
return; |
119 |
} |
|
120 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
121 |
debugPrintln(()->"using class loader ("+classLoader+") for search"); |
12005 | 122 |
} |
123 |
||
124 |
/** |
|
125 |
* <p>Creates a new {@link SchemaFactory} object for the specified |
|
126 |
* schema language.</p> |
|
127 |
* |
|
128 |
* @param schemaLanguage |
|
129 |
* See {@link SchemaFactory Schema Language} table in <code>SchemaFactory</code> |
|
130 |
* for the list of available schema languages. |
|
131 |
* |
|
132 |
* @return <code>null</code> if the callee fails to create one. |
|
133 |
* |
|
134 |
* @throws NullPointerException |
|
135 |
* If the <code>schemaLanguage</code> parameter is null. |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
136 |
* @throws SchemaFactoryConfigurationError |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
137 |
* If a configuration error is encountered. |
12005 | 138 |
*/ |
139 |
public SchemaFactory newFactory(String schemaLanguage) { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
140 |
if(schemaLanguage==null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
141 |
throw new NullPointerException(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
142 |
} |
12005 | 143 |
SchemaFactory f = _newFactory(schemaLanguage); |
144 |
if (f != null) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
145 |
debugPrintln(()->"factory '" + f.getClass().getName() + "' was found for " + schemaLanguage); |
12005 | 146 |
} else { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
147 |
debugPrintln(()->"unable to find a factory for " + schemaLanguage); |
12005 | 148 |
} |
149 |
return f; |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* <p>Lookup a <code>SchemaFactory</code> for the given <code>schemaLanguage</code>.</p> |
|
154 |
* |
|
155 |
* @param schemaLanguage Schema language to lookup <code>SchemaFactory</code> for. |
|
156 |
* |
|
157 |
* @return <code>SchemaFactory</code> for the given <code>schemaLanguage</code>. |
|
158 |
*/ |
|
159 |
private SchemaFactory _newFactory(String schemaLanguage) { |
|
160 |
SchemaFactory sf; |
|
161 |
||
162 |
String propertyName = SERVICE_CLASS.getName() + ":" + schemaLanguage; |
|
163 |
||
164 |
// system property look up |
|
165 |
try { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
166 |
debugPrintln(()->"Looking up system property '"+propertyName+"'" ); |
47312 | 167 |
String r = SecuritySupport.getSystemProperty(propertyName); |
12005 | 168 |
if(r!=null) { |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
169 |
debugPrintln(()->"The value is '"+r+"'"); |
12458 | 170 |
sf = createInstance(r, true); |
12005 | 171 |
if(sf!=null) return sf; |
172 |
} else |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
173 |
debugPrintln(()->"The property is undefined."); |
12005 | 174 |
} catch( Throwable t ) { |
175 |
if( debug ) { |
|
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
176 |
debugPrintln(()->"failed to look up system property '"+propertyName+"'" ); |
12005 | 177 |
t.printStackTrace(); |
178 |
} |
|
179 |
} |
|
180 |
||
47312 | 181 |
String javah = SecuritySupport.getSystemProperty( "java.home" ); |
12005 | 182 |
String configFile = javah + File.separator + |
27574 | 183 |
"conf" + File.separator + "jaxp.properties"; |
12005 | 184 |
|
185 |
||
27574 | 186 |
// try to read from $java.home/conf/jaxp.properties |
12005 | 187 |
try { |
188 |
if(firstTime){ |
|
189 |
synchronized(cacheProps){ |
|
190 |
if(firstTime){ |
|
191 |
File f=new File( configFile ); |
|
192 |
firstTime = false; |
|
47312 | 193 |
if(SecuritySupport.doesFileExist(f)){ |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
194 |
debugPrintln(()->"Read properties file " + f); |
47312 | 195 |
cacheProps.load(SecuritySupport.getFileInputStream(f)); |
12005 | 196 |
} |
197 |
} |
|
198 |
} |
|
199 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
200 |
final String factoryClassName = cacheProps.getProperty(propertyName); |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
201 |
debugPrintln(()->"found " + factoryClassName + " in $java.home/conf/jaxp.properties"); |
12005 | 202 |
|
203 |
if (factoryClassName != null) { |
|
12458 | 204 |
sf = createInstance(factoryClassName, true); |
12005 | 205 |
if(sf != null){ |
206 |
return sf; |
|
207 |
} |
|
208 |
} |
|
209 |
} catch (Exception ex) { |
|
210 |
if (debug) { |
|
211 |
ex.printStackTrace(); |
|
212 |
} |
|
213 |
} |
|
214 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
215 |
// Try with ServiceLoader |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
216 |
final SchemaFactory factoryImpl = findServiceProvider(schemaLanguage); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
217 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
218 |
// The following assertion should always be true. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
219 |
// 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
|
220 |
// assert factoryImpl == null || factoryImpl.isSchemaLanguageSupported(schemaLanguage); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
221 |
|
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
222 |
if (factoryImpl != null) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
223 |
return factoryImpl; |
12005 | 224 |
} |
225 |
||
226 |
// platform default |
|
227 |
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
|
228 |
debugPrintln(()->"attempting to use the platform default XML Schema validator"); |
12458 | 229 |
return createInstance("com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory", true); |
12005 | 230 |
} |
231 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
232 |
debugPrintln(()->"all things were tried, but none was found. bailing out."); |
12005 | 233 |
return null; |
234 |
} |
|
235 |
||
236 |
/** <p>Create class using appropriate ClassLoader.</p> |
|
237 |
* |
|
238 |
* @param className Name of class to create. |
|
239 |
* @return Created class or <code>null</code>. |
|
240 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
241 |
private Class<?> createClass(String className) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
242 |
Class<?> clazz; |
16953 | 243 |
// make sure we have access to restricted packages |
244 |
boolean internal = false; |
|
245 |
if (System.getSecurityManager() != null) { |
|
246 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
247 |
internal = true; |
|
248 |
} |
|
249 |
} |
|
12005 | 250 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
251 |
try { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
252 |
if (classLoader != null && !internal) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
253 |
clazz = Class.forName(className, false, classLoader); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
254 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
255 |
clazz = Class.forName(className); |
12005 | 256 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
257 |
} catch (Throwable t) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
258 |
if(debug) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
259 |
t.printStackTrace(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
260 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
261 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
262 |
} |
12005 | 263 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
264 |
return clazz; |
12005 | 265 |
} |
266 |
||
267 |
/** |
|
268 |
* <p>Creates an instance of the specified and returns it.</p> |
|
269 |
* |
|
270 |
* @param className |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
271 |
* fully qualified class name to be instantiated. |
12005 | 272 |
* |
273 |
* @return null |
|
274 |
* if it fails. Error messages will be printed by this method. |
|
275 |
*/ |
|
276 |
SchemaFactory createInstance( String className ) { |
|
12458 | 277 |
return createInstance( className, false ); |
278 |
} |
|
279 |
||
280 |
SchemaFactory createInstance( String className, boolean useServicesMechanism ) { |
|
12005 | 281 |
SchemaFactory schemaFactory = null; |
282 |
||
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
283 |
debugPrintln(()->"createInstance(" + className + ")"); |
12005 | 284 |
|
285 |
// get Class from className |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
286 |
Class<?> clazz = createClass(className); |
12005 | 287 |
if (clazz == null) { |
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
288 |
debugPrintln(()->"failed to getClass(" + className + ")"); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
289 |
return null; |
12005 | 290 |
} |
42806
35843e3d5ef1
8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents:
31497
diff
changeset
|
291 |
debugPrintln(()->"loaded " + className + " from " + which(clazz)); |
12005 | 292 |
|
293 |
// instantiate Class as a SchemaFactory |
|
294 |
try { |
|
45853
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
295 |
if (!SchemaFactory.class.isAssignableFrom(clazz)) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
296 |
throw new ClassCastException(clazz.getName() |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
297 |
+ " cannot be cast to " + SchemaFactory.class); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
298 |
} |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
299 |
if (!useServicesMechanism) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
300 |
schemaFactory = newInstanceNoServiceLoader(clazz); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
301 |
} |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
302 |
if (schemaFactory == null) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
303 |
schemaFactory = (SchemaFactory) clazz.getConstructor().newInstance(); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
304 |
} |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
305 |
} catch (ClassCastException | IllegalAccessException | IllegalArgumentException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
306 |
InstantiationException | InvocationTargetException | NoSuchMethodException | |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
307 |
SecurityException ex) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
308 |
debugPrintln(()->"could not instantiate " + clazz.getName()); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
309 |
if (debug) { |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
310 |
ex.printStackTrace(); |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
311 |
} |
bfa06be36a17
8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents:
42806
diff
changeset
|
312 |
return null; |
12005 | 313 |
} |
314 |
||
315 |
return schemaFactory; |
|
316 |
} |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
317 |
|
12458 | 318 |
/** |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
319 |
* Try to construct using newXMLSchemaFactoryNoServiceLoader |
12458 | 320 |
* method if available. |
321 |
*/ |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
322 |
private static SchemaFactory newInstanceNoServiceLoader( |
12458 | 323 |
Class<?> providerClass |
324 |
) { |
|
325 |
// Retain maximum compatibility if no security manager. |
|
326 |
if (System.getSecurityManager() == null) { |
|
327 |
return null; |
|
328 |
} |
|
329 |
try { |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
330 |
final Method creationMethod = |
12458 | 331 |
providerClass.getDeclaredMethod( |
332 |
"newXMLSchemaFactoryNoServiceLoader" |
|
333 |
); |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
334 |
final int modifiers = creationMethod.getModifiers(); |
12005 | 335 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
336 |
// 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
|
337 |
if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
338 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
339 |
} |
12005 | 340 |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
341 |
// Only calls "newXMLSchemaFactoryNoServiceLoader" if it's |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
342 |
// declared to return an instance of SchemaFactory. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
343 |
final Class<?> returnType = creationMethod.getReturnType(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
344 |
if (SERVICE_CLASS.isAssignableFrom(returnType)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
345 |
return SERVICE_CLASS.cast(creationMethod.invoke(null, (Object[])null)); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
346 |
} else { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
347 |
// Should not happen since |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
348 |
// XMLSchemaFactory.newXMLSchemaFactoryNoServiceLoader is |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
349 |
// declared to return XMLSchemaFactory. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
350 |
throw new ClassCastException(returnType |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
351 |
+ " cannot be cast to " + SERVICE_CLASS); |
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 |
} catch(ClassCastException e) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
354 |
throw new SchemaFactoryConfigurationError(e.getMessage(), e); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
355 |
} catch (NoSuchMethodException exc) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
356 |
return null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
357 |
} catch (Exception exc) { |
12005 | 358 |
return null; |
359 |
} |
|
360 |
} |
|
361 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
362 |
// Call isSchemaLanguageSupported with initial context. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
363 |
private boolean isSchemaLanguageSupportedBy(final SchemaFactory factory, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
364 |
final String schemaLanguage, |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
365 |
AccessControlContext acc) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
366 |
return AccessController.doPrivileged(new PrivilegedAction<Boolean>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
367 |
public Boolean run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
368 |
return factory.isSchemaLanguageSupported(schemaLanguage); |
12005 | 369 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
370 |
}, acc); |
12005 | 371 |
} |
372 |
||
373 |
/** |
|
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
374 |
* 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
|
375 |
* given schema language using the ServiceLoader. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
376 |
* |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
377 |
* @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
|
378 |
* @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
|
379 |
* if none is found. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
380 |
* @throws SchemaFactoryConfigurationError if a configuration error is found. |
12005 | 381 |
*/ |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
382 |
private SchemaFactory findServiceProvider(final String schemaLanguage) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
383 |
assert schemaLanguage != null; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
384 |
// store current context. |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
385 |
final AccessControlContext acc = AccessController.getContext(); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
386 |
try { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
387 |
return AccessController.doPrivileged(new PrivilegedAction<SchemaFactory>() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
388 |
public SchemaFactory run() { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
389 |
final ServiceLoader<SchemaFactory> loader = |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
390 |
ServiceLoader.load(SERVICE_CLASS); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
391 |
for (SchemaFactory factory : loader) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
392 |
// restore initial context to call |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
393 |
// factory.isSchemaLanguageSupported |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
394 |
if (isSchemaLanguageSupportedBy(factory, schemaLanguage, acc)) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
395 |
return factory; |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
396 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
397 |
} |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
398 |
return null; // no factory found. |
12005 | 399 |
} |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
400 |
}); |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
401 |
} catch (ServiceConfigurationError error) { |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
402 |
throw new SchemaFactoryConfigurationError( |
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
403 |
"Provider for " + SERVICE_CLASS + " cannot be created", error); |
12005 | 404 |
} |
405 |
} |
|
406 |
||
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
407 |
private static final Class<SchemaFactory> SERVICE_CLASS = SchemaFactory.class; |
12005 | 408 |
|
409 |
||
31287
d6581a7c221f
8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents:
27574
diff
changeset
|
410 |
// Used for debugging purposes |
17264
3aff554ad461
8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents:
16953
diff
changeset
|
411 |
private static String which( Class<?> clazz ) { |
47312 | 412 |
return SecuritySupport.getClassSource(clazz); |
12005 | 413 |
} |
414 |
} |