src/java.xml/share/classes/javax/xml/xpath/XPathFactoryFinder.java
author joehw
Wed, 04 Oct 2017 10:54:18 -0700
changeset 47312 d4f959806fe9
parent 47216 71c04702a3d5
child 47359 e1a6c0168741
permissions -rw-r--r--
8187593: Cleanup: removing SecuritySupport files Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     1
/*
44378
06aa1b671d17 8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents: 42806
diff changeset
     2
 * Copyright (c) 2004, 2017, Oracle and/or its affiliates. All rights reserved.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     4
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    10
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    15
 * accompanied this code).
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    16
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    20
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    23
 * questions.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    24
 */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    25
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    26
package javax.xml.xpath;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    27
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    28
import java.io.File;
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
    29
import java.lang.reflect.InvocationTargetException;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    30
import java.lang.reflect.Method;
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    31
import java.lang.reflect.Modifier;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    32
import java.security.AccessControlContext;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    33
import java.security.AccessController;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    34
import java.security.PrivilegedAction;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    35
import java.util.Properties;
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    36
import java.util.ServiceConfigurationError;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    37
import java.util.ServiceLoader;
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
    38
import java.util.function.Supplier;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    39
import jdk.xml.internal.SecuritySupport;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    40
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    41
/**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    42
 * Implementation of {@link XPathFactory#newInstance(String)}.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    43
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    44
 * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    45
 * @since 1.5
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    46
 */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    47
