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