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.ws.spi; 27 28 import java.io.*; 29 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.Properties; 34 import java.util.logging.Level; 35 import java.util.logging.Logger; 36 import javax.xml.ws.WebServiceException; 37 38 class FactoryFinder { 39 40 private static final Logger logger = Logger.getLogger("javax.xml.ws"); 41 42 private static final ServiceLoaderUtil.ExceptionHandler<WebServiceException> EXCEPTION_HANDLER = 43 new ServiceLoaderUtil.ExceptionHandler<WebServiceException>() { 44 @Override 45 public WebServiceException createException(Throwable throwable, String message) { 46 return new WebServiceException(message, throwable); 47 } 48 }; 49 50 /** 51 * Finds the implementation {@code Class} object for the given 98 fallbackClassName, classLoader, EXCEPTION_HANDLER); 99 } 100 101 private static Object fromSystemProperty(String factoryId, 102 String fallbackClassName, 103 ClassLoader classLoader) { 104 try { 105 String systemProp = System.getProperty(factoryId); 106 if (systemProp != null) { 107 return ServiceLoaderUtil.newInstance(systemProp, 108 fallbackClassName, classLoader, EXCEPTION_HANDLER); 109 } 110 } catch (SecurityException ignored) { 111 } 112 return null; 113 } 114 115 private static Object fromJDKProperties(String factoryId, 116 String fallbackClassName, 117 ClassLoader classLoader) { 118 Path path = null; 119 try { 120 String JAVA_HOME = System.getProperty("java.home"); 121 path = Paths.get(JAVA_HOME, "conf", "jaxws.properties"); 122 123 // to ensure backwards compatibility 124 if (!Files.exists(path)) { 125 path = Paths.get(JAVA_HOME, "lib", "jaxws.properties"); 126 } 127 128 if (!Files.exists(path)) { 129 Properties props = new Properties(); 130 try (InputStream inStream = Files.newInputStream(path)) { 131 props.load(inStream); 132 } 133 String factoryClassName = props.getProperty(factoryId); 134 return ServiceLoaderUtil.newInstance(factoryClassName, 135 fallbackClassName, classLoader, EXCEPTION_HANDLER); 136 } 137 } catch (Exception ignored) { 138 logger.log(Level.SEVERE, "Error reading JAX-WS configuration from [" + path + 139 "] file. Check it is accessible and has correct format.", ignored); 140 } 141 return null; 142 } 143 144 private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader"; 145 146 private static boolean isOsgi() { 147 try { 148 Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); 149 return true; 150 } catch (ClassNotFoundException ignored) { 151 } 152 return false; 153 } 154 155 private static Object lookupUsingOSGiServiceLoader(String factoryId) { 156 try { 157 // Use reflection to avoid having any dependendcy on ServiceLoader class 158 Class serviceClass = Class.forName(factoryId); | 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.ws.spi; 27 28 import java.io.*; 29 30 import java.nio.file.Paths; 31 import java.util.Properties; 32 import java.util.logging.Level; 33 import java.util.logging.Logger; 34 import javax.xml.ws.WebServiceException; 35 36 class FactoryFinder { 37 38 private static final Logger logger = Logger.getLogger("javax.xml.ws"); 39 40 private static final ServiceLoaderUtil.ExceptionHandler<WebServiceException> EXCEPTION_HANDLER = 41 new ServiceLoaderUtil.ExceptionHandler<WebServiceException>() { 42 @Override 43 public WebServiceException createException(Throwable throwable, String message) { 44 return new WebServiceException(message, throwable); 45 } 46 }; 47 48 /** 49 * Finds the implementation {@code Class} object for the given 96 fallbackClassName, classLoader, EXCEPTION_HANDLER); 97 } 98 99 private static Object fromSystemProperty(String factoryId, 100 String fallbackClassName, 101 ClassLoader classLoader) { 102 try { 103 String systemProp = System.getProperty(factoryId); 104 if (systemProp != null) { 105 return ServiceLoaderUtil.newInstance(systemProp, 106 fallbackClassName, classLoader, EXCEPTION_HANDLER); 107 } 108 } catch (SecurityException ignored) { 109 } 110 return null; 111 } 112 113 private static Object fromJDKProperties(String factoryId, 114 String fallbackClassName, 115 ClassLoader classLoader) { 116 File f = null; 117 try { 118 String JAVA_HOME = System.getProperty("java.home"); 119 f = Paths.get(JAVA_HOME, "conf", "jaxws.properties").toFile(); 120 121 // to ensure backwards compatibility 122 if (!f.exists()) { 123 f = Paths.get(JAVA_HOME, "lib", "jaxws.properties").toFile(); 124 } 125 126 if (f.exists()) { 127 Properties props = new Properties(); 128 try (InputStream inStream = new FileInputStream(f)) { 129 props.load(inStream); 130 } 131 String factoryClassName = props.getProperty(factoryId); 132 return ServiceLoaderUtil.newInstance(factoryClassName, 133 fallbackClassName, classLoader, EXCEPTION_HANDLER); 134 } 135 } catch (Exception ignored) { 136 String absolutePath = f != null ? f.getAbsolutePath() : "null"; 137 logger.log(Level.SEVERE, "Error reading JAX-WS configuration from [" + 138 absolutePath + 139 "] file. Check it is accessible and has correct format.", ignored); 140 } 141 return null; 142 } 143 144 private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "com.sun.org.glassfish.hk2.osgiresourcelocator.ServiceLoader"; 145 146 private static boolean isOsgi() { 147 try { 148 Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME); 149 return true; 150 } catch (ClassNotFoundException ignored) { 151 } 152 return false; 153 } 154 155 private static Object lookupUsingOSGiServiceLoader(String factoryId) { 156 try { 157 // Use reflection to avoid having any dependendcy on ServiceLoader class 158 Class serviceClass = Class.forName(factoryId); |