Skip to content

Commit 0e61467

Browse files
infeooverheadhunter
andcommitted
apply suggestions from code review
Co-authored-by: Sebastian Stenzel <overheadhunter@users.noreply.github.com>
1 parent 9718afc commit 0e61467

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

src/main/java/org/cryptomator/integrations/common/IntegrationsLoader.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.lang.reflect.Modifier;
1010
import java.util.Arrays;
1111
import java.util.Comparator;
12-
import java.util.Objects;
1312
import java.util.Optional;
1413
import java.util.ServiceConfigurationError;
1514
import java.util.ServiceLoader;
@@ -49,8 +48,7 @@ public static <T> Stream<T> loadAll(Class<T> clazz) {
4948
.filter(IntegrationsLoader::isSupportedOperatingSystem)
5049
.filter(IntegrationsLoader::passesStaticAvailabilityCheck)
5150
.sorted(Comparator.comparingInt(IntegrationsLoader::getPriority).reversed())
52-
.map(IntegrationsLoader::instantiateServiceProvider)
53-
.filter(Objects::nonNull)
51+
.flatMap(IntegrationsLoader::instantiateServiceProvider)
5452
.filter(IntegrationsLoader::passesInstanceAvailabilityCheck)
5553
.peek(impl -> logServiceIsAvailable(clazz, impl.getClass()));
5654
}
@@ -71,12 +69,20 @@ private static boolean isSupportedOperatingSystem(ServiceLoader.Provider<?> prov
7169
return annotations.length == 0 || Arrays.stream(annotations).anyMatch(OperatingSystem.Value::isCurrent);
7270
}
7371

74-
private static <T> T instantiateServiceProvider(ServiceLoader.Provider<T> provider) {
72+
private static <T> Stream<T> instantiateServiceProvider(ServiceLoader.Provider<T> provider) {
7573
try {
76-
return provider.get();
74+
return Stream.of(provider.get());
7775
} catch (ServiceConfigurationError err) {
78-
LOG.warn("Unable to load service provider {}.", provider.type().getName(), err);
79-
return null;
76+
//ServiceLoader.Provider::get throws this error if (from javadoc)
77+
if (err.getCause() == null || //the public static "provider()" method of a provider factory returns null
78+
err.getCause() instanceof ExceptionInInitializerError || // * the service provider cannot be instantiated,
79+
err.getCause() instanceof NoClassDefFoundError ||
80+
err.getCause() instanceof RuntimeException) {
81+
LOG.warn("Unable to load service provider {}.", provider.type().getName(), err);
82+
return Stream.empty();
83+
} else {
84+
throw err;
85+
}
8086
}
8187
}
8288

0 commit comments

Comments
 (0)