001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package javax.xml.soap;
020:
021: import java.io.BufferedReader;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.util.Properties;
027:
028: /**
029: * This class is used to locate factory classes for javax.xml.soap. It has package scope since it is
030: * not part of JAXM and should not be accessed from other packages.
031: */
032: class FactoryFinder {
033: /**
034: * instantiates an object go the given classname.
035: *
036: * @param factoryClassName
037: * @return a factory object
038: * @throws SOAPException
039: */
040: private static Object newInstance(String factoryClassName)
041: throws SOAPException {
042: ClassLoader classloader = null;
043: try {
044: classloader = Thread.currentThread()
045: .getContextClassLoader();
046: } catch (Exception exception) {
047: throw new SOAPException(exception.toString(), exception);
048: }
049:
050: try {
051: Class factory = null;
052: if (classloader == null) {
053: factory = Class.forName(factoryClassName);
054: } else {
055: try {
056: factory = classloader.loadClass(factoryClassName);
057: } catch (ClassNotFoundException cnfe) {
058: }
059: }
060: if (factory == null) {
061: classloader = FactoryFinder.class.getClassLoader();
062: factory = classloader.loadClass(factoryClassName);
063: }
064: return factory.newInstance();
065: } catch (ClassNotFoundException classnotfoundexception) {
066: throw new SOAPException("Provider " + factoryClassName
067: + " not found", classnotfoundexception);
068: } catch (Exception exception) {
069: throw new SOAPException("Provider " + factoryClassName
070: + " could not be instantiated: " + exception,
071: exception);
072: }
073: }
074:
075: /**
076: * Instantiates a factory object given the factory's property name and the default class name.
077: *
078: * @param factoryPropertyName
079: * @param defaultFactoryClassName
080: * @return a factory object
081: * @throws SOAPException
082: */
083: static Object find(String factoryPropertyName,
084: String defaultFactoryClassName) throws SOAPException {
085: try {
086: String factoryClassName = System
087: .getProperty(factoryPropertyName);
088: if (factoryClassName != null) {
089: return newInstance(factoryClassName);
090: }
091: } catch (SecurityException securityexception) {
092: }
093:
094: try {
095: String propertiesFileName = System.getProperty("java.home")
096: + File.separator + "lib" + File.separator
097: + "jaxm.properties";
098: File file = new File(propertiesFileName);
099: if (file.exists()) {
100: FileInputStream fileInput = new FileInputStream(file);
101: Properties properties = new Properties();
102: properties.load(fileInput);
103: fileInput.close();
104: String factoryClassName = properties
105: .getProperty(factoryPropertyName);
106: return newInstance(factoryClassName);
107: }
108: } catch (Exception exception1) {
109: }
110:
111: String factoryResource = "META-INF/services/"
112: + factoryPropertyName;
113:
114: try {
115: InputStream inputstream = getResource(factoryResource);
116: if (inputstream != null) {
117: BufferedReader bufferedreader = new BufferedReader(
118: new InputStreamReader(inputstream, "UTF-8"));
119: String factoryClassName = bufferedreader.readLine();
120: bufferedreader.close();
121: if ((factoryClassName != null)
122: && !"".equals(factoryClassName)) {
123: try {
124: return newInstance(factoryClassName);
125: } catch (Exception e) {
126: throw new SOAPException("Provider for "
127: + factoryPropertyName
128: + " cannot be found", null);
129: }
130: }
131: }
132: } catch (Exception exception2) {
133: }
134:
135: if (defaultFactoryClassName == null) {
136: throw new SOAPException("Provider for "
137: + factoryPropertyName + " cannot be found", null);
138: } else {
139: return newInstance(defaultFactoryClassName);
140: }
141: }
142:
143: /**
144: * Returns an input stream for the specified resource.
145: * <p/>
146: * <p>This method will firstly try <code>ClassLoader.getSystemResourceAsStream()</code> then the
147: * class loader of the current thread with <code>getResourceAsStream()</code> and finally
148: * attempt <code>getResourceAsStream()</code> on <code>FactoryFinder.class.getClassLoader()</code>.
149: *
150: * @param factoryResource the resource name
151: * @return an InputStream that can be used to read that resource, or <code>null</code> if the
152: * resource could not be resolved
153: */
154: private static InputStream getResource(String factoryResource) {
155: ClassLoader classloader = null;
156: try {
157: classloader = Thread.currentThread()
158: .getContextClassLoader();
159: } catch (SecurityException securityexception) {
160: }
161:
162: InputStream inputstream;
163: if (classloader == null) {
164: inputstream = ClassLoader
165: .getSystemResourceAsStream(factoryResource);
166: } else {
167: inputstream = classloader
168: .getResourceAsStream(factoryResource);
169: }
170:
171: if (inputstream == null) {
172: inputstream = FactoryFinder.class.getClassLoader()
173: .getResourceAsStream(factoryResource);
174: }
175: return inputstream;
176: }
177: }
|