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

com.enonic.xp.config.ConfigInterpolator Maven / Gradle / Ivy

The newest version!
package com.enonic.xp.config;

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

import org.osgi.framework.BundleContext;

import com.enonic.xp.core.internal.Interpolator;

public final class ConfigInterpolator
{
    private static final String ENV_PREFIX = "env.";

    private Map environment;

    private Properties systemProperties;

    private BundleContext bundleContext;

    public ConfigInterpolator()
    {
        this.environment = System.getenv();
        this.systemProperties = System.getProperties();
    }

    public ConfigInterpolator environment( final Map map )
    {
        this.environment = map;
        return this;
    }

    public ConfigInterpolator systemProperties( final Properties properties )
    {
        this.systemProperties = properties;
        return this;
    }

    public ConfigInterpolator bundleContext( final BundleContext bundleContext )
    {
        this.bundleContext = bundleContext;
        return this;
    }

    public Configuration interpolate( final Configuration source )
    {
        final Map target = new HashMap<>( source.asMap() );

        doInterpolate( target );

        return ConfigurationImpl.create( target );
    }

    private String lookupValue( final String key, final Map map )
    {
        final String value1 = map.get( key );
        if ( value1 != null )
        {
            return value1;
        }

        final String value2 = getFromBundleContext( key );
        if ( value2 != null )
        {
            return value2;
        }

        final String value3 = getFromSystemProperties( key );
        if ( value3 != null )
        {
            return value3;
        }

        return getFromEnvironment( key );
    }

    private String getFromBundleContext( final String key )
    {
        return this.bundleContext != null ? this.bundleContext.getProperty( key ) : null;
    }

    private String getFromSystemProperties( final String key )
    {
        return this.systemProperties != null ? this.systemProperties.getProperty( key ) : null;
    }

    private String getFromEnvironment( final String key )
    {
        if ( !key.startsWith( ENV_PREFIX ) )
        {
            return null;
        }

        final String envKey = key.substring( ENV_PREFIX.length() );
        return this.environment != null ? this.environment.get( envKey ) : null;
    }

    private void doInterpolate( final Map map )
    {
        final Function lookup = key -> lookupValue( key, map );
        for ( final Map.Entry entry : map.entrySet() )
        {
            final String key = entry.getKey();
            final String value = Interpolator.classic().interpolate( entry.getValue(), lookup ).trim();
            map.put( key, value );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy