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

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

Introduction

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

Prototype

@Override
    @Nullable
    public PropertySource<?> get(String name) 

Source Link

Usage

From source file:io.spring.initializr.web.autoconfigure.CloudfoundryEnvironmentPostProcessor.java

private static void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
    MapPropertySource target = null;/*from  w  w  w.j a  v  a 2 s .co m*/
    if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
        PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
        if (source instanceof MapPropertySource) {
            target = (MapPropertySource) source;
            for (String key : map.keySet()) {
                if (!target.containsProperty(key)) {
                    target.getSource().put(key, map.get(key));
                }
            }
        }
    }
    if (target == null) {
        target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
    }
    if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
        propertySources.addLast(target);
    }
}

From source file:ch.sdi.core.impl.cfg.ConfigUtils.java

/**
 * Tries to retrieve the named PropertySource from the environment. If not found it creates a
 * new one.//from   w  ww  .ja  va2  s . co  m
 * <p>
 *
 * @param aEnv
 * @param aPropertySourceName
 * @return the embedded property map
 * @throws SdiException if the found property source is not instance of PropertiesPropertySource
 */
public static Map<String, Object> getOrCreatePropertySource(ConfigurableEnvironment aEnv,
        String aPropertySourceName) throws SdiException {
    MutablePropertySources mps = aEnv.getPropertySources();
    PropertySource<?> ps = mps.get(aPropertySourceName);
    PropertiesPropertySource pps = null;

    if (ps == null) {
        Properties props = new Properties();
        pps = new PropertiesPropertySource(aPropertySourceName, props);
        mps.addFirst(pps);
    } else {
        if (!(ps instanceof PropertiesPropertySource)) {
            throw new SdiException("Found property source is not instance of PropertiesPropertySource "
                    + " but: " + ps.getClass().getName(), SdiException.EXIT_CODE_CONFIG_ERROR);
        } // if !( ps instanceof PropertiesPropertySource )

        pps = (PropertiesPropertySource) ps;
    }

    Map<String, Object> map = pps.getSource();
    return map;
}

From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java

/**
 * Checks that the property "security.ignored" is in the environment after SpringFoxEnvironmentPostProcessor is executed.
 *//* w  ww  .jav a  2  s. com*/
@Test
public void securityIgnoredPropertyTest() {
    ConfigurableEnvironment environment = context.getEnvironment();

    MutablePropertySources propertySources = environment.getPropertySources();
    PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources
            .get("ModifiedSecurityIgnoredPropertySource");
    Object securityIgnoredProperty = modifiedSecurityIgnoredPropertySource.getProperty("security.ignored");

    Assert.assertNotNull("security.gnored property must not be null", securityIgnoredProperty);
}

From source file:com.santander.serenity.devstack.springfox.config.itest.EnvironmentListenerIntegrationTests.java

/**
 * Checks that the property "security.ignored" has the correct values
 *///  w  ww . java  2  s .  c  o m
@Test
public void securityIgnoredPropertyValuesTest() {
    ConfigurableEnvironment environment = context.getEnvironment();

    MutablePropertySources propertySources = environment.getPropertySources();
    PropertySource<?> modifiedSecurityIgnoredPropertySource = propertySources
            .get("ModifiedSecurityIgnoredPropertySource");
    Object securityIgnoredPropertyValues = modifiedSecurityIgnoredPropertySource
            .getProperty("security.ignored");

    LinkedHashSet set = (LinkedHashSet) securityIgnoredPropertyValues;

    Assert.assertTrue("/webjars/** property is not in the security.ignored set", set.contains("/webjars/**"));
    Assert.assertTrue("/swagger-resources property is not in the security.ignored set",
            set.contains("/swagger-resources"));
    Assert.assertTrue("/v2/api-docs property is not in the security.ignored set", set.contains("/v2/api-docs"));
    Assert.assertTrue("/configuration/ui property is not in the security.ignored set",
            set.contains("/configuration/ui"));
    Assert.assertTrue("/configuration/security property is not in the security.ignored set",
            set.contains("/configuration/security"));
    Assert.assertTrue("/swagger-ui.html property is not in the security.ignored set",
            set.contains("/swagger-ui.html"));
}

