Path: blob/master/src/java.xml/share/classes/javax/xml/parsers/FactoryFinder.java
40948 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.xml.parsers;2627import java.io.File;28import java.security.AccessController;29import java.security.PrivilegedAction;30import java.util.Iterator;31import java.util.Properties;32import java.util.ServiceConfigurationError;33import java.util.ServiceLoader;34import java.util.function.Supplier;35import jdk.xml.internal.SecuritySupport;3637/**38* <p>Implements pluggable Parsers.</p>39*40* <p>This class is duplicated for each JAXP subpackage so keep it in41* sync. It is package private for secure class loading.</p>42*43* @author Santiago PericasGeertsen44*/45class FactoryFinder {46private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";47/**48* Internal debug flag.49*/50private static boolean debug = false;5152/**53* Cache for properties in java.home/conf/jaxp.properties54*/55private static final Properties cacheProps = new Properties();5657/**58* Flag indicating if properties from java.home/conf/jaxp.properties59* have been cached.60*/61static volatile boolean firstTime = true;6263// Define system property "jaxp.debug" to get output64static {65// Use try/catch block to support applets, which throws66// SecurityException out of this code.67try {68String val = SecuritySupport.getSystemProperty("jaxp.debug");69// Allow simply setting the prop to turn on debug70debug = val != null && !"false".equals(val);71}72catch (SecurityException se) {73debug = false;74}75}7677private static void dPrint(Supplier<String> msgGen) {78if (debug) {79System.err.println("JAXP: " + msgGen.get());80}81}8283/**84* Attempt to load a class using the class loader supplied. If that fails85* and fall back is enabled, the current (i.e. bootstrap) class loader is86* tried.87*88* If the class loader supplied is <code>null</code>, first try using the89* context class loader followed by the current (i.e. bootstrap) class90* loader.91*92* Use bootstrap classLoader if cl = null and useBSClsLoader is true93*/94static private Class<?> getProviderClass(String className, ClassLoader cl,95boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException96{97try {98if (cl == null) {99if (useBSClsLoader) {100return Class.forName(className, false, FactoryFinder.class.getClassLoader());101} else {102cl = SecuritySupport.getContextClassLoader();103if (cl == null) {104throw new ClassNotFoundException();105}106else {107return Class.forName(className, false, cl);108}109}110}111else {112return Class.forName(className, false, cl);113}114}115catch (ClassNotFoundException e1) {116if (doFallback) {117// Use current class loader - should always be bootstrap CL118return Class.forName(className, false, FactoryFinder.class.getClassLoader());119}120else {121throw e1;122}123}124}125126/**127* Create an instance of a class. Delegates to method128* <code>getProviderClass()</code> in order to load the class.129*130* @param type Base class / Service interface of the factory to131* instantiate.132*133* @param className Name of the concrete class corresponding to the134* service provider135*136* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>137* current <code>Thread</code>'s context classLoader is used to load the factory class.138*139* @param doFallback True if the current ClassLoader should be tried as140* a fallback if the class is not found using cl141*/142static <T> T newInstance(Class<T> type, String className, ClassLoader cl,143boolean doFallback)144throws FactoryConfigurationError145{146return newInstance(type, className, cl, doFallback, false);147}148149/**150* Create an instance of a class. Delegates to method151* <code>getProviderClass()</code> in order to load the class.152*153* @param type Base class / Service interface of the factory to154* instantiate.155*156* @param className Name of the concrete class corresponding to the157* service provider158*159* @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>160* current <code>Thread</code>'s context classLoader is used to load the factory class.161*162* @param doFallback True if the current ClassLoader should be tried as163* a fallback if the class is not found using cl164*165* @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter166* is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.167*/168@SuppressWarnings("removal")169static <T> T newInstance(Class<T> type, String className, ClassLoader cl,170boolean doFallback, boolean useBSClsLoader)171throws FactoryConfigurationError172{173assert type != null;174// make sure we have access to restricted packages175if (System.getSecurityManager() != null) {176if (className != null && className.startsWith(DEFAULT_PACKAGE)) {177cl = null;178useBSClsLoader = true;179}180}181182try {183Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);184if (!type.isAssignableFrom(providerClass)) {185throw new ClassCastException(className + " cannot be cast to " + type.getName());186}187Object instance = providerClass.getConstructor().newInstance();188final ClassLoader clD = cl;189dPrint(()->"created new instance of " + providerClass +190" using ClassLoader: " + clD);191return type.cast(instance);192}193catch (ClassNotFoundException x) {194throw new FactoryConfigurationError(x,195"Provider " + className + " not found");196}197catch (Exception x) {198throw new FactoryConfigurationError(x,199"Provider " + className + " could not be instantiated: " + x);200}201}202203/**204* Finds the implementation Class object in the specified order. Main205* entry point.206* @return Class object of factory, never null207*208* @param type Base class / Service interface of the209* factory to find.210* @param fallbackClassName Implementation class name, if nothing else211* is found. Use null to mean no fallback.212*213* Package private so this code can be shared.214*/215static <T> T find(Class<T> type, String fallbackClassName)216throws FactoryConfigurationError217{218final String factoryId = type.getName();219dPrint(()->"find factoryId =" + factoryId);220221// Use the system property first222try {223String systemProp = SecuritySupport.getSystemProperty(factoryId);224if (systemProp != null) {225dPrint(()->"found system property, value=" + systemProp);226return newInstance(type, systemProp, null, true);227}228}229catch (SecurityException se) {230if (debug) se.printStackTrace();231}232233// try to read from $java.home/conf/jaxp.properties234try {235if (firstTime) {236synchronized (cacheProps) {237if (firstTime) {238String configFile = SecuritySupport.getSystemProperty("java.home") + File.separator +239"conf" + File.separator + "jaxp.properties";240File f = new File(configFile);241firstTime = false;242if (SecuritySupport.doesFileExist(f)) {243dPrint(()->"Read properties file "+f);244cacheProps.load(SecuritySupport.getFileInputStream(f));245}246}247}248}249final String factoryClassName = cacheProps.getProperty(factoryId);250251if (factoryClassName != null) {252dPrint(()->"found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName);253return newInstance(type, factoryClassName, null, true);254}255}256catch (Exception ex) {257if (debug) ex.printStackTrace();258}259260// Try Jar Service Provider Mechanism261T provider = findServiceProvider(type);262if (provider != null) {263return provider;264}265if (fallbackClassName == null) {266throw new FactoryConfigurationError(267"Provider for " + factoryId + " cannot be found");268}269270dPrint(()->"loaded from fallback value: " + fallbackClassName);271return newInstance(type, fallbackClassName, null, true);272}273274/*275* Try to find provider using the ServiceLoader API276*277* @param type Base class / Service interface of the factory to find.278*279* @return instance of provider class if found or null280*/281@SuppressWarnings("removal")282private static <T> T findServiceProvider(final Class<T> type) {283try {284return AccessController.doPrivileged(new PrivilegedAction<T>() {285public T run() {286final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);287final Iterator<T> iterator = serviceLoader.iterator();288if (iterator.hasNext()) {289return iterator.next();290} else {291return null;292}293}294});295} catch(ServiceConfigurationError e) {296// It is not possible to wrap an error directly in297// FactoryConfigurationError - so we need to wrap the298// ServiceConfigurationError in a RuntimeException.299// The alternative would be to modify the logic in300// FactoryConfigurationError to allow setting a301// Throwable as the cause, but that could cause302// compatibility issues down the road.303final RuntimeException x = new RuntimeException(304"Provider for " + type + " cannot be created", e);305final FactoryConfigurationError error =306new FactoryConfigurationError(x, x.getMessage());307throw error;308}309}310311}312313314