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

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

Introduction

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

Prototype

public void addLast(PropertySource<?> propertySource) 

Source Link

Document

Add the given property source object with lowest precedence.

Usage

From source file:com.dangdang.config.service.support.spring.ConfigGroupSourceFactory.java

public static PropertySources create(ConfigGroup... configGroups) {
    Preconditions.checkNotNull(configGroups);
    final MutablePropertySources sources = new MutablePropertySources();
    for (ConfigGroup configGroup : configGroups) {
        sources.addLast(new ConfigGroupResource(configGroup));
    }/* w w w .  j  av  a  2s  .  c o m*/
    return sources;
}

From source file:com.dangdang.config.service.easyzk.support.spring.ZookeeperSourceFactory.java

public static PropertySources create(ConfigGroup... configGroups) {
    Preconditions.checkNotNull(configGroups);
    final MutablePropertySources sources = new MutablePropertySources();
    for (ConfigGroup configGroup : configGroups) {
        sources.addLast(new ZookeeperResource(configGroup));
    }/*from   w  w w  . j  av  a2s  .  c o  m*/
    return sources;
}

From source file:com.cloudera.director.aws.common.PropertyResolvers.java

/**
 * Creates a property resolver that pulls from multiple property resource
 * locations. The first "built-in" location must be successfully loaded, but
 * all other "custom" locations must load only if {code}allowMissing{code} is
 * false./*from w  w  w  .  ja  va  2 s.c  om*/
 *
 * @param allowMissing            true to allow custom resource locations to fail to load
 * @param builtInResourceLocation lowest precedence, required resource
 *                                location for properties
 * @param customResourceLocations additional resource locations for
 *                                properties, in increasing order of precedence
 * @return new property resolver
 * @throws IOException          if the built-in resource location could not be loaded,
 *                              or if {code}allowMissing{code} is false and any custom resource location
 *                              fails to load
 * @throws NullPointerException if any resource location is null
 */
public static PropertyResolver newMultiResourcePropertyResolver(boolean allowMissing,
        String builtInResourceLocation, String... customResourceLocations) throws IOException {
    MutablePropertySources sources = new MutablePropertySources();
    checkNotNull(builtInResourceLocation, "builtInResourceLocation is null");
    sources.addLast(buildPropertySource(BUILT_IN_NAME, builtInResourceLocation, false));
    String lastname = BUILT_IN_NAME;
    int customCtr = 1;
    for (String loc : customResourceLocations) {
        checkNotNull(loc, "customResourceLocations[" + (customCtr - 1) + "] is null");
        String thisname = CUSTOM_NAME_PREFIX + customCtr++;
        PropertySource source = buildPropertySource(thisname, loc, allowMissing);
        if (source != null) {
            sources.addBefore(lastname, source);
            lastname = thisname;
        }
    }

    return new PropertySourcesPropertyResolver(sources);
}

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  ww .  j  a  v a  2s .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:natalia.dymnikova.configuration.ConfiguredEnvironment.java

@Override
protected void customizePropertySources(final MutablePropertySources propertySources) {
    propertySources.addLast(new PropertySource<Object>(CONFIGURED_PROPERTY_SOURCE) {
        @Override/*  w  w w. j av  a2s  . co m*/
        public Object getProperty(final String name) {
            try {
                final com.typesafe.config.ConfigValue value = config.getValue(name);
                final ConfigValueType valueType = value.valueType();

                if (ConfigValueType.OBJECT == valueType || ConfigValueType.LIST == valueType) {
                    final Config config = ((ConfigObject) value).toConfig();
                    log.trace("Loaded configuration object {} = {}", name, config);
                    return config;
                } else {
                    log.trace("Loaded configuration value {} = {}", name, value);
                    return value.unwrapped();
                }
            } catch (final ConfigException e) {
                return null;
            }
        }
    });
}

From source file:org.greencheek.utils.environment.propertyplaceholder.spring.EnvironmentalPropertySourcesPlaceholderConfigurerWithSpringValueResolution.java

@Override
public void afterPropertiesSet() {
    if (mergerBuilder == null) {
        throw new IllegalStateException("The PropertyMergerBuilder must not be null.");
    }// ww w. java 2 s. c o m

    Properties p = mergerBuilder.build().getMergedProperties();

    StandardEnvironment env = new StandardEnvironment();
    MutablePropertySources sources = new MutablePropertySources();

    if (isSystemPropertiesResolutionEnabled())
        sources.addLast(new MapPropertySource(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
                env.getSystemProperties()));
    if (isEnvironmentPropertiesResolutionEnabled())
        sources.addLast(new SystemEnvironmentPropertySource(
                StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, env.getSystemEnvironment()));

    sources.addFirst(new PropertiesPropertySource(ENVIRONMENT_SPECIFIC_PROPERTIES, p));
    super.setPropertySources(sources);
}

