jaxp/src/share/classes/javax/xml/xpath/XPathFactoryFinder.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 2004-2005 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.xpath;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import java.io.File;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import java.io.InputStream;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import java.io.InputStreamReader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import java.net.URL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import java.util.Enumeration;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import java.util.Iterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import java.util.NoSuchElementException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import java.util.Properties;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * Implementation of {@link XPathFactory#newInstance(String)}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * @author <a href="Kohsuke.Kawaguchi@Sun.com">Kohsuke Kawaguchi</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * @since 1.5
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
class XPathFactoryFinder  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    private static SecuritySupport ss = new SecuritySupport() ;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    /** debug support code. */
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    private static boolean debug = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    static {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        // Use try/catch block to support applets
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
            debug = ss.getSystemProperty("jaxp.debug") != null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        } catch (Exception _) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
            debug = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     * <p>Cache properties for performance.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
        private static Properties cacheProps = new Properties();
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
         * <p>First time requires initialization overhead.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
         */
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        private static boolean firstTime = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     * <p>Conditional debug printing.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * @param msg to print
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    private static void debugPrintln(String msg) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
        if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            System.err.println("JAXP: " + msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     * <p><code>ClassLoader</code> to use to find <code>XPathFactory</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    private final ClassLoader classLoader;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * <p>Constructor that specifies <code>ClassLoader</code> to use
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     * to find <code>XPathFactory</code>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
     * @param loader
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     *      to be used to load resource, {@link XPathFactory}, and
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     *      {@link SchemaFactoryLoader} implementations during
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     *      the resolution process.
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
     *      If this parameter is null, the default system class loader
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
     *      will be used.
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    public XPathFactoryFinder(ClassLoader loader) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        this.classLoader = loader;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        if( debug ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            debugDisplayClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    private void debugDisplayClassLoader() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
            if( classLoader == ss.getContextClassLoader() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
                debugPrintln("using thread context class loader ("+classLoader+") for search");
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
                return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        } catch( Throwable _ ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
            ; // getContextClassLoader() undefined in JDK1.1
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        if( classLoader==ClassLoader.getSystemClassLoader() ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            debugPrintln("using system class loader ("+classLoader+") for search");
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        debugPrintln("using class loader ("+classLoader+") for search");
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
     * <p>Creates a new {@link XPathFactory} object for the specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * schema language.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     * @param uri
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     *       Identifies the underlying object model.
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * @return <code>null</code> if the callee fails to create one.
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     * @throws NullPointerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
     *      If the parameter is null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    public XPathFactory newFactory(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        if(uri==null)        throw new NullPointerException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        XPathFactory f = _newFactory(uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        if (f != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
            debugPrintln("factory '" + f.getClass().getName() + "' was found for " + uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            debugPrintln("unable to find a factory for " + uri);
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
        return f;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     * <p>Lookup a {@link XPathFactory} for the given object model.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * @param uri identifies the object model.
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     * @return {@link XPathFactory} for the given object model.
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
    private XPathFactory _newFactory(String uri) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        XPathFactory xpathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        String propertyName = SERVICE_CLASS.getName() + ":" + uri;
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        // system property look up
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
            debugPrintln("Looking up system property '"+propertyName+"'" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
            String r = ss.getSystemProperty(propertyName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
            if(r!=null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
                debugPrintln("The value is '"+r+"'");
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
                xpathFactory = createInstance(r);
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                if(xpathFactory != null)    return xpathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            } else
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
                debugPrintln("The property is undefined.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        } catch( Throwable t ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
            if( debug ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
                debugPrintln("failed to look up system property '"+propertyName+"'" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
                t.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        String javah = ss.getSystemProperty( "java.home" );
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        String configFile = javah + File.separator +
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        "lib" + File.separator + "jaxp.properties";
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        String factoryClassName = null ;
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        // try to read from $java.home/lib/jaxp.properties
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
            if(firstTime){
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
                synchronized(cacheProps){
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
                    if(firstTime){
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
                        File f=new File( configFile );
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
                        firstTime = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
                        if(ss.doesFileExist(f)){
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                            debugPrintln("Read properties file " + f);
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                            cacheProps.load(ss.getFileInputStream(f));
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
            factoryClassName = cacheProps.getProperty(propertyName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
            debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties");
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
            if (factoryClassName != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                xpathFactory = createInstance(factoryClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                if(xpathFactory != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                    return xpathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
        } catch (Exception ex) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
            if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                ex.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        // try META-INF/services files
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
        Iterator sitr = createServiceFileIterator();
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        while(sitr.hasNext()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
            URL resource = (URL)sitr.next();
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
            debugPrintln("looking into " + resource);
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
                xpathFactory = loadFromService(uri, resource.toExternalForm(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
                                                ss.getURLInputStream(resource));
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
                if (xpathFactory != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
                    return xpathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
            } catch(IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
                if( debug ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
                    debugPrintln("failed to read "+resource);
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
                    e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
        // platform default
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
        if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
            debugPrintln("attempting to use the platform default W3C DOM XPath lib");
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
            return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl");
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
        debugPrintln("all things were tried, but none was found. bailing out.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
    /** <p>Create class using appropriate ClassLoader.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
     * @param className Name of class to create.
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
     * @return Created class or <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    private Class createClass(String className) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
            Class clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
            // use approprite ClassLoader
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                    if (classLoader != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                            clazz = classLoader.loadClass(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
                    } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                            clazz = Class.forName(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
            } catch (Throwable t) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
                if(debug)   t.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
                    return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            return clazz;
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
     * <p>Creates an instance of the specified and returns it.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
     * @param className
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
     *      fully qualified class name to be instanciated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
     * @return null
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
     *      if it fails. Error messages will be printed by this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    XPathFactory createInstance( String className ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
        XPathFactory xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
        debugPrintln("createInstance(" + className + ")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
        // get Class from className
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
        Class clazz = createClass(className);
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        if (clazz == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
                debugPrintln("failed to getClass(" + className + ")");
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
        debugPrintln("loaded " + className + " from " + which(clazz));
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        // instantiate Class as a XPathFactory
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
        try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
                xPathFactory = (XPathFactory) clazz.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
        } catch (ClassCastException classCastException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
                debugPrintln("could not instantiate " + clazz.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
                if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
                        classCastException.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        } catch (IllegalAccessException illegalAccessException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
                debugPrintln("could not instantiate " + clazz.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
                if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
                        illegalAccessException.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
        } catch (InstantiationException instantiationException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
                debugPrintln("could not instantiate " + clazz.getName());
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
                if (debug) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
                        instantiationException.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
        return xPathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
     * <p>Look up a value in a property file.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
     * <p>Set <code>debug</code> to <code>true</code> to trace property evaluation.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
     * @param objectModel URI of object model to support.
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
     * @param inputName Name of <code>InputStream</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
     * @param in <code>InputStream</code> of properties.
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
     * @return <code>XPathFactory</code> as determined by <code>keyName</code> value or <code>null</code> if there was an error.
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
     * @throws IOException If IO error reading from <code>in</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    private XPathFactory loadFromService(
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
            String objectModel,
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
            String inputName,
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
            InputStream in)
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
            throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
            XPathFactory xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
            final Class[] stringClassArray = {"".getClass()};
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
            final Object[] objectModelObjectArray = {objectModel};
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
            final String isObjectModelSupportedMethod = "isObjectModelSupported";
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
            debugPrintln("Reading " + inputName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
            // read from InputStream until a match is found
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
            BufferedReader configFile = new BufferedReader(new InputStreamReader(in));
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
            String line = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
            while ((line = configFile.readLine()) != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
                    // '#' is comment char
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
                    int comment = line.indexOf("#");
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
                    switch (comment) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
                            case -1: break; // no comment
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
                            case 0: line = ""; break; // entire line is a comment
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
                            default: line = line.substring(0, comment); break; // trim comment
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
                    // trim whitespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
                    line = line.trim();
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
                    // any content left on line?
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
                    if (line.length() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
                    // line content is now the name of the class
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
                    Class clazz = createClass(line);
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
                    if (clazz == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                    // create an instance of the Class
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
                            xPathFactory = (XPathFactory) clazz.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
                    } catch (ClassCastException classCastExcpetion) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
                            xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
                    } catch (InstantiationException instantiationException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
                            xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
                    } catch (IllegalAccessException illegalAccessException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
                            xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
                            continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
                    // does this Class support desired object model?
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
                    try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                            Method isObjectModelSupported = clazz.getMethod(isObjectModelSupportedMethod, stringClassArray);
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
                            Boolean supported = (Boolean) isObjectModelSupported.invoke(xPathFactory, objectModelObjectArray);
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
                            if (supported.booleanValue()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
                                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
                            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
                    } catch (NoSuchMethodException noSuchMethodException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
                    } catch (IllegalAccessException illegalAccessException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
                    } catch (InvocationTargetException invocationTargetException) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
                    xPathFactory = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
            // clean up
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
            configFile.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
            // return new instance of XPathFactory or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
            return xPathFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
    /** Iterator that lazily computes one value and returns it. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    private static abstract class SingleIterator implements Iterator {
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        private boolean seen = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
        public final void remove() { throw new UnsupportedOperationException(); }
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
        public final boolean hasNext() { return !seen; }
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
        public final Object next() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
            if(seen)    throw new NoSuchElementException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
            seen = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
            return value();
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
        protected abstract Object value();
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
     * Looks up a value in a property file
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
     * while producing all sorts of debug messages.
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
     * @return null
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
     *      if there was an error.
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
    private XPathFactory loadFromProperty( String keyName, String resourceName, InputStream in )
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
        throws IOException {
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
        debugPrintln("Reading "+resourceName );
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
        Properties props = new Properties();
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
        props.load(in);
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
        in.close();
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
        String factoryClassName = props.getProperty(keyName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
        if(factoryClassName != null){
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
            debugPrintln("found "+keyName+" = " + factoryClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
            return createInstance(factoryClassName);
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
            debugPrintln(keyName+" is not in the property file");
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
     * Returns an {@link Iterator} that enumerates all
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
     * the META-INF/services files that we care.
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    private Iterator createServiceFileIterator() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
        if (classLoader == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
            return new SingleIterator() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
                protected Object value() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
                    ClassLoader classLoader = XPathFactoryFinder.class.getClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
                    return ss.getResourceAsURL(classLoader, SERVICE_ID);
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
                    //return (ClassLoader.getSystemResource( SERVICE_ID ));
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
            };
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
            try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
                //final Enumeration e = classLoader.getResources(SERVICE_ID);
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
                final Enumeration e = ss.getResources(classLoader, SERVICE_ID);
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
                if(!e.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
                    debugPrintln("no "+SERVICE_ID+" file was found");
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
                // wrap it into an Iterator.
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
                return new Iterator() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
                    public void remove() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
                        throw new UnsupportedOperationException();
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
                    public boolean hasNext() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
                        return e.hasMoreElements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
                    public Object next() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
                        return e.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
                };
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
            } catch (IOException e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
                debugPrintln("failed to enumerate resources "+SERVICE_ID);
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
                if(debug)   e.printStackTrace();
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
                return new ArrayList().iterator();  // empty iterator
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
    private static final Class SERVICE_CLASS = XPathFactory.class;
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
    private static final String SERVICE_ID = "META-INF/services/" + SERVICE_CLASS.getName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
    private static String which( Class clazz ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
        return which( clazz.getName(), clazz.getClassLoader() );
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
     * <p>Search the specified classloader for the given classname.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
     * @param classname the fully qualified name of the class to search for
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
     * @param loader the classloader to search
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
     * @return the source location of the resource, or null if it wasn't found
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
    private static String which(String classname, ClassLoader loader) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
        String classnameAsResource = classname.replace('.', '/') + ".class";
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        if( loader==null )  loader = ClassLoader.getSystemClassLoader();
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
        //URL it = loader.getResource(classnameAsResource);
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
        URL it = ss.getResourceAsURL(loader, classnameAsResource);
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
        if (it != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
            return it.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
        } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
}