src/java.xml/share/classes/com/sun/org/apache/xerces/internal/utils/ObjectFactory.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47312 d4f959806fe9
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
     3
 * @LastModified: Oct 2017
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
     4
 */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 42806
diff changeset
    11
 * the License.  You may obtain a copy of the License at
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    12
 *
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    14
 *
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    19
 * limitations under the License.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    20
 */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    21
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    22
package com.sun.org.apache.xerces.internal.utils;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    23
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
    24
import java.util.function.Supplier;
47312
d4f959806fe9 8187593: Cleanup: removing SecuritySupport files
joehw
parents: 47216
diff changeset
    25
import jdk.xml.internal.SecuritySupport;
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
    26
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    27
/**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    28
 * This class is duplicated for each JAXP subpackage so keep it in sync.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    29
 * It is package private and therefore is not exposed as part of the JAXP
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    30
 * API.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    31
 * <p>
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    32
 * This code is designed to implement the JAXP 1.1 spec pluggability
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    33
 * feature and is designed to run on JDK version 1.1 and
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    34
 * later, and to compile on JDK 1.2 and onward.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    35
 * The code also runs both as part of an unbundled jar file and
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    36
 * when bundled as part of the JDK.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    37
 * <p>
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    38
 *
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    39
 */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    40
public final class ObjectFactory {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    41
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    42
    //
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    43
    // Constants
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    44
    //
25590
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
    45
    private static final String JAXP_INTERNAL = "com.sun.org.apache";
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
    46
    private static final String STAX_INTERNAL = "com.sun.xml.internal";
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    47
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    48
    /** Set to true for debugging */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    49
    private static final boolean DEBUG = isDebugEnabled();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    50
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    51
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    52
    //
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    53
    // Private static methods
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    54
    //
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    55
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    56
    /** Returns true if debug has been enabled. */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    57
    private static boolean isDebugEnabled() {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    58
        try {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    59
            String val = SecuritySupport.getSystemProperty("xerces.debug");
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    60
            // Allow simply setting the prop to turn on debug
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    61
            return (val != null && (!"false".equals(val)));
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    62
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    63
        catch (SecurityException se) {}
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    64
        return false;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    65
    } // isDebugEnabled()
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    66
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    67
    /** Prints a message to standard error if debugging is enabled. */
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
    68
    private static void debugPrintln(Supplier<String> msgGen) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    69
        if (DEBUG) {
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
    70
            System.err.println("XERCES: " + msgGen.get());
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    71
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    72
    } // debugPrintln(String)
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    73
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    74
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    75
     * Figure out which ClassLoader to use.  For JDK 1.2 and later use
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    76
     * the context ClassLoader.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    77
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    78
    public static ClassLoader findClassLoader()
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    79
        throws ConfigurationError
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    80
    {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    81
        if (System.getSecurityManager()!=null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    82
            //this will ensure bootclassloader is used
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    83
            return null;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    84
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    85
        // Figure out which ClassLoader to use for loading the provider
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    86
        // class.  If there is a Context ClassLoader then use it.
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    87
        ClassLoader context = SecuritySupport.getContextClassLoader();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    88
        ClassLoader system = SecuritySupport.getSystemClassLoader();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    89
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    90
        ClassLoader chain = system;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    91
        while (true) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    92
            if (context == chain) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    93
                // Assert: we are on JDK 1.1 or we have no Context ClassLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    94
                // or any Context ClassLoader in chain of system classloader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    95
                // (including extension ClassLoader) so extend to widest
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    96
                // ClassLoader (always look in system ClassLoader if Xerces
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    97
                // is in boot/extension/system classpath and in current
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    98
                // ClassLoader otherwise); normal classloaders delegate
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
    99
                // back to system ClassLoader first so this widening doesn't
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   100
                // change the fact that context ClassLoader will be consulted
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   101
                ClassLoader current = ObjectFactory.class.getClassLoader();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   102
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   103
                chain = system;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   104
                while (true) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   105
                    if (current == chain) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   106
                        // Assert: Current ClassLoader in chain of
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   107
                        // boot/extension/system ClassLoaders
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   108
                        return system;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   109
                    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   110
                    if (chain == null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   111
                        break;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   112
                    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   113
                    chain = SecuritySupport.getParentClassLoader(chain);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   114
                }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   115
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   116
                // Assert: Current ClassLoader not in chain of
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   117
                // boot/extension/system ClassLoaders
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   118
                return current;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   119
            }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   120
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   121
            if (chain == null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   122
                // boot ClassLoader reached
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   123
                break;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   124
            }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   125
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   126
            // Check for any extension ClassLoaders in chain up to
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   127
            // boot ClassLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   128
            chain = SecuritySupport.getParentClassLoader(chain);
18352
a1e183c996d6 8013434: Xalan and Xerces internal ObjectFactory need rework
dfuchs
parents: 16953
diff changeset
   129
        }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   130
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   131
        // Assert: Context ClassLoader not in chain of
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   132
        // boot/extension/system ClassLoaders
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   133
        return context;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   134
    } // findClassLoader():ClassLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   135
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   136
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   137
     * Create an instance of a class using the same classloader for the ObjectFactory by default
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   138
     * or bootclassloader when Security Manager is in place
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   139
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   140
    public static Object newInstance(String className, boolean doFallback)
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   141
        throws ConfigurationError
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   142
    {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   143
        if (System.getSecurityManager()!=null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   144
            return newInstance(className, null, doFallback);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   145
        } else {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   146
            return newInstance(className,
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   147
                findClassLoader (), doFallback);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   148
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   149
    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   150
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   151
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   152
     * Create an instance of a class using the specified ClassLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   153
     */
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   154
    public static Object newInstance(String className, ClassLoader cl,
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   155
                                      boolean doFallback)
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   156
        throws ConfigurationError
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   157
    {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   158
        // assert(className != null);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   159
        try{
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
   160
            Class<?> providerClass = findProviderClass(className, cl, doFallback);
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 44797
diff changeset
   161
            Object instance = providerClass.getConstructor().newInstance();
42806
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
   162
            debugPrintln(()->"created new instance of " + providerClass +
35843e3d5ef1 8146271: File system contention in debug print via XPathFactory.newInstance
aefimov
parents: 25868
diff changeset
   163
                             " using ClassLoader: " + cl);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   164
            return instance;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   165
        } catch (ClassNotFoundException x) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   166
            throw new ConfigurationError(
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   167
                "Provider " + className + " not found", x);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   168
        } catch (Exception x) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   169
            throw new ConfigurationError(
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   170
                "Provider " + className + " could not be instantiated: " + x,
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   171
                x);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   172
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   173
    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   174
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   175
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   176
     * Find a Class using the same classloader for the ObjectFactory by default
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   177
     * or bootclassloader when Security Manager is in place
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   178
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
   179
    public static Class<?> findProviderClass(String className, boolean doFallback)
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   180
        throws ClassNotFoundException, ConfigurationError
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   181
    {
25590
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
   182
        return findProviderClass (className,
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   183
                findClassLoader (), doFallback);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   184
    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   185
    /**
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   186
     * Find a Class using the specified ClassLoader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   187
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
   188
    public static Class<?> findProviderClass(String className, ClassLoader cl,
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   189
                                      boolean doFallback)
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   190
        throws ClassNotFoundException, ConfigurationError
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   191
    {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   192
        //throw security exception if the calling thread is not allowed to access the package
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   193
        //restrict the access to package as speicified in java.security policy
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   194
        SecurityManager security = System.getSecurityManager();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   195
        if (security != null) {
25590
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
   196
            if (className.startsWith(JAXP_INTERNAL) ||
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
   197
                    className.startsWith(STAX_INTERNAL)) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   198
                cl = null;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   199
            } else {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   200
                final int lastDot = className.lastIndexOf(".");
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   201
                String packageName = className;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   202
                if (lastDot != -1) packageName = className.substring(0, lastDot);
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   203
                security.checkPackageAccess(packageName);
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   204
            }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   205
        }
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47312
diff changeset
   206
        Class<?> providerClass;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   207
        if (cl == null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   208
            //use the bootstrap ClassLoader.
25590
fe5f84c49b8e 8031330: Refactor ObjectFactory
joehw
parents: 25264
diff changeset
   209
            providerClass = Class.forName(className, false, ObjectFactory.class.getClassLoader());
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   210
        } else {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   211
            try {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   212
                providerClass = cl.loadClass(className);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   213
            } catch (ClassNotFoundException x) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   214
                if (doFallback) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   215
                    // Fall back to current classloader
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   216
                    ClassLoader current = ObjectFactory.class.getClassLoader();
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   217
                    if (current == null) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   218
                        providerClass = Class.forName(className);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   219
                    } else if (cl != current) {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   220
                        cl = current;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   221
                        providerClass = cl.loadClass(className);
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   222
                    } else {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   223
                        throw x;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   224
                    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   225
                } else {
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   226
                    throw x;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   227
                }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   228
            }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   229
        }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   230
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   231
        return providerClass;
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   232
    }
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   233
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents:
diff changeset
   234
} // class ObjectFactory