Search in sources :

Example 1 with ResourcePropertySource

use of org.springframework.core.io.support.ResourcePropertySource in project spring-framework by spring-projects.

the class TestPropertySourceUtils method addPropertiesFilesToEnvironment.

/**
	 * Add the {@link Properties} files from the given resource {@code locations}
	 * to the supplied {@link ConfigurableEnvironment environment}.
	 * <p>Property placeholders in resource locations (i.e., <code>${...}</code>)
	 * will be {@linkplain Environment#resolveRequiredPlaceholders(String) resolved}
	 * against the {@code Environment}.
	 * <p>Each properties file will be converted to a {@link ResourcePropertySource}
	 * that will be added to the {@link PropertySources} of the environment with
	 * highest precedence.
	 * @param environment the environment to update; never {@code null}
	 * @param resourceLoader the {@code ResourceLoader} to use to load each resource;
	 * never {@code null}
	 * @param locations the resource locations of {@code Properties} files to add
	 * to the environment; potentially empty but never {@code null}
	 * @since 4.3
	 * @see ResourcePropertySource
	 * @see TestPropertySource#locations
	 * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...)
	 * @throws IllegalStateException if an error occurs while processing a properties file
	 */
public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String... locations) {
    Assert.notNull(environment, "'environment' must not be null");
    Assert.notNull(resourceLoader, "'resourceLoader' must not be null");
    Assert.notNull(locations, "'locations' must not be null");
    try {
        for (String location : locations) {
            String resolvedLocation = environment.resolveRequiredPlaceholders(location);
            Resource resource = resourceLoader.getResource(resolvedLocation);
            environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
        }
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to add PropertySource to Environment", ex);
    }
}
Also used : ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException)

Example 2 with ResourcePropertySource

use of org.springframework.core.io.support.ResourcePropertySource in project spring-framework by spring-projects.

the class ConfigurationClassParser method addPropertySource.

private void addPropertySource(PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
    if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
        // We've already added a version, we need to extend it
        PropertySource<?> existing = propertySources.get(name);
        PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource);
        if (existing instanceof CompositePropertySource) {
            ((CompositePropertySource) existing).addFirstPropertySource(newSource);
        } else {
            if (existing instanceof ResourcePropertySource) {
                existing = ((ResourcePropertySource) existing).withResourceName();
            }
            CompositePropertySource composite = new CompositePropertySource(name);
            composite.addPropertySource(newSource);
            composite.addPropertySource(existing);
            propertySources.replace(name, composite);
        }
    } else {
        if (this.propertySourceNames.isEmpty()) {
            propertySources.addLast(propertySource);
        } else {
            String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
            propertySources.addBefore(firstProcessed, propertySource);
        }
    }
    this.propertySourceNames.add(name);
}
Also used : ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 3 with ResourcePropertySource

use of org.springframework.core.io.support.ResourcePropertySource in project bitsquare by bitsquare.

the class BitsquareEnvironment method homeDirProperties.

private PropertySource<?> homeDirProperties() throws Exception {
    String location = String.format("file:%s/.bitsquare/bitsquare.properties", getProperty("user.home"));
    Resource resource = resourceLoader.getResource(location);
    if (!resource.exists())
        return new PropertySource.StubPropertySource(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME);
    return new ResourcePropertySource(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME, resource);
}
Also used : ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) Resource(org.springframework.core.io.Resource) PropertySource(org.springframework.core.env.PropertySource) ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) JOptCommandLinePropertySource(io.bitsquare.util.spring.JOptCommandLinePropertySource)

Aggregations

ResourcePropertySource (org.springframework.core.io.support.ResourcePropertySource)3 Resource (org.springframework.core.io.Resource)2 JOptCommandLinePropertySource (io.bitsquare.util.spring.JOptCommandLinePropertySource)1 IOException (java.io.IOException)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)1 PropertySource (org.springframework.core.env.PropertySource)1