Skip to content

Package: ServiceLoaderUtil

ServiceLoaderUtil

nameinstructionbranchcomplexitylinemethod
ServiceLoaderUtil()
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%
checkPackageAccess(String)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
contextClassLoader(ServiceLoaderUtil.ExceptionHandler)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
firstByServiceLoader(Class, Logger, ServiceLoaderUtil.ExceptionHandler)
M: 53 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
newInstance(String, String, ClassLoader, ServiceLoaderUtil.ExceptionHandler)
M: 38 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
nullSafeLoadClass(String, ClassLoader)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
safeLoadClass(String, String, ClassLoader)
M: 19 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2015, 2020 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.util.ServiceLoader;
14: import java.util.logging.Level;
15: import java.util.logging.Logger;
16:
17: /**
18: * Shared ServiceLoader/FactoryFinder Utils shared among Jakarta SOAP with Attachments, Jakarta XML Binding
19: * and Jakarta XML Web Services.
20: * Class duplicated to all those projects.
21: *
22: * @author Miroslav.Kos@oracle.com
23: */
24: class ServiceLoaderUtil {
25:
26: static <P, T extends Exception> P firstByServiceLoader(Class<P> spiClass,
27: Logger logger,
28: ExceptionHandler<T> handler) throws T {
29: logger.log(Level.FINE, "Using java.util.ServiceLoader to find {0}", spiClass.getName());
30: // service discovery
31: try {
32: ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
33:
34:• for (P impl : serviceLoader) {
35: logger.fine("ServiceProvider loading Facility used; returning object [" +
36: impl.getClass().getName() + "]");
37:
38: return impl;
39: }
40: } catch (Throwable t) {
41: throw handler.createException(t, "Error while searching for service [" + spiClass.getName() + "]");
42: }
43: return null;
44: }
45:
46: static void checkPackageAccess(String className) {
47: // make sure that the current thread has an access to the package of the given name.
48: SecurityManager s = System.getSecurityManager();
49:• if (s != null) {
50: int i = className.lastIndexOf('.');
51:• if (i != -1) {
52: s.checkPackageAccess(className.substring(0, i));
53: }
54: }
55: }
56:
57: static Class nullSafeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
58:• if (classLoader == null) {
59: return Class.forName(className);
60: } else {
61: return classLoader.loadClass(className);
62: }
63: }
64:
65: // Returns instance of required class. It checks package access (security)
66: // unless it is defaultClassname. It means if you are trying to instantiate
67: // default implementation (fallback), pass the class name to both first and second parameter.
68: static <T extends Exception> Object newInstance(String className,
69: String defaultImplClassName, ClassLoader classLoader,
70: final ExceptionHandler<T> handler) throws T {
71: try {
72: return safeLoadClass(className, defaultImplClassName, classLoader).newInstance();
73: } catch (ClassNotFoundException x) {
74: throw handler.createException(x, "Provider " + className + " not found");
75: } catch (Exception x) {
76: throw handler.createException(x, "Provider " + className + " could not be instantiated: " + x);
77: }
78: }
79:
80: static Class safeLoadClass(String className,
81: String defaultImplClassName,
82: ClassLoader classLoader) throws ClassNotFoundException {
83:
84: try {
85: checkPackageAccess(className);
86: } catch (SecurityException se) {
87: // anyone can access the platform default factory class without permission
88:• if (defaultImplClassName != null && defaultImplClassName.equals(className)) {
89: return Class.forName(className);
90: }
91: // not platform default implementation ...
92: throw se;
93: }
94: return nullSafeLoadClass(className, classLoader);
95: }
96:
97: static <T extends Exception> ClassLoader contextClassLoader(ExceptionHandler<T> exceptionHandler) throws T {
98: try {
99: return Thread.currentThread().getContextClassLoader();
100: } catch (Exception x) {
101: throw exceptionHandler.createException(x, x.toString());
102: }
103: }
104:
105: static abstract class ExceptionHandler<T extends Exception> {
106:
107: public abstract T createException(Throwable throwable, String message);
108:
109: }
110:
111: }