Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/javax/xml/validation/SchemaFactoryFinder.java
32285 views
/*1* Copyright (c) 2003, 2017, 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.validation;2627import com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory;28import java.io.File;29import java.lang.reflect.Method;30import java.lang.reflect.Modifier;31import java.net.URL;32import java.security.AccessControlContext;33import java.security.AccessController;34import java.security.PrivilegedAction;35import java.util.Properties;36import java.util.ServiceConfigurationError;37import java.util.ServiceLoader;3839/**40* Implementation of {@link SchemaFactory#newInstance(String)}.41*42* @author <a href="[email protected]">Kohsuke Kawaguchi</a>43* @version $Revision: 1.8 $, $Date: 2010-11-01 04:36:13 $44* @since 1.545*/46class SchemaFactoryFinder {4748/** debug support code. */49private static boolean debug = false;50/**51*<p> Take care of restrictions imposed by java security model </p>52*/53private static final SecuritySupport ss = new SecuritySupport();54private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";55/**56* <p>Cache properties for performance.</p>57*/58private static final Properties cacheProps = new Properties();5960/**61* <p>First time requires initialization overhead.</p>62*/63private static volatile boolean firstTime = true;6465static {66// Use try/catch block to support applets67try {68debug = ss.getSystemProperty("jaxp.debug") != null;69} catch (Exception unused) {70debug = false;71}72}7374/**75* <p>Conditional debug printing.</p>76*77* @param msg to print78*/79private static void debugPrintln(String msg) {80if (debug) {81System.err.println("JAXP: " + msg);82}83}8485/**86* <p><code>ClassLoader</code> to use to find <code>SchemaFactory</code>.</p>87*/88private final ClassLoader classLoader;8990/**91* <p>Constructor that specifies <code>ClassLoader</code> to use92* to find <code>SchemaFactory</code>.</p>93*94* @param loader95* to be used to load resource, {@link SchemaFactory}, and96* {@link SchemaFactoryLoader} implementations during97* the resolution process.98* If this parameter is null, the default system class loader99* will be used.100*/101public SchemaFactoryFinder(ClassLoader loader) {102this.classLoader = loader;103if( debug ) {104debugDisplayClassLoader();105}106}107108private void debugDisplayClassLoader() {109try {110if( classLoader == ss.getContextClassLoader() ) {111debugPrintln("using thread context class loader ("+classLoader+") for search");112return;113}114} catch( Throwable unused ) {115// getContextClassLoader() undefined in JDK1.1116}117118if( classLoader==ClassLoader.getSystemClassLoader() ) {119debugPrintln("using system class loader ("+classLoader+") for search");120return;121}122123debugPrintln("using class loader ("+classLoader+") for search");124}125126/**127* <p>Creates a new {@link SchemaFactory} object for the specified128* schema language.</p>129*130* @param schemaLanguage131* See {@link SchemaFactory Schema Language} table in <code>SchemaFactory</code>132* for the list of available schema languages.133*134* @return <code>null</code> if the callee fails to create one.135*136* @throws NullPointerException137* If the <code>schemaLanguage</code> parameter is null.138* @throws SchemaFactoryConfigurationError139* If a configuration error is encountered.140*/141public SchemaFactory newFactory(String schemaLanguage) {142if(schemaLanguage==null) {143throw new NullPointerException();144}145SchemaFactory f = _newFactory(schemaLanguage);146if (f != null) {147debugPrintln("factory '" + f.getClass().getName() + "' was found for " + schemaLanguage);148} else {149debugPrintln("unable to find a factory for " + schemaLanguage);150}151return f;152}153154/**155* <p>Lookup a <code>SchemaFactory</code> for the given <code>schemaLanguage</code>.</p>156*157* @param schemaLanguage Schema language to lookup <code>SchemaFactory</code> for.158*159* @return <code>SchemaFactory</code> for the given <code>schemaLanguage</code>.160*/161private SchemaFactory _newFactory(String schemaLanguage) {162SchemaFactory sf;163164String propertyName = SERVICE_CLASS.getName() + ":" + schemaLanguage;165166// system property look up167try {168debugPrintln("Looking up system property '"+propertyName+"'" );169String r = ss.getSystemProperty(propertyName);170if(r!=null) {171debugPrintln("The value is '"+r+"'");172sf = createInstance(r);173if(sf!=null) return sf;174} else175debugPrintln("The property is undefined.");176} catch( Throwable t ) {177if( debug ) {178debugPrintln("failed to look up system property '"+propertyName+"'" );179t.printStackTrace();180}181}182183String javah = ss.getSystemProperty( "java.home" );184String configFile = javah + File.separator +185"lib" + File.separator + "jaxp.properties";186187188// try to read from $java.home/lib/jaxp.properties189try {190if(firstTime){191synchronized(cacheProps){192if(firstTime){193File f=new File( configFile );194firstTime = false;195if(ss.doesFileExist(f)){196debugPrintln("Read properties file " + f);197cacheProps.load(ss.getFileInputStream(f));198}199}200}201}202final String factoryClassName = cacheProps.getProperty(propertyName);203debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties");204205if (factoryClassName != null) {206sf = createInstance(factoryClassName);207if(sf != null){208return sf;209}210}211} catch (Exception ex) {212if (debug) {213ex.printStackTrace();214}215}216217// Try with ServiceLoader218final SchemaFactory factoryImpl = findServiceProvider(schemaLanguage);219220// The following assertion should always be true.221// Uncomment it, recompile, and run with -ea in case of doubts:222// assert factoryImpl == null || factoryImpl.isSchemaLanguageSupported(schemaLanguage);223224if (factoryImpl != null) {225return factoryImpl;226}227228// platform default229if(schemaLanguage.equals("http://www.w3.org/2001/XMLSchema")) {230debugPrintln("attempting to use the platform default XML Schema validator");231return new XMLSchemaFactory();232}233234debugPrintln("all things were tried, but none was found. bailing out.");235return null;236}237238/** <p>Create class using appropriate ClassLoader.</p>239*240* @param className Name of class to create.241* @return Created class or <code>null</code>.242*/243private Class<?> createClass(String className) {244Class<?> clazz;245// make sure we have access to restricted packages246boolean internal = false;247if (System.getSecurityManager() != null) {248if (className != null && className.startsWith(DEFAULT_PACKAGE)) {249internal = true;250}251}252253try {254if (classLoader != null && !internal) {255clazz = Class.forName(className, false, classLoader);256} else {257clazz = Class.forName(className);258}259} catch (Throwable t) {260if(debug) {261t.printStackTrace();262}263return null;264}265266return clazz;267}268269/**270* <p>Creates an instance of the specified and returns it.</p>271*272* @param className273* fully qualified class name to be instantiated.274*275* @return null276* if it fails. Error messages will be printed by this method.277*/278SchemaFactory createInstance(String className) {279SchemaFactory schemaFactory = null;280281debugPrintln("createInstance(" + className + ")");282283// get Class from className284Class<?> clazz = createClass(className);285if (clazz == null) {286debugPrintln("failed to getClass(" + className + ")");287return null;288}289debugPrintln("loaded " + className + " from " + which(clazz));290291// instantiate Class as a SchemaFactory292try {293if (!SchemaFactory.class.isAssignableFrom(clazz)) {294throw new ClassCastException(clazz.getName()295+ " cannot be cast to " + SchemaFactory.class);296}297schemaFactory = (SchemaFactory) clazz.newInstance();298} catch (ClassCastException classCastException) {299debugPrintln("could not instantiate " + clazz.getName());300if (debug) {301classCastException.printStackTrace();302}303return null;304} catch (IllegalAccessException illegalAccessException) {305debugPrintln("could not instantiate " + clazz.getName());306if (debug) {307illegalAccessException.printStackTrace();308}309return null;310} catch (InstantiationException instantiationException) {311debugPrintln("could not instantiate " + clazz.getName());312if (debug) {313instantiationException.printStackTrace();314}315return null;316}317318return schemaFactory;319}320321// Call isSchemaLanguageSupported with initial context.322private boolean isSchemaLanguageSupportedBy(final SchemaFactory factory,323final String schemaLanguage,324AccessControlContext acc) {325return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {326public Boolean run() {327return factory.isSchemaLanguageSupported(schemaLanguage);328}329}, acc);330}331332/**333* Finds a service provider subclass of SchemaFactory that supports the334* given schema language using the ServiceLoader.335*336* @param schemaLanguage The schema language for which we seek a factory.337* @return A SchemaFactory supporting the specified schema language, or null338* if none is found.339* @throws SchemaFactoryConfigurationError if a configuration error is found.340*/341private SchemaFactory findServiceProvider(final String schemaLanguage) {342assert schemaLanguage != null;343// store current context.344final AccessControlContext acc = AccessController.getContext();345try {346return AccessController.doPrivileged(new PrivilegedAction<SchemaFactory>() {347public SchemaFactory run() {348final ServiceLoader<SchemaFactory> loader =349ServiceLoader.load(SERVICE_CLASS);350for (SchemaFactory factory : loader) {351// restore initial context to call352// factory.isSchemaLanguageSupported353if (isSchemaLanguageSupportedBy(factory, schemaLanguage, acc)) {354return factory;355}356}357return null; // no factory found.358}359});360} catch (ServiceConfigurationError error) {361throw new SchemaFactoryConfigurationError(362"Provider for " + SERVICE_CLASS + " cannot be created", error);363}364}365366private static final Class<SchemaFactory> SERVICE_CLASS = SchemaFactory.class;367368369private static String which( Class<?> clazz ) {370return which( clazz.getName(), clazz.getClassLoader() );371}372373/**374* <p>Search the specified classloader for the given classname.</p>375*376* @param classname the fully qualified name of the class to search for377* @param loader the classloader to search378*379* @return the source location of the resource, or null if it wasn't found380*/381private static String which(String classname, ClassLoader loader) {382383String classnameAsResource = classname.replace('.', '/') + ".class";384385if( loader==null ) loader = ClassLoader.getSystemClassLoader();386387//URL it = loader.getResource(classnameAsResource);388URL it = ss.getResourceAsURL(loader, classnameAsResource);389if (it != null) {390return it.toString();391} else {392return null;393}394}395}396397398