src/share/jaxws_classes/javax/xml/soap/FactoryFinder.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 384
8f2986ff0235
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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

mercurial