001: /*
002: * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
003: * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
004: */
005:
006: package javax.xml.ws.spi;
007:
008: import java.io.InputStream;
009: import java.io.File;
010: import java.io.FileInputStream;
011:
012: import java.util.Properties;
013: import java.io.BufferedReader;
014: import java.io.InputStreamReader;
015: import javax.xml.ws.WebServiceException;
016:
017: class FactoryFinder {
018:
019: /**
020: * Creates an instance of the specified class using the specified
021: * <code>ClassLoader</code> object.
022: *
023: * @exception WebServiceException if the given class could not be found
024: * or could not be instantiated
025: */
026: private static Object newInstance(String className,
027: ClassLoader classLoader) {
028: try {
029: Class spiClass;
030: if (classLoader == null) {
031: spiClass = Class.forName(className);
032: } else {
033: spiClass = classLoader.loadClass(className);
034: }
035: return spiClass.newInstance();
036: } catch (ClassNotFoundException x) {
037: throw new WebServiceException("Provider " + className
038: + " not found", x);
039: } catch (Exception x) {
040: throw new WebServiceException("Provider " + className
041: + " could not be instantiated: " + x, x);
042: }
043: }
044:
045: /**
046: * Finds the implementation <code>Class</code> object for the given
047: * factory name, or if that fails, finds the <code>Class</code> object
048: * for the given fallback class name. The arguments supplied MUST be
049: * used in order. If using the first argument is successful, the second
050: * one will not be used.
051: * <P>
052: * This method is package private so that this code can be shared.
053: *
054: * @return the <code>Class</code> object of the specified message factory;
055: * may not be <code>null</code>
056: *
057: * @param factoryId the name of the factory to find, which is
058: * a system property
059: * @param fallbackClassName the implementation class name, which is
060: * to be used only if nothing else
061: * is found; <code>null</code> to indicate that
062: * there is no fallback class name
063: * @exception WebServiceException if there is an error
064: */
065: static Object find(String factoryId, String fallbackClassName) {
066: ClassLoader classLoader;
067: try {
068: classLoader = Thread.currentThread()
069: .getContextClassLoader();
070: } catch (Exception x) {
071: throw new WebServiceException(x.toString(), x);
072: }
073:
074: String serviceId = "META-INF/services/" + factoryId;
075: // try to find services in CLASSPATH
076: try {
077: InputStream is = null;
078: if (classLoader == null) {
079: is = ClassLoader.getSystemResourceAsStream(serviceId);
080: } else {
081: is = classLoader.getResourceAsStream(serviceId);
082: }
083:
084: if (is != null) {
085: BufferedReader rd = new BufferedReader(
086: new InputStreamReader(is, "UTF-8"));
087:
088: String factoryClassName = rd.readLine();
089: rd.close();
090:
091: if (factoryClassName != null
092: && !"".equals(factoryClassName)) {
093: return newInstance(factoryClassName, classLoader);
094: }
095: }
096: } catch (Exception ex) {
097: }
098:
099: // try to read from $java.home/lib/jaxws.properties
100: try {
101: String javah = System.getProperty("java.home");
102: String configFile = javah + File.separator + "lib"
103: + File.separator + "jaxws.properties";
104: File f = new File(configFile);
105: if (f.exists()) {
106: Properties props = new Properties();
107: props.load(new FileInputStream(f));
108: String factoryClassName = props.getProperty(factoryId);
109: return newInstance(factoryClassName, classLoader);
110: }
111: } catch (Exception ex) {
112: }
113:
114: // Use the system property
115: try {
116: String systemProp = System.getProperty(factoryId);
117: if (systemProp != null) {
118: return newInstance(systemProp, classLoader);
119: }
120: } catch (SecurityException se) {
121: }
122:
123: if (fallbackClassName == null) {
124: throw new WebServiceException("Provider for " + factoryId
125: + " cannot be found", null);
126: }
127:
128: return newInstance(fallbackClassName, classLoader);
129: }
130: }
|