class XPathFactoryFinder  {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
    48
    private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xpath.internal";
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    49
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    50
    /** debug support code. */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    51
    private static boolean debug = false;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    52
    static {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    53
        // Use try/catch block to support applets
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    54
        try {
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    55
            debug = SecuritySupport.getSystemProperty("jaxp.debug") != null;
15545
a19e13e7c521 8007389: Remove uses of _ as identifier in jaxp
emc
parents: 14942
diff changeset
    56
        } catch (Exception unused) {
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    57
            debug = false;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    58
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    59
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    60
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    61
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    62
     * <p>Cache properties for performance.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    63
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    64
    private static final Properties cacheProps = new Properties();
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    65
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    66
    /**
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    67
     * <p>First time requires initialization overhead.</p>
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    68
     */
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    69
    private volatile static boolean firstTime = true;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    70
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    71
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    72
     * <p>Conditional debug printing.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    73
     *
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
    74
     * @param msgGen Supplier function that returns debug message
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    75
     */
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
    76
    private static void debugPrintln(Supplier<String> msgGen) {
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    77
        if (debug) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
    78
            System.err.println("JAXP: " + msgGen.get());
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    79
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    80
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    81
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    82
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    83
     * <p><code>ClassLoader</code> to use to find <code>XPathFactory</code>.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    84
     */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    85
    private final ClassLoader classLoader;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    86
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    87
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    88
     * <p>Constructor that specifies <code>ClassLoader</code> to use
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    89
     * to find <code>XPathFactory</code>.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    90
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    91
     * @param loader
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    92
     *      to be used to load resource and {@link XPathFactory}
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    93
     *      implementations during the resolution process.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    94
     *      If this parameter is null, the default system class loader
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    95
     *      will be used.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    96
     */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    97
    public XPathFactoryFinder(ClassLoader loader) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    98
        this.classLoader = loader;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    99
        if( debug ) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   100
            debugDisplayClassLoader();
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   101
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   102
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   103
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   104
    private void debugDisplayClassLoader() {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   105
        try {
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   106
            if( classLoader == SecuritySupport.getContextClassLoader() ) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   107
                debugPrintln(() -> "using thread context class loader ("+classLoader+") for search");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   108
                return;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   109
            }
15545
a19e13e7c521 8007389: Remove uses of _ as identifier in jaxp
emc
parents: 14942
diff changeset
   110
        } catch( Throwable unused ) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   111
             // getContextClassLoader() undefined in JDK1.1
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   112
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   113
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   114
        if( classLoader==ClassLoader.getSystemClassLoader() ) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   115
            debugPrintln(() -> "using system class loader ("+classLoader+") for search");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   116
            return;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   117
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   118
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   119
        debugPrintln(() -> "using class loader ("+classLoader+") for search");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   120
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   121
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   122
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   123
     * <p>Creates a new {@link XPathFactory} object for the specified
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   124
     * object model.</p>
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   125
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   126
     * @param uri
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   127
     *       Identifies the underlying object model.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   128
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   129
     * @return <code>null</code> if the callee fails to create one.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   130
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   131
     * @throws NullPointerException
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   132
     *      If the parameter is null.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   133
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   134
    public XPathFactory newFactory(String uri) throws XPathFactoryConfigurationException {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   135
        if (uri == null) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   136
            throw new NullPointerException();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   137
        }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   138
        XPathFactory f = _newFactory(uri);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   139
        if (f != null) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   140
            debugPrintln(()->"factory '" + f.getClass().getName() + "' was found for " + uri);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   141
        } else {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   142
            debugPrintln(()->"unable to find a factory for " + uri);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   143
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   144
        return f;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   145
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   146
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   147
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   148
     * <p>Lookup a {@link XPathFactory} for the given object model.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   149
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   150
     * @param uri identifies the object model.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   151
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   152
     * @return {@link XPathFactory} for the given object model.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   153
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   154
    private XPathFactory _newFactory(String uri) throws XPathFactoryConfigurationException {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   155
        XPathFactory xpathFactory = null;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   156
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   157
        String propertyName = SERVICE_CLASS.getName() + ":" + uri;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   158
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   159
        // system property look up
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   160
        try {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   161
            debugPrintln(()->"Looking up system property '"+propertyName+"'" );
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   162
            String r = SecuritySupport.getSystemProperty(propertyName);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   163
            if(r!=null) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   164
                debugPrintln(()->"The value is '"+r+"'");
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   165
                xpathFactory = createInstance(r, true);
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   166
                if (xpathFactory != null) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   167
                    return xpathFactory;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   168
                }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   169
            } else
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   170
                debugPrintln(()->"The property is undefined.");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   171
        } catch( Throwable t ) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   172
            if( debug ) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   173
                debugPrintln(()->"failed to look up system property '"+propertyName+"'" );
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   174
                t.printStackTrace();
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   175
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   176
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   177
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   178
        String javah = SecuritySupport.getSystemProperty( "java.home" );
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   179
        String configFile = javah + File.separator +
44378
06aa1b671d17 8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents: 42806
diff changeset
   180
        "conf" + File.separator + "jaxp.properties";
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   181
44378
06aa1b671d17 8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents: 42806
diff changeset
   182
        // try to read from $java.home/conf/jaxp.properties
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   183
        try {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   184
            if(firstTime){
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   185
                synchronized(cacheProps){
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   186
                    if(firstTime){
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   187
                        File f=new File( configFile );
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   188
                        firstTime = false;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   189
                        if(SecuritySupport.doesFileExist(f)){
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   190
                            debugPrintln(()->"Read properties file " + f);
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   191
                            cacheProps.load(SecuritySupport.getFileInputStream(f));
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   192
                        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   193
                    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   194
                }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   195
            }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   196
            final String factoryClassName = cacheProps.getProperty(propertyName);
44378
06aa1b671d17 8177350: Two missed in the change from ${java.home}/lib to ${java.home}/conf
joehw
parents: 42806
diff changeset
   197
            debugPrintln(()->"found " + factoryClassName + " in $java.home/conf/jaxp.properties");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   198
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   199
            if (factoryClassName != null) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   200
                xpathFactory = createInstance(factoryClassName, true);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   201
                if(xpathFactory != null){
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   202
                    return xpathFactory;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   203
                }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   204
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   205
        } catch (Exception ex) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   206
            if (debug) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   207
                ex.printStackTrace();
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   208
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   209
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   210
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   211
        // Try with ServiceLoader
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   212
        assert xpathFactory == null;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   213
        xpathFactory = findServiceProvider(uri);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   214
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   215
        // The following assertion should always be true.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   216
        // Uncomment it, recompile, and run with -ea in case of doubts:
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   217
        // assert xpathFactory == null || xpathFactory.isObjectModelSupported(uri);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   218
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   219
        if (xpathFactory != null) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   220
            return xpathFactory;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   221
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   222
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   223
        // platform default
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   224
        if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   225
            debugPrintln(()->"attempting to use the platform default W3C DOM XPath lib");
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   226
            return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", true);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   227
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   228
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   229
        debugPrintln(()->"all things were tried, but none was found. bailing out.");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   230
        return null;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   231
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   232
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   233
    /** <p>Create class using appropriate ClassLoader.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   234
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   235
     * @param className Name of class to create.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   236
     * @return Created class or <code>null</code>.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   237
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   238
    private Class<?> createClass(String className) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   239
        Class clazz;
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   240
        // make sure we have access to restricted packages
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   241
        boolean internal = false;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   242
        if (System.getSecurityManager() != null) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   243
            if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   244
                internal = true;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   245
            }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   246
        }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   247
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   248
        // use approprite ClassLoader
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   249
        try {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   250
            if (classLoader != null && !internal) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   251
                    clazz = Class.forName(className, false, classLoader);
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   252
            } else {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   253
                    clazz = Class.forName(className);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   254
            }
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   255
        } catch (Throwable t) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   256
            if(debug) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   257
                t.printStackTrace();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   258
            }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   259
            return null;
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 15545
diff changeset
   260
        }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   261
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   262
        return clazz;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   263
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   264
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   265
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   266
     * <p>Creates an instance of the specified and returns it.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   267
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   268
     * @param className
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   269
     *      fully qualified class name to be instantiated.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   270
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   271
     * @return null
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   272
     *      if it fails. Error messages will be printed by this method.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   273
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   274
    XPathFactory createInstance( String className )
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   275
            throws XPathFactoryConfigurationException
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   276
    {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   277
        return createInstance( className, false );
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   278
    }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   279
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   280
    XPathFactory createInstance( String className, boolean useServicesMechanism  )
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   281
            throws XPathFactoryConfigurationException
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   282
    {
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   283
        XPathFactory xPathFactory = null;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   284
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   285
        debugPrintln(()->"createInstance(" + className + ")");
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   286
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   287
        // get Class from className
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   288
        Class<?> clazz = createClass(className);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   289
        if (clazz == null) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   290
            debugPrintln(()->"failed to getClass(" + className + ")");
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   291
            return null;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   292
        }
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 31497
diff changeset
   293
        debugPrintln(()->"loaded " + className + " from " + which(clazz));
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   294
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   295
        // instantiate Class as a XPathFactory
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   296
        try {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   297
            if (!useServicesMechanism) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   298
                xPathFactory = newInstanceNoServiceLoader(clazz);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   299
            }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   300
            if (xPathFactory == null) {
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   301
                xPathFactory = (XPathFactory) clazz.getConstructor().newInstance();
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   302
            }
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   303
        } catch (ClassCastException | IllegalAccessException | IllegalArgumentException |
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   304
            InstantiationException | InvocationTargetException | NoSuchMethodException |
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   305
            SecurityException ex) {
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   306
            debugPrintln(()->"could not instantiate " + clazz.getName());
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   307
            if (debug) {
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   308
                    ex.printStackTrace();
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   309
            }
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44378
diff changeset
   310
            return null;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   311
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   312
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   313
        return xPathFactory;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   314
    }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   315
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   316
     * Try to construct using newXPathFactoryNoServiceLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   317
     *   method if available.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   318
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   319
    private static XPathFactory newInstanceNoServiceLoader(
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   320
         Class<?> providerClass
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   321
    ) throws XPathFactoryConfigurationException {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   322
        // Retain maximum compatibility if no security manager.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   323
        if (System.getSecurityManager() == null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   324
            return null;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   325
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   326
        try {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   327
            Method creationMethod =
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   328
                    providerClass.getDeclaredMethod(
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   329
                        "newXPathFactoryNoServiceLoader"
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   330
                    );
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   331
            final int modifiers = creationMethod.getModifiers();
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   332
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   333
            // Do not call "newXPathFactoryNoServiceLoader" if it's
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   334
            // not public static.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   335
            if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   336
                return null;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   337
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   338
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   339
            // Only calls "newXPathFactoryNoServiceLoader" if it's
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   340
            // declared to return an instance of XPathFactory.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   341
            final Class<?> returnType = creationMethod.getReturnType();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   342
            if (SERVICE_CLASS.isAssignableFrom(returnType)) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   343
                return SERVICE_CLASS.cast(creationMethod.invoke(null, (Object[])null));
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   344
            } else {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   345
                // Should not happen since
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   346
                // XPathFactoryImpl.newXPathFactoryNoServiceLoader is
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   347
                // declared to return XPathFactory.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   348
                throw new ClassCastException(returnType
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   349
                            + " cannot be cast to " + SERVICE_CLASS);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   350
            }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   351
        } catch (ClassCastException e) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   352
            throw new XPathFactoryConfigurationException(e);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   353
        } catch (NoSuchMethodException exc) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   354
            return null;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   355
        } catch (Exception exc) {
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   356
            return null;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   357
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   358
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   359
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   360
    // Call isObjectModelSupportedBy with initial context.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   361
    private boolean isObjectModelSupportedBy(final XPathFactory factory,
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   362
            final String objectModel,
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   363
            AccessControlContext acc) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   364
        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   365
                    public Boolean run() {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   366
                        return factory.isObjectModelSupported(objectModel);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   367
                    }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   368
                }, acc);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   369
    }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   370
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   371
    /**
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   372
     * Finds a service provider subclass of XPathFactory that supports the
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   373
     * given object model using the ServiceLoader.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   374
     *
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   375
     * @param objectModel URI of object model to support.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   376
     * @return An XPathFactory supporting the specified object model, or null
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   377
     *         if none is found.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   378
     * @throws XPathFactoryConfigurationException if a configuration error is found.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   379
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   380
    private XPathFactory findServiceProvider(final String objectModel)
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   381
            throws XPathFactoryConfigurationException {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   382
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   383
        assert objectModel != null;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   384
        // store current context.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   385
        final AccessControlContext acc = AccessController.getContext();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   386
        try {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   387
            return AccessController.doPrivileged(new PrivilegedAction<XPathFactory>() {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   388
                public XPathFactory run() {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   389
                    final ServiceLoader<XPathFactory> loader =
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   390
                            ServiceLoader.load(SERVICE_CLASS);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   391
                    for (XPathFactory factory : loader) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   392
                        // restore initial context to call
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   393
                        // factory.isObjectModelSupportedBy
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   394
                        if (isObjectModelSupportedBy(factory, objectModel, acc)) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   395
                            return factory;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   396
                        }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   397
                    }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   398
                    return null; // no factory found.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   399
                }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   400
            });
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   401
        } catch (ServiceConfigurationError error) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   402
            throw new XPathFactoryConfigurationException(error);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   403
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   404
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   405
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   406
    private static final Class<XPathFactory> SERVICE_CLASS = XPathFactory.class;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   407
31287
d6581a7c221f 8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents: 28695
diff changeset
   408
    // Used for debugging purposes
d6581a7c221f 8129880: Cleanup usage of Class.getResource in jaxp
dfuchs
parents: 28695
diff changeset
   409
    private static String which( Class<?> clazz ) {
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
   410
        return SecuritySupport.getClassSource(clazz);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   411
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   412
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   413
}