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

io.airlift.configuration.ConfigurationUtils Maven / Gradle / Ivy

There is a newer version: 284
Show newest version
package io.airlift.configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.regex.Matcher.quoteReplacement;

public final class ConfigurationUtils
{
    private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{ENV:([a-zA-Z][a-zA-Z0-9_-]*)}");

    private ConfigurationUtils() {}

    public static Map replaceEnvironmentVariables(Map properties)
    {
        return replaceEnvironmentVariables(properties, System.getenv(), (k, v) -> {});
    }

    public static Map replaceEnvironmentVariables(
            Map properties,
            Map environment,
            BiConsumer onError)
    {
        Map replaced = new HashMap<>();
        properties.forEach((propertyKey, propertyValue) -> {
            StringBuilder replacedPropertyValue = new StringBuilder();
            Matcher matcher = ENV_PATTERN.matcher(propertyValue);
            while (matcher.find()) {
                String envName = matcher.group(1);
                String envValue = environment.get(envName);
                if (envValue == null) {
                    onError.accept(propertyKey, format("Configuration property '%s' references unset environment variable '%s'", propertyKey, envName));
                    return;
                }
                matcher.appendReplacement(replacedPropertyValue, quoteReplacement(envValue));
            }
            matcher.appendTail(replacedPropertyValue);
            replaced.put(propertyKey, replacedPropertyValue.toString());
        });
        return replaced;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy