| 1 |
/* |
| 2 |
* Copyright 2001-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
package javax.xml.soap; |
| 17 |
|
| 18 |
import java.io.BufferedReader; |
| 19 |
import java.io.File; |
| 20 |
import java.io.FileInputStream; |
| 21 |
import java.io.InputStream; |
| 22 |
import java.io.InputStreamReader; |
| 23 |
|
| 24 |
import java.util.Properties; |
| 25 |
|
| 26 |
/** |
| 27 |
* This class is used to locate factory classes for javax.xml.soap. |
| 28 |
* It has package scope since it is not part of JAXM and should not |
| 29 |
* be accessed from other packages. |
| 30 |
*/ |
| 31 |
class FactoryFinder { |
| 32 |
/** |
| 33 |
* instantiates an object go the given classname. |
| 34 |
* |
| 35 |
* @param factoryClassName |
| 36 |
* @return a factory object |
| 37 |
* @throws SOAPException |
| 38 |
*/ |
| 39 |
private static Object newInstance(String factoryClassName) throws SOAPException { |
| 40 |
ClassLoader classloader = null; |
| 41 |
try { |
| 42 |
classloader = Thread.currentThread().getContextClassLoader(); |
| 43 |
} catch (Exception exception) { |
| 44 |
throw new SOAPException(exception.toString(), exception); |
| 45 |
} |
| 46 |
|
| 47 |
try { |
| 48 |
Class factory = null; |
| 49 |
if (classloader == null) { |
| 50 |
factory = Class.forName(factoryClassName); |
| 51 |
} else { |
| 52 |
try { |
| 53 |
factory = classloader.loadClass(factoryClassName); |
| 54 |
} catch (ClassNotFoundException cnfe) {} |
| 55 |
} |
| 56 |
if (factory == null) { |
| 57 |
classloader = FactoryFinder.class.getClassLoader(); |
| 58 |
factory = classloader.loadClass(factoryClassName); |
| 59 |
} |
| 60 |
return factory.newInstance(); |
| 61 |
} catch (ClassNotFoundException classnotfoundexception) { |
| 62 |
throw new SOAPException("Provider " + factoryClassName + " not found", classnotfoundexception); |
| 63 |
} catch (Exception exception) { |
| 64 |
throw new SOAPException("Provider " + factoryClassName + " could not be instantiated: " + exception, exception); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Instantiates a factory object given the factory's property name and the |
| 70 |
* default class name. |
| 71 |
* |
| 72 |
* @param factoryPropertyName |
| 73 |
* @param defaultFactoryClassName |
| 74 |
* @return a factory object |
| 75 |
* @throws SOAPException |
| 76 |
*/ |
| 77 |
static Object find(String factoryPropertyName, String defaultFactoryClassName) throws SOAPException { |
| 78 |
try { |
| 79 |
String factoryClassName = System.getProperty(factoryPropertyName); |
| 80 |
if (factoryClassName != null) { |
| 81 |
return newInstance(factoryClassName); |
| 82 |
} |
| 83 |
} catch (SecurityException securityexception) {} |
| 84 |
|
| 85 |
try { |
| 86 |
String propertiesFileName = System.getProperty("java.home") |
| 87 |
+ File.separator + "lib" |
| 88 |
+ File.separator + "jaxm.properties"; |
| 89 |
File file = new File(propertiesFileName); |
| 90 |
if (file.exists()) { |
| 91 |
FileInputStream fileInput = new FileInputStream(file); |
| 92 |
Properties properties = new Properties(); |
| 93 |
properties.load(fileInput); |
| 94 |
fileInput.close(); |
| 95 |
String factoryClassName = properties.getProperty(factoryPropertyName); |
| 96 |
return newInstance(factoryClassName); |
| 97 |
} |
| 98 |
} catch (Exception exception1) {} |
| 99 |
|
| 100 |
String factoryResource = "META-INF/services/" + factoryPropertyName; |
| 101 |
|
| 102 |
try { |
| 103 |
InputStream inputstream = getResource(factoryResource); |
| 104 |
if (inputstream != null) { |
| 105 |
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(inputstream, "UTF-8")); |
| 106 |
String factoryClassName = bufferedreader.readLine(); |
| 107 |
bufferedreader.close(); |
| 108 |
if ((factoryClassName != null) && !"".equals(factoryClassName)) { |
| 109 |
return newInstance(factoryClassName); |
| 110 |
} |
| 111 |
} |
| 112 |
} catch (Exception exception2) {} |
| 113 |
|
| 114 |
if (defaultFactoryClassName == null) { |
| 115 |
throw new SOAPException("Provider for " + factoryPropertyName + " cannot be found", null); |
| 116 |
} else { |
| 117 |
return newInstance(defaultFactoryClassName); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns an input stream for the specified resource. |
| 123 |
* |
| 124 |
* <p>This method will firstly try |
| 125 |
* <code>ClassLoader.getSystemResourceAsStream()</code> then |
| 126 |
* the class loader of the current thread with |
| 127 |
* <code>getResourceAsStream()</code> and finally attempt |
| 128 |
* <code>getResourceAsStream()</code> on |
| 129 |
* <code>FactoryFinder.class.getClassLoader()</code>. |
| 130 |
* |
| 131 |
* @param factoryResource the resource name |
| 132 |
* @return an InputStream that can be used to read that resource, or |
| 133 |
* <code>null</code> if the resource could not be resolved |
| 134 |
*/ |
| 135 |
private static InputStream getResource(String factoryResource) { |
| 136 |
ClassLoader classloader = null; |
| 137 |
try { |
| 138 |
classloader = Thread.currentThread().getContextClassLoader(); |
| 139 |
} catch (SecurityException securityexception) {} |
| 140 |
|
| 141 |
InputStream inputstream; |
| 142 |
if (classloader == null) { |
| 143 |
inputstream = ClassLoader.getSystemResourceAsStream(factoryResource); |
| 144 |
} else { |
| 145 |
inputstream = classloader.getResourceAsStream(factoryResource); |
| 146 |
} |
| 147 |
|
| 148 |
if (inputstream == null) { |
| 149 |
inputstream = FactoryFinder.class.getClassLoader().getResourceAsStream(factoryResource); |
| 150 |
} |
| 151 |
return inputstream; |
| 152 |
} |
| 153 |
} |