jaxp/src/javax/xml/stream/FactoryFinder.java
author joehw
Wed, 04 Dec 2013 00:17:12 -0800
changeset 21997 65d2e8e1d666
parent 20965 913493fcadde
permissions -rw-r--r--
8027973: Error in the documentation for newFactory method of the javax.xml.stream factories Reviewed-by: alanb, dfuchs, lancea, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     1
/*
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
     2
 * Copyright (c) 2005, 2013, 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.stream;
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;
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    29
import java.security.AccessController;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    30
import java.security.PrivilegedAction;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    31
import java.util.Iterator;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    32
import java.util.Properties;
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    33
import java.util.ServiceConfigurationError;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    34
import java.util.ServiceLoader;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    35
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    36
/**
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    37
 * <p>Implements pluggable streams.</p>
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    38
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    39
 * <p>This class is duplicated for each JAXP subpackage so keep it in
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    40
 * sync.  It is package private for secure class loading.</p>
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
 * @author Santiago.PericasGeertsen@sun.com
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
class FactoryFinder {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    45
    // Check we have access to package.
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    46
    private static final String DEFAULT_PACKAGE = "com.sun.xml.internal.";
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    47
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    48
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    49
     * Internal debug flag.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    50
     */
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
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    53
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    54
     * Cache for properties in java.home/lib/jaxp.properties
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    55
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    56
    final private static Properties cacheProps = new Properties();
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    57
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
     * Flag indicating if properties from java.home/lib/jaxp.properties
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    60
     * have been cached.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    61
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    62
    private static volatile boolean firstTime = true;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    63
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    64
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    65
     * Security support class use to check access control before
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    66
     * getting certain system resources.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    67
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
    68
    final private static SecuritySupport ss = new SecuritySupport();
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    69
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    70
    // Define system property "jaxp.debug" to get output
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    71
    static {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    72
        // Use try/catch block to support applets, which throws
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    73
        // SecurityException out of this code.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    74
        try {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    75
            String val = ss.getSystemProperty("jaxp.debug");
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    76
            // Allow simply setting the prop to turn on debug
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    77
            debug = val != null && !"false".equals(val);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    78
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    79
        catch (SecurityException se) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    80
            debug = false;
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
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    84
    private static void dPrint(String msg) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    85
        if (debug) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    86
            System.err.println("JAXP: " + msg);
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
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    89
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
     * Attempt to load a class using the class loader supplied. If that fails
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    92
     * and fall back is enabled, the current (i.e. bootstrap) class loader is
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    93
     * tried.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    94
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    95
     * If the class loader supplied is <code>null</code>, first try using the
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    96
     * context class loader followed by the current (i.e. bootstrap) class
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    97
     * loader.
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    98
     *
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
    99
     * Use bootstrap classLoader if cl = null and useBSClsLoader is true
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   100
     */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   101
    static private Class getProviderClass(String className, ClassLoader cl,
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   102
            boolean doFallback, boolean useBSClsLoader) throws ClassNotFoundException
12005
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
        try {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   105
            if (cl == null) {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   106
                if (useBSClsLoader) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   107
                    return Class.forName(className, false, FactoryFinder.class.getClassLoader());
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   108
                } else {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   109
                    cl = ss.getContextClassLoader();
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   110
                    if (cl == null) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   111
                        throw new ClassNotFoundException();
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   112
                    }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   113
                    else {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   114
                        return Class.forName(className, false, cl);
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   115
                    }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   116
                }
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
            else {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   119
                return Class.forName(className, false, cl);
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
        catch (ClassNotFoundException e1) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   123
            if (doFallback) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   124
                // Use current class loader - should always be bootstrap CL
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   125
                return Class.forName(className, false, FactoryFinder.class.getClassLoader());
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   126
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   127
            else {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   128
                throw e1;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   129
            }
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
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   132
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   133
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   134
     * Create an instance of a class. Delegates to method
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   135
     * <code>getProviderClass()</code> in order to load the class.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   136
     *
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   137
     * @param type Base class / Service interface  of the factory to
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   138
     *             instantiate.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   139
     *
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   140
     * @param className Name of the concrete class corresponding to the
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   141
     * service provider
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   142
     *
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   143
     * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   144
     * current <code>Thread</code>'s context classLoader is used to load the factory class.
12005
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
     * @param doFallback True if the current ClassLoader should be tried as
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   147
     * a fallback if the class is not found using cl
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   148
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   149
    static <T> T newInstance(Class<T> type, String className, ClassLoader cl, boolean doFallback)
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   150
        throws FactoryConfigurationError
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   151
    {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   152
        return newInstance(type, className, cl, doFallback, false);
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   153
    }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   154
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   155
    /**
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   156
     * Create an instance of a class. Delegates to method
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   157
     * <code>getProviderClass()</code> in order to load the class.
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   158
     *
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   159
     * @param type Base class / Service interface  of the factory to
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   160
     *             instantiate.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   161
     *
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   162
     * @param className Name of the concrete class corresponding to the
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   163
     * service provider
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   164
     *
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   165
     * @param cl <code>ClassLoader</code> used to load the factory class. If <code>null</code>
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   166
     * current <code>Thread</code>'s context classLoader is used to load the factory class.
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   167
     *
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   168
     * @param doFallback True if the current ClassLoader should be tried as
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   169
     * a fallback if the class is not found using cl
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   170
     *
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   171
     * @param useBSClsLoader True if cl=null actually meant bootstrap classLoader. This parameter
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   172
     * is needed since DocumentBuilderFactory/SAXParserFactory defined null as context classLoader.
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   173
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   174
    static <T> T newInstance(Class<T> type, String className, ClassLoader cl,
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   175
                              boolean doFallback, boolean useBSClsLoader)
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   176
        throws FactoryConfigurationError
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   177
    {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   178
        assert type != null;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   179
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   180
        // make sure we have access to restricted packages
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   181
        if (System.getSecurityManager() != null) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   182
            if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   183
                cl = null;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   184
                useBSClsLoader = true;
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   185
            }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   186
        }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12458
diff changeset
   187
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   188
        try {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   189
            Class<?> providerClass = getProviderClass(className, cl, doFallback, useBSClsLoader);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   190
            if (!type.isAssignableFrom(providerClass)) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   191
                throw new ClassCastException(className + " cannot be cast to " + type.getName());
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   192
            }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   193
            Object instance = providerClass.newInstance();
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   194
            if (debug) {    // Extra check to avoid computing cl strings
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   195
                dPrint("created new instance of " + providerClass +
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   196
                       " using ClassLoader: " + cl);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   197
            }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   198
            return type.cast(instance);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   199
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   200
        catch (ClassNotFoundException x) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   201
            throw new FactoryConfigurationError(
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   202
                "Provider " + className + " not found", x);
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
        catch (Exception x) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   205
            throw new FactoryConfigurationError(
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   206
                "Provider " + className + " could not be instantiated: " + x,
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   207
                x);
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
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   211
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   212
     * Finds the implementation Class object in the specified order.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   213
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   214
     * @return Class object of factory, never null
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   215
     *
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   216
     * @param type                  Base class / Service interface  of the
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   217
     *                              factory to find.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   218
     *
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   219
     * @param fallbackClassName     Implementation class name, if nothing else
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   220
     *                              is found.  Use null to mean no fallback.
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
     * Package private so this code can be shared.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   223
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   224
    static <T> T find(Class<T> type, String fallbackClassName)
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   225
        throws FactoryConfigurationError
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   226
    {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   227
        return find(type, type.getName(), null, fallbackClassName);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   228
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   229
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   230
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   231
     * Finds the implementation Class object in the specified order.  Main
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   232
     * entry point.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   233
     * @return Class object of factory, never null
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   234
     *
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   235
     * @param type                  Base class / Service interface  of the
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   236
     *                              factory to find.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   237
     *
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   238
     * @param factoryId             Name of the factory to find, same as
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   239
     *                              a property name
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   240
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   241
     * @param cl                    ClassLoader to be used to load the class, null means to use
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   242
     * the bootstrap ClassLoader
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   243
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   244
     * @param fallbackClassName     Implementation class name, if nothing else
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   245
     *                              is found.  Use null to mean no fallback.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   246
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   247
     * Package private so this code can be shared.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   248
     */
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   249
    static <T> T find(Class<T> type, String factoryId, ClassLoader cl, String fallbackClassName)
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   250
        throws FactoryConfigurationError
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   251
    {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   252
        dPrint("find factoryId =" + factoryId);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   253
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   254
        // Use the system property first
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   255
        try {
20965
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   256
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   257
            final String systemProp;
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   258
            if (type.getName().equals(factoryId)) {
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   259
                systemProp = ss.getSystemProperty(factoryId);
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   260
            } else {
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   261
                systemProp = System.getProperty(factoryId);
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   262
            }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   263
            if (systemProp != null) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   264
                dPrint("found system property, value=" + systemProp);
21997
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   265
                return newInstance(type, systemProp, cl, true);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   266
            }
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
        catch (SecurityException se) {
20965
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   269
            throw new FactoryConfigurationError(
913493fcadde 8013502: Improve stream factories
dfuchs
parents: 17264
diff changeset
   270
                    "Failed to read factoryId '" + factoryId + "'", se);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   271
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   272
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   273
        // Try read $java.home/lib/stax.properties followed by
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   274
        // $java.home/lib/jaxp.properties if former not present
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   275
        String configFile = null;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   276
        try {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   277
            if (firstTime) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   278
                synchronized (cacheProps) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   279
                    if (firstTime) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   280
                        configFile = ss.getSystemProperty("java.home") + File.separator +
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   281
                            "lib" + File.separator + "stax.properties";
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   282
                        File f = new File(configFile);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   283
                        firstTime = false;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   284
                        if (ss.doesFileExist(f)) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   285
                            dPrint("Read properties file "+f);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   286
                            cacheProps.load(ss.getFileInputStream(f));
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   287
                        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   288
                        else {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   289
                            configFile = ss.getSystemProperty("java.home") + File.separator +
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   290
                                "lib" + File.separator + "jaxp.properties";
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   291
                            f = new File(configFile);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   292
                            if (ss.doesFileExist(f)) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   293
                                dPrint("Read properties file "+f);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   294
                                cacheProps.load(ss.getFileInputStream(f));
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   295
                            }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   296
                        }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   297
                    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   298
                }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   299
            }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   300
            final String factoryClassName = cacheProps.getProperty(factoryId);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   301
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   302
            if (factoryClassName != null) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   303
                dPrint("found in " + configFile + " value=" + factoryClassName);
21997
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   304
                return newInstance(type, factoryClassName, cl, true);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   305
            }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   306
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   307
        catch (Exception ex) {
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   308
            if (debug) ex.printStackTrace();
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   309
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   310
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   311
        if (type.getName().equals(factoryId)) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   312
            // Try Jar Service Provider Mechanism
21997
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   313
            final T provider = findServiceProvider(type, cl);
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   314
            if (provider != null) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   315
                return provider;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   316
            }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   317
        } else {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   318
            // We're in the case where a 'custom' factoryId was provided,
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   319
            // and in every case where that happens, we expect that
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   320
            // fallbackClassName will be null.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   321
            assert fallbackClassName == null;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   322
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   323
        if (fallbackClassName == null) {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   324
            throw new FactoryConfigurationError(
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   325
                "Provider for " + factoryId + " cannot be found", null);
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   326
        }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   327
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   328
        dPrint("loaded from fallback value: " + fallbackClassName);
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   329
        return newInstance(type, fallbackClassName, cl, true);
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   330
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   331
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
     * Try to find provider using the ServiceLoader API
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   334
     *
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   335
     * @param type Base class / Service interface  of the factory to find.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   336
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   337
     * @return instance of provider class if found or null
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   338
     */
21997
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   339
    private static <T> T findServiceProvider(final Class<T> type, final ClassLoader cl) {
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   340
        try {
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   341
            return AccessController.doPrivileged(new PrivilegedAction<T>() {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   342
                @Override
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   343
                public T run() {
21997
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   344
                    final ServiceLoader<T> serviceLoader;
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   345
                    if (cl == null) {
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   346
                        //the current thread's context class loader
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   347
                        serviceLoader = ServiceLoader.load(type);
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   348
                    } else {
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   349
                        serviceLoader = ServiceLoader.load(type, cl);
65d2e8e1d666 8027973: Error in the documentation for newFactory method of the javax.xml.stream factories
joehw
parents: 20965
diff changeset
   350
                    }
17264
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   351
                    final Iterator<T> iterator = serviceLoader.iterator();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   352
                    if (iterator.hasNext()) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   353
                        return iterator.next();
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   354
                    } else {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   355
                        return null;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   356
                    }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   357
                }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   358
            });
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   359
        } catch(ServiceConfigurationError e) {
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   360
            // It is not possible to wrap an error directly in
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   361
            // FactoryConfigurationError - so we need to wrap the
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   362
            // ServiceConfigurationError in a RuntimeException.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   363
            // The alternative would be to modify the logic in
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   364
            // FactoryConfigurationError to allow setting a
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   365
            // Throwable as the cause, but that could cause
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   366
            // compatibility issues down the road.
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   367
            final RuntimeException x = new RuntimeException(
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   368
                    "Provider for " + type + " cannot be created", e);
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   369
            final FactoryConfigurationError error =
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   370
                    new FactoryConfigurationError(x, x.getMessage());
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   371
            throw error;
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   372
          }
3aff554ad461 8005954: JAXP Plugability Layer should use java.util.ServiceLoader
dfuchs
parents: 16953
diff changeset
   373
      }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   374
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
   375
}