From 0e614673fba539120a57cd246c2e88c5226ebe36 Mon Sep 17 00:00:00 2001 From: Armin Schrenk Date: Thu, 22 Feb 2024 17:50:51 +0100 Subject: [PATCH] apply suggestions from code review Co-authored-by: Sebastian Stenzel --- .../common/IntegrationsLoader.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java b/src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java index 3854ba7..d1628b1 100644 --- a/src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java +++ b/src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java @@ -9,7 +9,6 @@ import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.Comparator; -import java.util.Objects; import java.util.Optional; import java.util.ServiceConfigurationError; import java.util.ServiceLoader; @@ -49,8 +48,7 @@ public static Stream loadAll(Class clazz) { .filter(IntegrationsLoader::isSupportedOperatingSystem) .filter(IntegrationsLoader::passesStaticAvailabilityCheck) .sorted(Comparator.comparingInt(IntegrationsLoader::getPriority).reversed()) - .map(IntegrationsLoader::instantiateServiceProvider) - .filter(Objects::nonNull) + .flatMap(IntegrationsLoader::instantiateServiceProvider) .filter(IntegrationsLoader::passesInstanceAvailabilityCheck) .peek(impl -> logServiceIsAvailable(clazz, impl.getClass())); } @@ -71,12 +69,20 @@ private static boolean isSupportedOperatingSystem(ServiceLoader.Provider prov return annotations.length == 0 || Arrays.stream(annotations).anyMatch(OperatingSystem.Value::isCurrent); } - private static T instantiateServiceProvider(ServiceLoader.Provider provider) { + private static Stream instantiateServiceProvider(ServiceLoader.Provider provider) { try { - return provider.get(); + return Stream.of(provider.get()); } catch (ServiceConfigurationError err) { - LOG.warn("Unable to load service provider {}.", provider.type().getName(), err); - return null; + //ServiceLoader.Provider::get throws this error if (from javadoc) + if (err.getCause() == null || //the public static "provider()" method of a provider factory returns null + err.getCause() instanceof ExceptionInInitializerError || // * the service provider cannot be instantiated, + err.getCause() instanceof NoClassDefFoundError || + err.getCause() instanceof RuntimeException) { + LOG.warn("Unable to load service provider {}.", provider.type().getName(), err); + return Stream.empty(); + } else { + throw err; + } } }