ohair@286: /*
mkos@384: * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286: *
ohair@286: * This code is free software; you can redistribute it and/or modify it
ohair@286: * under the terms of the GNU General Public License version 2 only, as
ohair@286: * published by the Free Software Foundation. Oracle designates this
ohair@286: * particular file as subject to the "Classpath" exception as provided
ohair@286: * by Oracle in the LICENSE file that accompanied this code.
ohair@286: *
ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286: * version 2 for more details (a copy is included in the LICENSE file that
ohair@286: * accompanied this code).
ohair@286: *
ohair@286: * You should have received a copy of the GNU General Public License version
ohair@286: * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286: *
ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286: * or visit www.oracle.com if you need additional information or have any
ohair@286: * questions.
ohair@286: */
ohair@286:
ohair@286: package javax.xml.soap;
ohair@286:
ohair@286: import java.io.*;
ohair@286: import java.util.Properties;
ohair@286:
ohair@286:
ohair@286: class FactoryFinder {
ohair@286:
ohair@286: /**
ohair@286: * Creates an instance of the specified class using the specified
ohair@286: * ClassLoader
object.
ohair@286: *
ohair@286: * @exception SOAPException if the given class could not be found
ohair@286: * or could not be instantiated
ohair@286: */
ohair@286: private static Object newInstance(String className,
mkos@384: ClassLoader classLoader)
mkos@384: throws SOAPException
ohair@286: {
ohair@286: try {
mkos@384: Class spiClass = safeLoadClass(className, classLoader);
ohair@286: return spiClass.newInstance();
mkos@384:
ohair@286: } catch (ClassNotFoundException x) {
mkos@384: throw new SOAPException("Provider " + className + " not found", x);
ohair@286: } catch (Exception x) {
mkos@384: throw new SOAPException("Provider " + className + " could not be instantiated: " + x, x);
ohair@286: }
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Finds the implementation Class
object for the given
ohair@286: * factory name, or null if that fails.
ohair@286: *
ohair@286: * This method is package private so that this code can be shared.
ohair@286: *
ohair@286: * @return the Class
object of the specified message factory;
ohair@286: * or null
ohair@286: *
ohair@286: * @param factoryId the name of the factory to find, which is
ohair@286: * a system property
ohair@286: * @exception SOAPException if there is a SOAP error
ohair@286: */
alanb@368: static Object find(String factoryId)
mkos@384: throws SOAPException
ohair@286: {
alanb@368: return find(factoryId, null, false);
ohair@286: }
ohair@286:
ohair@286: /**
ohair@286: * Finds the implementation Class
object for the given
ohair@286: * factory name, or if that fails, finds the Class
object
ohair@286: * for the given fallback class name. The arguments supplied must be
ohair@286: * used in order. If using the first argument is successful, the second
ohair@286: * one will not be used.
ohair@286: *
ohair@286: * This method is package private so that this code can be shared.
ohair@286: *
ohair@286: * @return the Class
object of the specified message factory;
mkos@384: * may be null
ohair@286: *
ohair@286: * @param factoryId the name of the factory to find, which is
ohair@286: * a system property
ohair@286: * @param fallbackClassName the implementation class name, which is
ohair@286: * to be used only if nothing else
ohair@286: * is found; null
to indicate that
ohair@286: * there is no fallback class name
ohair@286: * @exception SOAPException if there is a SOAP error
ohair@286: */
ohair@286: static Object find(String factoryId, String fallbackClassName)
mkos@384: throws SOAPException
ohair@286: {
alanb@368: return find(factoryId, fallbackClassName, true);
alanb@368: }
ohair@286:
alanb@368: /**
alanb@368: * Finds the implementation Class
object for the given
alanb@368: * factory name, or if that fails, finds the Class
object
alanb@368: * for the given default class name, but only if tryFallback
alanb@368: * is true
. The arguments supplied must be used in order
alanb@368: * If using the first argument is successful, the second one will not
alanb@368: * be used. Note the default class name may be needed even if fallback
mkos@384: * is not to be attempted, so certain error conditions can be handled.
alanb@368: *
alanb@368: * This method is package private so that this code can be shared.
alanb@368: *
alanb@368: * @return the Class
object of the specified message factory;
alanb@368: * may not be null
alanb@368: *
alanb@368: * @param factoryId the name of the factory to find, which is
alanb@368: * a system property
alanb@368: * @param defaultClassName the implementation class name, which is
alanb@368: * to be used only if nothing else
alanb@368: * is found; null
to indicate
alanb@368: * that there is no default class name
alanb@368: * @param tryFallback whether to try the default class as a
alanb@368: * fallback
alanb@368: * @exception SOAPException if there is a SOAP error
alanb@368: */
alanb@368: static Object find(String factoryId, String defaultClassName,
mkos@384: boolean tryFallback) throws SOAPException {
ohair@286: ClassLoader classLoader;
ohair@286: try {
ohair@286: classLoader = Thread.currentThread().getContextClassLoader();
ohair@286: } catch (Exception x) {
ohair@286: throw new SOAPException(x.toString(), x);
ohair@286: }
ohair@286:
alanb@368: // Use the system property first
alanb@368: try {
alanb@368: String systemProp =
mkos@384: System.getProperty( factoryId );
alanb@368: if( systemProp!=null) {
mkos@384: return newInstance(systemProp, classLoader);
alanb@368: }
alanb@368: } catch (SecurityException se) {
alanb@368: }
alanb@368:
alanb@368: // try to read from $java.home/lib/jaxm.properties
alanb@368: try {
alanb@368: String javah=System.getProperty( "java.home" );
alanb@368: String configFile = javah + File.separator +
mkos@384: "lib" + File.separator + "jaxm.properties";
alanb@368: File f=new File( configFile );
alanb@368: if( f.exists()) {
alanb@368: Properties props=new Properties();
alanb@368: props.load( new FileInputStream(f));
alanb@368: String factoryClassName = props.getProperty(factoryId);
mkos@384: return newInstance(factoryClassName, classLoader);
alanb@368: }
alanb@368: } catch(Exception ex ) {
alanb@368: }
alanb@368:
alanb@368: String serviceId = "META-INF/services/" + factoryId;
alanb@368: // try to find services in CLASSPATH
alanb@368: try {
alanb@368: InputStream is=null;
alanb@368: if (classLoader == null) {
alanb@368: is=ClassLoader.getSystemResourceAsStream(serviceId);
alanb@368: } else {
alanb@368: is=classLoader.getResourceAsStream(serviceId);
alanb@368: }
alanb@368:
alanb@368: if( is!=null ) {
alanb@368: BufferedReader rd =
mkos@384: new BufferedReader(new InputStreamReader(is, "UTF-8"));
alanb@368:
alanb@368: String factoryClassName = rd.readLine();
alanb@368: rd.close();
alanb@368:
alanb@368: if (factoryClassName != null &&
mkos@384: ! "".equals(factoryClassName)) {
mkos@384: return newInstance(factoryClassName, classLoader);
alanb@368: }
alanb@368: }
alanb@368: } catch( Exception ex ) {
alanb@368: }
alanb@368:
alanb@368: // If not found and fallback should not be tried, return a null result.
alanb@368: if (!tryFallback)
alanb@368: return null;
alanb@368:
alanb@368: // We didn't find the class through the usual means so try the default
alanb@368: // (built in) factory if specified.
alanb@368: if (defaultClassName == null) {
ohair@286: throw new SOAPException(
mkos@384: "Provider for " + factoryId + " cannot be found", null);
ohair@286: }
mkos@384: return newInstance(defaultClassName, classLoader);
alanb@368: }
ohair@286:
alanb@368: /**
alanb@368: * Loads the class, provided that the calling thread has an access to the
alanb@368: * class being loaded. If this is the specified default factory class and it
alanb@368: * is restricted by package.access we get a SecurityException and can do a
alanb@368: * Class.forName() on it so it will be loaded by the bootstrap class loader.
alanb@368: */
alanb@368: private static Class safeLoadClass(String className,
mkos@384: ClassLoader classLoader)
alanb@368: throws ClassNotFoundException {
alanb@368: try {
alanb@368: // make sure that the current thread has an access to the package of the given name.
alanb@368: SecurityManager s = System.getSecurityManager();
alanb@368: if (s != null) {
alanb@368: int i = className.lastIndexOf('.');
alanb@368: if (i != -1) {
alanb@368: s.checkPackageAccess(className.substring(0, i));
alanb@368: }
alanb@368: }
alanb@368:
alanb@368: if (classLoader == null)
alanb@368: return Class.forName(className);
alanb@368: else
alanb@368: return classLoader.loadClass(className);
alanb@368: } catch (SecurityException se) {
mkos@384: // (only) default implementation can be loaded
mkos@384: // using bootstrap class loader:
mkos@384: if (isDefaultImplementation(className))
alanb@368: return Class.forName(className);
mkos@384:
alanb@368: throw se;
alanb@368: }
ohair@286: }
mkos@384:
mkos@384: private static boolean isDefaultImplementation(String className) {
mkos@384: return MessageFactory.DEFAULT_MESSAGE_FACTORY.equals(className) ||
mkos@384: SOAPFactory.DEFAULT_SOAP_FACTORY.equals(className) ||
mkos@384: SOAPConnectionFactory.DEFAULT_SOAP_CONNECTION_FACTORY.equals(className) ||
mkos@384: SAAJMetaFactory.DEFAULT_META_FACTORY_CLASS.equals(className);
mkos@384: }
ohair@286: }