Example usage for org.springframework.core.env PropertiesPropertySource PropertiesPropertySource

List of usage examples for org.springframework.core.env PropertiesPropertySource PropertiesPropertySource

Introduction

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

Prototype

protected PropertiesPropertySource(String name, Map<String, Object> source) 

Source Link

Usage

From source file:org.cloudfoundry.reconfiguration.spring.CloudPropertySourceApplicationContextInitializer.java

private void addCloudPropertySource(ConfigurableApplicationContext applicationContext) {
    MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();

    if (hasCloudPropertySource(propertySources)) {
        this.logger.fine("'cloud' already in PropertySources");
    } else {/*w ww.j a v a 2s.  c  om*/
        this.logger.info("Adding 'cloud' PropertySource to ApplicationContext");

        Cloud cloud = this.cloudUtils.getCloudFactory().getCloud();
        PropertySource<?> cloudPropertySource = new PropertiesPropertySource(CLOUD_PROPERTY_SOURCE,
                cloud.getCloudProperties());

        propertySources.addLast(cloudPropertySource);
    }
}

From source file:org.openbaton.autoscaling.utils.Utils.java

public static void loadExternalProperties(ConfigurableEnvironment properties) {
    if (properties.containsProperty("external-properties-file")
            && properties.getProperty("external-properties-file") != null) {
        try {//from   w  w  w  .j a  va  2  s . c o  m
            InputStream is = new FileInputStream(new File(properties.getProperty("external-properties-file")));
            Properties externalProperties = new Properties();
            externalProperties.load(is);
            PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(
                    "external-properties", externalProperties);

            MutablePropertySources propertySources = properties.getPropertySources();
            propertySources.addFirst(propertiesPropertySource);
        } catch (IOException e) {
            log.warn("Not found external-properties-file: "
                    + properties.getProperty("external-properties-file"));
        }
    }
}

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

/**
 * Test method for {@link ch.sdi.core.impl.cfg.ConfigUtils#getPropertyNamesStartingWith(Class)}.
 *//*from   w w w . j a v a2  s.  c  o  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:org.cloudfoundry.identity.uaa.integration.TestProfileEnvironment.java

private TestProfileEnvironment() {

    List<Resource> resources = new ArrayList<Resource>();

    for (String location : DEFAULT_PROFILE_CONFIG_FILE_LOCATIONS) {
        location = environment.resolvePlaceholders(location);
        Resource resource = recourceLoader.getResource(location);
        if (resource != null && resource.exists()) {
            resources.add(resource);/*  w ww  .  jav  a2s. c  o m*/
        }
    }

    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(resources.toArray(new Resource[resources.size()]));
    factory.setDocumentMatchers(Collections.singletonMap("platform",
            environment.acceptsProfiles("postgresql") ? "postgresql" : "hsqldb"));
    factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE);
    Properties properties = factory.getObject();

    logger.debug("Decoding environment properties: " + properties.size());
    if (!properties.isEmpty()) {
        for (Enumeration<?> names = properties.propertyNames(); names.hasMoreElements();) {
            String name = (String) names.nextElement();
            String value = properties.getProperty(name);
            if (value != null) {
                properties.setProperty(name, environment.resolvePlaceholders(value));
            }
        }
        if (properties.containsKey("spring_profiles")) {
            properties.setProperty(StandardEnvironment.ACTIVE_PROFILES_PROPERTY_NAME,
                    properties.getProperty("spring_profiles"));
        }
        // System properties should override the ones in the config file, so add it last
        environment.getPropertySources().addLast(new PropertiesPropertySource("uaa.yml", properties));
    }

    EnvironmentPropertiesFactoryBean environmentProperties = new EnvironmentPropertiesFactoryBean();
    environmentProperties.setEnvironment(environment);
    environmentProperties.setDefaultProperties(properties);
    Map<String, ?> debugProperties = environmentProperties.getObject();
    logger.debug("Environment properties: " + debugProperties);
}

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 .  ja  va 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:edu.jhuapl.openessence.config.AppInitializer.java

private List<PropertySource<?>> getPropertySources(Collection<Resource> resources) {
    List<PropertySource<?>> propertySources = new ArrayList<PropertySource<?>>();

    for (Resource r : resources) {
        String filename = r.getFilename();
        if (filename == null) {
            throw new IllegalArgumentException("Cannot have resource with no file");
        }/*from   w  ww. ja v  a 2 s .c om*/

        String name = FilenameUtils.getBaseName(r.getFilename());

        Properties source;
        try {
            source = PropertiesLoaderUtils.loadProperties(r);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

        propertySources.add(new PropertiesPropertySource(name, source));

        try {
            log.info("Adding file {} as property source named '{}'", r.getFile(), name);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

    return propertySources;
}

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

PropertySource<?> defaultProperties() throws Exception {
    return new PropertiesPropertySource(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME, new Properties() {
        {/*  www.  j  av  a  2s. c  o m*/
            setProperty(APP_DATA_DIR_KEY, appDataDir);
            setProperty(APP_NAME_KEY, appName);

            setProperty(UserAgent.NAME_KEY, appName);
            setProperty(UserAgent.VERSION_KEY, BitsquareEnvironment.this.getRequiredProperty(APP_VERSION_KEY));

            setProperty(WalletService.DIR_KEY, appDataDir);
            setProperty(WalletService.PREFIX_KEY, appName);

            setProperty(Persistence.DIR_KEY, appDataDir);
            setProperty(Persistence.PREFIX_KEY, appName + "_pref");

            setProperty(ViewCB.TITLE_KEY, appName);
        }
    });
}

From source file:com.rllc.batch.webcast.DynamicFtpChannelResolver.java

/**
 * Use Spring 3.1. environment support to set properties for the
 * file-specific application context./* www . j a  v a2s . co  m*/
 *
 * @param ctx
 * @param customer
 */
private void setEnvironmentForFile(ConfigurableApplicationContext ctx, String fileName) {
    StandardEnvironment environment = new StandardEnvironment();
    Properties props = new Properties();

    // populate properties for file
    props.setProperty("host", env.getProperty("batch.sftp.hostname"));
    props.setProperty("user", env.getProperty("batch.sftp.username"));
    props.setProperty("password", env.getProperty("batch.sftp.password"));
    props.setProperty("port", env.getProperty("batch.sftp.port"));
    props.setProperty("remote.directory", getRemoteDirectoryForFile(fileName));
    PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
    environment.getPropertySources().addLast(pps);
    ctx.setEnvironment(environment);
}

From source file:com.github.exper0.efilecopier.ftp.DynamicFtpChannelResolver.java

/**
 * Use Spring 3.1. environment support to set properties for the
 * customer-specific application context.
 *
 * @param ctx//  w  ww .  j a  v a2  s  .c o m
 * @param customer
 */
private void setEnvironmentForCustomer(ConfigurableApplicationContext ctx, String customer) {

    StandardEnvironment env = new StandardEnvironment();
    Properties props = new Properties();
    // populate properties for customer
    props.setProperty("remote.directory", "/");
    props.setProperty("session.factory", "session.factory");
    PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
    env.getPropertySources().addLast(pps);
    ctx.setEnvironment(env);
}