12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.xml.parsers;
|
|
27 |
|
12458
|
28 |
import java.io.BufferedReader;
|
12005
|
29 |
import java.io.File;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.io.InputStreamReader;
|
12458
|
33 |
import java.util.Properties;
|
12005
|
34 |
|
|
35 |
/**
|
|
36 |
* <p>Implements pluggable Datatypes.</p>
|
|
37 |
*
|
|
38 |
* <p>This class is duplicated for each JAXP subpackage so keep it in
|
|
39 |
* sync. It is package private for secure class loading.</p>
|
|
40 |
*
|
|
41 |
* @author Santiago.PericasGeertsen@sun.com
|
12458
|
42 |
* @author Huizhe.Wang@oracle.com
|
12005
|
43 |
*/
|
|
44 |
class FactoryFinder {
|
16953
|
45 |
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
|
12005
|
46 |
/**
|
|
47 |
* Internal debug flag.
|
|
48 |
*/
|
|
49 |
private static boolean debug = false;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Cache for properties in java.home/lib/jaxp.properties
|
|
53 |
*/
|
|
54 |
static Properties cacheProps = new Properties();
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Flag indicating if properties from java.home/lib/jaxp.properties
|
|
58 |
* have been cached.
|
|
59 |
*/
|
12458
|
60 |
static volatile boolean firstTime = true;
|
12005
|
61 |
|
|
62 |
/**
|
|
63 |
* Security support class use to check access control before
|
|
64 |
* getting certain system resources.
|
|
65 |
*/
|
|
66 |
static SecuritySupport ss = new SecuritySupport();
|
|
67 |
|
|
68 |
// Define system property "jaxp.debug" to get output
|
|
69 |
static {
|
|
70 |
// Use try/catch block to support applets, which throws
|
|
71 |
// SecurityException out of this code.
|
|
72 |
try {
|
|
73 |
String val = ss.getSystemProperty("jaxp.debug");
|
|
74 |
// Allow simply setting the prop to turn on debug
|
|
75 |
debug = val != null && !"false".equals(val);
|
|
76 |
}
|
|
77 |
catch (SecurityException se) {
|
|
78 |
debug = false;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
private static void dPrint(String msg) {
|
|
83 |
if (debug) {
|
|
84 |
System.err.println("JAXP: " + msg);
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
/**
|
|
89 |
* Attempt to load a class using the class loader supplied. If that fails
|
|
90 |
* and fall back is enabled, the current (i.e. bootstrap) class loader is
|
|
91 |
* tried.
|
|
92 |
*
|
|
93 |
* If the class loader supplied is <code>null</code>, first try using the
|
|
94 |
* context class loader followed by the current (i.e. bootstrap) class
|
|
95 |
* loader.
|
12458
|
96 |
*
|
|
97 |
* Use bootstrap classLoader if cl = null and useBSClsLoader is true
|
12005
|
98 |
*/
|
|
99 |
static private Class getProviderClass(String className, ClassLoader cl,
|
12458
|
100 |
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
|
12005
|
101 |
{
|
|
102 |
try {
|
|
103 |
if (cl == null) {
|
12458
|
104 |
if (useBSClsLoader) {
|
|
105 |
return Class.forName(className, true, FactoryFinder.class.getClassLoader());
|
|
106 |
} else {
|
|
107 |
cl = ss.getContextClassLoader();
|
|
108 |
if (cl == null) {
|
|
109 |
throw new ClassNotFoundException();
|
|
110 |
}
|
|
111 |
else {
|
|
112 |
return cl.loadClass(className);
|
|
113 |
}
|
12005
|
114 |
}
|
|
115 |
}
|
|
116 |
else {
|
|
117 |
return cl.loadClass(className);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
catch (ClassNotFoundException e1) {
|
|
121 |
if (doFallback) {
|
|
122 |
// Use current class loader - should always be bootstrap CL
|
|
123 |
return Class.forName(className, true, FactoryFinder.class.getClassLoader());
|
|
124 |
}
|
|
125 |
else {
|
|
126 |
throw e1;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Create an instance of a class. Delegates to method
|
|
133 |
* <code>getProviderClass()</code> in order to load the class.
|
|
134 |
*
|
|
135 |
* @param className Name of the concrete class corresponding to the
|
|
136 |
* service provider
|
|
137 |
*
|
12458
|
138 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
|
|
139 |
* current <code>Thread</code>'s context classLoader is used to load the factory class.
|
12005
|
140 |
*
|
|
141 |
* @param doFallback True if the current ClassLoader should be tried as
|
|
142 |
* a fallback if the class is not found using cl
|
|
143 |
*/
|
|
144 |
static Object newInstance(String className, ClassLoader cl, boolean doFallback)
|
|
145 |
throws ConfigurationError
|
|
146 |
{
|
12458
|
147 |
return newInstance(className, cl, doFallback, false);
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Create an instance of a class. Delegates to method
|
|
152 |
* <code>getProviderClass()</code> in order to load the class.
|
|
153 |
*
|
|
154 |
* @param className Name of the concrete class corresponding to the
|
|
155 |
* service provider
|
|
156 |
*
|
|
157 |
* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
|
|
158 |
* current <code>Thread</code>'s context classLoader is used to load the factory class.
|
|
159 |
*
|
|
160 |
* @param doFallback True if the current ClassLoader should be tried as
|
|
161 |
* a fallback if the class is not found using cl
|
|
162 |
*
|
|
163 |
* @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter
|
|
164 |
* is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.
|
|
165 |
*/
|
|
166 |
static Object newInstance(String className, ClassLoader cl, boolean doFallback, boolean useBSClsLoader)
|
|
167 |
throws ConfigurationError
|
|
168 |
{
|
16953
|
169 |
// make sure we have access to restricted packages
|
|
170 |
if (System.getSecurityManager() != null) {
|
|
171 |
if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
|
|
172 |
cl = null;
|
|
173 |
useBSClsLoader = true;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
12005
|
177 |
try {
|
12458
|
178 |
Class providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
|
12005
|
179 |
Object instance = providerClass.newInstance();
|
|
180 |
if (debug) { // Extra check to avoid computing cl strings
|
|
181 |
dPrint("created new instance of " + providerClass +
|
|
182 |
" using ClassLoader: " + cl);
|
|
183 |
}
|
|
184 |
return instance;
|
|
185 |
}
|
|
186 |
catch (ClassNotFoundException x) {
|
|
187 |
throw new ConfigurationError(
|
|
188 |
"Provider " + className + " not found", x);
|
|
189 |
}
|
|
190 |
catch (Exception x) {
|
|
191 |
throw new ConfigurationError(
|
|
192 |
"Provider " + className + " could not be instantiated: " + x,
|
|
193 |
x);
|
|
194 |
}
|
|
195 |
}
|
|
196 |
|
|
197 |
/**
|
|
198 |
* Finds the implementation Class object in the specified order. Main
|
|
199 |
* entry point.
|
|
200 |
* @return Class object of factory, never null
|
|
201 |
*
|
|
202 |
* @param factoryId Name of the factory to find, same as
|
|
203 |
* a property name
|
|
204 |
* @param fallbackClassName Implementation class name, if nothing else
|
|
205 |
* is found. Use null to mean no fallback.
|
|
206 |
*
|
|
207 |
* Package private so this code can be shared.
|
|
208 |
*/
|
|
209 |
static Object find(String factoryId, String fallbackClassName)
|
|
210 |
throws ConfigurationError
|
|
211 |
{
|
|
212 |
dPrint("find factoryId =" + factoryId);
|
|
213 |
|
|
214 |
// Use the system property first
|
|
215 |
try {
|
|
216 |
String systemProp = ss.getSystemProperty(factoryId);
|
|
217 |
if (systemProp != null) {
|
|
218 |
dPrint("found system property, value=" + systemProp);
|
|
219 |
return newInstance(systemProp, null, true);
|
|
220 |
}
|
|
221 |
}
|
|
222 |
catch (SecurityException se) {
|
|
223 |
if (debug) se.printStackTrace();
|
|
224 |
}
|
|
225 |
|
|
226 |
// try to read from $java.home/lib/jaxp.properties
|
|
227 |
try {
|
|
228 |
String factoryClassName = null;
|
|
229 |
if (firstTime) {
|
|
230 |
synchronized (cacheProps) {
|
|
231 |
if (firstTime) {
|
|
232 |
String configFile = ss.getSystemProperty("java.home") + File.separator +
|
|
233 |
"lib" + File.separator + "jaxp.properties";
|
|
234 |
File f = new File(configFile);
|
|
235 |
firstTime = false;
|
|
236 |
if (ss.doesFileExist(f)) {
|
|
237 |
dPrint("Read properties file "+f);
|
|
238 |
cacheProps.load(ss.getFileInputStream(f));
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
}
|
|
243 |
factoryClassName = cacheProps.getProperty(factoryId);
|
|
244 |
|
|
245 |
if (factoryClassName != null) {
|
|
246 |
dPrint("found in $java.home/jaxp.properties, value=" + factoryClassName);
|
|
247 |
return newInstance(factoryClassName, null, true);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
catch (Exception ex) {
|
|
251 |
if (debug) ex.printStackTrace();
|
|
252 |
}
|
|
253 |
|
|
254 |
// Try Jar Service Provider Mechanism
|
|
255 |
Object provider = findJarServiceProvider(factoryId);
|
|
256 |
if (provider != null) {
|
|
257 |
return provider;
|
|
258 |
}
|
|
259 |
if (fallbackClassName == null) {
|
|
260 |
throw new ConfigurationError(
|
|
261 |
"Provider for " + factoryId + " cannot be found", null);
|
|
262 |
}
|
|
263 |
|
|
264 |
dPrint("loaded from fallback value: " + fallbackClassName);
|
|
265 |
return newInstance(fallbackClassName, null, true);
|
|
266 |
}
|
|
267 |
|
|
268 |
/*
|
|
269 |
* Try to find provider using Jar Service Provider Mechanism
|
|
270 |
*
|
|
271 |
* @return instance of provider class if found or null
|
|
272 |
*/
|
|
273 |
private static Object findJarServiceProvider(String factoryId)
|
|
274 |
throws ConfigurationError
|
|
275 |
{
|
|
276 |
String serviceId = "META-INF/services/" + factoryId;
|
|
277 |
InputStream is = null;
|
|
278 |
|
|
279 |
// First try the Context ClassLoader
|
|
280 |
ClassLoader cl = ss.getContextClassLoader();
|
12458
|
281 |
boolean useBSClsLoader = false;
|
12005
|
282 |
if (cl != null) {
|
|
283 |
is = ss.getResourceAsStream(cl, serviceId);
|
|
284 |
|
|
285 |
// If no provider found then try the current ClassLoader
|
|
286 |
if (is == null) {
|
|
287 |
cl = FactoryFinder.class.getClassLoader();
|
|
288 |
is = ss.getResourceAsStream(cl, serviceId);
|
12458
|
289 |
useBSClsLoader = true;
|
12005
|
290 |
}
|
|
291 |
} else {
|
|
292 |
// No Context ClassLoader, try the current ClassLoader
|
|
293 |
cl = FactoryFinder.class.getClassLoader();
|
|
294 |
is = ss.getResourceAsStream(cl, serviceId);
|
12458
|
295 |
useBSClsLoader = true;
|
12005
|
296 |
}
|
|
297 |
|
|
298 |
if (is == null) {
|
|
299 |
// No provider found
|
|
300 |
return null;
|
|
301 |
}
|
|
302 |
|
|
303 |
if (debug) { // Extra check to avoid computing cl strings
|
|
304 |
dPrint("found jar resource=" + serviceId + " using ClassLoader: " + cl);
|
|
305 |
}
|
|
306 |
|
|
307 |
BufferedReader rd;
|
|
308 |
try {
|
|
309 |
rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
|
310 |
}
|
|
311 |
catch (java.io.UnsupportedEncodingException e) {
|
|
312 |
rd = new BufferedReader(new InputStreamReader(is));
|
|
313 |
}
|
|
314 |
|
|
315 |
String factoryClassName = null;
|
|
316 |
try {
|
|
317 |
// XXX Does not handle all possible input as specified by the
|
|
318 |
// Jar Service Provider specification
|
|
319 |
factoryClassName = rd.readLine();
|
|
320 |
rd.close();
|
|
321 |
} catch (IOException x) {
|
|
322 |
// No provider found
|
|
323 |
return null;
|
|
324 |
}
|
|
325 |
|
|
326 |
if (factoryClassName != null && !"".equals(factoryClassName)) {
|
|
327 |
dPrint("found in resource, value=" + factoryClassName);
|
|
328 |
|
|
329 |
// Note: here we do not want to fall back to the current
|
|
330 |
// ClassLoader because we want to avoid the case where the
|
|
331 |
// resource file was found using one ClassLoader and the
|
|
332 |
// provider class was instantiated using a different one.
|
12458
|
333 |
return newInstance(factoryClassName, cl, false, useBSClsLoader);
|
12005
|
334 |
}
|
|
335 |
|
|
336 |
// No provider found
|
|
337 |
return null;
|
|
338 |
}
|
|
339 |
|
|
340 |
static class ConfigurationError extends Error {
|
|
341 |
private Exception exception;
|
|
342 |
|
|
343 |
/**
|
|
344 |
* Construct a new instance with the specified detail string and
|
|
345 |
* exception.
|
|
346 |
*/
|
|
347 |
ConfigurationError(String msg, Exception x) {
|
|
348 |
super(msg);
|
|
349 |
this.exception = x;
|
|
350 |
}
|
|
351 |
|
|
352 |
Exception getException() {
|
|
353 |
return exception;
|
|
354 |
}
|
|
355 |
/**
|
|
356 |
* use the exception chaining mechanism of JDK1.4
|
|
357 |
*/
|
|
358 |
@Override
|
|
359 |
public Throwable getCause() {
|
|
360 |
return exception;
|
|
361 |
}
|
|
362 |
}
|
|
363 |
|
|
364 |
}
|