jaxp/src/share/classes/javax/xml/stream/FactoryFinder.java
author ohair
Mon, 14 Apr 2008 14:52:27 -0700
changeset 311 3c14f21bf3f7
parent 6 7f561c08de6b
permissions -rw-r--r--
6484686: The next directory looks like it is no longer part of the build (deploy makefiles) Summary: Getting rid of the _OUTPUTDIR settings. Using BUILD_PARENT_DIRECTORY instead. This solves problems with the "/build/windows-i586*" paths getting mangled on Windows builds (fastdebug builds in particular). Reviewed-by: tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 * have any questions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
package javax.xml.stream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import java.io.BufferedReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import java.io.InputStreamReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * <p>Implements pluggable Datatypes.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * <p>This class is duplicated for each JAXP subpackage so keep it in
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * sync.  It is package private for secure class loading.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * @author Santiago.PericasGeertsen@sun.com
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
class FactoryFinder {
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * Internal debug flag.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    private static boolean debug = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     * Cache for properties in java.home/lib/jaxp.properties
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    static Properties cacheProps = new Properties();
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
     * Flag indicating if properties from java.home/lib/jaxp.properties
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
     * have been cached.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    static boolean firstTime = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     * Security support class use to check access control before
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     * getting certain system resources.
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    static SecuritySupport ss = new SecuritySupport();
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    // Define system property "jaxp.debug" to get output
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    static {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        // Use try/catch block to support applets, which throws
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        // SecurityException out of this code.
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
            String val = ss.getSystemProperty("jaxp.debug");
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
            // Allow simply setting the prop to turn on debug
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
            debug = val != null && !"false".equals(val);
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        catch (SecurityException se) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            debug = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    private static void dPrint(String msg) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
        if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
            System.err.println("JAXP: " + msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Attempt to load a class using the class loader supplied. If that fails
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * and fall back is enabled, the current (i.e. bootstrap) class loader is
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     * tried.
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     * If the class loader supplied is <code>null</code>, first try using the
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * context class loader followed by the current (i.e. bootstrap) class
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     * loader.
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    static private Class getProviderClass(String className, ClassLoader cl,
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
            boolean doFallback) throws ClassNotFoundException
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
                cl = ss.getContextClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
                if (cl == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
                    throw new ClassNotFoundException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                    return cl.loadClass(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                return cl.loadClass(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        catch (ClassNotFoundException e1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
            if (doFallback) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
                // Use current class loader - should always be bootstrap CL
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
                return Class.forName(className, true, FactoryFinder.class.getClassLoader());
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
                throw e1;
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * Create an instance of a class. Delegates to method
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * <code>getProviderClass()</code> in order to load the class.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * @param className Name of the concrete class corresponding to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * service provider
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     * @param cl ClassLoader to use to load the class, null means to use
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     * the bootstrap ClassLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     * @param doFallback True if the current ClassLoader should be tried as
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
     * a fallback if the class is not found using cl
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    static Object newInstance(String className, ClassLoader cl, boolean doFallback)
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        throws ConfigurationError
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            Class providerClass = getProviderClass(className, cl, doFallback);
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            Object instance = providerClass.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            if (debug) {    // Extra check to avoid computing cl strings
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
                dPrint("created new instance of " + providerClass +
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
                       " using ClassLoader: " + cl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
            return instance;
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        catch (ClassNotFoundException x) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            throw new ConfigurationError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                "Provider " + className + " not found", x);
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        catch (Exception x) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
            throw new ConfigurationError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
                "Provider " + className + " could not be instantiated: " + x,
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
                x);
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     * Finds the implementation Class object in the specified order.  Main
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     * entry point.
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     * @return Class object of factory, never null
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
     * @param factoryId             Name of the factory to find, same as
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
     *                              a property name
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
     * @param fallbackClassName     Implementation class name, if nothing else
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
     *                              is found.  Use null to mean no fallback.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
     * Package private so this code can be shared.
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
    static Object find(String factoryId, String fallbackClassName)
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        throws ConfigurationError
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        dPrint("find factoryId =" + factoryId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        // Use the system property first
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            String systemProp = ss.getSystemProperty(factoryId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
            if (systemProp != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                dPrint("found system property, value=" + systemProp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
                return newInstance(systemProp, null, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
        catch (SecurityException se) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
            if (debug) se.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
        // Try read $java.home/lib/stax.properties followed by
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
        // $java.home/lib/jaxp.properties if former not present
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
        String configFile = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
            String factoryClassName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
            if (firstTime) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
                synchronized (cacheProps) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
                    if (firstTime) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
                        configFile = ss.getSystemProperty("java.home") + File.separator +
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
                            "lib" + File.separator + "stax.properties";
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                        File f = new File(configFile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                        firstTime = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                        if (ss.doesFileExist(f)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                            dPrint("Read properties file "+f);
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
                            cacheProps.load(ss.getFileInputStream(f));
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
                        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                            configFile = ss.getSystemProperty("java.home") + File.separator +
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
                                "lib" + File.separator + "jaxp.properties";
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
                            f = new File(configFile);
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                            if (ss.doesFileExist(f)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                                dPrint("Read properties file "+f);
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
                                cacheProps.load(ss.getFileInputStream(f));
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
            factoryClassName = cacheProps.getProperty(factoryId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            if (factoryClassName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
                dPrint("found in " + configFile + " value=" + factoryClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
                return newInstance(factoryClassName, null, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
        catch (Exception ex) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
            if (debug) ex.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        // Try Jar Service Provider Mechanism
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        Object provider = findJarServiceProvider(factoryId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
        if (provider != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
            return provider;
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        if (fallbackClassName == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            throw new ConfigurationError(
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
                "Provider for " + factoryId + " cannot be found", null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
        dPrint("loaded from fallback value: " + fallbackClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
        return newInstance(fallbackClassName, null, true);
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
    /*
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
     * Try to find provider using Jar Service Provider Mechanism
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
     * @return instance of provider class if found or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
    private static Object findJarServiceProvider(String factoryId)
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
        throws ConfigurationError
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
        String serviceId = "META-INF/services/" + factoryId;
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
        InputStream is = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
        // First try the Context ClassLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
        ClassLoader cl = ss.getContextClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
        if (cl != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
            is = ss.getResourceAsStream(cl, serviceId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
            // If no provider found then try the current ClassLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            if (is == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
                cl = FactoryFinder.class.getClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
                is = ss.getResourceAsStream(cl, serviceId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
            // No Context ClassLoader, try the current ClassLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
            cl = FactoryFinder.class.getClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            is = ss.getResourceAsStream(cl, serviceId);
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        if (is == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
            // No provider found
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        if (debug) {    // Extra check to avoid computing cl strings
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
            dPrint("found jar resource=" + serviceId + " using ClassLoader: " + cl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        BufferedReader rd;
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
            rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        catch (java.io.UnsupportedEncodingException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
            rd = new BufferedReader(new InputStreamReader(is));
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        String factoryClassName = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            // XXX Does not handle all possible input as specified by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
            // Jar Service Provider specification
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
            factoryClassName = rd.readLine();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
            rd.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
        } catch (IOException x) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
            // No provider found
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
        if (factoryClassName != null && !"".equals(factoryClassName)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
            dPrint("found in resource, value=" + factoryClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            // Note: here we do not want to fall back to the current
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
            // ClassLoader because we want to avoid the case where the
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            // resource file was found using one ClassLoader and the
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            // provider class was instantiated using a different one.
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
            return newInstance(factoryClassName, cl, false);
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
        // No provider found
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    static class ConfigurationError extends Error {
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
        private Exception exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
         * Construct a new instance with the specified detail string and
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
         * exception.
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
        ConfigurationError(String msg, Exception x) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
            super(msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
            this.exception = x;
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
        Exception getException() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
            return exception;
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
}