|
1 /* |
|
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package javax.xml.soap; |
|
27 |
|
28 import java.io.*; |
|
29 import java.util.Properties; |
|
30 |
|
31 |
|
32 class FactoryFinder { |
|
33 |
|
34 /** |
|
35 * Creates an instance of the specified class using the specified |
|
36 * <code>ClassLoader</code> object. |
|
37 * |
|
38 * @exception SOAPException if the given class could not be found |
|
39 * or could not be instantiated |
|
40 */ |
|
41 private static Object newInstance(String className, |
|
42 ClassLoader classLoader) |
|
43 throws SOAPException |
|
44 { |
|
45 try { |
|
46 Class spiClass; |
|
47 if (classLoader == null) { |
|
48 spiClass = Class.forName(className); |
|
49 } else { |
|
50 spiClass = classLoader.loadClass(className); |
|
51 } |
|
52 return spiClass.newInstance(); |
|
53 } catch (ClassNotFoundException x) { |
|
54 throw new SOAPException( |
|
55 "Provider " + className + " not found", x); |
|
56 } catch (Exception x) { |
|
57 throw new SOAPException( |
|
58 "Provider " + className + " could not be instantiated: " + x, |
|
59 x); |
|
60 } |
|
61 } |
|
62 |
|
63 /** |
|
64 * Finds the implementation <code>Class</code> object for the given |
|
65 * factory name, or null if that fails. |
|
66 * <P> |
|
67 * This method is package private so that this code can be shared. |
|
68 * |
|
69 * @return the <code>Class</code> object of the specified message factory; |
|
70 * or <code>null</code> |
|
71 * |
|
72 * @param factoryId the name of the factory to find, which is |
|
73 * a system property |
|
74 * @exception SOAPException if there is a SOAP error |
|
75 */ |
|
76 static Object find(String factId) |
|
77 throws SOAPException |
|
78 { |
|
79 final ClassLoader classLoader; |
|
80 final String factoryId = factId; |
|
81 try { |
|
82 classLoader = Thread.currentThread().getContextClassLoader(); |
|
83 } catch (Exception x) { |
|
84 throw new SOAPException(x.toString(), x); |
|
85 } |
|
86 |
|
87 // Use the system property first |
|
88 try { |
|
89 String systemProp = |
|
90 System.getProperty( factoryId ); |
|
91 if( systemProp!=null) { |
|
92 return newInstance(systemProp, classLoader); |
|
93 } |
|
94 } catch (SecurityException se) { |
|
95 } |
|
96 |
|
97 // try to read from $java.home/lib/jaxm.properties |
|
98 try { |
|
99 String javah=System.getProperty( "java.home" ); |
|
100 String configFile = javah + File.separator + |
|
101 "lib" + File.separator + "jaxm.properties"; |
|
102 final File f=new File( configFile ); |
|
103 if( f.exists()) { |
|
104 Properties props=new Properties(); |
|
105 props.load( new FileInputStream(f)); |
|
106 String factoryClassName = props.getProperty(factoryId); |
|
107 return newInstance(factoryClassName, classLoader); |
|
108 } |
|
109 } catch(Exception ex ) { |
|
110 } |
|
111 |
|
112 String serviceId = "META-INF/services/" + factoryId; |
|
113 // try to find services in CLASSPATH |
|
114 try { |
|
115 InputStream is=null; |
|
116 if (classLoader == null) { |
|
117 is=ClassLoader.getSystemResourceAsStream(serviceId); |
|
118 } else { |
|
119 is=classLoader.getResourceAsStream(serviceId); |
|
120 } |
|
121 |
|
122 if( is!=null ) { |
|
123 BufferedReader rd = |
|
124 new BufferedReader(new InputStreamReader(is, "UTF-8")); |
|
125 |
|
126 String factoryClassName = rd.readLine(); |
|
127 rd.close(); |
|
128 |
|
129 if (factoryClassName != null && |
|
130 ! "".equals(factoryClassName)) { |
|
131 return newInstance(factoryClassName, classLoader); |
|
132 } |
|
133 } |
|
134 } catch( Exception ex ) { |
|
135 } |
|
136 |
|
137 return null; |
|
138 } |
|
139 |
|
140 /** |
|
141 * Finds the implementation <code>Class</code> object for the given |
|
142 * factory name, or if that fails, finds the <code>Class</code> object |
|
143 * for the given fallback class name. The arguments supplied must be |
|
144 * used in order. If using the first argument is successful, the second |
|
145 * one will not be used. |
|
146 * <P> |
|
147 * This method is package private so that this code can be shared. |
|
148 * |
|
149 * @return the <code>Class</code> object of the specified message factory; |
|
150 * may not be <code>null</code> |
|
151 * |
|
152 * @param factoryId the name of the factory to find, which is |
|
153 * a system property |
|
154 * @param fallbackClassName the implementation class name, which is |
|
155 * to be used only if nothing else |
|
156 * is found; <code>null</code> to indicate that |
|
157 * there is no fallback class name |
|
158 * @exception SOAPException if there is a SOAP error |
|
159 */ |
|
160 static Object find(String factoryId, String fallbackClassName) |
|
161 throws SOAPException |
|
162 { |
|
163 |
|
164 Object obj = find(factoryId); |
|
165 if (obj != null) |
|
166 return obj; |
|
167 |
|
168 ClassLoader classLoader; |
|
169 try { |
|
170 classLoader = Thread.currentThread().getContextClassLoader(); |
|
171 } catch (Exception x) { |
|
172 throw new SOAPException(x.toString(), x); |
|
173 } |
|
174 |
|
175 if (fallbackClassName == null) { |
|
176 throw new SOAPException( |
|
177 "Provider for " + factoryId + " cannot be found", null); |
|
178 } |
|
179 |
|
180 return newInstance(fallbackClassName, classLoader); |
|
181 } |
|
182 } |