diff --git a/nb-annotations/src/main/java/io/nosqlbench/nb/annotations/ServiceSelector.java b/nb-annotations/src/main/java/io/nosqlbench/nb/annotations/ServiceSelector.java new file mode 100644 index 000000000..356f2fb42 --- /dev/null +++ b/nb-annotations/src/main/java/io/nosqlbench/nb/annotations/ServiceSelector.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2022 nosqlbench + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.nosqlbench.nb.annotations; + +import java.util.List; +import java.util.Optional; +import java.util.ServiceLoader; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * A service loader filter which works with {@link io.nosqlbench.nb.annotations.Service} to load a named service. + * This version requires the caller to provide the service loader instance, since it is now caller sensitive. + * + * Use it like this:
{@code
+ *       ResultValueFilterType filterType =
+ *           SelectorFilter.of("core", ServiceLoader.load(ResultValueFilterType.class)).get();
+ * }
+ * + * @param The service type + */ +public class ServiceSelector implements Predicate> { + private final String name; + private final ServiceLoader loader; + + public ServiceSelector(String name, ServiceLoader loader) { + this.name = name; + this.loader = loader; + } + + public static ServiceSelector of(String name, ServiceLoader loader) { + return new ServiceSelector<>(name, loader); + } + + public static boolean matches(String name, ServiceLoader.Provider provider) { + Service annotation = provider.type().getAnnotation(Service.class); + if (annotation == null) { + return false; + } + return (annotation.selector().equals(name)); + } + + @Override + public boolean test(ServiceLoader.Provider provider) { + return false; + } + + public T getOne() { + List services = getAll(); + if (services.size() == 0) { + 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() + ")"); + } + return services.get(0); + } + + public List getAll() { + List services = loader + .stream() + .peek(l -> { + if (l.type().getAnnotation(Service.class) == null) { + throw new RuntimeException( + "Annotator services must be annotated with distinct selectors\n" + + "such as @Selector(\"myimpl42\")" + ); + } + } + ) + .filter(l -> l.type().getAnnotation(Service.class) != null) + .filter(l -> l.type().getAnnotation(Service.class).selector().equals(name)) + .map(ServiceLoader.Provider::get) + .toList(); + return services; + } + + public Optional get() { + List services = getAll(); + if (services.size() == 1) { + return Optional.of(services.get(0)); + } + if (services.size()==0) { + throw new RuntimeException("No services were found for '" + name + "'."); + } + 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() + ")"); + + + } +}