From source file:com.github.eddumelendez.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration.java

private Map<String, Object> getLdapPorts(MutablePropertySources sources) {
    PropertySource<?> propertySource = sources.get("ldap.ports");
    if (propertySource == null) {
        propertySource = new MapPropertySource("ldap.ports", new HashMap<String, Object>());
        sources.addFirst(propertySource);
    }/*from  w  w  w.  j av a2 s.  c  o m*/
    return (Map<String, Object>) propertySource.getSource();
}

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 {//from   ww  w  .  j  a v  a  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:io.micrometer.spring.MetricsEnvironmentPostProcessor.java

private void addDefaultProperty(ConfigurableEnvironment environment, String name, String value) {
    MutablePropertySources sources = environment.getPropertySources();
    Map<String, Object> map = null;
    if (sources.contains("defaultProperties")) {
        PropertySource<?> source = sources.get("defaultProperties");
        if (source instanceof MapPropertySource) {
            map = ((MapPropertySource) source).getSource();
        }//from  w  ww .ja v a  2  s . co m
    } else {
        map = new LinkedHashMap<>();
        sources.addLast(new MapPropertySource("defaultProperties", map));
    }
    if (map != null) {
        map.put(name, value);
    }
}

From source file:com.example.journal.env.JournalEnvironmentPostProcessor.java

private void addOrReplace(MutablePropertySources propertySources, Map<String, Object> map) {
    MapPropertySource target = null;/*w ww . ja v a2  s. c o  m*/
    if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
        PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
        if (source instanceof MapPropertySource) {
            target = (MapPropertySource) source;
            for (String key : map.keySet()) {
                if (!target.containsProperty(key)) {
                    target.getSource().put(key, map.get(key));
                }
            }
        }
    }
    if (target == null) {
        target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
    }
    if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
        propertySources.addLast(target);
    }
}

From source file:org.springframework.boot.SpringApplication.java

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment.//from   w  ww . j ava  2  s .  co m
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 */
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
    MutablePropertySources sources = environment.getPropertySources();
    if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
        sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
    }
    if (this.addCommandLineProperties && args.length > 0) {
        String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
        if (sources.contains(name)) {
            PropertySource<?> source = sources.get(name);
            CompositePropertySource composite = new CompositePropertySource(name);
            composite
                    .addPropertySource(new SimpleCommandLinePropertySource(name + "-" + args.hashCode(), args));
            composite.addPropertySource(source);
            sources.replace(name, composite);
        } else {
            sources.addFirst(new SimpleCommandLinePropertySource(args));
        }
    }
}

From source file:org.springframework.cloud.dataflow.server.config.DefaultEnvironmentPostProcessor.java

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    Map<String, Object> internalDefaults = new HashMap<>();
    Map<String, Object> defaults = new HashMap<>();
    MutablePropertySources existingPropertySources = environment.getPropertySources();

    contributeDefaults(internalDefaults, serverDefaultsResource);
    contributeDefaults(defaults, serverResource);

    String defaultPropertiesKey = "defaultProperties";

    if (!existingPropertySources.contains(defaultPropertiesKey)
            || existingPropertySources.get(defaultPropertiesKey) == null) {
        existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, internalDefaults));
        existingPropertySources.addLast(new MapPropertySource(defaultPropertiesKey, defaults));
    } else {//from  www  .ja  va 2 s .c o m
        PropertySource<?> propertySource = existingPropertySources.get(defaultPropertiesKey);
        @SuppressWarnings("unchecked")
        Map<String, Object> mapOfProperties = Map.class.cast(propertySource.getSource());
        for (String k : internalDefaults.keySet()) {
            Set<String> setOfPropertyKeys = mapOfProperties.keySet();
            if (!setOfPropertyKeys.contains(k)) {
                mapOfProperties.put(k, internalDefaults.get(k));
                logger.debug(k + '=' + internalDefaults.get(k));
            }
        }
        for (String k : defaults.keySet()) {
            Set<String> setOfPropertyKeys = mapOfProperties.keySet();
            if (!setOfPropertyKeys.contains(k)) {
                mapOfProperties.put(k, defaults.get(k));
                logger.debug(k + '=' + defaults.get(k));
            }
        }
    }

}