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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial