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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package javax.xml.ws.spi;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29
aoqi@0 30 import java.util.Properties;
aoqi@0 31 import javax.xml.ws.WebServiceException;
aoqi@0 32
aoqi@0 33 class FactoryFinder {
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * Creates an instance of the specified class using the specified
aoqi@0 37 * <code>ClassLoader</code> object.
aoqi@0 38 *
aoqi@0 39 * @exception WebServiceException if the given class could not be found
aoqi@0 40 * or could not be instantiated
aoqi@0 41 */
aoqi@0 42 private static Object newInstance(String className,
aoqi@0 43 ClassLoader classLoader)
aoqi@0 44 {
aoqi@0 45 try {
aoqi@0 46 Class spiClass = safeLoadClass(className, classLoader);
aoqi@0 47 return spiClass.newInstance();
aoqi@0 48 } catch (ClassNotFoundException x) {
aoqi@0 49 throw new WebServiceException(
aoqi@0 50 "Provider " + className + " not found", x);
aoqi@0 51 } catch (Exception x) {
aoqi@0 52 throw new WebServiceException(
aoqi@0 53 "Provider " + className + " could not be instantiated: " + x,
aoqi@0 54 x);
aoqi@0 55 }
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 /**
aoqi@0 59 * Finds the implementation <code>Class</code> object for the given
aoqi@0 60 * factory name, or if that fails, finds the <code>Class</code> object
aoqi@0 61 * for the given fallback class name. The arguments supplied MUST be
aoqi@0 62 * used in order. If using the first argument is successful, the second
aoqi@0 63 * one will not be used.
aoqi@0 64 * <P>
aoqi@0 65 * This method is package private so that this code can be shared.
aoqi@0 66 *
aoqi@0 67 * @return the <code>Class</code> object of the specified message factory;
aoqi@0 68 * may not be <code>null</code>
aoqi@0 69 *
aoqi@0 70 * @param factoryId the name of the factory to find, which is
aoqi@0 71 * a system property
aoqi@0 72 * @param fallbackClassName the implementation class name, which is
aoqi@0 73 * to be used only if nothing else
aoqi@0 74 * is found; <code>null</code> to indicate that
aoqi@0 75 * there is no fallback class name
aoqi@0 76 * @exception WebServiceException if there is an error
aoqi@0 77 */
aoqi@0 78 static Object find(String factoryId, String fallbackClassName)
aoqi@0 79 {
aoqi@0 80 if (isOsgi()) {
aoqi@0 81 return lookupUsingOSGiServiceLoader(factoryId);
aoqi@0 82 }
aoqi@0 83 ClassLoader classLoader;
aoqi@0 84 try {
aoqi@0 85 classLoader = Thread.currentThread().getContextClassLoader();
aoqi@0 86 } catch (Exception x) {
aoqi@0 87 throw new WebServiceException(x.toString(), x);
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 String serviceId = "META-INF/services/" + factoryId;
aoqi@0 91 // try to find services in CLASSPATH
aoqi@0 92 BufferedReader rd = null;
aoqi@0 93 try {
aoqi@0 94 InputStream is;
aoqi@0 95 if (classLoader == null) {
aoqi@0 96 is=ClassLoader.getSystemResourceAsStream(serviceId);
aoqi@0 97 } else {
aoqi@0 98 is=classLoader.getResourceAsStream(serviceId);
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 if( is!=null ) {
aoqi@0 102 rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
aoqi@0 103
aoqi@0 104 String factoryClassName = rd.readLine();
aoqi@0 105
aoqi@0 106 if (factoryClassName != null &&
aoqi@0 107 ! "".equals(factoryClassName)) {
aoqi@0 108 return newInstance(factoryClassName, classLoader);
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111 } catch( Exception ignored) {
aoqi@0 112 } finally {
aoqi@0 113 close(rd);
aoqi@0 114 }
aoqi@0 115
aoqi@0 116
aoqi@0 117 // try to read from $java.home/lib/jaxws.properties
aoqi@0 118 FileInputStream inStream = null;
aoqi@0 119 try {
aoqi@0 120 String javah=System.getProperty( "java.home" );
aoqi@0 121 String configFile = javah + File.separator +
aoqi@0 122 "lib" + File.separator + "jaxws.properties";
aoqi@0 123 File f=new File( configFile );
aoqi@0 124 if( f.exists()) {
aoqi@0 125 Properties props=new Properties();
aoqi@0 126 inStream = new FileInputStream(f);
aoqi@0 127 props.load(inStream);
aoqi@0 128 String factoryClassName = props.getProperty(factoryId);
aoqi@0 129 return newInstance(factoryClassName, classLoader);
aoqi@0 130 }
aoqi@0 131 } catch(Exception ignored) {
aoqi@0 132 } finally {
aoqi@0 133 close(inStream);
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 // Use the system property
aoqi@0 137 try {
aoqi@0 138 String systemProp =
aoqi@0 139 System.getProperty( factoryId );
aoqi@0 140 if( systemProp!=null) {
aoqi@0 141 return newInstance(systemProp, classLoader);
aoqi@0 142 }
aoqi@0 143 } catch (SecurityException ignored) {
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 if (fallbackClassName == null) {
aoqi@0 147 throw new WebServiceException(
aoqi@0 148 "Provider for " + factoryId + " cannot be found", null);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 return newInstance(fallbackClassName, classLoader);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 private static void close(Closeable closeable) {
aoqi@0 155 if (closeable != null) {
aoqi@0 156 try {
aoqi@0 157 closeable.close();
aoqi@0 158 } catch (IOException ignored) {
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161 }
aoqi@0 162
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Loads the class, provided that the calling thread has an access to the class being loaded.
aoqi@0 166 */
aoqi@0 167 private static Class safeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
aoqi@0 168 try {
aoqi@0 169 // make sure that the current thread has an access to the package of the given name.
aoqi@0 170 SecurityManager s = System.getSecurityManager();
aoqi@0 171 if (s != null) {
aoqi@0 172 int i = className.lastIndexOf('.');
aoqi@0 173 if (i != -1) {
aoqi@0 174 s.checkPackageAccess(className.substring(0, i));
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 if (classLoader == null)
aoqi@0 179 return Class.forName(className);
aoqi@0 180 else
aoqi@0 181 return classLoader.loadClass(className);
aoqi@0 182 } catch (SecurityException se) {
aoqi@0 183 // anyone can access the platform default factory class without permission
aoqi@0 184 if (Provider.DEFAULT_JAXWSPROVIDER.equals(className))
aoqi@0 185 return Class.forName(className);
aoqi@0 186 throw se;
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
aoqi@0 191
aoqi@0 192 private static boolean isOsgi() {
aoqi@0 193 try {
aoqi@0 194 Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
aoqi@0 195 return true;
aoqi@0 196 } catch (ClassNotFoundException ignored) {
aoqi@0 197 }
aoqi@0 198 return false;
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 private static Object lookupUsingOSGiServiceLoader(String factoryId) {
aoqi@0 202 try {
aoqi@0 203 // Use reflection to avoid having any dependendcy on ServiceLoader class
aoqi@0 204 Class serviceClass = Class.forName(factoryId);
aoqi@0 205 Class[] args = new Class[]{serviceClass};
aoqi@0 206 Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
aoqi@0 207 java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
aoqi@0 208 java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator();
aoqi@0 209 return iter.hasNext() ? iter.next() : null;
aoqi@0 210 } catch (Exception ignored) {
aoqi@0 211 // log and continue
aoqi@0 212 return null;
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215
aoqi@0 216 }

mercurial