src/share/jaxws_classes/javax/xml/ws/spi/FactoryFinder.java

changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
equal deleted inserted replaced
366:8c0b6bccfe47 368:0989ad8c0860
1 /* 1 /*
2 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. 2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * 4 *
5 * This code is free software; you can redistribute it and/or modify it 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 6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this 7 * published by the Free Software Foundation. Oracle designates this
23 * questions. 23 * questions.
24 */ 24 */
25 25
26 package javax.xml.ws.spi; 26 package javax.xml.ws.spi;
27 27
28 import java.io.InputStream; 28 import java.io.*;
29 import java.io.File;
30 import java.io.FileInputStream;
31 29
32 import java.util.Properties; 30 import java.util.Properties;
33 import java.io.BufferedReader;
34 import java.io.InputStreamReader;
35 import javax.xml.ws.WebServiceException; 31 import javax.xml.ws.WebServiceException;
36 32
37 class FactoryFinder { 33 class FactoryFinder {
38 34
39 /** 35 /**
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.
182 191
183 private static boolean isOsgi() { 192 private static boolean isOsgi() {
184 try { 193 try {
185 Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); 194 Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
186 return true; 195 return true;
187 } catch (ClassNotFoundException cnfe) { 196 } catch (ClassNotFoundException ignored) {
188 } 197 }
189 return false; 198 return false;
190 } 199 }
191 200
192 private static Object lookupUsingOSGiServiceLoader(String factoryId) { 201 private static Object lookupUsingOSGiServiceLoader(String factoryId) {
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

mercurial