19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: ObjectFactory.java 468637 2006-10-28 06:51:02Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xalan.extensions;
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class is duplicated for each JAXP subpackage so keep it in sync.
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * It is package private and therefore is not exposed as part of the JAXP
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * API.
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p>
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This code is designed to implement the JAXP 1.1 spec pluggability
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * feature and is designed to run on JDK version 1.1 and
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * later, and to compile on JDK 1.2 and onward.
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The code also runs both as part of an unbundled jar file and
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * when bundled as part of the JDK.
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p>
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class was moved from the <code>javax.xml.parsers.ObjectFactory</code>
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * class and modified to be used as a general utility for creating objects
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * dynamically.
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @version $Id: ObjectFactory.java 468637 2006-10-28 06:51:02Z minchau $
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonclass ObjectFactory {
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Figure out which ClassLoader to use.  For JDK 1.2 and later use
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * the context ClassLoader.
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static ClassLoader findClassLoader()
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        throws ConfigurationError
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // BEGIN android-changed
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //     the context class loader is always sufficient
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return Thread.currentThread().getContextClassLoader();
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // END android-changed
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    } // findClassLoader():ClassLoader
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * Find a Class using the specified ClassLoader
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static Class findProviderClass(String className, ClassLoader cl,
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                                           boolean doFallback)
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        throws ClassNotFoundException, ConfigurationError
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //throw security exception if the calling thread is not allowed to access the
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //class. Restrict the access to the package classes as specified in java.security policy.
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        SecurityManager security = System.getSecurityManager();
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        try{
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                if (security != null){
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    final int lastDot = className.lastIndexOf(".");
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    String packageName = className;
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    if (lastDot != -1) packageName = className.substring(0, lastDot);
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    security.checkPackageAccess(packageName);
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                 }
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }catch(SecurityException e){
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            throw e;
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        Class providerClass;
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (cl == null) {
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // XXX Use the bootstrap ClassLoader.  There is no way to
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // load a class using the bootstrap ClassLoader that works
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // in both JDK 1.1 and Java 2.  However, this should still
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // work b/c the following should be true:
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            //
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // (cl == null) iff current ClassLoader == null
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            //
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // Thus Class.forName(String) will use the current
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            // ClassLoader which will be the bootstrap ClassLoader.
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            providerClass = Class.forName(className);
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } else {
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            try {
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                providerClass = cl.loadClass(className);
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            } catch (ClassNotFoundException x) {
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                if (doFallback) {
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    // Fall back to current classloader
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    ClassLoader current = ObjectFactory.class.getClassLoader();
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    if (current == null) {
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        providerClass = Class.forName(className);
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    } else if (cl != current) {
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        cl = current;
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        providerClass = cl.loadClass(className);
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    } else {
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                        throw x;
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    }
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                } else {
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                    throw x;
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                }
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            }
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        return providerClass;
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Classes
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    //
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    /**
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     * A configuration error.
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson     */
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static class ConfigurationError
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        extends Error {
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson                static final long serialVersionUID = 8564305128443551853L;
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // Data
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        /** Exception. */
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        private Exception exception;
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // Constructors
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        /**
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson         * Construct a new instance with the specified detail string and
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson         * exception.
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson         */
1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        ConfigurationError(String msg, Exception x) {
1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            super(msg);
1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            this.exception = x;
1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } // <init>(String,Exception)
1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        // Public methods
1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        //
1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        /** Returns the exception associated to this error. */
1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        Exception getException() {
1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return exception;
1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        } // getException():Exception
1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    } // class ConfigurationError
1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} // class ObjectFactory
155