1 /*
2 * Copyright (c) 2005, 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.stream;
27
28 import java.io.BufferedReader;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.util.Properties;
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
42 */
43 class FactoryFinder {
44 // Check we have access to package.
45 private static final String DEFAULT_PACKAGE = "com.sun.xml.internal.";
46
47 /**
48 * Internal debug flag.
49 */
50 private static boolean debug = false;
51
52 /**
53 * Cache for properties in java.home/lib/jaxp.properties
54 */
55 static Properties cacheProps = new Properties();
56
57 /**
58 * Flag indicating if properties from java.home/lib/jaxp.properties
59 * have been cached.
60 */
61 static volatile boolean firstTime = true;
62
63 /**
64 * Security support class use to check access control before
65 * getting certain system resources.
66 */
67 static SecuritySupport ss = new SecuritySupport();
68
69 // Define system property "jaxp.debug" to get output
70 static {
71 // Use try/catch block to support applets, which throws
72 // SecurityException out of this code.
73 try {
74 String val = ss.getSystemProperty("jaxp.debug");
75 // Allow simply setting the prop to turn on debug
76 debug = val != null && !"false".equals(val);
77 }
78 catch (SecurityException se) {
79 debug = false;
80 }
81 }
82
83 private static void dPrint(String msg) {
84 if (debug) {
85 System.err.println("JAXP: " + msg);
86 }
87 }
88
89 /**
90 * Attempt to load a class using the class loader supplied. If that fails
91 * and fall back is enabled, the current (i.e. bootstrap) class loader is
92 * tried.
93 *
94 * If the class loader supplied is <code>null</code>, first try using the
95 * context class loader followed by the current (i.e. bootstrap) class
96 * loader.
97 *
98 * Use bootstrap classLoader if cl = null and useBSClsLoader is true
99 */
100 static private Class getProviderClass(String className, ClassLoader cl,
101 boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
102 {
103 try {
104 if (cl == null) {
105 if (useBSClsLoader) {
106 return Class.forName(className, true, FactoryFinder.class.getClassLoader());
107 } else {
108 cl = ss.getContextClassLoader();
109 if (cl == null) {
110 throw new ClassNotFoundException();
111 }
112 else {
113 return cl.loadClass(className);
114 }
115 }
116 }
117 else {
118 return cl.loadClass(className);
119 }
120 }
121 catch (ClassNotFoundException e1) {
122 if (doFallback) {
123 // Use current class loader - should always be bootstrap CL
124 return Class.forName(className, true, FactoryFinder.class.getClassLoader());
125 }
126 else {
127 throw e1;
128 }
129 }
130 }
131
132 /**
133 * Create an instance of a class. Delegates to method
134 * <code>getProviderClass()</code> in order to load the class.
135 *
136 * @param className Name of the concrete class corresponding to the
137 * service provider
138 *
139 * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
140 * current <code>Thread</code>'s context classLoader is used to load the factory class.
141 *
142 * @param doFallback True if the current ClassLoader should be tried as
143 * a fallback if the class is not found using cl
144 */
145 static Object newInstance(String className, ClassLoader cl, boolean doFallback)
146 throws ConfigurationError
147 {
148 return newInstance(className, cl, doFallback, false);
149 }
150
151 /**
152 * Create an instance of a class. Delegates to method
153 * <code>getProviderClass()</code> in order to load the class.
154 *
155 * @param className Name of the concrete class corresponding to the
156 * service provider
157 *
158 * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
159 * current <code>Thread</code>'s context classLoader is used to load the factory class.
160 *
161 * @param doFallback True if the current ClassLoader should be tried as
162 * a fallback if the class is not found using cl
163 *
164 * @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter
165 * is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.
166 */
167 static Object newInstance(String className, ClassLoader cl, boolean doFallback, boolean useBSClsLoader)
168 throws ConfigurationError
169 {
170 // make sure we have access to restricted packages
171 if (System.getSecurityManager() != null) {
172 if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
173 cl = null;
174 useBSClsLoader = true;
175 }
176 }
177
178 try {
179 Class providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
180 Object instance = providerClass.newInstance();
181 if (debug) { // Extra check to avoid computing cl strings
182 dPrint("created new instance of " + providerClass +
183 " using ClassLoader: " + cl);
184 }
185 return instance;
186 }
187 catch (ClassNotFoundException x) {
188 throw new ConfigurationError(
189 "Provider " + className + " not found", x);
190 }
191 catch (Exception x) {
192 throw new ConfigurationError(
193 "Provider " + className + " could not be instantiated: " + x,
194 x);
195 }
196 }
197
198 /**
199 * Finds the implementation Class object in the specified order.
200 *
201 * @return Class object of factory, never null
202 *
203 * @param factoryId Name of the factory to find, same as
204 * a property name
205 * @param fallbackClassName Implementation class name, if nothing else
206 * is found. Use null to mean no fallback.
207 *
208 * Package private so this code can be shared.
209 */
210 static Object find(String factoryId, String fallbackClassName)
211 throws ConfigurationError
212 {
213 return find(factoryId, null, fallbackClassName);
214 }
215
216 /**
217 * Finds the implementation Class object in the specified order. Main
218 * entry point.
219 * @return Class object of factory, never null
220 *
221 * @param factoryId Name of the factory to find, same as
222 * a property name
223 *
224 * @param cl ClassLoader to be used to load the class, null means to use
225 * the bootstrap ClassLoader
226 *
227 * @param fallbackClassName Implementation class name, if nothing else
228 * is found. Use null to mean no fallback.
229 *
230 * Package private so this code can be shared.
231 */
232 static Object find(String factoryId, ClassLoader cl, String fallbackClassName)
233 throws ConfigurationError
234 {
235 dPrint("find factoryId =" + factoryId);
236
237 // Use the system property first
238 try {
239 String systemProp = ss.getSystemProperty(factoryId);
240 if (systemProp != null) {
241 dPrint("found system property, value=" + systemProp);
242 return newInstance(systemProp, null, true);
243 }
244 }
245 catch (SecurityException se) {
246 if (debug) se.printStackTrace();
247 }
248
249 // Try read $java.home/lib/stax.properties followed by
250 // $java.home/lib/jaxp.properties if former not present
251 String configFile = null;
252 try {
253 String factoryClassName = null;
254 if (firstTime) {
255 synchronized (cacheProps) {
256 if (firstTime) {
257 configFile = ss.getSystemProperty("java.home") + File.separator +
258 "lib" + File.separator + "stax.properties";
259 File f = new File(configFile);
260 firstTime = false;
261 if (ss.doesFileExist(f)) {
262 dPrint("Read properties file "+f);
263 cacheProps.load(ss.getFileInputStream(f));
264 }
265 else {
266 configFile = ss.getSystemProperty("java.home") + File.separator +
267 "lib" + File.separator + "jaxp.properties";
268 f = new File(configFile);
269 if (ss.doesFileExist(f)) {
270 dPrint("Read properties file "+f);
271 cacheProps.load(ss.getFileInputStream(f));
272 }
273 }
274 }
275 }
276 }
277 factoryClassName = cacheProps.getProperty(factoryId);
278
279 if (factoryClassName != null) {
280 dPrint("found in " + configFile + " value=" + factoryClassName);
281 return newInstance(factoryClassName, null, true);
282 }
283 }
284 catch (Exception ex) {
285 if (debug) ex.printStackTrace();
286 }
287
288 // Try Jar Service Provider Mechanism
289 Object provider = findJarServiceProvider(factoryId);
290 if (provider != null) {
291 return provider;
292 }
293 if (fallbackClassName == null) {
294 throw new ConfigurationError(
295 "Provider for " + factoryId + " cannot be found", null);
296 }
297
298 dPrint("loaded from fallback value: " + fallbackClassName);
299 return newInstance(fallbackClassName, cl, true);
300 }
301
302 /*
303 * Try to find provider using Jar Service Provider Mechanism
304 *
305 * @return instance of provider class if found or null
306 */
307 private static Object findJarServiceProvider(String factoryId)
308 throws ConfigurationError
309 {
310 String serviceId = "META-INF/services/" + factoryId;
311 InputStream is = null;
312
313 // First try the Context ClassLoader
314 ClassLoader cl = ss.getContextClassLoader();
315 boolean useBSClsLoader = false;
316 if (cl != null) {
317 is = ss.getResourceAsStream(cl, serviceId);
318
319 // If no provider found then try the current ClassLoader
320 if (is == null) {
321 cl = FactoryFinder.class.getClassLoader();
322 is = ss.getResourceAsStream(cl, serviceId);
323 useBSClsLoader = true;
324 }
325 } else {
326 // No Context ClassLoader, try the current ClassLoader
327 cl = FactoryFinder.class.getClassLoader();
328 is = ss.getResourceAsStream(cl, serviceId);
329 useBSClsLoader = true;
330 }
331
332 if (is == null) {
333 // No provider found
334 return null;
335 }
336
337 if (debug) { // Extra check to avoid computing cl strings
338 dPrint("found jar resource=" + serviceId + " using ClassLoader: " + cl);
339 }
340
341 BufferedReader rd;
342 try {
343 rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
344 }
345 catch (java.io.UnsupportedEncodingException e) {
346 rd = new BufferedReader(new InputStreamReader(is));
347 }
348
349 String factoryClassName = null;
350 try {
351 // XXX Does not handle all possible input as specified by the
352 // Jar Service Provider specification
353 factoryClassName = rd.readLine();
354 rd.close();
355 } catch (IOException x) {
356 // No provider found
357 return null;
358 }
359
360 if (factoryClassName != null && !"".equals(factoryClassName)) {
361 dPrint("found in resource, value=" + factoryClassName);
362
363 // Note: here we do not want to fall back to the current
364 // ClassLoader because we want to avoid the case where the
365 // resource file was found using one ClassLoader and the
366 // provider class was instantiated using a different one.
367 return newInstance(factoryClassName, cl, false, useBSClsLoader);
368 }
369
370 // No provider found
371 return null;
372 }
373
374 static class ConfigurationError extends Error {
375 private Exception exception;
376
377 /**
378 * Construct a new instance with the specified detail string and
379 * exception.
380 */
381 ConfigurationError(String msg, Exception x) {
382 super(msg);
383 this.exception = x;
384 }
385
386 Exception getException() {
387 return exception;
388 }
389 /**
390 * use the exception chaining mechanism of JDK1.4
391 */
392 @Override
393 public Throwable getCause() {
394 return exception;
395 }
396 }
397
398 }
|
1 /*
2 * Copyright (c) 2005, 2013, 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.stream;
27
28 import java.io.File;
29 import java.security.AccessController;
30 import java.security.PrivilegedAction;
31 import java.util.Iterator;
32 import java.util.Properties;
33 import java.util.ServiceConfigurationError;
34 import java.util.ServiceLoader;
35
36 /**
37 * <p>Implements pluggable streams.</p>
38 *
39 * <p>This class is duplicated for each JAXP subpackage so keep it in
40 * sync. It is package private for secure class loading.</p>
41 *
42 * @author Santiago.PericasGeertsen@sun.com
43 */
44 class FactoryFinder {
45 // Check we have access to package.
46 private static final String DEFAULT_PACKAGE = "com.sun.xml.internal.";
47
48 /**
49 * Internal debug flag.
50 */
51 private static boolean debug = false;
52
53 /**
54 * Cache for properties in java.home/lib/jaxp.properties
55 */
56 final private static Properties cacheProps = new Properties();
57
58 /**
59 * Flag indicating if properties from java.home/lib/jaxp.properties
60 * have been cached.
61 */
62 private static volatile boolean firstTime = true;
63
64 /**
65 * Security support class use to check access control before
66 * getting certain system resources.
67 */
68 final private static SecuritySupport ss = new SecuritySupport();
69
70 // Define system property "jaxp.debug" to get output
71 static {
72 // Use try/catch block to support applets, which throws
73 // SecurityException out of this code.
74 try {
75 String val = ss.getSystemProperty("jaxp.debug");
76 // Allow simply setting the prop to turn on debug
77 debug = val != null && !"false".equals(val);
78 }
79 catch (SecurityException se) {
80 debug = false;
81 }
82 }
83
84 private static void dPrint(String msg) {
85 if (debug) {
86 System.err.println("JAXP: " + msg);
87 }
88 }
89
90 /**
91 * Attempt to load a class using the class loader supplied. If that fails
92 * and fall back is enabled, the current (i.e. bootstrap) class loader is
93 * tried.
94 *
95 * If the class loader supplied is <code>null</code>, first try using the
96 * context class loader followed by the current (i.e. bootstrap) class
97 * loader.
98 *
99 * Use bootstrap classLoader if cl = null and useBSClsLoader is true
100 */
101 static private Class getProviderClass(String className, ClassLoader cl,
102 boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
103 {
104 try {
105 if (cl == null) {
106 if (useBSClsLoader) {
107 return Class.forName(className, false, FactoryFinder.class.getClassLoader());
108 } else {
109 cl = ss.getContextClassLoader();
110 if (cl == null) {
111 throw new ClassNotFoundException();
112 }
113 else {
114 return Class.forName(className, false, cl);
115 }
116 }
117 }
118 else {
119 return Class.forName(className, false, cl);
120 }
121 }
122 catch (ClassNotFoundException e1) {
123 if (doFallback) {
124 // Use current class loader - should always be bootstrap CL
125 return Class.forName(className, false, FactoryFinder.class.getClassLoader());
126 }
127 else {
128 throw e1;
129 }
130 }
131 }
132
133 /**
134 * Create an instance of a class. Delegates to method
135 * <code>getProviderClass()</code> in order to load the class.
136 *
137 * @param type Base class / Service interface of the factory to
138 * instantiate.
139 *
140 * @param className Name of the concrete class corresponding to the
141 * service provider
142 *
143 * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
144 * current <code>Thread</code>'s context classLoader is used to load the factory class.
145 *
146 * @param doFallback True if the current ClassLoader should be tried as
147 * a fallback if the class is not found using cl
148 */
149 static <T> T newInstance(Class<T> type, String className, ClassLoader cl, boolean doFallback)
150 throws FactoryConfigurationError
151 {
152 return newInstance(type, className, cl, doFallback, false);
153 }
154
155 /**
156 * Create an instance of a class. Delegates to method
157 * <code>getProviderClass()</code> in order to load the class.
158 *
159 * @param type Base class / Service interface of the factory to
160 * instantiate.
161 *
162 * @param className Name of the concrete class corresponding to the
163 * service provider
164 *
165 * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
166 * current <code>Thread</code>'s context classLoader is used to load the factory class.
167 *
168 * @param doFallback True if the current ClassLoader should be tried as
169 * a fallback if the class is not found using cl
170 *
171 * @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter
172 * is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.
173 */
174 static <T> T newInstance(Class<T> type, String className, ClassLoader cl,
175 boolean doFallback, boolean useBSClsLoader)
176 throws FactoryConfigurationError
177 {
178 assert type != null;
179
180 // make sure we have access to restricted packages
181 if (System.getSecurityManager() != null) {
182 if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
183 cl = null;
184 useBSClsLoader = true;
185 }
186 }
187
188 try {
189 Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
190 if (!type.isAssignableFrom(providerClass)) {
191 throw new ClassCastException(className + " cannot be cast to " + type.getName());
192 }
193 Object instance = providerClass.newInstance();
194 if (debug) { // Extra check to avoid computing cl strings
195 dPrint("created new instance of " + providerClass +
196 " using ClassLoader: " + cl);
197 }
198 return type.cast(instance);
199 }
200 catch (ClassNotFoundException x) {
201 throw new FactoryConfigurationError(
202 "Provider " + className + " not found", x);
203 }
204 catch (Exception x) {
205 throw new FactoryConfigurationError(
206 "Provider " + className + " could not be instantiated: " + x,
207 x);
208 }
209 }
210
211 /**
212 * Finds the implementation Class object in the specified order.
213 *
214 * @return Class object of factory, never null
215 *
216 * @param type Base class / Service interface of the
217 * factory to find.
218 *
219 * @param fallbackClassName Implementation class name, if nothing else
220 * is found. Use null to mean no fallback.
221 *
222 * Package private so this code can be shared.
223 */
224 static <T> T find(Class<T> type, String fallbackClassName)
225 throws FactoryConfigurationError
226 {
227 return find(type, type.getName(), null, fallbackClassName);
228 }
229
230 /**
231 * Finds the implementation Class object in the specified order. Main
232 * entry point.
233 * @return Class object of factory, never null
234 *
235 * @param type Base class / Service interface of the
236 * factory to find.
237 *
238 * @param factoryId Name of the factory to find, same as
239 * a property name
240 *
241 * @param cl ClassLoader to be used to load the class, null means to use
242 * the bootstrap ClassLoader
243 *
244 * @param fallbackClassName Implementation class name, if nothing else
245 * is found. Use null to mean no fallback.
246 *
247 * Package private so this code can be shared.
248 */
249 static <T> T find(Class<T> type, String factoryId, ClassLoader cl, String fallbackClassName)
250 throws FactoryConfigurationError
251 {
252 dPrint("find factoryId =" + factoryId);
253
254 // Use the system property first
255 try {
256 String systemProp = ss.getSystemProperty(factoryId);
257 if (systemProp != null) {
258 dPrint("found system property, value=" + systemProp);
259 // There's a bug here - because 'cl' is ignored.
260 // This will be handled separately.
261 return newInstance(type, systemProp, null, true);
262 }
263 }
264 catch (SecurityException se) {
265 if (debug) se.printStackTrace();
266 }
267
268 // Try read $java.home/lib/stax.properties followed by
269 // $java.home/lib/jaxp.properties if former not present
270 String configFile = null;
271 try {
272 if (firstTime) {
273 synchronized (cacheProps) {
274 if (firstTime) {
275 configFile = ss.getSystemProperty("java.home") + File.separator +
276 "lib" + File.separator + "stax.properties";
277 File f = new File(configFile);
278 firstTime = false;
279 if (ss.doesFileExist(f)) {
280 dPrint("Read properties file "+f);
281 cacheProps.load(ss.getFileInputStream(f));
282 }
283 else {
284 configFile = ss.getSystemProperty("java.home") + File.separator +
285 "lib" + File.separator + "jaxp.properties";
286 f = new File(configFile);
287 if (ss.doesFileExist(f)) {
288 dPrint("Read properties file "+f);
289 cacheProps.load(ss.getFileInputStream(f));
290 }
291 }
292 }
293 }
294 }
295 final String factoryClassName = cacheProps.getProperty(factoryId);
296
297 if (factoryClassName != null) {
298 dPrint("found in " + configFile + " value=" + factoryClassName);
299 // There's a bug here - because 'cl' is ignored.
300 // This will be handled separately.
301 return newInstance(type, factoryClassName, null, true);
302 }
303 }
304 catch (Exception ex) {
305 if (debug) ex.printStackTrace();
306 }
307
308 if (type.getName().equals(factoryId)) {
309 // Try Jar Service Provider Mechanism
310 final T provider = findServiceProvider(type);
311 if (provider != null) {
312 return provider;
313 }
314 } else {
315 // We're in the case where a 'custom' factoryId was provided,
316 // and in every case where that happens, we expect that
317 // fallbackClassName will be null.
318 assert fallbackClassName == null;
319 }
320 if (fallbackClassName == null) {
321 throw new FactoryConfigurationError(
322 "Provider for " + factoryId + " cannot be found", null);
323 }
324
325 dPrint("loaded from fallback value: " + fallbackClassName);
326 return newInstance(type, fallbackClassName, cl, true);
327 }
328
329 /*
330 * Try to find provider using the ServiceLoader API
331 *
332 * @param type Base class / Service interface of the factory to find.
333 *
334 * @return instance of provider class if found or null
335 */
336 private static <T> T findServiceProvider(final Class<T> type) {
337 try {
338 return AccessController.doPrivileged(new PrivilegedAction<T>() {
339 @Override
340 public T run() {
341 final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
342 final Iterator<T> iterator = serviceLoader.iterator();
343 if (iterator.hasNext()) {
344 return iterator.next();
345 } else {
346 return null;
347 }
348 }
349 });
350 } catch(ServiceConfigurationError e) {
351 // It is not possible to wrap an error directly in
352 // FactoryConfigurationError - so we need to wrap the
353 // ServiceConfigurationError in a RuntimeException.
354 // The alternative would be to modify the logic in
355 // FactoryConfigurationError to allow setting a
356 // Throwable as the cause, but that could cause
357 // compatibility issues down the road.
358 final RuntimeException x = new RuntimeException(
359 "Provider for " + type + " cannot be created", e);
360 final FactoryConfigurationError error =
361 new FactoryConfigurationError(x, x.getMessage());
362 throw error;
363 }
364 }
365
366 }
|