Skip to content

Package: FactoryFinder$1

FactoryFinder$1

nameinstructionbranchcomplexitylinemethod
createException(Throwable, String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2005, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: package jakarta.xml.ws.spi;
12:
13: import java.io.*;
14:
15: import java.nio.file.Files;
16: import java.nio.file.Path;
17: import java.nio.file.Paths;
18: import java.util.Properties;
19: import java.util.logging.Level;
20: import java.util.logging.Logger;
21: import jakarta.xml.ws.WebServiceException;
22:
23: class FactoryFinder {
24:
25: private static final Logger logger = Logger.getLogger("jakarta.xml.ws");
26:
27: private static final ServiceLoaderUtil.ExceptionHandler<WebServiceException> EXCEPTION_HANDLER =
28: new ServiceLoaderUtil.ExceptionHandler<WebServiceException>() {
29: @Override
30: public WebServiceException createException(Throwable throwable, String message) {
31: return new WebServiceException(message, throwable);
32: }
33: };
34:
35: /**
36: * Finds the implementation {@code Class} object for the given
37: * factory name, or if that fails, finds the {@code Class} object
38: * for the given fallback class name. The arguments supplied MUST be
39: * used in order. If using the first argument is successful, the second
40: * one will not be used.
41: * <P>
42: * This method is package private so that this code can be shared.
43: *
44: * @return the {@code Class} object of the specified message factory;
45: * may not be {@code null}
46: *
47: * @param factoryClass the name of the factory to find, which is
48: * a system property
49: * @param fallbackClassName the implementation class name, which is
50: * to be used only if nothing else
51: * is found; {@code null} to indicate that
52: * there is no fallback class name
53: * @exception WebServiceException if there is an error
54: */
55: @SuppressWarnings("unchecked")
56: static <T> T find(Class<T> factoryClass, String fallbackClassName) {
57: ClassLoader classLoader = ServiceLoaderUtil.contextClassLoader(EXCEPTION_HANDLER);
58:
59: T provider = ServiceLoaderUtil.firstByServiceLoader(factoryClass, logger, EXCEPTION_HANDLER);
60: if (provider != null) return provider;
61:
62: String factoryId = factoryClass.getName();
63:
64: // try to read from $java.home/lib/jaxws.properties
65: provider = (T) fromJDKProperties(factoryId, fallbackClassName, classLoader);
66: if (provider != null) return provider;
67:
68: // Use the system property
69: provider = (T) fromSystemProperty(factoryId, fallbackClassName, classLoader);
70: if (provider != null) return provider;
71:
72: // handling Glassfish (platform specific default)
73: if (isOsgi()) {
74: provider = (T) lookupUsingOSGiServiceLoader(factoryId);
75: if (provider != null) {
76: return provider;
77: }
78: }
79:
80: if (fallbackClassName == null) {
81: throw new WebServiceException(
82: "Provider for " + factoryId + " cannot be found", null);
83: }
84:
85: return (T) ServiceLoaderUtil.newInstance(fallbackClassName,
86: fallbackClassName, classLoader, EXCEPTION_HANDLER);
87: }
88:
89: private static Object fromSystemProperty(String factoryId,
90: String fallbackClassName,
91: ClassLoader classLoader) {
92: try {
93: String systemProp = System.getProperty(factoryId);
94: if (systemProp != null) {
95: return ServiceLoaderUtil.newInstance(systemProp,
96: fallbackClassName, classLoader, EXCEPTION_HANDLER);
97: }
98: } catch (SecurityException ignored) {
99: }
100: return null;
101: }
102:
103: private static Object fromJDKProperties(String factoryId,
104: String fallbackClassName,
105: ClassLoader classLoader) {
106: Path path = null;
107: try {
108: String JAVA_HOME = System.getProperty("java.home");
109: path = Paths.get(JAVA_HOME, "conf", "jaxws.properties");
110:
111: // to ensure backwards compatibility
112: if (!Files.exists(path)) {
113: path = Paths.get(JAVA_HOME, "lib", "jaxws.properties");
114: }
115:
116: if (Files.exists(path)) {
117: Properties props = new Properties();
118: try (InputStream inStream = Files.newInputStream(path)) {
119: props.load(inStream);
120: }
121: String factoryClassName = props.getProperty(factoryId);
122: return ServiceLoaderUtil.newInstance(factoryClassName,
123: fallbackClassName, classLoader, EXCEPTION_HANDLER);
124: }
125: } catch (Exception ignored) {
126: logger.log(Level.SEVERE, "Error reading Jakarta XML Web Services configuration from [" + path +
127: "] file. Check it is accessible and has correct format.", ignored);
128: }
129: return null;
130: }
131:
132: private static final String OSGI_SERVICE_LOADER_CLASS_NAME = "org.glassfish.hk2.osgiresourcelocator.ServiceLoader";
133:
134: private static boolean isOsgi() {
135: try {
136: Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
137: return true;
138: } catch (ClassNotFoundException ignored) {
139: }
140: return false;
141: }
142:
143: private static Object lookupUsingOSGiServiceLoader(String factoryId) {
144: try {
145: // Use reflection to avoid having any dependendcy on ServiceLoader class
146: Class serviceClass = Class.forName(factoryId);
147: Class[] args = new Class[]{serviceClass};
148: Class target = Class.forName(OSGI_SERVICE_LOADER_CLASS_NAME);
149: java.lang.reflect.Method m = target.getMethod("lookupProviderInstances", Class.class);
150: java.util.Iterator iter = ((Iterable) m.invoke(null, (Object[]) args)).iterator();
151: return iter.hasNext() ? iter.next() : null;
152: } catch (Exception ignored) {
153: // log and continue
154: return null;
155: }
156: }
157:
158: }