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