Use Helidon service loader.

Signed-off-by: Tomas Langer <tomas.langer@oracle.com>
This commit is contained in:
Tomas Langer
2020-01-31 09:15:41 +01:00
committed by Tomas Langer
parent f635adfae5
commit 5752712514
11 changed files with 67 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -145,6 +145,7 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
private boolean useSysPropExclude = true;
private boolean useSystemServiceLoader = true;
private boolean replaceImplementations = true;
private int defaultPriority = Prioritized.DEFAULT_PRIORITY;
private Builder(ServiceLoader<T> serviceLoader) {
this.serviceLoader = serviceLoader;
@@ -166,10 +167,10 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
serviceLoader.forEach(service -> {
if (replaceImplementations) {
if (!uniqueImplementations.contains(service.getClass().getName())) {
services.add(new ServiceWithPriority<>(service));
services.add(ServiceWithPriority.createFindPriority(service, defaultPriority));
}
} else {
services.add(new ServiceWithPriority<>(service));
services.add(ServiceWithPriority.createFindPriority(service, defaultPriority));
}
});
}
@@ -237,7 +238,7 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
* @return updated builder instance
*/
public Builder<T> addService(T service) {
this.customServices.add(new ServiceWithPriority<>(service));
this.customServices.add(ServiceWithPriority.createFindPriority(service, defaultPriority));
return this;
}
@@ -249,7 +250,7 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
* @return updated builder instance
*/
public Builder<T> addService(T service, int priority) {
this.customServices.add(new ServiceWithPriority<>(service, priority));
this.customServices.add(ServiceWithPriority.create(service, priority));
return this;
}
@@ -277,6 +278,17 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
return this;
}
/**
* Configure default priority for services that do not have any.
*
* @param defaultPriority default priority to use, defaults to {@link io.helidon.common.Prioritized#DEFAULT_PRIORITY}
* @return updated builder instance
*/
public Builder<T> defaultPriority(int defaultPriority) {
this.defaultPriority = defaultPriority;
return this;
}
private boolean notExcluded(ServiceWithPriority<T> service) {
String className = service.instance.getClass().getName();
if (excludedServiceClasses.contains(className)) {
@@ -335,8 +347,12 @@ public final class HelidonServiceLoader<T> implements Iterable<T> {
}
}
private ServiceWithPriority(T service) {
this(service, Priorities.find(service, Prioritized.DEFAULT_PRIORITY));
private static <T> ServiceWithPriority<T> create(T instance, int priority) {
return new ServiceWithPriority<>(instance, priority);
}
private static <T> ServiceWithPriority<T> createFindPriority(T instance, int defaultPriority) {
return new ServiceWithPriority<>(instance, Priorities.find(instance, defaultPriority));
}
private int priority() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -654,12 +654,17 @@ class BuilderImpl implements Config.Builder {
}
private static void loadMapperServices(List<ConfigMapperProvider> providers) {
ServiceLoader.load(ConfigMapperProvider.class)
HelidonServiceLoader.builder(ServiceLoader.load(ConfigMapperProvider.class))
.defaultPriority(ConfigMapperProvider.PRIORITY)
.build()
.forEach(providers::add);
}
private static List<ConfigParser> loadParserServices() {
return loadPrioritizedServices(ConfigParser.class, ConfigParser.PRIORITY);
return HelidonServiceLoader.builder(ServiceLoader.load(ConfigParser.class))
.defaultPriority(ConfigParser.PRIORITY)
.build()
.asList();
}
private void addAutoLoadedFilters() {
@@ -680,17 +685,15 @@ class BuilderImpl implements Config.Builder {
/*
* Map each autoloaded ConfigFilter to a filter-providing function.
*/
ConfigUtils.asStream(ServiceLoader.load(ConfigFilter.class).iterator())
HelidonServiceLoader.builder(ServiceLoader.load(ConfigFilter.class))
.defaultPriority(ConfigFilter.PRIORITY)
.build()
.asList()
.stream()
.map(filter -> (Function<Config, ConfigFilter>) (Config t) -> filter)
.forEach(this::addFilter);
}
private static <T> List<T> loadPrioritizedServices(Class<T> serviceClass, int priority) {
return ConfigUtils
.asPrioritizedStream(ServiceLoader.load(serviceClass), priority)
.collect(Collectors.toList());
}
/**
* {@link ConfigContext} implementation.
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -60,7 +60,7 @@ public final class ConfigUtils {
* @return stream of items.
*/
public static <S> Stream<S> asStream(Iterable<? extends S> items) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(items.iterator(), Spliterator.ORDERED), false);
return asStream(items.iterator());
}
/**
@@ -70,8 +70,8 @@ public final class ConfigUtils {
* @param iterator iterator over the items
* @return stream of the items
*/
public static <S> Stream<S> asStream(Iterator<S> iterator) {
return asStream(() -> iterator);
public static <S> Stream<S> asStream(Iterator<? extends S> iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import java.util.logging.Logger;
import io.helidon.common.Errors;
import io.helidon.common.HelidonFeatures;
import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.config.Config;
import io.helidon.security.EndpointConfig;
import io.helidon.security.ProviderRequest;
@@ -69,7 +70,8 @@ public final class PolicyValidator implements AbacValidator<PolicyValidator.Poli
private PolicyValidator(Builder builder) {
//first find all services
ServiceLoader<PolicyExecutorService> services = ServiceLoader.load(PolicyExecutorService.class);
HelidonServiceLoader<PolicyExecutorService> services =
HelidonServiceLoader.create(ServiceLoader.load(PolicyExecutorService.class));
for (PolicyExecutorService service : services) {
executors.add(service.instantiate(builder.config.get(service.configKey())));

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
module io.helidon.security.abac.policy {
requires io.helidon.security.providers.abac;
requires java.logging;
requires io.helidon.common.serviceloader;
exports io.helidon.security.abac.policy;
exports io.helidon.security.abac.policy.spi;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import javax.annotation.security.RolesAllowed;
import io.helidon.common.Errors;
import io.helidon.common.HelidonFeatures;
import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.config.Config;
import io.helidon.security.AuthorizationResponse;
import io.helidon.security.EndpointConfig;
@@ -70,7 +71,8 @@ public final class AbacProvider extends SynchronousProvider implements Authoriza
private final boolean failIfNoneValidated;
private AbacProvider(Builder builder) {
ServiceLoader<AbacValidatorService> services = ServiceLoader.load(AbacValidatorService.class);
HelidonServiceLoader<AbacValidatorService> services =
HelidonServiceLoader.create(ServiceLoader.load(AbacValidatorService.class));
for (AbacValidatorService service : services) {
validators.add(service.instantiate(builder.config.get(service.configKey())));

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ module io.helidon.security.providers.abac {
requires transitive io.helidon.config;
requires transitive io.helidon.common;
requires transitive io.helidon.security;
requires io.helidon.common.serviceloader;
exports io.helidon.security.providers.abac;
exports io.helidon.security.providers.abac.spi;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ import java.util.stream.Collectors;
import io.helidon.common.HelidonFeatures;
import io.helidon.common.HelidonFlavor;
import io.helidon.common.configurable.ThreadPoolSupplier;
import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.config.Config;
import io.helidon.security.internal.SecurityAuditEvent;
import io.helidon.security.spi.AuditProvider;
@@ -1036,7 +1037,9 @@ public class Security {
Map<String, SecurityProviderService> classNameToService) {
Set<String> configKeys = new HashSet<>();
ServiceLoader<SecurityProviderService> loader = ServiceLoader.load(SecurityProviderService.class);
HelidonServiceLoader<SecurityProviderService> loader =
HelidonServiceLoader.create(ServiceLoader.load(SecurityProviderService.class));
loader.forEach(service -> {
String configKey = service.providerConfigKey();
if (null != configKey) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ module io.helidon.security {
requires io.helidon.security.util;
requires io.helidon.common.context;
requires io.opentracing.noop;
requires io.helidon.common.serviceloader;
exports io.helidon.security;
exports io.helidon.security.spi;

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import javax.ws.rs.core.MultivaluedMap;
import io.helidon.common.HelidonFeatures;
import io.helidon.common.context.Contexts;
import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.tracing.config.SpanTracingConfig;
import io.helidon.tracing.config.TracingConfigUtil;
import io.helidon.tracing.jersey.client.internal.TracingContext;
@@ -153,7 +154,7 @@ public class ClientTracingFilter implements ClientRequestFilter, ClientResponseF
* Required by integrated platform.
*/
public ClientTracingFilter() {
Iterator<TracerProvider> iterator = ServiceLoader.load(TracerProvider.class)
Iterator<TracerProvider> iterator = HelidonServiceLoader.create(ServiceLoader.load(TracerProvider.class))
.iterator();
if (iterator.hasNext()) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -36,12 +36,14 @@ module io.helidon.tracing.jersey.client {
requires io.helidon.common;
requires io.helidon.common.context;
requires io.helidon.webclient.jaxrs;
requires io.helidon.common.serviceloader;
exports io.helidon.tracing.jersey.client;
// needed to propagate tracing context from server to client
opens io.helidon.tracing.jersey.client.internal to io.helidon.tracing.jersey,io.helidon.microprofile.tracing;
exports io.helidon.tracing.jersey.client.internal;
exports io.helidon.tracing.jersey.client.internal to io.helidon.tracing.jersey,io.helidon.microprofile.tracing;
uses io.helidon.tracing.spi.TracerProvider;
provides AutoDiscoverable with io.helidon.tracing.jersey.client.ClientTracingAutoDiscoverable;
}