-
Notifications
You must be signed in to change notification settings - Fork 33
/
ServiceLoaderUtil.java
112 lines (96 loc) · 4.37 KB
/
ServiceLoaderUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package jakarta.activation;
import java.util.ServiceLoader;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Shared ServiceLoader/FactoryFinder Utils shared among JAF, MAIL, SAAJ, JAXB and JAXWS
* Class duplicated to all those projects.
*
* @author Miroslav.Kos@oracle.com
*/
class ServiceLoaderUtil {
static <P, T extends Exception> P firstByServiceLoader(Class<P> spiClass,
Logger logger,
ExceptionHandler<T> handler) throws T {
logger.log(Level.FINE, "Using java.util.ServiceLoader to find {0}", spiClass.getName());
// service discovery
try {
ServiceLoader<P> serviceLoader = ServiceLoader.load(spiClass);
for (P impl : serviceLoader) {
logger.log(Level.FINE, "ServiceProvider loading Facility used; returning object [{0}]", impl.getClass().getName());
return impl;
}
} catch (Throwable t) {
throw handler.createException(t, "Error while searching for service [" + spiClass.getName() + "]");
}
return null;
}
static void checkPackageAccess(String className) {
// make sure that the current thread has an access to the package of the given name.
SecurityManager s = System.getSecurityManager();
if (s != null) {
int i = className.lastIndexOf('.');
if (i != -1) {
s.checkPackageAccess(className.substring(0, i));
}
}
}
@SuppressWarnings({"unchecked"})
static <P> Class<P> nullSafeLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException {
if (classLoader == null) {
return (Class<P>) Class.forName(className);
} else {
return (Class<P>) classLoader.loadClass(className);
}
}
// Returns instance of required class. It checks package access (security)
// unless it is defaultClassname. It means if you are trying to instantiate
// default implementation (fallback), pass the class name to both first and second parameter.
static <P, T extends Exception> P newInstance(String className,
String defaultImplClassName, ClassLoader classLoader,
final ExceptionHandler<T> handler) throws T {
try {
Class<P> cls = safeLoadClass(className, defaultImplClassName, classLoader);
return cls.getConstructor().newInstance();
} catch (ClassNotFoundException x) {
throw handler.createException(x, "Provider " + className + " not found");
} catch (Exception x) {
throw handler.createException(x, "Provider " + className + " could not be instantiated: " + x);
}
}
@SuppressWarnings({"unchecked"})
static <P> Class<P> safeLoadClass(String className,
String defaultImplClassName,
ClassLoader classLoader) throws ClassNotFoundException {
try {
checkPackageAccess(className);
} catch (SecurityException se) {
// anyone can access the platform default factory class without permission
if (defaultImplClassName != null && defaultImplClassName.equals(className)) {
return (Class<P>) Class.forName(className);
}
// not platform default implementation ...
throw se;
}
return nullSafeLoadClass(className, classLoader);
}
static <T extends Exception> ClassLoader contextClassLoader(ExceptionHandler<T> exceptionHandler) throws T {
try {
return Thread.currentThread().getContextClassLoader();
} catch (Exception x) {
throw exceptionHandler.createException(x, x.toString());
}
}
static abstract class ExceptionHandler<T extends Exception> {
public abstract T createException(Throwable throwable, String message);
}
}