|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package javax.xml.transform; |
|
|
|
import java.io.File; |
|
import java.lang.reflect.Method; |
|
import java.lang.reflect.Modifier; |
|
import java.security.AccessController; |
|
import java.security.PrivilegedAction; |
|
import java.util.Iterator; |
|
import java.util.Properties; |
|
import java.util.ServiceConfigurationError; |
|
import java.util.ServiceLoader; |
|
import java.util.function.Supplier; |
|
import jdk.xml.internal.SecuritySupport; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
class FactoryFinder { |
|
private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xalan.internal."; |
|
|
|
|
|
|
|
*/ |
|
private static boolean debug = false; |
|
|
|
|
|
|
|
*/ |
|
private final static Properties cacheProps = new Properties(); |
|
|
|
|
|
|
|
|
|
*/ |
|
static volatile boolean firstTime = true; |
|
|
|
|
|
static { |
|
// Use try/catch block to support applets, which throws |
|
|
|
try { |
|
String val = SecuritySupport.getSystemProperty("jaxp.debug"); |
|
|
|
debug = val != null && !"false".equals(val); |
|
} |
|
catch (SecurityException se) { |
|
debug = false; |
|
} |
|
} |
|
|
|
private static void dPrint(Supplier<String> msgGen) { |
|
if (debug) { |
|
System.err.println("JAXP: " + msgGen.get()); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static private Class<?> getProviderClass(String className, ClassLoader cl, |
|
boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException |
|
{ |
|
try { |
|
if (cl == null) { |
|
if (useBSClsLoader) { |
|
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
|
} else { |
|
cl = SecuritySupport.getContextClassLoader(); |
|
if (cl == null) { |
|
throw new ClassNotFoundException(); |
|
} |
|
else { |
|
return Class.forName(className, false, cl); |
|
} |
|
} |
|
} |
|
else { |
|
return Class.forName(className, false, cl); |
|
} |
|
} |
|
catch (ClassNotFoundException e1) { |
|
if (doFallback) { |
|
|
|
return Class.forName(className, false, FactoryFinder.class.getClassLoader()); |
|
} |
|
else { |
|
throw e1; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("removal") |
|
static <T> T newInstance(Class<T> type, String className, ClassLoader cl, |
|
boolean doFallback) |
|
throws TransformerFactoryConfigurationError |
|
{ |
|
assert type != null; |
|
|
|
boolean useBSClsLoader = false; |
|
|
|
if (System.getSecurityManager() != null) { |
|
if (className != null && className.startsWith(DEFAULT_PACKAGE)) { |
|
cl = null; |
|
useBSClsLoader = true; |
|
} |
|
} |
|
|
|
try { |
|
Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader); |
|
if (!type.isAssignableFrom(providerClass)) { |
|
throw new ClassCastException(className + " cannot be cast to " + type.getName()); |
|
} |
|
Object instance = providerClass.getConstructor().newInstance(); |
|
|
|
final ClassLoader clD = cl; |
|
dPrint(()->"created new instance of " + providerClass + |
|
" using ClassLoader: " + clD); |
|
return type.cast(instance); |
|
} |
|
catch (ClassNotFoundException x) { |
|
throw new TransformerFactoryConfigurationError(x, |
|
"Provider " + className + " not found"); |
|
} |
|
catch (Exception x) { |
|
throw new TransformerFactoryConfigurationError(x, |
|
"Provider " + className + " could not be instantiated: " + x); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
static <T> T find(Class<T> type, String fallbackClassName) |
|
throws TransformerFactoryConfigurationError |
|
{ |
|
assert type != null; |
|
|
|
final String factoryId = type.getName(); |
|
|
|
dPrint(()->"find factoryId =" + factoryId); |
|
|
|
try { |
|
String systemProp = SecuritySupport.getSystemProperty(factoryId); |
|
if (systemProp != null) { |
|
dPrint(()->"found system property, value=" + systemProp); |
|
return newInstance(type, systemProp, null, true); |
|
} |
|
} |
|
catch (SecurityException se) { |
|
if (debug) se.printStackTrace(); |
|
} |
|
|
|
|
|
try { |
|
if (firstTime) { |
|
synchronized (cacheProps) { |
|
if (firstTime) { |
|
String configFile = SecuritySupport.getSystemProperty("java.home") + File.separator + |
|
"conf" + File.separator + "jaxp.properties"; |
|
File f = new File(configFile); |
|
firstTime = false; |
|
if (SecuritySupport.doesFileExist(f)) { |
|
dPrint(()->"Read properties file "+f); |
|
cacheProps.load(SecuritySupport.getFileInputStream(f)); |
|
} |
|
} |
|
} |
|
} |
|
final String factoryClassName = cacheProps.getProperty(factoryId); |
|
|
|
if (factoryClassName != null) { |
|
dPrint(()->"found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName); |
|
return newInstance(type, factoryClassName, null, true); |
|
} |
|
} |
|
catch (Exception ex) { |
|
if (debug) ex.printStackTrace(); |
|
} |
|
|
|
|
|
T provider = findServiceProvider(type); |
|
if (provider != null) { |
|
return provider; |
|
} |
|
if (fallbackClassName == null) { |
|
throw new TransformerFactoryConfigurationError(null, |
|
"Provider for " + factoryId + " cannot be found"); |
|
} |
|
|
|
dPrint(()->"loaded from fallback value: " + fallbackClassName); |
|
return newInstance(type, fallbackClassName, null, true); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("removal") |
|
private static <T> T findServiceProvider(final Class<T> type) |
|
throws TransformerFactoryConfigurationError |
|
{ |
|
try { |
|
return AccessController.doPrivileged(new PrivilegedAction<T>() { |
|
public T run() { |
|
final ServiceLoader<T> serviceLoader = ServiceLoader.load(type); |
|
final Iterator<T> iterator = serviceLoader.iterator(); |
|
if (iterator.hasNext()) { |
|
return iterator.next(); |
|
} else { |
|
return null; |
|
} |
|
} |
|
}); |
|
} catch(ServiceConfigurationError e) { |
|
// It is not possible to wrap an error directly in |
|
// FactoryConfigurationError - so we need to wrap the |
|
// ServiceConfigurationError in a RuntimeException. |
|
// The alternative would be to modify the logic in |
|
// FactoryConfigurationError to allow setting a |
|
// Throwable as the cause, but that could cause |
|
|
|
final RuntimeException x = new RuntimeException( |
|
"Provider for " + type + " cannot be created", e); |
|
final TransformerFactoryConfigurationError error = |
|
new TransformerFactoryConfigurationError(x, x.getMessage()); |
|
throw error; |
|
} |
|
} |
|
} |