Tue, 09 Apr 2013 14:51:13 +0100
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) 2012, 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.soap; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.*; |
ohair@286 | 29 | import java.util.Properties; |
ohair@286 | 30 | |
ohair@286 | 31 | |
ohair@286 | 32 | class FactoryFinder { |
ohair@286 | 33 | |
ohair@286 | 34 | /** |
ohair@286 | 35 | * Creates an instance of the specified class using the specified |
ohair@286 | 36 | * <code>ClassLoader</code> object. |
ohair@286 | 37 | * |
ohair@286 | 38 | * @exception SOAPException if the given class could not be found |
ohair@286 | 39 | * or could not be instantiated |
ohair@286 | 40 | */ |
ohair@286 | 41 | private static Object newInstance(String className, |
alanb@368 | 42 | ClassLoader classLoader, |
alanb@368 | 43 | String defaultFactoryClass) |
ohair@286 | 44 | throws SOAPException |
ohair@286 | 45 | { |
ohair@286 | 46 | try { |
alanb@368 | 47 | Class spiClass = safeLoadClass(className, classLoader, defaultFactoryClass); |
ohair@286 | 48 | return spiClass.newInstance(); |
ohair@286 | 49 | } catch (ClassNotFoundException x) { |
ohair@286 | 50 | throw new SOAPException( |
ohair@286 | 51 | "Provider " + className + " not found", x); |
ohair@286 | 52 | } catch (Exception x) { |
ohair@286 | 53 | throw new SOAPException( |
ohair@286 | 54 | "Provider " + className + " could not be instantiated: " + x, |
ohair@286 | 55 | x); |
ohair@286 | 56 | } |
ohair@286 | 57 | } |
ohair@286 | 58 | |
ohair@286 | 59 | /** |
ohair@286 | 60 | * Finds the implementation <code>Class</code> object for the given |
ohair@286 | 61 | * factory name, or null if that fails. |
ohair@286 | 62 | * <P> |
ohair@286 | 63 | * This method is package private so that this code can be shared. |
ohair@286 | 64 | * |
ohair@286 | 65 | * @return the <code>Class</code> object of the specified message factory; |
ohair@286 | 66 | * or <code>null</code> |
ohair@286 | 67 | * |
ohair@286 | 68 | * @param factoryId the name of the factory to find, which is |
ohair@286 | 69 | * a system property |
ohair@286 | 70 | * @exception SOAPException if there is a SOAP error |
ohair@286 | 71 | */ |
alanb@368 | 72 | static Object find(String factoryId) |
ohair@286 | 73 | throws SOAPException |
ohair@286 | 74 | { |
alanb@368 | 75 | return find(factoryId, null, false); |
ohair@286 | 76 | } |
ohair@286 | 77 | |
ohair@286 | 78 | /** |
ohair@286 | 79 | * Finds the implementation <code>Class</code> object for the given |
ohair@286 | 80 | * factory name, or if that fails, finds the <code>Class</code> object |
ohair@286 | 81 | * for the given fallback class name. The arguments supplied must be |
ohair@286 | 82 | * used in order. If using the first argument is successful, the second |
ohair@286 | 83 | * one will not be used. |
ohair@286 | 84 | * <P> |
ohair@286 | 85 | * This method is package private so that this code can be shared. |
ohair@286 | 86 | * |
ohair@286 | 87 | * @return the <code>Class</code> object of the specified message factory; |
ohair@286 | 88 | * may not be <code>null</code> |
ohair@286 | 89 | * |
ohair@286 | 90 | * @param factoryId the name of the factory to find, which is |
ohair@286 | 91 | * a system property |
ohair@286 | 92 | * @param fallbackClassName the implementation class name, which is |
ohair@286 | 93 | * to be used only if nothing else |
ohair@286 | 94 | * is found; <code>null</code> to indicate that |
ohair@286 | 95 | * there is no fallback class name |
ohair@286 | 96 | * @exception SOAPException if there is a SOAP error |
ohair@286 | 97 | */ |
ohair@286 | 98 | static Object find(String factoryId, String fallbackClassName) |
ohair@286 | 99 | throws SOAPException |
ohair@286 | 100 | { |
alanb@368 | 101 | return find(factoryId, fallbackClassName, true); |
alanb@368 | 102 | } |
ohair@286 | 103 | |
alanb@368 | 104 | /** |
alanb@368 | 105 | * Finds the implementation <code>Class</code> object for the given |
alanb@368 | 106 | * factory name, or if that fails, finds the <code>Class</code> object |
alanb@368 | 107 | * for the given default class name, but only if <code>tryFallback</code> |
alanb@368 | 108 | * is <code>true</code>. The arguments supplied must be used in order |
alanb@368 | 109 | * If using the first argument is successful, the second one will not |
alanb@368 | 110 | * be used. Note the default class name may be needed even if fallback |
alanb@368 | 111 | * is not to be attempted, so certain error condiitons can be handled. |
alanb@368 | 112 | * <P> |
alanb@368 | 113 | * This method is package private so that this code can be shared. |
alanb@368 | 114 | * |
alanb@368 | 115 | * @return the <code>Class</code> object of the specified message factory; |
alanb@368 | 116 | * may not be <code>null</code> |
alanb@368 | 117 | * |
alanb@368 | 118 | * @param factoryId the name of the factory to find, which is |
alanb@368 | 119 | * a system property |
alanb@368 | 120 | * @param defaultClassName the implementation class name, which is |
alanb@368 | 121 | * to be used only if nothing else |
alanb@368 | 122 | * is found; <code>null</code> to indicate |
alanb@368 | 123 | * that there is no default class name |
alanb@368 | 124 | * @param tryFallback whether to try the default class as a |
alanb@368 | 125 | * fallback |
alanb@368 | 126 | * @exception SOAPException if there is a SOAP error |
alanb@368 | 127 | */ |
alanb@368 | 128 | static Object find(String factoryId, String defaultClassName, |
alanb@368 | 129 | boolean tryFallback) throws SOAPException { |
ohair@286 | 130 | ClassLoader classLoader; |
ohair@286 | 131 | try { |
ohair@286 | 132 | classLoader = Thread.currentThread().getContextClassLoader(); |
ohair@286 | 133 | } catch (Exception x) { |
ohair@286 | 134 | throw new SOAPException(x.toString(), x); |
ohair@286 | 135 | } |
ohair@286 | 136 | |
alanb@368 | 137 | // Use the system property first |
alanb@368 | 138 | try { |
alanb@368 | 139 | String systemProp = |
alanb@368 | 140 | System.getProperty( factoryId ); |
alanb@368 | 141 | if( systemProp!=null) { |
alanb@368 | 142 | return newInstance(systemProp, classLoader, defaultClassName); |
alanb@368 | 143 | } |
alanb@368 | 144 | } catch (SecurityException se) { |
alanb@368 | 145 | } |
alanb@368 | 146 | |
alanb@368 | 147 | // try to read from $java.home/lib/jaxm.properties |
alanb@368 | 148 | try { |
alanb@368 | 149 | String javah=System.getProperty( "java.home" ); |
alanb@368 | 150 | String configFile = javah + File.separator + |
alanb@368 | 151 | "lib" + File.separator + "jaxm.properties"; |
alanb@368 | 152 | File f=new File( configFile ); |
alanb@368 | 153 | if( f.exists()) { |
alanb@368 | 154 | Properties props=new Properties(); |
alanb@368 | 155 | props.load( new FileInputStream(f)); |
alanb@368 | 156 | String factoryClassName = props.getProperty(factoryId); |
alanb@368 | 157 | return newInstance(factoryClassName, classLoader, defaultClassName); |
alanb@368 | 158 | } |
alanb@368 | 159 | } catch(Exception ex ) { |
alanb@368 | 160 | } |
alanb@368 | 161 | |
alanb@368 | 162 | String serviceId = "META-INF/services/" + factoryId; |
alanb@368 | 163 | // try to find services in CLASSPATH |
alanb@368 | 164 | try { |
alanb@368 | 165 | InputStream is=null; |
alanb@368 | 166 | if (classLoader == null) { |
alanb@368 | 167 | is=ClassLoader.getSystemResourceAsStream(serviceId); |
alanb@368 | 168 | } else { |
alanb@368 | 169 | is=classLoader.getResourceAsStream(serviceId); |
alanb@368 | 170 | } |
alanb@368 | 171 | |
alanb@368 | 172 | if( is!=null ) { |
alanb@368 | 173 | BufferedReader rd = |
alanb@368 | 174 | new BufferedReader(new InputStreamReader(is, "UTF-8")); |
alanb@368 | 175 | |
alanb@368 | 176 | String factoryClassName = rd.readLine(); |
alanb@368 | 177 | rd.close(); |
alanb@368 | 178 | |
alanb@368 | 179 | if (factoryClassName != null && |
alanb@368 | 180 | ! "".equals(factoryClassName)) { |
alanb@368 | 181 | return newInstance(factoryClassName, classLoader, defaultClassName); |
alanb@368 | 182 | } |
alanb@368 | 183 | } |
alanb@368 | 184 | } catch( Exception ex ) { |
alanb@368 | 185 | } |
alanb@368 | 186 | |
alanb@368 | 187 | // If not found and fallback should not be tried, return a null result. |
alanb@368 | 188 | if (!tryFallback) |
alanb@368 | 189 | return null; |
alanb@368 | 190 | |
alanb@368 | 191 | // We didn't find the class through the usual means so try the default |
alanb@368 | 192 | // (built in) factory if specified. |
alanb@368 | 193 | if (defaultClassName == null) { |
ohair@286 | 194 | throw new SOAPException( |
ohair@286 | 195 | "Provider for " + factoryId + " cannot be found", null); |
ohair@286 | 196 | } |
alanb@368 | 197 | return newInstance(defaultClassName, classLoader, defaultClassName); |
alanb@368 | 198 | } |
ohair@286 | 199 | |
alanb@368 | 200 | /** |
alanb@368 | 201 | * Loads the class, provided that the calling thread has an access to the |
alanb@368 | 202 | * class being loaded. If this is the specified default factory class and it |
alanb@368 | 203 | * is restricted by package.access we get a SecurityException and can do a |
alanb@368 | 204 | * Class.forName() on it so it will be loaded by the bootstrap class loader. |
alanb@368 | 205 | */ |
alanb@368 | 206 | private static Class safeLoadClass(String className, |
alanb@368 | 207 | ClassLoader classLoader, String defaultFactoryClass) |
alanb@368 | 208 | throws ClassNotFoundException { |
alanb@368 | 209 | try { |
alanb@368 | 210 | // make sure that the current thread has an access to the package of the given name. |
alanb@368 | 211 | SecurityManager s = System.getSecurityManager(); |
alanb@368 | 212 | if (s != null) { |
alanb@368 | 213 | int i = className.lastIndexOf('.'); |
alanb@368 | 214 | if (i != -1) { |
alanb@368 | 215 | s.checkPackageAccess(className.substring(0, i)); |
alanb@368 | 216 | } |
alanb@368 | 217 | } |
alanb@368 | 218 | |
alanb@368 | 219 | if (classLoader == null) |
alanb@368 | 220 | return Class.forName(className); |
alanb@368 | 221 | else |
alanb@368 | 222 | return classLoader.loadClass(className); |
alanb@368 | 223 | } catch (SecurityException se) { |
alanb@368 | 224 | // The FactoryFinder is in the bootstrap class loader, so |
alanb@368 | 225 | // the following should work, but we only attempt it |
alanb@368 | 226 | // if it the the default class. |
alanb@368 | 227 | if (className.equals(defaultFactoryClass)) |
alanb@368 | 228 | return Class.forName(className); |
alanb@368 | 229 | throw se; |
alanb@368 | 230 | } |
ohair@286 | 231 | } |
ohair@286 | 232 | } |