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