jaxp/src/java.xml/share/classes/javax/xml/xpath/XPathFactoryFinder.java
changeset 42806 35843e3d5ef1
parent 31497 4a6b2e733c0d
child 44378 06aa1b671d17
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
    32 import java.security.AccessController;
    32 import java.security.AccessController;
    33 import java.security.PrivilegedAction;
    33 import java.security.PrivilegedAction;
    34 import java.util.Properties;
    34 import java.util.Properties;
    35 import java.util.ServiceConfigurationError;
    35 import java.util.ServiceConfigurationError;
    36 import java.util.ServiceLoader;
    36 import java.util.ServiceLoader;
       
    37 import java.util.function.Supplier;
    37 
    38 
    38 /**
    39 /**
    39  * Implementation of {@link XPathFactory#newInstance(String)}.
    40  * Implementation of {@link XPathFactory#newInstance(String)}.
    40  *
    41  *
    41  * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
    42  * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
    67     private volatile static boolean firstTime = true;
    68     private volatile static boolean firstTime = true;
    68 
    69 
    69     /**
    70     /**
    70      * <p>Conditional debug printing.</p>
    71      * <p>Conditional debug printing.</p>
    71      *
    72      *
    72      * @param msg to print
    73      * @param msgGen Supplier function that returns debug message
    73      */
    74      */
    74     private static void debugPrintln(String msg) {
    75     private static void debugPrintln(Supplier<String> msgGen) {
    75         if (debug) {
    76         if (debug) {
    76             System.err.println("JAXP: " + msg);
    77             System.err.println("JAXP: " + msgGen.get());
    77         }
    78         }
    78     }
    79     }
    79 
    80 
    80     /**
    81     /**
    81      * <p><code>ClassLoader</code> to use to find <code>XPathFactory</code>.</p>
    82      * <p><code>ClassLoader</code> to use to find <code>XPathFactory</code>.</p>
   100     }
   101     }
   101 
   102 
   102     private void debugDisplayClassLoader() {
   103     private void debugDisplayClassLoader() {
   103         try {
   104         try {
   104             if( classLoader == ss.getContextClassLoader() ) {
   105             if( classLoader == ss.getContextClassLoader() ) {
   105                 debugPrintln("using thread context class loader ("+classLoader+") for search");
   106                 debugPrintln(() -> "using thread context class loader ("+classLoader+") for search");
   106                 return;
   107                 return;
   107             }
   108             }
   108         } catch( Throwable unused ) {
   109         } catch( Throwable unused ) {
   109              // getContextClassLoader() undefined in JDK1.1
   110              // getContextClassLoader() undefined in JDK1.1
   110         }
   111         }
   111 
   112 
   112         if( classLoader==ClassLoader.getSystemClassLoader() ) {
   113         if( classLoader==ClassLoader.getSystemClassLoader() ) {
   113             debugPrintln("using system class loader ("+classLoader+") for search");
   114             debugPrintln(() -> "using system class loader ("+classLoader+") for search");
   114             return;
   115             return;
   115         }
   116         }
   116 
   117 
   117         debugPrintln("using class loader ("+classLoader+") for search");
   118         debugPrintln(() -> "using class loader ("+classLoader+") for search");
   118     }
   119     }
   119 
   120 
   120     /**
   121     /**
   121      * <p>Creates a new {@link XPathFactory} object for the specified
   122      * <p>Creates a new {@link XPathFactory} object for the specified
   122      * object model.</p>
   123      * object model.</p>
   133         if (uri == null) {
   134         if (uri == null) {
   134             throw new NullPointerException();
   135             throw new NullPointerException();
   135         }
   136         }
   136         XPathFactory f = _newFactory(uri);
   137         XPathFactory f = _newFactory(uri);
   137         if (f != null) {
   138         if (f != null) {
   138             debugPrintln("factory '" + f.getClass().getName() + "' was found for " + uri);
   139             debugPrintln(()->"factory '" + f.getClass().getName() + "' was found for " + uri);
   139         } else {
   140         } else {
   140             debugPrintln("unable to find a factory for " + uri);
   141             debugPrintln(()->"unable to find a factory for " + uri);
   141         }
   142         }
   142         return f;
   143         return f;
   143     }
   144     }
   144 
   145 
   145     /**
   146     /**
   154 
   155 
   155         String propertyName = SERVICE_CLASS.getName() + ":" + uri;
   156         String propertyName = SERVICE_CLASS.getName() + ":" + uri;
   156 
   157 
   157         // system property look up
   158         // system property look up
   158         try {
   159         try {
   159             debugPrintln("Looking up system property '"+propertyName+"'" );
   160             debugPrintln(()->"Looking up system property '"+propertyName+"'" );
   160             String r = ss.getSystemProperty(propertyName);
   161             String r = ss.getSystemProperty(propertyName);
   161             if(r!=null) {
   162             if(r!=null) {
   162                 debugPrintln("The value is '"+r+"'");
   163                 debugPrintln(()->"The value is '"+r+"'");
   163                 xpathFactory = createInstance(r, true);
   164                 xpathFactory = createInstance(r, true);
   164                 if (xpathFactory != null) {
   165                 if (xpathFactory != null) {
   165                     return xpathFactory;
   166                     return xpathFactory;
   166                 }
   167                 }
   167             } else
   168             } else
   168                 debugPrintln("The property is undefined.");
   169                 debugPrintln(()->"The property is undefined.");
   169         } catch( Throwable t ) {
   170         } catch( Throwable t ) {
   170             if( debug ) {
   171             if( debug ) {
   171                 debugPrintln("failed to look up system property '"+propertyName+"'" );
   172                 debugPrintln(()->"failed to look up system property '"+propertyName+"'" );
   172                 t.printStackTrace();
   173                 t.printStackTrace();
   173             }
   174             }
   174         }
   175         }
   175 
   176 
   176         String javah = ss.getSystemProperty( "java.home" );
   177         String javah = ss.getSystemProperty( "java.home" );
   183                 synchronized(cacheProps){
   184                 synchronized(cacheProps){
   184                     if(firstTime){
   185                     if(firstTime){
   185                         File f=new File( configFile );
   186                         File f=new File( configFile );
   186                         firstTime = false;
   187                         firstTime = false;
   187                         if(ss.doesFileExist(f)){
   188                         if(ss.doesFileExist(f)){
   188                             debugPrintln("Read properties file " + f);
   189                             debugPrintln(()->"Read properties file " + f);
   189                             cacheProps.load(ss.getFileInputStream(f));
   190                             cacheProps.load(ss.getFileInputStream(f));
   190                         }
   191                         }
   191                     }
   192                     }
   192                 }
   193                 }
   193             }
   194             }
   194             final String factoryClassName = cacheProps.getProperty(propertyName);
   195             final String factoryClassName = cacheProps.getProperty(propertyName);
   195             debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties");
   196             debugPrintln(()->"found " + factoryClassName + " in $java.home/jaxp.properties");
   196 
   197 
   197             if (factoryClassName != null) {
   198             if (factoryClassName != null) {
   198                 xpathFactory = createInstance(factoryClassName, true);
   199                 xpathFactory = createInstance(factoryClassName, true);
   199                 if(xpathFactory != null){
   200                 if(xpathFactory != null){
   200                     return xpathFactory;
   201                     return xpathFactory;
   218             return xpathFactory;
   219             return xpathFactory;
   219         }
   220         }
   220 
   221 
   221         // platform default
   222         // platform default
   222         if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
   223         if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
   223             debugPrintln("attempting to use the platform default W3C DOM XPath lib");
   224             debugPrintln(()->"attempting to use the platform default W3C DOM XPath lib");
   224             return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", true);
   225             return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", true);
   225         }
   226         }
   226 
   227 
   227         debugPrintln("all things were tried, but none was found. bailing out.");
   228         debugPrintln(()->"all things were tried, but none was found. bailing out.");
   228         return null;
   229         return null;
   229     }
   230     }
   230 
   231 
   231     /** <p>Create class using appropriate ClassLoader.</p>
   232     /** <p>Create class using appropriate ClassLoader.</p>
   232      *
   233      *
   278     XPathFactory createInstance( String className, boolean useServicesMechanism  )
   279     XPathFactory createInstance( String className, boolean useServicesMechanism  )
   279             throws XPathFactoryConfigurationException
   280             throws XPathFactoryConfigurationException
   280     {
   281     {
   281         XPathFactory xPathFactory = null;
   282         XPathFactory xPathFactory = null;
   282 
   283 
   283         debugPrintln("createInstance(" + className + ")");
   284         debugPrintln(()->"createInstance(" + className + ")");
   284 
   285 
   285         // get Class from className
   286         // get Class from className
   286         Class<?> clazz = createClass(className);
   287         Class<?> clazz = createClass(className);
   287         if (clazz == null) {
   288         if (clazz == null) {
   288             debugPrintln("failed to getClass(" + className + ")");
   289             debugPrintln(()->"failed to getClass(" + className + ")");
   289             return null;
   290             return null;
   290         }
   291         }
   291         debugPrintln("loaded " + className + " from " + which(clazz));
   292         debugPrintln(()->"loaded " + className + " from " + which(clazz));
   292 
   293 
   293         // instantiate Class as a XPathFactory
   294         // instantiate Class as a XPathFactory
   294         try {
   295         try {
   295             if (!useServicesMechanism) {
   296             if (!useServicesMechanism) {
   296                 xPathFactory = newInstanceNoServiceLoader(clazz);
   297                 xPathFactory = newInstanceNoServiceLoader(clazz);
   297             }
   298             }
   298             if (xPathFactory == null) {
   299             if (xPathFactory == null) {
   299                 xPathFactory = (XPathFactory) clazz.newInstance();
   300                 xPathFactory = (XPathFactory) clazz.newInstance();
   300             }
   301             }
   301         } catch (ClassCastException classCastException) {
   302         } catch (ClassCastException classCastException) {
   302                 debugPrintln("could not instantiate " + clazz.getName());
   303                 debugPrintln(()->"could not instantiate " + clazz.getName());
   303                 if (debug) {
   304                 if (debug) {
   304                         classCastException.printStackTrace();
   305                         classCastException.printStackTrace();
   305                 }
   306                 }
   306                 return null;
   307                 return null;
   307         } catch (IllegalAccessException illegalAccessException) {
   308         } catch (IllegalAccessException illegalAccessException) {
   308                 debugPrintln("could not instantiate " + clazz.getName());
   309                 debugPrintln(()->"could not instantiate " + clazz.getName());
   309                 if (debug) {
   310                 if (debug) {
   310                         illegalAccessException.printStackTrace();
   311                         illegalAccessException.printStackTrace();
   311                 }
   312                 }
   312                 return null;
   313                 return null;
   313         } catch (InstantiationException instantiationException) {
   314         } catch (InstantiationException instantiationException) {
   314                 debugPrintln("could not instantiate " + clazz.getName());
   315                 debugPrintln(()->"could not instantiate " + clazz.getName());
   315                 if (debug) {
   316                 if (debug) {
   316                         instantiationException.printStackTrace();
   317                         instantiationException.printStackTrace();
   317                 }
   318                 }
   318                 return null;
   319                 return null;
   319         }
   320         }