ohair@286: /*
ohair@286: * Copyright (c) 2004, 2011, 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,
ohair@286: ClassLoader classLoader)
ohair@286: throws SOAPException
ohair@286: {
ohair@286: try {
ohair@286: Class spiClass;
ohair@286: if (classLoader == null) {
ohair@286: spiClass = Class.forName(className);
ohair@286: } else {
ohair@286: spiClass = classLoader.loadClass(className);
ohair@286: }
ohair@286: return spiClass.newInstance();
ohair@286: } catch (ClassNotFoundException x) {
ohair@286: throw new SOAPException(
ohair@286: "Provider " + className + " not found", x);
ohair@286: } catch (Exception x) {
ohair@286: throw new SOAPException(
ohair@286: "Provider " + className + " could not be instantiated: " + x,
ohair@286: 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: */
ohair@286: static Object find(String factId)
ohair@286: throws SOAPException
ohair@286: {
ohair@286: final ClassLoader classLoader;
ohair@286: final String factoryId = factId;
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:
ohair@286: // Use the system property first
ohair@286: try {
ohair@286: String systemProp =
ohair@286: System.getProperty( factoryId );
ohair@286: if( systemProp!=null) {
ohair@286: return newInstance(systemProp, classLoader);
ohair@286: }
ohair@286: } catch (SecurityException se) {
ohair@286: }
ohair@286:
ohair@286: // try to read from $java.home/lib/jaxm.properties
ohair@286: try {
ohair@286: String javah=System.getProperty( "java.home" );
ohair@286: String configFile = javah + File.separator +
ohair@286: "lib" + File.separator + "jaxm.properties";
ohair@286: final File f=new File( configFile );
ohair@286: if( f.exists()) {
ohair@286: Properties props=new Properties();
ohair@286: props.load( new FileInputStream(f));
ohair@286: String factoryClassName = props.getProperty(factoryId);
ohair@286: return newInstance(factoryClassName, classLoader);
ohair@286: }
ohair@286: } catch(Exception ex ) {
ohair@286: }
ohair@286:
ohair@286: String serviceId = "META-INF/services/" + factoryId;
ohair@286: // try to find services in CLASSPATH
ohair@286: try {
ohair@286: InputStream is=null;
ohair@286: if (classLoader == null) {
ohair@286: is=ClassLoader.getSystemResourceAsStream(serviceId);
ohair@286: } else {
ohair@286: is=classLoader.getResourceAsStream(serviceId);
ohair@286: }
ohair@286:
ohair@286: if( is!=null ) {
ohair@286: BufferedReader rd =
ohair@286: new BufferedReader(new InputStreamReader(is, "UTF-8"));
ohair@286:
ohair@286: String factoryClassName = rd.readLine();
ohair@286: rd.close();
ohair@286:
ohair@286: if (factoryClassName != null &&
ohair@286: ! "".equals(factoryClassName)) {
ohair@286: return newInstance(factoryClassName, classLoader);
ohair@286: }
ohair@286: }
ohair@286: } catch( Exception ex ) {
ohair@286: }
ohair@286:
ohair@286: return null;
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;
ohair@286: * may not 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)
ohair@286: throws SOAPException
ohair@286: {
ohair@286:
ohair@286: Object obj = find(factoryId);
ohair@286: if (obj != null)
ohair@286: return obj;
ohair@286:
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:
ohair@286: if (fallbackClassName == null) {
ohair@286: throw new SOAPException(
ohair@286: "Provider for " + factoryId + " cannot be found", null);
ohair@286: }
ohair@286:
ohair@286: return newInstance(fallbackClassName, classLoader);
ohair@286: }
ohair@286: }