allow service loader to expose providers

This commit is contained in:
Jonathan Shook 2023-05-18 14:57:52 -05:00
parent 6883aed6c1
commit ba78d80fd6

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022 nosqlbench * Copyright (c) 2022-2023 nosqlbench
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,21 +59,21 @@ public class ServiceSelector<T> implements Predicate<ServiceLoader.Provider<? ex
return false; return false;
} }
public T getOne() { public ServiceLoader.Provider<? extends T> getOneProvider() {
List<? extends T> services = getAll(); List<? extends ServiceLoader.Provider<? extends T>> providers = getAllProviders();
if (services.size() == 0) { if (providers.size()==0 || providers.size()>1) {
throw new RuntimeException("You requested exactly one instance of a service by name '" + name + "', but got " + throw new RuntimeException("You requested exactly one instance of a service by name '" + name + "', but got " +
(services.stream().map(s -> s.getClass().getSimpleName())).collect(Collectors.joining(",")) + " (" + services.stream().count() + ")"); (providers.stream().map(s -> s.getClass().getSimpleName())).collect(Collectors.joining(",")) + " (" + providers.stream().count() + ")");
} else if (services.size()==1) {
return services.get(0);
} }
throw new RuntimeException("You requested exactly one instance of a service by name '" + name + "', but got " + return providers.get(0);
(services.stream().map(s -> s.getClass().getSimpleName())).collect(Collectors.joining(",")) + " (" + services.stream().count() + ")");
} }
public List<? extends T> getAll() { public T getOne() {
List<? extends T> services = loader return getOneProvider().get();
}
public List<? extends ServiceLoader.Provider<? extends T>> getAllProviders() {
List<? extends ServiceLoader.Provider<? extends T>> providers = loader
.stream() .stream()
.peek(l -> { .peek(l -> {
if (l.type().getAnnotation(Service.class) == null) { if (l.type().getAnnotation(Service.class) == null) {
@ -86,9 +86,14 @@ public class ServiceSelector<T> implements Predicate<ServiceLoader.Provider<? ex
) )
.filter(l -> l.type().getAnnotation(Service.class) != null) .filter(l -> l.type().getAnnotation(Service.class) != null)
.filter(l -> l.type().getAnnotation(Service.class).selector().equals(name)) .filter(l -> l.type().getAnnotation(Service.class).selector().equals(name))
.toList();
return providers;
}
public List<? extends T> getAll() {
List<? extends ServiceLoader.Provider<? extends T>> providers = getAllProviders();
return providers.stream()
.map(ServiceLoader.Provider::get) .map(ServiceLoader.Provider::get)
.toList(); .toList();
return services;
} }
public Optional<? extends T> get() { public Optional<? extends T> get() {