1 /*
   2  * Copyright (c) 2003, 2017, 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 
  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 import java.util.function.Supplier;
  36 import jdk.xml.internal.SecuritySupport;
  37 
  38 /**
  39  * <p>Implements pluggable Parsers.</p>
  40  *
  41  * <p>This class is duplicated for each JAXP subpackage so keep it in
  42  * sync.  It is package private for secure class loading.</p>
  43  *
  44  * @author Santiago.PericasGeertsen@sun.com
  45  * @author Huizhe.Wang@oracle.com
  46  */
  47 class FactoryFinder {
  48     private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
  49     /**
  50      * Internal debug flag.
  51      */
  52     private static boolean debug = false;
  53 
  54     /**
  55      * Cache for properties in java.home/conf/jaxp.properties
  56      */
  57     private static final Properties cacheProps = new Properties();
  58 
  59     /**
  60      * Flag indicating if properties from java.home/conf/jaxp.properties
  61      * have been cached.
  62      */
  63     static volatile boolean firstTime = true;
  64 
  65     // Define system property "jaxp.debug" to get output
  66     static {
  67         // Use try/catch block to support applets, which throws
  68         // SecurityException out of this code.
  69         try {
  70             String val = SecuritySupport.getSystemProperty("jaxp.debug");
  71             // Allow simply setting the prop to turn on debug
  72             debug = val != null && !"false".equals(val);
  73         }
  74         catch (SecurityException se) {
  75             debug = false;
  76         }
  77     }
  78 
  79     private static void dPrint(Supplier<String> msgGen) {
  80         if (debug) {
  81             System.err.println("JAXP: " + msgGen.get());
  82         }
  83     }
  84 
  85     /**
  86      * Attempt to load a class using the class loader supplied. If that fails
  87      * and fall back is enabled, the current (i.e. bootstrap) class loader is
  88      * tried.
  89      *
  90      * If the class loader supplied is <code>null</code>, first try using the
  91      * context class loader followed by the current (i.e. bootstrap) class
  92      * loader.
  93      *
  94      * Use bootstrap classLoader if cl = null and useBSClsLoader is true
  95      */
  96     static private Class<?> getProviderClass(String className, ClassLoader cl,
  97             boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
  98     {
  99         try {
 100             if (cl == null) {
 101                 if (useBSClsLoader) {
 102                     return Class.forName(className, false, FactoryFinder.class.getClassLoader());
 103                 } else {
 104                     cl = SecuritySupport.getContextClassLoader();
 105                     if (cl == null) {
 106                         throw new ClassNotFoundException();
 107                     }
 108                     else {
 109                         return Class.forName(className, false, cl);
 110                     }
 111                 }
 112             }
 113             else {
 114                 return Class.forName(className, false, cl);
 115             }
 116         }
 117         catch (ClassNotFoundException e1) {
 118             if (doFallback) {
 119                 // Use current class loader - should always be bootstrap CL
 120                 return Class.forName(className, false, FactoryFinder.class.getClassLoader());
 121             }
 122             else {
 123                 throw e1;
 124             }
 125         }
 126     }
 127 
 128     /**
 129      * Create an instance of a class. Delegates to method
 130      * <code>getProviderClass()</code> in order to load the class.
 131      *
 132      * @param type Base class / Service interface  of the factory to
 133      *             instantiate.
 134      *
 135      * @param className Name of the concrete class corresponding to the
 136      * service provider
 137      *
 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.
 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 <T> T newInstance(Class<T> type, String className, ClassLoader cl,
 145                              boolean doFallback)
 146         throws FactoryConfigurationError
 147     {
 148         return newInstance(type, 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 type Base class / Service interface  of the factory to
 156      *             instantiate.
 157      *
 158      * @param className Name of the concrete class corresponding to the
 159      * service provider
 160      *
 161      * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
 162      * current <code>Thread</code>'s context classLoader is used to load the factory class.
 163      *
 164      * @param doFallback True if the current ClassLoader should be tried as
 165      * a fallback if the class is not found using cl
 166      *
 167      * @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter
 168      * is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.
 169      */
 170     static <T> T newInstance(Class<T> type, String className, ClassLoader cl,
 171                              boolean doFallback, boolean useBSClsLoader)
 172         throws FactoryConfigurationError
 173     {
 174         assert type != null;
 175         // make sure we have access to restricted packages
 176         if (System.getSecurityManager() != null) {
 177             if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
 178                 cl = null;
 179                 useBSClsLoader = true;
 180             }
 181         }
 182 
 183         try {
 184             Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
 185             if (!type.isAssignableFrom(providerClass)) {
 186                 throw new ClassCastException(className + " cannot be cast to " + type.getName());
 187             }
 188             Object instance = providerClass.getConstructor().newInstance();
 189             final ClassLoader clD = cl;
 190             dPrint(()->"created new instance of " + providerClass +
 191                        " using ClassLoader: " + clD);
 192             return type.cast(instance);
 193         }
 194         catch (ClassNotFoundException x) {
 195             throw new FactoryConfigurationError(x,
 196                 "Provider " + className + " not found");
 197         }
 198         catch (Exception x) {
 199             throw new FactoryConfigurationError(x,
 200                 "Provider " + className + " could not be instantiated: " + x);
 201         }
 202     }
 203 
 204     /**
 205      * Finds the implementation Class object in the specified order.  Main
 206      * entry point.
 207      * @return Class object of factory, never null
 208      *
 209      * @param type                  Base class / Service interface  of the
 210      *                              factory to find.
 211      * @param fallbackClassName     Implementation class name, if nothing else
 212      *                              is found.  Use null to mean no fallback.
 213      *
 214      * Package private so this code can be shared.
 215      */
 216     static <T> T find(Class<T> type, String fallbackClassName)
 217         throws FactoryConfigurationError
 218     {
 219         final String factoryId = type.getName();
 220         dPrint(()->"find factoryId =" + factoryId);
 221 
 222         // Use the system property first
 223         try {
 224             String systemProp = SecuritySupport.getSystemProperty(factoryId);
 225             if (systemProp != null) {
 226                 dPrint(()->"found system property, value=" + systemProp);
 227                 return newInstance(type, systemProp, null, true);
 228             }
 229         }
 230         catch (SecurityException se) {
 231             if (debug) se.printStackTrace();
 232         }
 233 
 234         // try to read from $java.home/conf/jaxp.properties
 235         try {
 236             if (firstTime) {
 237                 synchronized (cacheProps) {
 238                     if (firstTime) {
 239                         String configFile = SecuritySupport.getSystemProperty("java.home") + File.separator +
 240                             "conf" + File.separator + "jaxp.properties";
 241                         File f = new File(configFile);
 242                         firstTime = false;
 243                         if (SecuritySupport.doesFileExist(f)) {
 244                             dPrint(()->"Read properties file "+f);
 245                             cacheProps.load(SecuritySupport.getFileInputStream(f));
 246                         }
 247                     }
 248                 }
 249             }
 250             final String factoryClassName = cacheProps.getProperty(factoryId);
 251 
 252             if (factoryClassName != null) {
 253                 dPrint(()->"found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName);
 254                 return newInstance(type, factoryClassName, null, true);
 255             }
 256         }
 257         catch (Exception ex) {
 258             if (debug) ex.printStackTrace();
 259         }
 260 
 261         // Try Jar Service Provider Mechanism
 262         T provider = findServiceProvider(type);
 263         if (provider != null) {
 264             return provider;
 265         }
 266         if (fallbackClassName == null) {
 267             throw new FactoryConfigurationError(
 268                 "Provider for " + factoryId + " cannot be found");
 269         }
 270 
 271         dPrint(()->"loaded from fallback value: " + fallbackClassName);
 272         return newInstance(type, fallbackClassName, null, true);
 273     }
 274 
 275     /*
 276      * Try to find provider using the ServiceLoader API
 277      *
 278      * @param type Base class / Service interface  of the factory to find.
 279      *
 280      * @return instance of provider class if found or null
 281      */
 282     private static <T> T findServiceProvider(final Class<T> type) {
 283         try {
 284             return AccessController.doPrivileged(new PrivilegedAction<T>() {
 285                 public T run() {
 286                     final ServiceLoader<T> serviceLoader = ServiceLoader.load(type);
 287                     final Iterator<T> iterator = serviceLoader.iterator();
 288                     if (iterator.hasNext()) {
 289                         return iterator.next();
 290                     } else {
 291                         return null;
 292                     }
 293                  }
 294             });
 295         } catch(ServiceConfigurationError e) {
 296             // It is not possible to wrap an error directly in
 297             // FactoryConfigurationError - so we need to wrap the
 298             // ServiceConfigurationError in a RuntimeException.
 299             // The alternative would be to modify the logic in
 300             // FactoryConfigurationError to allow setting a
 301             // Throwable as the cause, but that could cause
 302             // compatibility issues down the road.
 303             final RuntimeException x = new RuntimeException(
 304                     "Provider for " + type + " cannot be created", e);
 305             final FactoryConfigurationError error =
 306                     new FactoryConfigurationError(x, x.getMessage());
 307             throw error;
 308         }
 309     }
 310 
 311 }