rgoers commented on a change in pull request #804:
URL: https://github.com/apache/logging-log4j2/pull/804#discussion_r830502914
##########
File path:
log4j-api/src/main/java/org/apache/logging/log4j/util/ServiceLoaderUtil.java
##########
@@ -110,28 +139,64 @@ private ServiceLoaderUtil() {
* @param logger An action to perform for each broken service.
* @param services A list to add the services.
*/
- private static <T> void addServices(final Class<T> serviceType, final
ClassLoader classLoader,
- final Consumer<Throwable> logger, final Collection<T> services) {
- final Iterator<T> iterator = ServiceLoader.load(serviceType,
classLoader).iterator();
- while (iterator.hasNext()) {
- try {
- final T service = iterator.next();
- if (classLoader.equals(service.getClass().getClassLoader())) {
- services.add(service);
- }
- } catch (ServiceConfigurationError e) {
- if (logger != null) {
- logger.accept(e);
- }
- }
- }
+ private static <T> Stream<T> loadClassloaderServices(final Class<T>
serviceType, final ClassLoader classLoader,
+ final boolean verbose) {
+ return StreamSupport.stream(new
ServiceLoaderSpliterator<T>(serviceType, classLoader, verbose), false);
}
- private static <T> void addOsgiServices(final Class<T> serviceType, final
Collection<T> services) {
+ private static <T> Stream<? extends T> loadOsgiServices(final Class<T>
serviceType) {
final Iterable<? extends T> iterable =
org.glassfish.hk2.osgiresourcelocator.ServiceLoader
.lookupProviderInstances(serviceType);
- if (iterable != null) {
- iterable.forEach(services::add);
+ return iterable != null ? StreamSupport.stream(iterable.spliterator(),
false) : Stream.empty();
+ }
+
+ private static class ServiceLoaderSpliterator<S> implements Spliterator<S>
{
+
+ private final Iterator<S> serviceIterator;
+ private final ClassLoader classLoader;
+ private final Logger logger;
+ private final String serviceName;
+
+ public ServiceLoaderSpliterator(final Class<S> serviceType, final
ClassLoader classLoader,
+ final boolean verbose) {
+ this.serviceIterator = ServiceLoader.load(serviceType,
classLoader).iterator();
Review comment:
@ppkarwasz No matter how you slice it, so long as the call originates
from the log4j-api module it won't work in JPMS. I would suggest that you use
`ServiceLoader.load(Class<S> service, ClassLoader classLoader)` from the
lambda. You will end up calling it once for each ClassLoader you have. In
master I don't have to do this since anything running on the module path is
going to have a ModuleLayer.
Another option, of course, is to create a Java 9 specific version of this
that supports using ModuleLayer. Then the code you currently have would work.
That is probably the route I would go.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]