All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.atleon.core.ConfigSource Maven / Gradle / Ivy

package io.atleon.core;

import io.atleon.util.ConfigLoading;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * A reactive Provider that creates configurations for reactive resources. In addition to
 * supporting {@link ConfigInterceptor}s, extensions of this class support {@link ConfigProcessor}s,
 * which allow reactive (non-blocking) enrichment of properties before generating the resulting
 * Config objects.
 *
 * @param  The type of Config to reactively produce
 * @param  The type of this ConfigFactory
 */
public abstract class ConfigSource> extends ConfigProvider, S> {

    public static final String PROCESSORS_PROPERTY = "atleon.config.processors";

    protected ConfigSource() {

    }

    protected ConfigSource(String name) {
        super(name);
    }

    protected ConfigSource(Function, Optional> propertiesToName) {
        super(propertiesToName);
    }

    @Override
    protected final Mono create(String name, Map properties) {
        return applyProcessors(name, properties)
            .doOnNext(this::validateProperties)
            .map(this::postProcessProperties);
    }

    @Override
    protected final Mono create(Map properties) {
        return applyProcessors(properties)
            .doOnNext(this::validateProperties)
            .map(this::postProcessProperties);
    }

    protected final Mono> applyProcessors(String name, Map properties) {
        Mono> result = Mono.just(properties);
        for (ConfigProcessor processor : loadProcessors(properties)) {
            result = result.flatMap(configs -> processor.process(name, configs));
        }
        return result;
    }

    protected final Mono> applyProcessors(Map properties) {
        Mono> result = Mono.just(properties);
        for (ConfigProcessor processor : loadProcessors(properties)) {
            result = result.flatMap(processor::process);
        }
        return result;
    }

    protected List loadProcessors(Map properties) {
        List processors = defaultInterceptors().stream()
            .map(ConfigInterceptor::asProcessor)
            .collect(Collectors.toList());
        processors.addAll(ConfigLoading.loadListOfInstancesOrEmpty(properties, PROCESSORS_PROPERTY, ConfigProcessor.class));
        return processors;
    }

    protected abstract void validateProperties(Map properties);

    protected abstract T postProcessProperties(Map properties);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy