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