From source file:com.careerly.common.support.ReloadablePropertySourcesPlaceholderConfigurer.java

private void reload() throws IOException {
    logger.info("properties changed, reloading..");

    // Properties?
    Properties properties = super.mergeProperties();
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, properties));
    ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
    // BeanFactory??removeEmbeddedValueResolver??Resolver?
    valueResolver.expire();//from   w w  w  . j  ava2  s.  co  m
    valueResolver = new PropertyStringValueResolver(propertyResolver);
    beanFactory.addEmbeddedValueResolver(valueResolver);

    // ??
    for (String beanName : beanNames) {
        Object bean = beanFactory.getBean(beanName);
        autowiredAnnotationBeanPostProcessor.processInjection(bean);
    }
}

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

/**
 * Test method for {@link ch.sdi.core.impl.cfg.ConfigUtils#getPropertyNamesStartingWith(Class)}.
 *//*from   www .j a va 2 s  .co m*/
@Test
public void testGetPropertyNamesStartingWith() {
    Properties props1 = new Properties();
    PropertiesPropertySource pps1 = new PropertiesPropertySource("Props1", props1);
    props1.put("sdi.collect.comment.2", "//");
    props1.put("sdi.collect.comment.1", "#");
    props1.put("key", "value");

    Properties props2 = new Properties();
    PropertiesPropertySource pps2 = new PropertiesPropertySource("Props2", props2);
    props1.put("sdi.collect.comment.2", ";");
    props1.put("sdi.collect.comment.1", "?");

    MutablePropertySources mps = myEnv.getPropertySources();
    mps.addFirst(pps1);
    mps.addLast(pps2);

    Collection<String> received = ConfigUtils.getPropertyNamesStartingWith(myEnv, "sdi.collect.comment.");
    Assert.assertNotNull(received);
    myLog.debug("received: " + received);
    Assert.assertEquals(2, received.size());
    // assert also if the retrieved keys are sorted:
    int i = 1;
    for (Iterator<String> iterator = received.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        Assert.assertEquals("sdi.collect.comment." + i, key);
        i++;
    }

    myLog.debug("testing composite property source");
    CompositePropertySource cps = new CompositePropertySource("CompositeTest");
    cps.addPropertySource(pps1);
    cps.addPropertySource(pps2);

    TestUtils.removeAllFromEnvironment(myEnv);
    mps = myEnv.getPropertySources();
    mps.addFirst(pps1);

    received = ConfigUtils.getPropertyNamesStartingWith(myEnv, "sdi.collect.comment.");
    Assert.assertNotNull(received);
    myLog.debug("received: " + received);
    Assert.assertEquals(2, received.size());
    for (Iterator<String> iterator = received.iterator(); iterator.hasNext();) {
        String key = iterator.next();
        Assert.assertTrue(key.startsWith("sdi.collect.comment."));
    }

}

From source file:io.bitsquare.app.BitsquareEnvironment.java

BitsquareEnvironment(PropertySource commandLineProperties) {
    String userDataDir = commandLineProperties.containsProperty(USER_DATA_DIR_KEY)
            ? (String) commandLineProperties.getProperty(USER_DATA_DIR_KEY)
            : DEFAULT_USER_DATA_DIR;/*from w w w  .  ja va  2s.c  o  m*/

    this.appName = commandLineProperties.containsProperty(APP_NAME_KEY)
            ? (String) commandLineProperties.getProperty(APP_NAME_KEY)
            : DEFAULT_APP_NAME;

    this.appDataDir = commandLineProperties.containsProperty(APP_DATA_DIR_KEY)
            ? (String) commandLineProperties.getProperty(APP_DATA_DIR_KEY)
            : appDataDir(userDataDir, appName);

    MutablePropertySources propertySources = this.getPropertySources();
    propertySources.addFirst(commandLineProperties);
    try {
        propertySources.addLast(filesystemProperties());
        propertySources.addLast(classpathProperties());
        propertySources.addLast(defaultProperties());
    } catch (Exception ex) {
        throw new BitsquareException(ex);
    }
}

From source file:cf.spring.config.YamlPropertyContextInitializer.java

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    try {/*w  w w. j  a  va2  s.com*/
        final ConfigurableEnvironment environment = applicationContext.getEnvironment();
        final Resource resource = applicationContext
                .getResource(environment.getProperty(locationProperty, locationDefault));
        final YamlDocument yamlDocument;
        LOGGER.info("Loading config from: {}", resource);
        yamlDocument = YamlDocument.load(resource);
        final MutablePropertySources propertySources = environment.getPropertySources();
        final PropertySource propertySource = new YamlPropertySource(name, yamlDocument);
        if (addFirst) {
            propertySources.addFirst(propertySource);
        } else {
            propertySources.addLast(propertySource);
        }

        applicationContext.getBeanFactory().registerSingleton(name, yamlDocument);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}