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