Home » xml-commons-external-1.4.01-src » javax » xml » messaging » [javadoc | source]

    1   /*
    2    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    3    *
    4    * Copyright 2000-2007 Sun Microsystems, Inc. All rights reserved. 
    5    *
    6    * The contents of this file are subject to the terms of either the GNU
    7    * General Public License Version 2 only ("GPL") or the Common Development
    8    * and Distribution License ("CDDL") (collectively, the "License").  You may
    9    * not use this file except in compliance with the License.  You can obtain
   10    * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
   11    * or mq/legal/LICENSE.txt.  See the License for the specific language
   12    * governing permissions and limitations under the License.
   13    * 
   14    * When distributing the software, include this License Header Notice in each
   15    * file and include the License file at mq/legal/LICENSE.txt.  Sun designates
   16    * this particular file as subject to the "Classpath" exception as provided by
   17    * Sun in the GPL Version 2 section of the License file that accompanied this
   18    * code.  If applicable, add the following below the License Header, with the
   19    * fields enclosed by brackets [] replaced by your own identifying information:
   20    * "Portions Copyrighted [year] [name of copyright owner]"
   21    * 
   22    * Contributor(s):
   23    * 
   24    * If you wish your version of this file to be governed by only the CDDL or
   25    * only the GPL Version 2, indicate your decision by adding "[Contributor]
   26    * elects to include this software in this distribution under the [CDDL or GPL
   27    * Version 2] license."  If you don't indicate a single choice of license, a
   28    * recipient has the option to distribute your version of this file under
   29    * either the CDDL, the GPL Version 2 or  to extend the choice of license to
   30    * its licensees as provided above.  However, if you add GPL Version 2 code
   31    * and therefore, elected the GPL Version 2 license, then the option applies
   32    * only if the new code is made subject to such option by the copyright holder. 
   33    */
   34   
   35   /*
   36    * @(#)FactoryFinder.java	1.5 07/02/07
   37    */ 
   38   
   39   package javax.xml.messaging;
   40   
   41   import java.io.InputStream;
   42   import java.io.IOException;
   43   import java.io.File;
   44   import java.io.FileInputStream;
   45   
   46   import java.util.Properties;
   47   import java.util.MissingResourceException;
   48   import java.io.BufferedReader;
   49   import java.io.InputStreamReader;
   50   
   51   import com.sun.messaging.AdministeredObject;
   52   
   53   class FactoryFinder {
   54   
   55       /**
   56        * Creates an instance of the specified class using the specified 
   57        * <code>ClassLoader</code> object.
   58        *
   59        * @exception JAXMException if the given class could not be found
   60        *            or could not be instantiated
   61        */
   62       private static Object newInstance(String className,
   63                                         ClassLoader classLoader)
   64           throws JAXMException
   65       {
   66   
   67           String emsg = "";
   68           try {
   69               emsg = AdministeredObject.cr.getKString(AdministeredObject.cr.X_NO_FACTORY_CLASS, className);
   70           } catch (MissingResourceException mre) {
   71           }
   72   
   73           try {
   74               Class spiClass;
   75               if (classLoader == null) {
   76                   spiClass = Class.forName(className);
   77               } else {
   78                   spiClass = classLoader.loadClass(className);
   79               }
   80               return spiClass.newInstance();
   81           } catch (ClassNotFoundException x) {
   82               throw new JAXMException(emsg);
   83                   //"Provider " + className + " not found", x);
   84           } catch (Exception x) {
   85               throw new JAXMException(emsg);
   86                   //"Provider " + className + " could not be instantiated: " + x,
   87                   //x);
   88           }
   89       }
   90   
   91       /**
   92        * Finds the implementation <code>Class</code> object for the given
   93        * factory name, or if that fails, finds the <code>Class</code> object
   94        * for the given fallback class name. The arguments supplied must be
   95        * used in order. If using the first argument is successful, the second
   96        * one will not be used.
   97        * <P>
   98        * This method is package private so that this code can be shared.
   99        *
  100        * @return the <code>Class</code> object of the specified message factory;
  101        *         may not be <code>null</code>
  102        *
  103        * @param factoryId             the name of the factory to find, which is
  104        *                              a system property
  105        * @param fallbackClassName     the implementation class name, which is
  106        *                              to be used only if nothing else
  107        *                              is found; <code>null</code> to indicate that
  108        *                              there is no fallback class name
  109        * @exception JAXMException if there is a SOAP error
  110        */
  111       static Object find(String factoryId, String fallbackClassName)
  112           throws JAXMException
  113       {
  114           ClassLoader classLoader;
  115           try {
  116               classLoader = Thread.currentThread().getContextClassLoader();
  117           } catch (Exception x) {
  118               throw new JAXMException(x.toString(), x);
  119           }
  120   
  121           // Use the system property first
  122           try {
  123               String systemProp =
  124                   System.getProperty( factoryId );
  125               if( systemProp!=null) {
  126                   return newInstance(systemProp, classLoader);
  127               }
  128           } catch (SecurityException se) {
  129           }
  130   
  131           // try to read from $java.home/lib/jaxm.properties
  132           try {
  133               String javah=System.getProperty( "java.home" );
  134               String configFile = javah + File.separator +
  135                   "lib" + File.separator + "jaxm.properties";
  136               File f=new File( configFile );
  137               if( f.exists()) {
  138                   Properties props=new Properties();
  139                   props.load( new FileInputStream(f));
  140                   String factoryClassName = props.getProperty(factoryId);
  141                   return newInstance(factoryClassName, classLoader);
  142               }
  143           } catch(Exception ex ) {
  144           }
  145   
  146           String serviceId = "META-INF/services/" + factoryId;
  147           // try to find services in CLASSPATH
  148           try {
  149               InputStream is=null;
  150               if (classLoader == null) {
  151                   is=ClassLoader.getSystemResourceAsStream(serviceId);
  152               } else {
  153                   is=classLoader.getResourceAsStream(serviceId);
  154               }
  155           
  156               if( is!=null ) {
  157                   BufferedReader rd =
  158                       new BufferedReader(new InputStreamReader(is, "UTF-8"));
  159           
  160                   String factoryClassName = rd.readLine();
  161                   rd.close();
  162   
  163                   if (factoryClassName != null &&
  164                       ! "".equals(factoryClassName)) {
  165                       return newInstance(factoryClassName, classLoader);
  166                   }
  167               }
  168           } catch( Exception ex ) {
  169           }
  170   
  171           if (fallbackClassName == null) {
  172               throw new JAXMException(
  173                   "Provider for " + factoryId + " cannot be found", null);
  174           }
  175   
  176           return newInstance(fallbackClassName, classLoader);
  177       }
  178   }

Home » xml-commons-external-1.4.01-src » javax » xml » messaging » [javadoc | source]