jaxp/src/java.xml/share/classes/javax/xml/datatype/FactoryFinder.java
changeset 42806 35843e3d5ef1
parent 27574 2e8afdf5c6fb
child 45853 bfa06be36a17
equal deleted inserted replaced
42805:857b5e6eef37 42806:35843e3d5ef1
     1 /*
     1 /*
     2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    30 import java.security.PrivilegedAction;
    30 import java.security.PrivilegedAction;
    31 import java.util.Iterator;
    31 import java.util.Iterator;
    32 import java.util.Properties;
    32 import java.util.Properties;
    33 import java.util.ServiceConfigurationError;
    33 import java.util.ServiceConfigurationError;
    34 import java.util.ServiceLoader;
    34 import java.util.ServiceLoader;
       
    35 import java.util.function.Supplier;
    35 
    36 
    36 /**
    37 /**
    37  * <p>Implements pluggable Datatypes.</p>
    38  * <p>Implements pluggable Datatypes.</p>
    38  *
    39  *
    39  * <p>This class is duplicated for each JAXP subpackage so keep it in
    40  * <p>This class is duplicated for each JAXP subpackage so keep it in
    78         catch (SecurityException se) {
    79         catch (SecurityException se) {
    79             debug = false;
    80             debug = false;
    80         }
    81         }
    81     }
    82     }
    82 
    83 
    83     private static void dPrint(String msg) {
    84     private static void dPrint(Supplier<String> msgGen) {
    84         if (debug) {
    85         if (debug) {
    85             System.err.println("JAXP: " + msg);
    86             System.err.println("JAXP: " + msgGen.get());
    86         }
    87         }
    87     }
    88     }
    88 
    89 
    89     /**
    90     /**
    90      * Attempt to load a class using the class loader supplied. If that fails
    91      * Attempt to load a class using the class loader supplied. If that fails
   188             Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
   189             Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
   189             if (!type.isAssignableFrom(providerClass)) {
   190             if (!type.isAssignableFrom(providerClass)) {
   190                 throw new ClassCastException(className + " cannot be cast to " + type.getName());
   191                 throw new ClassCastException(className + " cannot be cast to " + type.getName());
   191             }
   192             }
   192             Object instance = providerClass.newInstance();
   193             Object instance = providerClass.newInstance();
   193             if (debug) {    // Extra check to avoid computing cl strings
   194             final ClassLoader clD = cl;
   194                 dPrint("created new instance of " + providerClass +
   195             dPrint(()->"created new instance of " + providerClass +
   195                        " using ClassLoader: " + cl);
   196                        " using ClassLoader: " + clD);
   196             }
       
   197             return type.cast(instance);
   197             return type.cast(instance);
   198         }
   198         }
   199         catch (ClassNotFoundException x) {
   199         catch (ClassNotFoundException x) {
   200             throw new DatatypeConfigurationException(
   200             throw new DatatypeConfigurationException(
   201                 "Provider " + className + " not found", x);
   201                 "Provider " + className + " not found", x);
   221      */
   221      */
   222     static <T> T find(Class<T> type, String fallbackClassName)
   222     static <T> T find(Class<T> type, String fallbackClassName)
   223         throws DatatypeConfigurationException
   223         throws DatatypeConfigurationException
   224     {
   224     {
   225         final String factoryId = type.getName();
   225         final String factoryId = type.getName();
   226         dPrint("find factoryId =" + factoryId);
   226         dPrint(()->"find factoryId =" + factoryId);
   227 
   227 
   228         // Use the system property first
   228         // Use the system property first
   229         try {
   229         try {
   230             String systemProp = ss.getSystemProperty(factoryId);
   230             String systemProp = ss.getSystemProperty(factoryId);
   231             if (systemProp != null) {
   231             if (systemProp != null) {
   232                 dPrint("found system property, value=" + systemProp);
   232                 dPrint(()->"found system property, value=" + systemProp);
   233                 return newInstance(type, systemProp, null, true);
   233                 return newInstance(type, systemProp, null, true);
   234             }
   234             }
   235         }
   235         }
   236         catch (SecurityException se) {
   236         catch (SecurityException se) {
   237             if (debug) se.printStackTrace();
   237             if (debug) se.printStackTrace();
   245                         String configFile = ss.getSystemProperty("java.home") + File.separator +
   245                         String configFile = ss.getSystemProperty("java.home") + File.separator +
   246                             "conf" + File.separator + "jaxp.properties";
   246                             "conf" + File.separator + "jaxp.properties";
   247                         File f = new File(configFile);
   247                         File f = new File(configFile);
   248                         firstTime = false;
   248                         firstTime = false;
   249                         if (ss.doesFileExist(f)) {
   249                         if (ss.doesFileExist(f)) {
   250                             dPrint("Read properties file "+f);
   250                             dPrint(()->"Read properties file "+f);
   251                             cacheProps.load(ss.getFileInputStream(f));
   251                             cacheProps.load(ss.getFileInputStream(f));
   252                         }
   252                         }
   253                     }
   253                     }
   254                 }
   254                 }
   255             }
   255             }
   256             final String factoryClassName = cacheProps.getProperty(factoryId);
   256             final String factoryClassName = cacheProps.getProperty(factoryId);
   257 
   257 
   258             if (factoryClassName != null) {
   258             if (factoryClassName != null) {
   259                 dPrint("found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName);
   259                 dPrint(()->"found in ${java.home}/conf/jaxp.properties, value=" + factoryClassName);
   260                 return newInstance(type, factoryClassName, null, true);
   260                 return newInstance(type, factoryClassName, null, true);
   261             }
   261             }
   262         }
   262         }
   263         catch (Exception ex) {
   263         catch (Exception ex) {
   264             if (debug) ex.printStackTrace();
   264             if (debug) ex.printStackTrace();
   272         if (fallbackClassName == null) {
   272         if (fallbackClassName == null) {
   273             throw new DatatypeConfigurationException(
   273             throw new DatatypeConfigurationException(
   274                 "Provider for " + factoryId + " cannot be found");
   274                 "Provider for " + factoryId + " cannot be found");
   275         }
   275         }
   276 
   276 
   277         dPrint("loaded from fallback value: " + fallbackClassName);
   277         dPrint(()->"loaded from fallback value: " + fallbackClassName);
   278         return newInstance(type, fallbackClassName, null, true);
   278         return newInstance(type, fallbackClassName, null, true);
   279     }
   279     }
   280 
   280 
   281     /*
   281     /*
   282      * Try to find provider using the ServiceLoader API
   282      * Try to find provider using the ServiceLoader API