91 throw new WebServiceException(x.toString(), x); |
87 throw new WebServiceException(x.toString(), x); |
92 } |
88 } |
93 |
89 |
94 String serviceId = "META-INF/services/" + factoryId; |
90 String serviceId = "META-INF/services/" + factoryId; |
95 // try to find services in CLASSPATH |
91 // try to find services in CLASSPATH |
96 try { |
92 BufferedReader rd = null; |
97 InputStream is=null; |
93 try { |
|
94 InputStream is; |
98 if (classLoader == null) { |
95 if (classLoader == null) { |
99 is=ClassLoader.getSystemResourceAsStream(serviceId); |
96 is=ClassLoader.getSystemResourceAsStream(serviceId); |
100 } else { |
97 } else { |
101 is=classLoader.getResourceAsStream(serviceId); |
98 is=classLoader.getResourceAsStream(serviceId); |
102 } |
99 } |
103 |
100 |
104 if( is!=null ) { |
101 if( is!=null ) { |
105 BufferedReader rd = |
102 rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); |
106 new BufferedReader(new InputStreamReader(is, "UTF-8")); |
|
107 |
103 |
108 String factoryClassName = rd.readLine(); |
104 String factoryClassName = rd.readLine(); |
109 rd.close(); |
|
110 |
105 |
111 if (factoryClassName != null && |
106 if (factoryClassName != null && |
112 ! "".equals(factoryClassName)) { |
107 ! "".equals(factoryClassName)) { |
113 return newInstance(factoryClassName, classLoader); |
108 return newInstance(factoryClassName, classLoader); |
114 } |
109 } |
115 } |
110 } |
116 } catch( Exception ex ) { |
111 } catch( Exception ignored) { |
|
112 } finally { |
|
113 close(rd); |
117 } |
114 } |
118 |
115 |
119 |
116 |
120 // try to read from $java.home/lib/jaxws.properties |
117 // try to read from $java.home/lib/jaxws.properties |
|
118 FileInputStream inStream = null; |
121 try { |
119 try { |
122 String javah=System.getProperty( "java.home" ); |
120 String javah=System.getProperty( "java.home" ); |
123 String configFile = javah + File.separator + |
121 String configFile = javah + File.separator + |
124 "lib" + File.separator + "jaxws.properties"; |
122 "lib" + File.separator + "jaxws.properties"; |
125 File f=new File( configFile ); |
123 File f=new File( configFile ); |
126 if( f.exists()) { |
124 if( f.exists()) { |
127 Properties props=new Properties(); |
125 Properties props=new Properties(); |
128 props.load( new FileInputStream(f)); |
126 inStream = new FileInputStream(f); |
|
127 props.load(inStream); |
129 String factoryClassName = props.getProperty(factoryId); |
128 String factoryClassName = props.getProperty(factoryId); |
130 return newInstance(factoryClassName, classLoader); |
129 return newInstance(factoryClassName, classLoader); |
131 } |
130 } |
132 } catch(Exception ex ) { |
131 } catch(Exception ignored) { |
133 } |
132 } finally { |
134 |
133 close(inStream); |
|
134 } |
135 |
135 |
136 // Use the system property |
136 // Use the system property |
137 try { |
137 try { |
138 String systemProp = |
138 String systemProp = |
139 System.getProperty( factoryId ); |
139 System.getProperty( factoryId ); |
140 if( systemProp!=null) { |
140 if( systemProp!=null) { |
141 return newInstance(systemProp, classLoader); |
141 return newInstance(systemProp, classLoader); |
142 } |
142 } |
143 } catch (SecurityException se) { |
143 } catch (SecurityException ignored) { |
144 } |
144 } |
145 |
145 |
146 if (fallbackClassName == null) { |
146 if (fallbackClassName == null) { |
147 throw new WebServiceException( |
147 throw new WebServiceException( |
148 "Provider for " + factoryId + " cannot be found", null); |
148 "Provider for " + factoryId + " cannot be found", null); |
149 } |
149 } |
150 |
150 |
151 return newInstance(fallbackClassName, classLoader); |
151 return newInstance(fallbackClassName, classLoader); |
|
152 } |
|
153 |
|
154 private static void close(Closeable closeable) { |
|
155 if (closeable != null) { |
|
156 try { |
|
157 closeable.close(); |
|
158 } catch (IOException ignored) { |
|
159 } |
|
160 } |
152 } |
161 } |
153 |
162 |
154 |
163 |
155 /** |
164 /** |
156 * Loads the class, provided that the calling thread has an access to the class being loaded. |
165 * Loads the class, provided that the calling thread has an access to the class being loaded. |
194 // Use reflection to avoid having any dependendcy on ServiceLoader class |
203 // Use reflection to avoid having any dependendcy on ServiceLoader class |
195 Class serviceClass = Class.forName(factoryId); |
204 Class serviceClass = Class.forName(factoryId); |
196 Class[] args = new Class[]{serviceClass}; |
205 Class[] args = new Class[]{serviceClass}; |
197 Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); |
206 Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); |
198 java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class); |
207 java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class); |
199 java.util.Iterator iter = ((Iterable) m.invoke(null, args)).iterator(); |
208 java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator(); |
200 return iter.hasNext() ? iter.next() : null; |
209 return iter.hasNext() ? iter.next() : null; |
201 } catch (Exception e) { |
210 } catch (Exception ignored) { |
202 // log and continue |
211 // log and continue |
203 return null; |
212 return null; |
204 } |
213 } |
205 } |
214 } |
206 |
215 |