Search in sources :

Example 11 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project jeesuite-libs by vakinge.

the class EnvironmentHelper method getAllProperties.

public static Map<String, Object> getAllProperties(String prefix) {
    init();
    if (environment == null)
        return null;
    MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources();
    Map<String, Object> properties = new LinkedHashMap<String, Object>();
    for (PropertySource<?> source : propertySources) {
        if (source.getName().startsWith("servlet") || source.getName().startsWith("system")) {
            continue;
        }
        if (source instanceof EnumerablePropertySource) {
            for (String name : ((EnumerablePropertySource<?>) source).getPropertyNames()) {
                boolean match = StringUtils.isEmpty(prefix);
                if (!match) {
                    match = name.startsWith(prefix);
                }
                if (match) {
                    Object value = source.getProperty(name);
                    if (value != null) {
                        properties.put(name, value);
                    }
                }
            }
        }
    }
    return Collections.unmodifiableMap(properties);
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) LinkedHashMap(java.util.LinkedHashMap)

Example 12 with EnumerablePropertySource

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

the class YamlPropertySourceLoaderTests method loadOriginAware.

@Test
void loadOriginAware() throws Exception {
    Resource resource = new ClassPathResource("test-yaml.yml", getClass());
    List<PropertySource<?>> loaded = this.loader.load("resource", resource);
    for (PropertySource<?> source : loaded) {
        EnumerablePropertySource<?> enumerableSource = (EnumerablePropertySource<?>) source;
        for (String name : enumerableSource.getPropertyNames()) {
            System.out.println(name + " = " + enumerableSource.getProperty(name));
        }
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) ClassPathResource(org.springframework.core.io.ClassPathResource) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) ClassPathResource(org.springframework.core.io.ClassPathResource) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) Test(org.junit.jupiter.api.Test)

Example 13 with EnumerablePropertySource

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

the class EnvironmentEndpoint method getEnvironmentDescriptor.

private EnvironmentDescriptor getEnvironmentDescriptor(Predicate<String> propertyNamePredicate) {
    PlaceholdersResolver resolver = getResolver();
    List<PropertySourceDescriptor> propertySources = new ArrayList<>();
    getPropertySourcesAsMap().forEach((sourceName, source) -> {
        if (source instanceof EnumerablePropertySource) {
            propertySources.add(describeSource(sourceName, (EnumerablePropertySource<?>) source, resolver, propertyNamePredicate));
        }
    });
    return new EnvironmentDescriptor(Arrays.asList(this.environment.getActiveProfiles()), propertySources);
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) PlaceholdersResolver(org.springframework.boot.context.properties.bind.PlaceholdersResolver) PropertySourcesPlaceholdersResolver(org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver) ArrayList(java.util.ArrayList)

Example 14 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project CzechIdMng by bcvsolutions.

the class DefaultConfigurationService method getAllProperties.

private static Map<String, Object> getAllProperties(PropertySource<?> aPropSource) {
    Map<String, Object> result = new HashMap<>();
    if (aPropSource instanceof CompositePropertySource) {
        CompositePropertySource cps = (CompositePropertySource) aPropSource;
        cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps)));
        return result;
    }
    if (aPropSource instanceof EnumerablePropertySource<?>) {
        EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource;
        Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key)));
        return result;
    }
    return result;
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) CompositePropertySource(org.springframework.core.env.CompositePropertySource) HashMap(java.util.HashMap) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString)

Example 15 with EnumerablePropertySource

use of org.springframework.core.env.EnumerablePropertySource in project incubator-dubbo-spring-boot-project by apache.

the class EnvironmentUtils method doExtraProperties.

// /**
// * Gets {@link PropertySource} Map , the {@link PropertySource#getName()} as key
// *
// * @param environment {@link ConfigurableEnvironment}
// * @return Read-only Map
// */
// public static Map<String, PropertySource<?>> getPropertySources(ConfigurableEnvironment environment) {
// return Collections.unmodifiableMap(doGetPropertySources(environment));
// }
private static Map<String, Object> doExtraProperties(ConfigurableEnvironment environment) {
    // orderly
    Map<String, Object> properties = new LinkedHashMap<>();
    Map<String, PropertySource<?>> map = doGetPropertySources(environment);
    for (PropertySource<?> source : map.values()) {
        if (source instanceof EnumerablePropertySource) {
            EnumerablePropertySource propertySource = (EnumerablePropertySource) source;
            String[] propertyNames = propertySource.getPropertyNames();
            if (ObjectUtils.isEmpty(propertyNames)) {
                continue;
            }
            for (String propertyName : propertyNames) {
                if (!properties.containsKey(propertyName)) {
                    // put If absent
                    properties.put(propertyName, propertySource.getProperty(propertyName));
                }
            }
        }
    }
    return properties;
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) LinkedHashMap(java.util.LinkedHashMap) CompositePropertySource(org.springframework.core.env.CompositePropertySource) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource)

Aggregations

EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)22 CompositePropertySource (org.springframework.core.env.CompositePropertySource)8 MutablePropertySources (org.springframework.core.env.MutablePropertySources)7 PropertySource (org.springframework.core.env.PropertySource)7 HashMap (java.util.HashMap)5 LinkedHashMap (java.util.LinkedHashMap)4 Environment (org.springframework.core.env.Environment)4 Map (java.util.Map)3 Properties (java.util.Properties)3 Test (org.junit.jupiter.api.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Collectors (java.util.stream.Collectors)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 MapPropertySource (org.springframework.core.env.MapPropertySource)2