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

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

package io.atleon.core;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;

public final class EnvironmentalConfigs implements ConfigInterceptor {

    public static final String PREFIX = "atleon.config.source.";

    @Override
    public Map intercept(String name, Map configs) {
        String prefix = String.format("%s%s.", PREFIX, name);
        Map result = new HashMap<>(configs);
        result.putAll(loadPrefixedEnvironmentalProperties(prefix));
        return result;
    }

    @Override
    public Map intercept(Map configs) {
        return configs;
    }

    private static Map loadPrefixedEnvironmentalProperties(String prefix) {
        Map properties = new HashMap<>();
        consumePrefixed(System.getenv(), key -> key.replaceAll("_", ".").toLowerCase(), prefix, properties::put);
        consumePrefixed(System.getProperties(), Object::toString, prefix, properties::put);
        return properties;
    }

    private static  void consumePrefixed(
        Map source,
        Function keyToPropertyKey,
        String prefix,
        BiConsumer consumer
    ) {
        source.forEach((key, value) -> {
            String propertyKey = keyToPropertyKey.apply(key);
            if (propertyKey.startsWith(prefix)) {
                consumer.accept(propertyKey.substring(prefix.length()), value);
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy