Skip to content

Commit

Permalink
apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Stenzel <[email protected]>
  • Loading branch information
infeo and overheadhunter committed Feb 22, 2024
1 parent 9718afc commit 0e61467
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,8 +48,7 @@ public static <T> Stream<T> loadAll(Class<T> 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()));
}
Expand All @@ -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> T instantiateServiceProvider(ServiceLoader.Provider<T> provider) {
private static <T> Stream<T> instantiateServiceProvider(ServiceLoader.Provider<T> 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;
}
}
}

Expand Down

0 comments on commit 0e61467

Please sign in to comment.