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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 2004, 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.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,
ohair@286 42 ClassLoader classLoader)
ohair@286 43 throws SOAPException
ohair@286 44 {
ohair@286 45 try {
ohair@286 46 Class spiClass;
ohair@286 47 if (classLoader == null) {
ohair@286 48 spiClass = Class.forName(className);
ohair@286 49 } else {
ohair@286 50 spiClass = classLoader.loadClass(className);
ohair@286 51 }
ohair@286 52 return spiClass.newInstance();
ohair@286 53 } catch (ClassNotFoundException x) {
ohair@286 54 throw new SOAPException(
ohair@286 55 "Provider " + className + " not found", x);
ohair@286 56 } catch (Exception x) {
ohair@286 57 throw new SOAPException(
ohair@286 58 "Provider " + className + " could not be instantiated: " + x,
ohair@286 59 x);
ohair@286 60 }
ohair@286 61 }
ohair@286 62
ohair@286 63 /**
ohair@286 64 * Finds the implementation <code>Class</code> object for the given
ohair@286 65 * factory name, or null if that fails.
ohair@286 66 * <P>
ohair@286 67 * This method is package private so that this code can be shared.
ohair@286 68 *
ohair@286 69 * @return the <code>Class</code> object of the specified message factory;
ohair@286 70 * or <code>null</code>
ohair@286 71 *
ohair@286 72 * @param factoryId the name of the factory to find, which is
ohair@286 73 * a system property
ohair@286 74 * @exception SOAPException if there is a SOAP error
ohair@286 75 */
ohair@286 76 static Object find(String factId)
ohair@286 77 throws SOAPException
ohair@286 78 {
ohair@286 79 final ClassLoader classLoader;
ohair@286 80 final String factoryId = factId;
ohair@286 81 try {
ohair@286 82 classLoader = Thread.currentThread().getContextClassLoader();
ohair@286 83 } catch (Exception x) {
ohair@286 84 throw new SOAPException(x.toString(), x);
ohair@286 85 }
ohair@286 86
ohair@286 87 // Use the system property first
ohair@286 88 try {
ohair@286 89 String systemProp =
ohair@286 90 System.getProperty( factoryId );
ohair@286 91 if( systemProp!=null) {
ohair@286 92 return newInstance(systemProp, classLoader);
ohair@286 93 }
ohair@286 94 } catch (SecurityException se) {
ohair@286 95 }
ohair@286 96
ohair@286 97 // try to read from $java.home/lib/jaxm.properties
ohair@286 98 try {
ohair@286 99 String javah=System.getProperty( "java.home" );
ohair@286 100 String configFile = javah + File.separator +
ohair@286 101 "lib" + File.separator + "jaxm.properties";
ohair@286 102 final File f=new File( configFile );
ohair@286 103 if( f.exists()) {
ohair@286 104 Properties props=new Properties();
ohair@286 105 props.load( new FileInputStream(f));
ohair@286 106 String factoryClassName = props.getProperty(factoryId);
ohair@286 107 return newInstance(factoryClassName, classLoader);
ohair@286 108 }
ohair@286 109 } catch(Exception ex ) {
ohair@286 110 }
ohair@286 111
ohair@286 112 String serviceId = "META-INF/services/" + factoryId;
ohair@286 113 // try to find services in CLASSPATH
ohair@286 114 try {
ohair@286 115 InputStream is=null;
ohair@286 116 if (classLoader == null) {
ohair@286 117 is=ClassLoader.getSystemResourceAsStream(serviceId);
ohair@286 118 } else {
ohair@286 119 is=classLoader.getResourceAsStream(serviceId);
ohair@286 120 }
ohair@286 121
ohair@286 122 if( is!=null ) {
ohair@286 123 BufferedReader rd =
ohair@286 124 new BufferedReader(new InputStreamReader(is, "UTF-8"));
ohair@286 125
ohair@286 126 String factoryClassName = rd.readLine();
ohair@286 127 rd.close();
ohair@286 128
ohair@286 129 if (factoryClassName != null &&
ohair@286 130 ! "".equals(factoryClassName)) {
ohair@286 131 return newInstance(factoryClassName, classLoader);
ohair@286 132 }
ohair@286 133 }
ohair@286 134 } catch( Exception ex ) {
ohair@286 135 }
ohair@286 136
ohair@286 137 return null;
ohair@286 138 }
ohair@286 139
ohair@286 140 /**
ohair@286 141 * Finds the implementation <code>Class</code> object for the given
ohair@286 142 * factory name, or if that fails, finds the <code>Class</code> object
ohair@286 143 * for the given fallback class name. The arguments supplied must be
ohair@286 144 * used in order. If using the first argument is successful, the second
ohair@286 145 * one will not be used.
ohair@286 146 * <P>
ohair@286 147 * This method is package private so that this code can be shared.
ohair@286 148 *
ohair@286 149 * @return the <code>Class</code> object of the specified message factory;
ohair@286 150 * may not be <code>null</code>
ohair@286 151 *
ohair@286 152 * @param factoryId the name of the factory to find, which is
ohair@286 153 * a system property
ohair@286 154 * @param fallbackClassName the implementation class name, which is
ohair@286 155 * to be used only if nothing else
ohair@286 156 * is found; <code>null</code> to indicate that
ohair@286 157 * there is no fallback class name
ohair@286 158 * @exception SOAPException if there is a SOAP error
ohair@286 159 */
ohair@286 160 static Object find(String factoryId, String fallbackClassName)
ohair@286 161 throws SOAPException
ohair@286 162 {
ohair@286 163
ohair@286 164 Object obj = find(factoryId);
ohair@286 165 if (obj != null)
ohair@286 166 return obj;
ohair@286 167
ohair@286 168 ClassLoader classLoader;
ohair@286 169 try {
ohair@286 170 classLoader = Thread.currentThread().getContextClassLoader();
ohair@286 171 } catch (Exception x) {
ohair@286 172 throw new SOAPException(x.toString(), x);
ohair@286 173 }
ohair@286 174
ohair@286 175 if (fallbackClassName == null) {
ohair@286 176 throw new SOAPException(
ohair@286 177 "Provider for " + factoryId + " cannot be found", null);
ohair@286 178 }
ohair@286 179
ohair@286 180 return newInstance(fallbackClassName, classLoader);
ohair@286 181 }
ohair@286 182 }

mercurial