Search in sources :

Example 16 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project cas by apereo.

the class DefaultCasConfigurationPropertiesSourceLocator method loadEmbeddedYamlOverriddenProperties.

private PropertySource<?> loadEmbeddedYamlOverriddenProperties(final ResourceLoader resourceLoader, final Environment environment) {
    val profiles = ConfigurationPropertiesLoaderFactory.getApplicationProfiles(environment);
    val yamlFiles = profiles.stream().map(profile -> String.format("classpath:/application-%s.yml", profile)).sorted().map(resourceLoader::getResource).collect(Collectors.toList());
    yamlFiles.add(resourceLoader.getResource("classpath:/application.yml"));
    LOGGER.debug("Loading embedded YAML configuration files [{}]", yamlFiles);
    val composite = new CompositePropertySource("embeddedYamlOverriddenCompositeProperties");
    yamlFiles.forEach(resource -> {
        LOGGER.trace("Loading YAML properties from [{}]", resource);
        val source = configurationPropertiesLoaderFactory.getLoader(resource, String.format("embeddedYamlOverriddenProperties-%s", resource.getFilename())).load();
        composite.addPropertySource(source);
    });
    return composite;
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) Unchecked(org.jooq.lambda.Unchecked) ResourceLoader(org.springframework.core.io.ResourceLoader) PropertySource(org.springframework.core.env.PropertySource) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) FileSystemResource(org.springframework.core.io.FileSystemResource) Collectors(java.util.stream.Collectors) ConfigurationPropertiesLoaderFactory(org.apereo.cas.configuration.loader.ConfigurationPropertiesLoaderFactory) File(java.io.File) CompositePropertySource(org.springframework.core.env.CompositePropertySource) FunctionUtils(org.apereo.cas.util.function.FunctionUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Environment(org.springframework.core.env.Environment) CollectionUtils(org.apereo.cas.util.CollectionUtils) Optional(java.util.Optional) CasConfigurationPropertiesSourceLocator(org.apereo.cas.configuration.api.CasConfigurationPropertiesSourceLocator) Resource(org.springframework.core.io.Resource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 17 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project java-chassis by ServiceComb.

the class GovernanceProperties method getProperties.

private void getProperties(PropertySource<?> propertySource, Set<String> allKeys) {
    if (propertySource instanceof CompositePropertySource) {
        // recursively get EnumerablePropertySource
        CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
        compositePropertySource.getPropertySources().forEach(ps -> getProperties(ps, allKeys));
        return;
    }
    if (propertySource instanceof EnumerablePropertySource) {
        EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
        Collections.addAll(allKeys, enumerablePropertySource.getPropertyNames());
        return;
    }
    LOGGER.debug("None EnumerablePropertySource ignored in {}, propertySourceName = [{}]", this.getClass().getName(), propertySource.getName());
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource)

Example 18 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource 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 19 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project spring-boot by spring-projects.

the class EnvironmentEndpointTests method testCompositeSource.

@SuppressWarnings("unchecked")
@Test
public void testCompositeSource() throws Exception {
    EnvironmentEndpoint report = getEndpointBean();
    CompositePropertySource source = new CompositePropertySource("composite");
    source.addPropertySource(new MapPropertySource("one", Collections.singletonMap("foo", (Object) "bar")));
    source.addPropertySource(new MapPropertySource("two", Collections.singletonMap("foo", (Object) "spam")));
    this.context.getEnvironment().getPropertySources().addFirst(source);
    Map<String, Object> env = report.invoke();
    assertThat(((Map<String, Object>) env.get("composite:one")).get("foo")).isEqualTo("bar");
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Map(java.util.Map) Test(org.junit.Test)

Example 20 with CompositePropertySource

use of org.springframework.core.env.CompositePropertySource in project spring-boot by spring-projects.

the class PropertiesConfigurationFactoryMapTests method testBindFromCompositePropertySource.

@Test
public void testBindFromCompositePropertySource() throws Exception {
    this.targetName = "foo";
    setupFactory();
    MutablePropertySources sources = new MutablePropertySources();
    CompositePropertySource composite = new CompositePropertySource("composite");
    composite.addPropertySource(new MapPropertySource("map", Collections.singletonMap("foo.map.name", (Object) "blah")));
    sources.addFirst(composite);
    this.factory.setPropertySources(sources);
    this.factory.afterPropertiesSet();
    Foo foo = this.factory.getObject();
    assertThat(foo.map.get("name")).isEqualTo("blah");
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Aggregations

CompositePropertySource (org.springframework.core.env.CompositePropertySource)35 MapPropertySource (org.springframework.core.env.MapPropertySource)11 Test (org.junit.Test)10 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 lombok.val (lombok.val)9 HashMap (java.util.HashMap)7 Test (org.junit.jupiter.api.Test)7 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)6 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 IOException (java.io.IOException)4 Map (java.util.Map)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)4 PropertySource (org.springframework.core.env.PropertySource)4 Arrays (java.util.Arrays)3 List (java.util.List)3 FileSystemResource (org.springframework.core.io.FileSystemResource)3 Resource (org.springframework.core.io.Resource)3 Config (com.ctrip.framework.apollo.Config)2 File (java.io.File)2