src/share/jaxws_classes/javax/xml/ws/spi/FactoryFinder.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2005, 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  */
    26 package javax.xml.ws.spi;
    28 import java.io.*;
    30 import java.util.Properties;
    31 import javax.xml.ws.WebServiceException;
    33 class FactoryFinder {
    35     /**
    36      * Creates an instance of the specified class using the specified
    37      * <code>ClassLoader</code> object.
    38      *
    39      * @exception WebServiceException if the given class could not be found
    40      *            or could not be instantiated
    41      */
    42     private static Object newInstance(String className,
    43                                       ClassLoader classLoader)
    44     {
    45         try {
    46             Class spiClass = safeLoadClass(className, classLoader);
    47             return spiClass.newInstance();
    48         } catch (ClassNotFoundException x) {
    49             throw new WebServiceException(
    50                 "Provider " + className + " not found", x);
    51         } catch (Exception x) {
    52             throw new WebServiceException(
    53                 "Provider " + className + " could not be instantiated: " + x,
    54                 x);
    55         }
    56     }
    58     /**
    59      * Finds the implementation <code>Class</code> object for the given
    60      * factory name, or if that fails, finds the <code>Class</code> object
    61      * for the given fallback class name. The arguments supplied MUST be
    62      * used in order. If using the first argument is successful, the second
    63      * one will not be used.
    64      * <P>
    65      * This method is package private so that this code can be shared.
    66      *
    67      * @return the <code>Class</code> object of the specified message factory;
    68      *         may not be <code>null</code>
    69      *
    70      * @param factoryId             the name of the factory to find, which is
    71      *                              a system property
    72      * @param fallbackClassName     the implementation class name, which is
    73      *                              to be used only if nothing else
    74      *                              is found; <code>null</code> to indicate that
    75      *                              there is no fallback class name
    76      * @exception WebServiceException if there is an error
    77      */
    78     static Object find(String factoryId, String fallbackClassName)
    79     {
    80         if (isOsgi()) {
    81             return lookupUsingOSGiServiceLoader(factoryId);
    82         }
    83         ClassLoader classLoader;
    84         try {
    85             classLoader = Thread.currentThread().getContextClassLoader();
    86         } catch (Exception x) {
    87             throw new WebServiceException(x.toString(), x);
    88         }
    90         String serviceId = "META-INF/services/" + factoryId;
    91         // try to find services in CLASSPATH
    92         BufferedReader rd = null;
    93         try {
    94             InputStream is;
    95             if (classLoader == null) {
    96                 is=ClassLoader.getSystemResourceAsStream(serviceId);
    97             } else {
    98                 is=classLoader.getResourceAsStream(serviceId);
    99             }
   101             if( is!=null ) {
   102                 rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
   104                 String factoryClassName = rd.readLine();
   106                 if (factoryClassName != null &&
   107                     ! "".equals(factoryClassName)) {
   108                     return newInstance(factoryClassName, classLoader);
   109                 }
   110             }
   111         } catch( Exception ignored) {
   112         } finally {
   113             close(rd);
   114         }
   117         // try to read from $java.home/lib/jaxws.properties
   118         FileInputStream inStream = null;
   119         try {
   120             String javah=System.getProperty( "java.home" );
   121             String configFile = javah + File.separator +
   122                 "lib" + File.separator + "jaxws.properties";
   123             File f=new File( configFile );
   124             if( f.exists()) {
   125                 Properties props=new Properties();
   126                 inStream = new FileInputStream(f);
   127                 props.load(inStream);
   128                 String factoryClassName = props.getProperty(factoryId);
   129                 return newInstance(factoryClassName, classLoader);
   130             }
   131         } catch(Exception ignored) {
   132         } finally {
   133             close(inStream);
   134         }
   136         // Use the system property
   137         try {
   138             String systemProp =
   139                 System.getProperty( factoryId );
   140             if( systemProp!=null) {
   141                 return newInstance(systemProp, classLoader);
   142             }
   143         } catch (SecurityException ignored) {
   144         }
   146         if (fallbackClassName == null) {
   147             throw new WebServiceException(
   148                 "Provider for " + factoryId + " cannot be found", null);
   149         }
   151         return newInstance(fallbackClassName, classLoader);
   152     }
   154     private static void close(Closeable closeable) {
   155         if (closeable != null) {
   156             try {
   157                 closeable.close();
   158             } catch (IOException ignored) {
   159             }
   160         }
   161     }
   164     /**
   165      * Loads the class, provided that the calling thread has an access to the class being loaded.
   166      */
   167     private static Class safeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
   168         try {
   169             // make sure that the current thread has an access to the package of the given name.
   170             SecurityManager s = System.getSecurityManager();
   171             if (s != null) {
   172                 int i = className.lastIndexOf('.');
   173                 if (i != -1) {
   174                     s.checkPackageAccess(className.substring(0, i));
   175                 }
   176             }
   178             if (classLoader == null)
   179                 return Class.forName(className);
   180             else
   181                 return classLoader.loadClass(className);
   182         } catch (SecurityException se) {
   183             // anyone can access the platform default factory class without permission
   184             if (Provider.DEFAULT_JAXWSPROVIDER.equals(className))
   185                 return Class.forName(className);
   186             throw se;
   187         }
   188     }
   190     private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
   192     private static boolean isOsgi() {
   193         try {
   194             Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   195             return true;
   196         } catch (ClassNotFoundException ignored) {
   197         }
   198         return false;
   199     }
   201     private static Object lookupUsingOSGiServiceLoader(String factoryId) {
   202         try {
   203             // Use reflection to avoid having any dependendcy on ServiceLoader class
   204             Class serviceClass = Class.forName(factoryId);
   205             Class[] args = new Class[]{serviceClass};
   206             Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   207             java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
   208             java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator();
   209             return iter.hasNext() ? iter.next() : null;
   210         } catch (Exception ignored) {
   211             // log and continue
   212             return null;
   213         }
   214     }
   216 }

mercurial