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

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     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	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,207 @@
     1.4 +/*
     1.5 + * Copyright (c) 2005, 2011, 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.InputStream;
    1.32 +import java.io.File;
    1.33 +import java.io.FileInputStream;
    1.34 +
    1.35 +import java.util.Properties;
    1.36 +import java.io.BufferedReader;
    1.37 +import java.io.InputStreamReader;
    1.38 +import javax.xml.ws.WebServiceException;
    1.39 +
    1.40 +class FactoryFinder {
    1.41 +
    1.42 +    /**
    1.43 +     * Creates an instance of the specified class using the specified
    1.44 +     * <code>ClassLoader</code> object.
    1.45 +     *
    1.46 +     * @exception WebServiceException if the given class could not be found
    1.47 +     *            or could not be instantiated
    1.48 +     */
    1.49 +    private static Object newInstance(String className,
    1.50 +                                      ClassLoader classLoader)
    1.51 +    {
    1.52 +        try {
    1.53 +            Class spiClass = safeLoadClass(className, classLoader);
    1.54 +            return spiClass.newInstance();
    1.55 +        } catch (ClassNotFoundException x) {
    1.56 +            throw new WebServiceException(
    1.57 +                "Provider " + className + " not found", x);
    1.58 +        } catch (Exception x) {
    1.59 +            throw new WebServiceException(
    1.60 +                "Provider " + className + " could not be instantiated: " + x,
    1.61 +                x);
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +    /**
    1.66 +     * Finds the implementation <code>Class</code> object for the given
    1.67 +     * factory name, or if that fails, finds the <code>Class</code> object
    1.68 +     * for the given fallback class name. The arguments supplied MUST be
    1.69 +     * used in order. If using the first argument is successful, the second
    1.70 +     * one will not be used.
    1.71 +     * <P>
    1.72 +     * This method is package private so that this code can be shared.
    1.73 +     *
    1.74 +     * @return the <code>Class</code> object of the specified message factory;
    1.75 +     *         may not be <code>null</code>
    1.76 +     *
    1.77 +     * @param factoryId             the name of the factory to find, which is
    1.78 +     *                              a system property
    1.79 +     * @param fallbackClassName     the implementation class name, which is
    1.80 +     *                              to be used only if nothing else
    1.81 +     *                              is found; <code>null</code> to indicate that
    1.82 +     *                              there is no fallback class name
    1.83 +     * @exception WebServiceException if there is an error
    1.84 +     */
    1.85 +    static Object find(String factoryId, String fallbackClassName)
    1.86 +    {
    1.87 +        if (isOsgi()) {
    1.88 +            return lookupUsingOSGiServiceLoader(factoryId);
    1.89 +        }
    1.90 +        ClassLoader classLoader;
    1.91 +        try {
    1.92 +            classLoader = Thread.currentThread().getContextClassLoader();
    1.93 +        } catch (Exception x) {
    1.94 +            throw new WebServiceException(x.toString(), x);
    1.95 +        }
    1.96 +
    1.97 +        String serviceId = "META-INF/services/" + factoryId;
    1.98 +        // try to find services in CLASSPATH
    1.99 +        try {
   1.100 +            InputStream is=null;
   1.101 +            if (classLoader == null) {
   1.102 +                is=ClassLoader.getSystemResourceAsStream(serviceId);
   1.103 +            } else {
   1.104 +                is=classLoader.getResourceAsStream(serviceId);
   1.105 +            }
   1.106 +
   1.107 +            if( is!=null ) {
   1.108 +                BufferedReader rd =
   1.109 +                    new BufferedReader(new InputStreamReader(is, "UTF-8"));
   1.110 +
   1.111 +                String factoryClassName = rd.readLine();
   1.112 +                rd.close();
   1.113 +
   1.114 +                if (factoryClassName != null &&
   1.115 +                    ! "".equals(factoryClassName)) {
   1.116 +                    return newInstance(factoryClassName, classLoader);
   1.117 +                }
   1.118 +            }
   1.119 +        } catch( Exception ex ) {
   1.120 +        }
   1.121 +
   1.122 +
   1.123 +        // try to read from $java.home/lib/jaxws.properties
   1.124 +        try {
   1.125 +            String javah=System.getProperty( "java.home" );
   1.126 +            String configFile = javah + File.separator +
   1.127 +                "lib" + File.separator + "jaxws.properties";
   1.128 +            File f=new File( configFile );
   1.129 +            if( f.exists()) {
   1.130 +                Properties props=new Properties();
   1.131 +                props.load( new FileInputStream(f));
   1.132 +                String factoryClassName = props.getProperty(factoryId);
   1.133 +                return newInstance(factoryClassName, classLoader);
   1.134 +            }
   1.135 +        } catch(Exception ex ) {
   1.136 +        }
   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 se) {
   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 +
   1.158 +    /**
   1.159 +     * Loads the class, provided that the calling thread has an access to the class being loaded.
   1.160 +     */
   1.161 +    private static Class safeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
   1.162 +        try {
   1.163 +            // make sure that the current thread has an access to the package of the given name.
   1.164 +            SecurityManager s = System.getSecurityManager();
   1.165 +            if (s != null) {
   1.166 +                int i = className.lastIndexOf('.');
   1.167 +                if (i != -1) {
   1.168 +                    s.checkPackageAccess(className.substring(0, i));
   1.169 +                }
   1.170 +            }
   1.171 +
   1.172 +            if (classLoader == null)
   1.173 +                return Class.forName(className);
   1.174 +            else
   1.175 +                return classLoader.loadClass(className);
   1.176 +        } catch (SecurityException se) {
   1.177 +            // anyone can access the platform default factory class without permission
   1.178 +            if (Provider.DEFAULT_JAXWSPROVIDER.equals(className))
   1.179 +                return Class.forName(className);
   1.180 +            throw se;
   1.181 +        }
   1.182 +    }
   1.183 +
   1.184 +    private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
   1.185 +
   1.186 +    private static boolean isOsgi() {
   1.187 +        try {
   1.188 +            Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   1.189 +            return true;
   1.190 +        } catch (ClassNotFoundException cnfe) {
   1.191 +        }
   1.192 +        return false;
   1.193 +    }
   1.194 +
   1.195 +    private static Object lookupUsingOSGiServiceLoader(String factoryId) {
   1.196 +        try {
   1.197 +            // Use reflection to avoid having any dependendcy on ServiceLoader class
   1.198 +            Class serviceClass = Class.forName(factoryId);
   1.199 +            Class[] args = new Class[]{serviceClass};
   1.200 +            Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
   1.201 +            java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
   1.202 +            java.util.Iterator iter = ((Iterable) m.invoke(null, args)).iterator();
   1.203 +            return iter.hasNext() ? iter.next() : null;
   1.204 +        } catch (Exception e) {
   1.205 +            // log and continue
   1.206 +            return null;
   1.207 +        }
   1.208 +    }
   1.209 +
   1.210 +}

mercurial