src/javax/xml/stream/FactoryFinder.java

Print this page




   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.stream;
  27 
  28 import java.io.InputStream;
  29 import java.io.IOException;
  30 import java.io.File;
  31 import java.io.FileInputStream;
  32 
  33 import java.util.Properties;
  34 import java.io.BufferedReader;
  35 import java.io.InputStreamReader;
  36 
  37 /**
  38  * <p>Implements pluggable Datatypes.</p>
  39  *
  40  * <p>This class is duplicated for each JAXP subpackage so keep it in
  41  * sync.  It is package private for secure class loading.</p>
  42  *
  43  * @author Santiago.PericasGeertsen@sun.com
  44  */
  45 class FactoryFinder {
  46 
  47     /**
  48      * Internal debug flag.
  49      */
  50     private static boolean debug = false;
  51 
  52     /**
  53      * Cache for properties in java.home/lib/jaxp.properties
  54      */
  55     static Properties cacheProps = new Properties();
  56 
  57     /**
  58      * Flag indicating if properties from java.home/lib/jaxp.properties
  59      * have been cached.
  60      */
  61     static volatile boolean firstTime = true;
  62 
  63     /**
  64      * Security support class use to check access control before
  65      * getting certain system resources.
  66      */
  67     static SecuritySupport ss = new SecuritySupport();
  68 
  69     // Define system property "jaxp.debug" to get output
  70     static {
  71         // Use try/catch block to support applets, which throws
  72         // SecurityException out of this code.
  73         try {
  74             String val = ss.getSystemProperty("jaxp.debug");
  75             // Allow simply setting the prop to turn on debug
  76             debug = val != null && !"false".equals(val);
  77         }
  78         catch (SecurityException se) {
  79             debug = false;
  80         }
  81     }
  82 
  83     private static void dPrint(String msg) {
  84         if (debug) {
  85             System.err.println("JAXP: " + msg);
  86         }
  87     }
  88 
  89     /**
  90      * Attempt to load a class using the class loader supplied. If that fails
  91      * and fall back is enabled, the current (i.e. bootstrap) class loader is
  92      * tried.
  93      *
  94      * If the class loader supplied is <code>null</code>, first try using the
  95      * context class loader followed by the current (i.e. bootstrap) class
  96      * loader.
  97      */
  98     static private Class getProviderClass(String className, ClassLoader cl,
  99             boolean doFallback) throws ClassNotFoundException
 100     {
 101         try {
 102             if (cl == null) {
 103                 cl = ss.getContextClassLoader();
 104                 if (cl == null) {
 105                     throw new ClassNotFoundException();
 106                 }
 107                 else {
 108                     return cl.loadClass(className);
 109                 }
 110             }
 111             else {
 112                 return cl.loadClass(className);
 113             }
 114         }
 115         catch (ClassNotFoundException e1) {
 116             if (doFallback) {
 117                 // Use current class loader - should always be bootstrap CL
 118                 return Class.forName(className, true, FactoryFinder.class.getClassLoader());
 119             }
 120             else {
 121                 throw e1;
 122             }
 123         }
 124     }
 125 
 126     /**
 127      * Create an instance of a class. Delegates to method
 128      * <code>getProviderClass()</code> in order to load the class.
 129      *
 130      * @param className Name of the concrete class corresponding to the
 131      * service provider
 132      *
 133      * @param cl ClassLoader to use to load the class, null means to use
 134      * the bootstrap ClassLoader
 135      *
 136      * @param doFallback True if the current ClassLoader should be tried as
 137      * a fallback if the class is not found using cl
 138      */
 139     static Object newInstance(String className, ClassLoader cl, boolean doFallback)
 140         throws ConfigurationError
 141     {
 142         try {
 143             Class providerClass = getProviderClass(className, cl, doFallback);
 144             Object instance = providerClass.newInstance();
 145             if (debug) {    // Extra check to avoid computing cl strings
 146                 dPrint("created new instance of " + providerClass +
 147                        " using ClassLoader: " + cl);
 148             }
 149             return instance;
 150         }
 151         catch (ClassNotFoundException x) {
 152             throw new ConfigurationError(
 153                 "Provider " + className + " not found", x);
 154         }
 155         catch (Exception x) {
 156             throw new ConfigurationError(
 157                 "Provider " + className + " could not be instantiated: " + x,
 158                 x);
 159         }
 160     }
 161 
 162     /**
 163      * Finds the implementation Class object in the specified order.
 164      *
 165      * @return Class object of factory, never null
 166      *
 167      * @param factoryId             Name of the factory to find, same as
 168      *                              a property name
 169      * @param fallbackClassName     Implementation class name, if nothing else
 170      *                              is found.  Use null to mean no fallback.
 171      *
 172      * Package private so this code can be shared.
 173      */
 174     static Object find(String factoryId, String fallbackClassName)
 175         throws ConfigurationError
 176     {
 177         return find(factoryId, null, fallbackClassName);
 178     }
 179 
 180     /**
 181      * Finds the implementation Class object in the specified order.  Main
 182      * entry point.

 183      * @return Class object of factory, never null
 184      *
 185      * @param factoryId             Name of the factory to find, same as
 186      *                              a property name
 187      *
 188      * @param cl                    ClassLoader to be used to load the class, null means to use
 189      * the bootstrap ClassLoader
 190      *
 191      * @param fallbackClassName     Implementation class name, if nothing else
 192      *                              is found.  Use null to mean no fallback.
 193      *
 194      * Package private so this code can be shared.
 195      */
 196     static Object find(String factoryId, ClassLoader cl, String fallbackClassName)
 197         throws ConfigurationError
 198     {
 199         dPrint("find factoryId =" + factoryId);
 200 
 201         // Use the system property first
 202         try {
 203             String systemProp = ss.getSystemProperty(factoryId);
 204             if (systemProp != null) {
 205                 dPrint("found system property, value=" + systemProp);
 206                 return newInstance(systemProp, null, true);
 207             }



 208         }
 209         catch (SecurityException se) {
 210             if (debug) se.printStackTrace();
 211         }
 212 
 213         // Try read $java.home/lib/stax.properties followed by
 214         // $java.home/lib/jaxp.properties if former not present
 215         String configFile = null;
 216         try {
 217             String factoryClassName = null;
 218             if (firstTime) {
 219                 synchronized (cacheProps) {
 220                     if (firstTime) {
 221                         configFile = ss.getSystemProperty("java.home") + File.separator +
 222                             "lib" + File.separator + "stax.properties";
 223                         File f = new File(configFile);
 224                         firstTime = false;
 225                         if (ss.doesFileExist(f)) {
 226                             dPrint("Read properties file "+f);
 227                             cacheProps.load(ss.getFileInputStream(f));
 228                         }
 229                         else {
 230                             configFile = ss.getSystemProperty("java.home") + File.separator +
 231                                 "lib" + File.separator + "jaxp.properties";
 232                             f = new File(configFile);
 233                             if (ss.doesFileExist(f)) {
 234                                 dPrint("Read properties file "+f);
 235                                 cacheProps.load(ss.getFileInputStream(f));
 236                             }
 237                         }
 238                     }
 239                 }
 240             }
 241             factoryClassName = cacheProps.getProperty(factoryId);
 242 
 243             if (factoryClassName != null) {
 244                 dPrint("found in " + configFile + " value=" + factoryClassName);
 245                 return newInstance(factoryClassName, null, true);
 246             }



 247         }
 248         catch (Exception ex) {
 249             if (debug) ex.printStackTrace();
 250         }
 251 
 252         // Try Jar Service Provider Mechanism
 253         Object provider = findJarServiceProvider(factoryId);
 254         if (provider != null) {
 255             return provider;
 256         }
 257         if (fallbackClassName == null) {
 258             throw new ConfigurationError(
 259                 "Provider for " + factoryId + " cannot be found", null);
 260         }
 261 
 262         dPrint("loaded from fallback value: " + fallbackClassName);
 263         return newInstance(fallbackClassName, cl, true);
 264     }
 265 
 266     /*
 267      * Try to find provider using Jar Service Provider Mechanism
 268      *
 269      * @return instance of provider class if found or null
 270      */
 271     private static Object findJarServiceProvider(String factoryId)
 272         throws ConfigurationError
 273     {
 274         String serviceId = "META-INF/services/" + factoryId;
 275         InputStream is = null;
 276 
 277         // First try the Context ClassLoader
 278         ClassLoader cl = ss.getContextClassLoader();
 279         if (cl != null) {
 280             is = ss.getResourceAsStream(cl, serviceId);
 281 
 282             // If no provider found then try the current ClassLoader
 283             if (is == null) {
 284                 cl = FactoryFinder.class.getClassLoader();
 285                 is = ss.getResourceAsStream(cl, serviceId);
 286             }
 287         } else {
 288             // No Context ClassLoader, try the current ClassLoader
 289             cl = FactoryFinder.class.getClassLoader();
 290             is = ss.getResourceAsStream(cl, serviceId);
 291         }
 292 
 293         if (is == null) {
 294             // No provider found
 295             return null;
 296         }
 297 
 298         if (debug) {    // Extra check to avoid computing cl strings
 299             dPrint("found jar resource=" + serviceId + " using ClassLoader: " + cl);
 300         }
 301 
 302         BufferedReader rd;
 303         try {
 304             rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
 305         }
 306         catch (java.io.UnsupportedEncodingException e) {
 307             rd = new BufferedReader(new InputStreamReader(is));




 308         }
 309 
 310         String factoryClassName = null;
 311         try {
 312             // XXX Does not handle all possible input as specified by the
 313             // Jar Service Provider specification
 314             factoryClassName = rd.readLine();
 315             rd.close();
 316         } catch (IOException x) {
 317             // No provider found
 318             return null;
 319         }
 320 
 321         if (factoryClassName != null && !"".equals(factoryClassName)) {
 322             dPrint("found in resource, value=" + factoryClassName);
 323 
 324             // Note: here we do not want to fall back to the current
 325             // ClassLoader because we want to avoid the case where the
 326             // resource file was found using one ClassLoader and the
 327             // provider class was instantiated using a different one.
 328             return newInstance(factoryClassName, cl, false);
 329         }
 330 
 331         // No provider found
 332         return null;
 333     }
 334 
 335     static class ConfigurationError extends Error {
 336         private Exception exception;
 337 
 338         /**
 339          * Construct a new instance with the specified detail string and
 340          * exception.
 341          */
 342         ConfigurationError(String msg, Exception x) {
 343             super(msg);
 344             this.exception = x;
 345         }
 346 
 347         Exception getException() {
 348             return exception;
 349         }
 350         /**
 351         * use the exception chaining mechanism of JDK1.4
 352         */
 353         @Override
 354         public Throwable getCause() {
 355             return exception;
 356         }
 357     }
 358 
 359 }


   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.stream;
  27 


  28 import java.io.File;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.Properties;
  32 import java.util.ServiceConfigurationError;
  33 import java.util.ServiceLoader;
  34 
  35 /**
  36  * <p>Implements pluggable Datatypes.</p>
  37  *
  38  * <p>This class is duplicated for each JAXP subpackage so keep it in sync. It
  39  * is package private for secure class loading.</p>
  40  *
  41  * @author Santiago.PericasGeertsen@sun.com
  42  */
  43 class FactoryFinder {
  44 
  45     /**
  46      * Internal debug flag.
  47      */
  48     private static boolean debug = false;

  49     /**
  50      * Cache for properties in java.home/lib/jaxp.properties
  51      */
  52     static Properties cacheProps = new Properties();

  53     /**
  54      * Flag indicating if properties from java.home/lib/jaxp.properties have
  55      * been cached.
  56      */
  57     static volatile boolean firstTime = true;

  58     /**
  59      * Security support class use to check access control before getting certain
  60      * system resources.
  61      */
  62     static SecuritySupport ss = new SecuritySupport();
  63 
  64     // Define system property "jaxp.debug" to get output
  65     static {
  66         // Use try/catch block to support applets, which throws
  67         // SecurityException out of this code.
  68         try {
  69             String val = ss.getSystemProperty("jaxp.debug");
  70             // Allow simply setting the prop to turn on debug
  71             debug = val != null && !"false".equals(val);
  72         } catch (SecurityException se) {

  73             debug = false;
  74         }
  75     }
  76 
  77     private static void dPrint(String msg) {
  78         if (debug) {
  79             System.err.println("JAXP: " + msg);
  80         }
  81     }
  82 
  83     /**
  84      * Attempt to load a class using the class loader supplied. If that fails
  85      * and fall back is enabled, the current (i.e. bootstrap) class loader is
  86      * tried.
  87      *
  88      * If the class loader supplied is
  89      * <code>null</code>, first try using the context class loader followed by
  90      * the current (i.e. bootstrap) class loader.
  91      */
  92     static private Class getProviderClass(String className, ClassLoader cl,
  93             boolean doFallback) throws ClassNotFoundException {

  94         try {
  95             if (cl == null) {
  96                 cl = ss.getContextClassLoader();
  97                 if (cl == null) {
  98                     throw new ClassNotFoundException();
  99                 } else {

 100                     return cl.loadClass(className);
 101                 }
 102             } else {

 103                 return cl.loadClass(className);
 104             }
 105         } catch (ClassNotFoundException e1) {

 106             if (doFallback) {
 107                 // Use current class loader - should always be bootstrap CL
 108                 return Class.forName(className, true, FactoryFinder.class.getClassLoader());
 109             } else {

 110                 throw e1;
 111             }
 112         }
 113     }
 114 
 115     /**
 116      * Create an instance of a class. Delegates to method
 117      * <code>getProviderClass()</code> in order to load the class.
 118      *
 119      * @param className Name of the concrete class corresponding to the service
 120      * provider
 121      *
 122      * @param cl ClassLoader to use to load the class, null means to use the
 123      * bootstrap ClassLoader
 124      *
 125      * @param doFallback True if the current ClassLoader should be tried as a
 126      * fallback if the class is not found using cl
 127      */
 128     static Object newInstance(String className, ClassLoader cl, boolean doFallback)
 129             throws FactoryConfigurationError {

 130         try {
 131             Class providerClass = getProviderClass(className, cl, doFallback);
 132             Object instance = providerClass.newInstance();
 133             if (debug) {    // Extra check to avoid computing cl strings
 134                 dPrint("created new instance of " + providerClass
 135                         + " using ClassLoader: " + cl);
 136             }
 137             return instance;
 138         } catch (ClassNotFoundException x) {
 139             throw new FactoryConfigurationError(

 140                     "Provider " + className + " not found", x);
 141         } catch (Exception x) {
 142             throw new FactoryConfigurationError(

 143                     "Provider " + className + " could not be instantiated: " + x,
 144                     x);
 145         }
 146     }
 147 
 148     /**
 149      * Finds the implementation Class object in the specified order.
 150      *
 151      * @return Class object of factory, never null
 152      *
 153      * @param factoryId Name of the factory to find, same as a property name
 154      * @param fallbackClassName Implementation class name, if nothing else is
 155      * found. Use null to mean no fallback.

 156      *
 157      * Package private so this code can be shared.
 158      */
 159     static Object find(Class factoryClass, String factoryId, String fallbackClassName)
 160             throws FactoryConfigurationError {
 161         return find(factoryClass, factoryId, null, fallbackClassName);

 162     }
 163 
 164     /**
 165      * Finds the implementation Class object in the specified order. Main entry
 166      * point.
 167      *
 168      * @return Class object of factory, never null
 169      *
 170      * @param factoryId Name of the factory to find, same as a property name

 171      *
 172      * @param cl ClassLoader to be used to load the class, null means to use the
 173      * bootstrap ClassLoader
 174      *
 175      * @param fallbackClassName Implementation class name, if nothing else is
 176      * found. Use null to mean no fallback.
 177      *
 178      * Package private so this code can be shared.
 179      */
 180     static Object find(Class factoryClass, String factoryId, ClassLoader cl, String fallbackClassName)
 181             throws FactoryConfigurationError {

 182         dPrint("find factoryId =" + factoryId);
 183 
 184         // Use the system property first
 185         try {
 186             String systemProp = ss.getSystemProperty(factoryId);
 187             if (systemProp != null) {
 188                 dPrint("found system property, value=" + systemProp);
 189                 return newInstance(systemProp, null, true);
 190             }
 191         } catch (SecurityException se) {
 192             if (debug) {
 193                 se.printStackTrace();
 194             }


 195         }
 196 
 197         // Try read $java.home/lib/stax.properties followed by
 198         // $java.home/lib/jaxp.properties if former not present
 199         String configFile = null;
 200         try {
 201             String factoryClassName = null;
 202             if (firstTime) {
 203                 synchronized (cacheProps) {
 204                     if (firstTime) {
 205                         configFile = ss.getSystemProperty("java.home") + File.separator
 206                                 + "lib" + File.separator + "stax.properties";
 207                         File f = new File(configFile);
 208                         firstTime = false;
 209                         if (ss.doesFileExist(f)) {
 210                             dPrint("Read properties file " + f);
 211                             cacheProps.load(ss.getFileInputStream(f));
 212                         } else {
 213                             configFile = ss.getSystemProperty("java.home") + File.separator
 214                                     + "lib" + File.separator + "jaxp.properties";

 215                             f = new File(configFile);
 216                             if (ss.doesFileExist(f)) {
 217                                 dPrint("Read properties file " + f);
 218                                 cacheProps.load(ss.getFileInputStream(f));
 219                             }
 220                         }
 221                     }
 222                 }
 223             }
 224             factoryClassName = cacheProps.getProperty(factoryId);
 225 
 226             if (factoryClassName != null) {
 227                 dPrint("found in " + configFile + " value=" + factoryClassName);
 228                 return newInstance(factoryClassName, null, true);
 229             }
 230         } catch (Exception ex) {
 231             if (debug) {
 232                 ex.printStackTrace();
 233             }


 234         }
 235 
 236         // Try Jar Service Provider Mechanism
 237         Object provider = findServiceProvider(factoryClass, fallbackClassName);
 238         if (provider != null) {
 239             return provider;
 240         }
 241         if (fallbackClassName == null) {
 242             throw new FactoryConfigurationError(
 243                     "Provider for " + factoryId + " cannot be found", null);
 244         }
 245 
 246         dPrint("loaded from fallback value: " + fallbackClassName);
 247         return newInstance(fallbackClassName, cl, true);
 248     }
 249 
 250     /*
 251      * Try to find a provider using Service Loader
 252      *
 253      * @return instance of provider class if found or null
 254      */
 255     private static Object findServiceProvider(final Class factoryClass, final String fallbackClassName)
 256             throws FactoryConfigurationError {






























 257         try {
 258             return AccessController.doPrivileged(new PrivilegedAction() {
 259                 public Object run() {
 260                     Object defaultProvider = null;
 261                     for (Object factory : ServiceLoader.load(factoryClass)) {
 262                         if (factory.getClass().getName().equals(fallbackClassName)) {
 263                             defaultProvider = factory;
 264                         } else {
 265                             return factory;
 266                         }










 267                     }
 268                     if (defaultProvider != null) {
 269                         return defaultProvider;







 270                     }


 271                     return null;
 272                 }
 273             });
 274         } catch (ServiceConfigurationError e) {
 275             FactoryConfigurationError err = new FactoryConfigurationError((Exception) e.getCause(), e.getMessage());
 276             err.initCause(e);
 277             throw err;
 278             //throw new FactoryConfigurationError((Exception) e.getCause(), e.getMessage());
















 279         }
 280     }

 281 }