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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/javax/xml/ws/spi/FactoryFinder.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,216 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.xml.ws.spi;
    1.30 +
    1.31 +import java.io.*;
    1.32 +
    1.33 +import java.util.Properties;
    1.34 +import javax.xml.ws.WebServiceException;
    1.35 +
    1.36 +class FactoryFinder {
    1.37 +
    1.38 +    /**
    1.39 +     * Creates an instance of the specified class using the specified
    1.40 +     * <code>ClassLoader</code> object.
    1.41 +     *
    1.42 +     * @exception WebServiceException if the given class could not be found
    1.43 +     *            or could not be instantiated
    1.44 +     */
    1.45 +    private static Object newInstance(String className,
    1.46 +                                      ClassLoader classLoader)
    1.47 +    {
    1.48 +        try {
    1.49 +            Class spiClass = safeLoadClass(className, classLoader);
    1.50 +            return spiClass.newInstance();
    1.51 +        } catch (ClassNotFoundException x) {
    1.52 +            throw new WebServiceException(
    1.53 +                "Provider " + className + " not found", x);
    1.54 +        } catch (Exception x) {
    1.55 +            throw new WebServiceException(
    1.56 +                "Provider " + className + " could not be instantiated: " + x,
    1.57 +                x);
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +    /**
    1.62 +     * Finds the implementation <code>Class</code> object for the given
    1.63 +     * factory name, or if that fails, finds the <code>Class</code> object
    1.64 +     * for the given fallback class name. The arguments supplied MUST be
    1.65 +     * used in order. If using the first argument is successful, the second
    1.66 +     * one will not be used.
    1.67 +     * <P>
    1.68 +     * This method is package private so that this code can be shared.
    1.69 +     *
    1.70 +     * @return the <code>Class</code> object of the specified message factory;
    1.71 +     *         may not be <code>null</code>
    1.72 +     *
    1.73 +     * @param factoryId             the name of the factory to find, which is
    1.74 +     *                              a system property
    1.75 +     * @param fallbackClassName     the implementation class name, which is
    1.76 +     *                              to be used only if nothing else
    1.77 +     *                              is found; <code>null</code> to indicate that
    1.78 +     *                              there is no fallback class name
    1.79 +     * @exception WebServiceException if there is an error
    1.80 +     */
    1.81 +    static Object find(String factoryId, String fallbackClassName)
    1.82 +    {
    1.83 +        if (isOsgi()) {
    1.84 +            return lookupUsingOSGiServiceLoader(factoryId);
    1.85 +        }
    1.86 +        ClassLoader classLoader;
    1.87 +        try {
    1.88 +            classLoader = Thread.currentThread().getContextClassLoader();
    1.89 +        } catch (Exception x) {
    1.90 +            throw new WebServiceException(x.toString(), x);
    1.91 +        }
    1.92 +
    1.93 +        String serviceId = "META-INF/services/" + factoryId;
    1.94 +        // try to find services in CLASSPATH
    1.95 +        BufferedReader rd = null;
    1.96 +        try {
    1.97 +            InputStream is;
    1.98 +            if (classLoader == null) {
    1.99 +                is=ClassLoader.getSystemResourceAsStream(serviceId);
   1.100 +            } else {
   1.101 +                is=classLoader.getResourceAsStream(serviceId);
   1.102 +            }
   1.103 +
   1.104 +            if( is!=null ) {
   1.105 +                rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
   1.106 +
   1.107 +                String factoryClassName = rd.readLine();
   1.108 +
   1.109 +                if (factoryClassName != null &&
   1.110 +                    ! "".equals(factoryClassName)) {
   1.111 +                    return newInstance(factoryClassName, classLoader);
   1.112 +                }
   1.113 +            }
   1.114 +        } catch( Exception ignored) {
   1.115 +        } finally {
   1.116 +            close(rd);
   1.117 +        }
   1.118 +
   1.119 +
   1.120 +        // try to read from $java.home/lib/jaxws.properties
   1.121 +        FileInputStream inStream = null;
   1.122 +        try {
   1.123 +            String javah=System.getProperty( "java.home" );
   1.124 +            String configFile = javah + File.separator +
   1.125 +                "lib" + File.separator + "jaxws.properties";
   1.126 +            File f=new File( configFile );
   1.127 +            if( f.exists()) {
   1.128 +                Properties props=new Properties();
   1.129 +                inStream = new FileInputStream(f);
   1.130 +                props.load(inStream);
   1.131 +                String factoryClassName = props.getProperty(factoryId);
   1.132 +                return newInstance(factoryClassName, classLoader);
   1.133 +            }
   1.134 +        } catch(Exception ignored) {
   1.135 +        } finally {
   1.136 +            close(inStream);
   1.137 +        }
   1.138 +
   1.139 +        // Use the system property
   1.140 +        try {
   1.141 +            String systemProp =
   1.142 +                System.getProperty( factoryId );
   1.143 +            if( systemProp!=null) {
   1.144 +                return newInstance(systemProp, classLoader);
   1.145 +            }
   1.146 +        } catch (SecurityException ignored) {
   1.147 +        }
   1.148 +
   1.149 +        if (fallbackClassName == null) {
   1.150 +            throw new WebServiceException(
   1.151 +                "Provider for " + factoryId + " cannot be found", null);
   1.152 +        }
   1.153 +
   1.154 +        return newInstance(fallbackClassName, classLoader);
   1.155 +    }
   1.156 +
   1.157 +    private static void close(Closeable closeable) {
   1.158 +        if (closeable != null) {
   1.159 +            try {
   1.160 +                closeable.close();
   1.161 +            } catch (IOException ignored) {
   1.162 +            }
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +
   1.167 +    /**
   1.168 +     * Loads the class, provided that the calling thread has an access to the class being loaded.
   1.169 +     */
   1.170 +    private static Class safeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
   1.171 +        try {
   1.172 +            // make sure that the current thread has an access to the package of the given name.
   1.173 +            SecurityManager s = System.getSecurityManager();
   1.174 +            if (s != null) {
   1.175 +                int i = className.lastIndexOf('.');
   1.176 +                if (i != -1) {
   1.177 +                    s.checkPackageAccess(className.substring(0, i));
   1.178 +                }
   1.179 +            }
   1.180 +
   1.181 +            if (classLoader == null)
   1.182 +                return Class.forName(className);
   1.183 +            else
   1.184 +                return classLoader.loadClass(className);
   1.185 +        } catch (SecurityException se) {
   1.186 +            // anyone can access the platform default factory class without permission
   1.187 +            if (Provider.DEFAULT_JAXWSPROVIDER.equals(className))
   1.188 +                return Class.forName(className);
   1.189 +            throw se;
   1.190 +        }
   1.191 +    }
   1.192 +
   1.193 +    private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
   1.194 +
   1.195 +    private static boolean isOsgi() {
   1.196 +        try {
   1.197 +            Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   1.198 +            return true;
   1.199 +        } catch (ClassNotFoundException ignored) {
   1.200 +        }
   1.201 +        return false;
   1.202 +    }
   1.203 +
   1.204 +    private static Object lookupUsingOSGiServiceLoader(String factoryId) {
   1.205 +        try {
   1.206 +            // Use reflection to avoid having any dependendcy on ServiceLoader class
   1.207 +            Class serviceClass = Class.forName(factoryId);
   1.208 +            Class[] args = new Class[]{serviceClass};
   1.209 +            Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   1.210 +            java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
   1.211 +            java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator();
   1.212 +            return iter.hasNext() ? iter.next() : null;
   1.213 +        } catch (Exception ignored) {
   1.214 +            // log and continue
   1.215 +            return null;
   1.216 +        }
   1.217 +    }
   1.218 +
   1.219 +}

mercurial