Example usage for org.springframework.core.env MutablePropertySources addAfter

List of usage examples for org.springframework.core.env MutablePropertySources addAfter

Introduction

In this page you can find the example usage for org.springframework.core.env MutablePropertySources addAfter.

Prototype

public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) 

Source Link

Document

Add the given property source object with precedence immediately lower than the named relative property source.

Usage

From source file:ch.sdi.UserPropertyOverloader.java

public void overrideByUserProperties() {
    List<Class<?>> candidates = findCandidates();

    for (Class<?> clazz : candidates) {
        myLog.debug("candidate for user property overloading: " + clazz.getName());
        String fileName = SdiMainProperties.USER_OVERRIDE_PREFIX + ConfigUtils.makePropertyResourceName(clazz);
        InputStream is = this.getClass().getResourceAsStream("/" + fileName);

        if (is == null) {
            myLog.debug("Skipping non existing user overloading property file: " + fileName);
            continue;
        } // if is == null

        myLog.debug("Found overloading property file: " + fileName);
        Properties props = new Properties();
        try {/*w  w  w .j ava 2s .  c  o  m*/
            props.load(is);
        } catch (IOException t) {
            myLog.error("Problems loading property file " + fileName);
        }

        myEnv.setIgnoreUnresolvableNestedPlaceholders(true);
        try {
            props.stringPropertyNames().stream().map(key -> {
                String origValue = myEnv.getProperty(key);
                String result = "Key " + key + ": ";
                return (origValue == null || origValue.isEmpty())
                        ? result + "No default value found. Adding new value to environment: \""
                                + props.getProperty(key) + "\""
                        : result + "Overriding default value \"" + origValue + "\" with new value: \""
                                + props.getProperty(key) + "\"";
            }).forEach(msg -> myLog.debug(msg));
        } finally {
            myEnv.setIgnoreUnresolvableNestedPlaceholders(false);
        }

        PropertySource<?> ps = new PropertiesPropertySource(fileName, props);
        MutablePropertySources mps = myEnv.getPropertySources();
        if (mps.get(ConfigUtils.PROP_SOURCE_NAME_CMD_LINE) != null) {
            mps.addAfter(ConfigUtils.PROP_SOURCE_NAME_CMD_LINE, ps);
        } else {
            mps.addFirst(ps);
        }
        myLog.debug("PropertySources after adding overloading: " + mps);
    }

}

From source file:org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {
        Properties properties = new Properties();
        addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
        addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
        MutablePropertySources propertySources = environment.getPropertySources();
        if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
            propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                    new PropertiesPropertySource("vcap", properties));
        } else {//from   w w  w . j  a  v a 2  s.co m
            propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
        }
    }
}

From source file:org.springframework.boot.cloudfoundry.VcapApplicationListener.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;/*  w ww . j  a v  a 2  s.c  o  m*/
    }
    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));
    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }
}

From source file:org.springframework.boot.cloudfoundry.VcapEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;//from  www.jav  a  2  s . c om
    }
    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));
    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }
}

From source file:org.springframework.boot.context.initializer.VcapApplicationContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {

    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;//from   ww w  . ja v a  2  s.  c o  m
    }

    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));

    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }

}

From source file:org.springframework.boot.context.listener.VcapApplicationListener.java

@Override
public void onApplicationEvent(SpringApplicationEnvironmentAvailableEvent event) {

    ConfigurableEnvironment environment = event.getEnvironment();
    if (!environment.containsProperty(VCAP_APPLICATION) && !environment.containsProperty(VCAP_SERVICES)) {
        return;//from w ww.  j av a 2  s .c o m
    }

    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment), "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment), "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new PropertiesPropertySource("vcap", properties));

    } else {
        propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }

}

From source file:org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer.java

private void insert(MutablePropertySources propertySources, MapPropertySource propertySource) {
    if (propertySources.contains(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
        propertySources.addAfter(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME, propertySource);
    } else {//from w  w w  .j ava  2s  .  c  om
        propertySources.addFirst(propertySource);
    }
}

From source file:org.springframework.yarn.boot.support.YarnBootClientApplicationListener.java

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    MutablePropertySources propertySources = event.getEnvironment().getPropertySources();
    if (propertySources.contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
        if (log.isDebugEnabled()) {
            log.debug("Adding afterCommandLineArgs property source after "
                    + CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME);
        }// w ww .  j  av  a 2  s .  com
        propertySources.addAfter(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
                new MapPropertySource("afterCommandLineArgs", propertySourceMap));
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Adding afterCommandLineArgs property source as first");
        }
        propertySources.addFirst(new MapPropertySource("afterCommandLineArgs", propertySourceMap));
    }
}