< prev index next >

src/java.xml.ws/share/classes/javax/xml/soap/FactoryFinder.java

Print this page


   1 /*
   2  * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.soap;
  27 
  28 import java.io.*;
  29 import java.util.Properties;
  30 
  31 
  32 class FactoryFinder {
  33 
  34     /**
  35      * Creates an instance of the specified class using the specified
  36      * <code>ClassLoader</code> object.
  37      *
  38      * @exception SOAPException if the given class could not be found
  39      *            or could not be instantiated
  40      */
  41     private static Object newInstance(String className,
  42                                       ClassLoader classLoader)
  43             throws SOAPException
  44     {
  45         try {
  46             Class spiClass = safeLoadClass(className, classLoader);
  47             return spiClass.newInstance();
  48 
  49         } catch (ClassNotFoundException x) {
  50             throw new SOAPException("Provider " + className + " not found", x);
  51         } catch (Exception x) {
  52             throw new SOAPException("Provider " + className + " could not be instantiated: " + x, x);
  53         }
  54     }
  55 
  56     /**
  57      * Finds the implementation <code>Class</code> object for the given
  58      * factory name, or null if that fails.
  59      * <P>
  60      * This method is package private so that this code can be shared.
  61      *
  62      * @return the <code>Class</code> object of the specified message factory;
  63      *         or <code>null</code>
  64      *
  65      * @param factoryId             the name of the factory to find, which is
  66      *                              a system property
  67      * @exception SOAPException if there is a SOAP error
  68      */
  69     static Object find(String factoryId)
  70             throws SOAPException
  71     {
  72         return find(factoryId, null, false);
  73     }
  74 
  75     /**
  76      * Finds the implementation <code>Class</code> object for the given
  77      * factory name, or if that fails, finds the <code>Class</code> object
  78      * for the given fallback class name. The arguments supplied must be
  79      * used in order. If using the first argument is successful, the second
  80      * one will not be used.
  81      * <P>
  82      * This method is package private so that this code can be shared.
  83      *
  84      * @return the <code>Class</code> object of the specified message factory;
  85      *         may be <code>null</code>
  86      *
  87      * @param factoryId             the name of the factory to find, which is
  88      *                              a system property
  89      * @param fallbackClassName     the implementation class name, which is
  90      *                              to be used only if nothing else
  91      *                              is found; <code>null</code> to indicate that
  92      *                              there is no fallback class name
  93      * @exception SOAPException if there is a SOAP error
  94      */
  95     static Object find(String factoryId, String fallbackClassName)
  96             throws SOAPException
  97     {
  98         return find(factoryId, fallbackClassName, true);
  99     }
 100 
 101     /**
 102      * Finds the implementation <code>Class</code> object for the given
 103      * factory name, or if that fails, finds the <code>Class</code> object
 104      * for the given default class name, but only if <code>tryFallback</code>
 105      * is <code>true</code>.  The arguments supplied must be used in order
 106      * If using the first argument is successful, the second one will not
 107      * be used.  Note the default class name may be needed even if fallback
 108      * is not to be attempted, so certain error conditions can be handled.
 109      * <P>
 110      * This method is package private so that this code can be shared.
 111      *
 112      * @return the <code>Class</code> object of the specified message factory;
 113      *         may not be <code>null</code>
 114      *
 115      * @param factoryId             the name of the factory to find, which is
 116      *                              a system property
 117      * @param defaultClassName      the implementation class name, which is
 118      *                              to be used only if nothing else
 119      *                              is found; <code>null</code> to indicate
 120      *                              that there is no default class name
 121      * @param tryFallback           whether to try the default class as a
 122      *                              fallback
 123      * @exception SOAPException if there is a SOAP error
 124      */
 125     static Object find(String factoryId, String defaultClassName,
 126                        boolean tryFallback) throws SOAPException {
 127         ClassLoader classLoader;
 128         try {
 129             classLoader = Thread.currentThread().getContextClassLoader();
 130         } catch (Exception x) {
 131             throw new SOAPException(x.toString(), x);
 132         }
 133 
 134         // Use the system property first
 135         try {
 136             String systemProp =
 137                     System.getProperty( factoryId );
 138             if( systemProp!=null) {
 139                 return newInstance(systemProp, classLoader);


   1 /*
   2  * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.xml.soap;
  27 
  28 import java.io.*;
  29 import java.util.Properties;
  30 
  31 
  32 class FactoryFinder {
  33 
  34     /**
  35      * Creates an instance of the specified class using the specified
  36      * {@code ClassLoader} object.
  37      *
  38      * @exception SOAPException if the given class could not be found
  39      *            or could not be instantiated
  40      */
  41     private static Object newInstance(String className,
  42                                       ClassLoader classLoader)
  43             throws SOAPException
  44     {
  45         try {
  46             Class spiClass = safeLoadClass(className, classLoader);
  47             return spiClass.newInstance();
  48 
  49         } catch (ClassNotFoundException x) {
  50             throw new SOAPException("Provider " + className + " not found", x);
  51         } catch (Exception x) {
  52             throw new SOAPException("Provider " + className + " could not be instantiated: " + x, x);
  53         }
  54     }
  55 
  56     /**
  57      * Finds the implementation {@code Class} object for the given
  58      * factory name, or null if that fails.
  59      * <P>
  60      * This method is package private so that this code can be shared.
  61      *
  62      * @return the {@code Class} object of the specified message factory;
  63      *         or {@code null}
  64      *
  65      * @param factoryId             the name of the factory to find, which is
  66      *                              a system property
  67      * @exception SOAPException if there is a SOAP error
  68      */
  69     static Object find(String factoryId)
  70             throws SOAPException
  71     {
  72         return find(factoryId, null, false);
  73     }
  74 
  75     /**
  76      * Finds the implementation {@code Class} object for the given
  77      * factory name, or if that fails, finds the {@code Class} object
  78      * for the given fallback class name. The arguments supplied must be
  79      * used in order. If using the first argument is successful, the second
  80      * one will not be used.
  81      * <P>
  82      * This method is package private so that this code can be shared.
  83      *
  84      * @return the {@code Class} object of the specified message factory;
  85      *         may be {@code null}
  86      *
  87      * @param factoryId             the name of the factory to find, which is
  88      *                              a system property
  89      * @param fallbackClassName     the implementation class name, which is
  90      *                              to be used only if nothing else
  91      *                              is found; {@code null} to indicate that
  92      *                              there is no fallback class name
  93      * @exception SOAPException if there is a SOAP error
  94      */
  95     static Object find(String factoryId, String fallbackClassName)
  96             throws SOAPException
  97     {
  98         return find(factoryId, fallbackClassName, true);
  99     }
 100 
 101     /**
 102      * Finds the implementation {@code Class} object for the given
 103      * factory name, or if that fails, finds the {@code Class} object
 104      * for the given default class name, but only if {@code tryFallback}
 105      * is {@code true}.  The arguments supplied must be used in order
 106      * If using the first argument is successful, the second one will not
 107      * be used.  Note the default class name may be needed even if fallback
 108      * is not to be attempted, so certain error conditions can be handled.
 109      * <P>
 110      * This method is package private so that this code can be shared.
 111      *
 112      * @return the {@code Class} object of the specified message factory;
 113      *         may not be {@code null}
 114      *
 115      * @param factoryId             the name of the factory to find, which is
 116      *                              a system property
 117      * @param defaultClassName      the implementation class name, which is
 118      *                              to be used only if nothing else
 119      *                              is found; {@code null} to indicate
 120      *                              that there is no default class name
 121      * @param tryFallback           whether to try the default class as a
 122      *                              fallback
 123      * @exception SOAPException if there is a SOAP error
 124      */
 125     static Object find(String factoryId, String defaultClassName,
 126                        boolean tryFallback) throws SOAPException {
 127         ClassLoader classLoader;
 128         try {
 129             classLoader = Thread.currentThread().getContextClassLoader();
 130         } catch (Exception x) {
 131             throw new SOAPException(x.toString(), x);
 132         }
 133 
 134         // Use the system property first
 135         try {
 136             String systemProp =
 137                     System.getProperty( factoryId );
 138             if( systemProp!=null) {
 139                 return newInstance(systemProp, classLoader);


< prev index next >