Search in sources :

Example 16 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class EnvironmentController method convertToProperties.

private Map<String, Object> convertToProperties(Environment profiles) {
    // Map of unique keys containing full map of properties for each unique
    // key
    Map<String, Map<String, Object>> map = new LinkedHashMap<>();
    List<PropertySource> sources = new ArrayList<>(profiles.getPropertySources());
    Collections.reverse(sources);
    Map<String, Object> combinedMap = new TreeMap<>();
    for (PropertySource source : sources) {
        @SuppressWarnings("unchecked") Map<String, Object> value = (Map<String, Object>) source.getSource();
        for (String key : value.keySet()) {
            if (!key.contains("[")) {
                // Not an array, add unique key to the map
                combinedMap.put(key, value.get(key));
            } else {
                // An existing array might have already been added to the property map
                // of an unequal size to the current array. Replace the array key in
                // the current map.
                key = key.substring(0, key.indexOf("["));
                Map<String, Object> filtered = new TreeMap<>();
                for (String index : value.keySet()) {
                    if (index.startsWith(key + "[")) {
                        filtered.put(index, value.get(index));
                    }
                }
                map.put(key, filtered);
            }
        }
    }
    // Combine all unique keys for array values into the combined map
    for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
        combinedMap.putAll(entry.getValue());
    }
    postProcessProperties(combinedMap);
    return combinedMap;
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 17 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class EnvironmentRepositoryPropertySourceLocator method locate.

@Override
public org.springframework.core.env.PropertySource<?> locate(Environment environment) {
    CompositePropertySource composite = new CompositePropertySource("configService");
    for (PropertySource source : repository.findOne(name, profiles, label).getPropertySources()) {
        @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource();
        composite.addPropertySource(new MapPropertySource(source.getName(), map));
    }
    return composite;
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) Map(java.util.Map) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 18 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class CipherEnvironmentEncryptor method decrypt.

private Environment decrypt(Environment environment, TextEncryptorLocator encryptor) {
    Environment result = new Environment(environment);
    for (PropertySource source : environment.getPropertySources()) {
        Map<Object, Object> map = new LinkedHashMap<Object, Object>(source.getSource());
        for (Map.Entry<Object, Object> entry : new LinkedHashSet<>(map.entrySet())) {
            Object key = entry.getKey();
            String name = key.toString();
            String value = entry.getValue().toString();
            if (value.startsWith("{cipher}")) {
                map.remove(key);
                try {
                    value = value.substring("{cipher}".length());
                    value = encryptor.locate(this.helper.getEncryptorKeys(name, StringUtils.arrayToCommaDelimitedString(environment.getProfiles()), value)).decrypt(this.helper.stripPrefix(value));
                } catch (Exception e) {
                    value = "<n/a>";
                    name = "invalid." + name;
                    String message = "Cannot decrypt key: " + key + " (" + e.getClass() + ": " + e.getMessage() + ")";
                    if (logger.isDebugEnabled()) {
                        logger.debug(message, e);
                    } else if (logger.isWarnEnabled()) {
                        logger.warn(message);
                    }
                }
                map.put(name, value);
            }
        }
        result.add(new PropertySource(source.getName(), map));
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Environment(org.springframework.cloud.config.environment.Environment) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) PropertySource(org.springframework.cloud.config.environment.PropertySource) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class NativeEnvironmentRepository method clean.

protected Environment clean(Environment value) {
    Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel(), this.version, value.getState());
    for (PropertySource source : value.getPropertySources()) {
        String name = source.getName();
        if (this.environment.getPropertySources().contains(name)) {
            continue;
        }
        name = name.replace("applicationConfig: [", "");
        name = name.replace("]", "");
        if (this.searchLocations != null) {
            boolean matches = false;
            String normal = name;
            if (normal.startsWith("file:")) {
                normal = StringUtils.cleanPath(new File(normal.substring("file:".length())).getAbsolutePath());
            }
            String profile = result.getProfiles() == null ? null : StringUtils.arrayToCommaDelimitedString(result.getProfiles());
            for (String pattern : getLocations(result.getName(), profile, result.getLabel()).getLocations()) {
                if (!pattern.contains(":")) {
                    pattern = "file:" + pattern;
                }
                if (pattern.startsWith("file:")) {
                    pattern = StringUtils.cleanPath(new File(pattern.substring("file:".length())).getAbsolutePath()) + "/";
                }
                if (logger.isTraceEnabled()) {
                    logger.trace("Testing pattern: " + pattern + " with property source: " + name);
                }
                if (normal.startsWith(pattern) && !normal.substring(pattern.length()).contains("/")) {
                    matches = true;
                    break;
                }
            }
            if (!matches) {
                // Don't include this one: it wasn't matched by our search locations
                if (logger.isDebugEnabled()) {
                    logger.debug("Not adding property source: " + name);
                }
                continue;
            }
        }
        logger.info("Adding property source: " + name);
        result.add(new PropertySource(name, source.getSource()));
    }
    return result;
}
Also used : StandardEnvironment(org.springframework.core.env.StandardEnvironment) Environment(org.springframework.cloud.config.environment.Environment) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) File(java.io.File) MapPropertySource(org.springframework.core.env.MapPropertySource) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Example 20 with PropertySource

use of org.springframework.cloud.config.environment.PropertySource in project spring-cloud-config by spring-cloud.

the class EnvironmentControllerTests method whenPlaceholders.

private void whenPlaceholders() {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("foo", "bar");
    this.environment.add(new PropertySource("one", map));
    this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
    Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) PropertySource(org.springframework.cloud.config.environment.PropertySource)

Aggregations

PropertySource (org.springframework.cloud.config.environment.PropertySource)33 Test (org.junit.Test)20 LinkedHashMap (java.util.LinkedHashMap)19 Environment (org.springframework.cloud.config.environment.Environment)12 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 MapPropertySource (org.springframework.core.env.MapPropertySource)4 CompositePropertySource (org.springframework.core.env.CompositePropertySource)3 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)1 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 YamlPropertiesFactoryBean (org.springframework.beans.factory.config.YamlPropertiesFactoryBean)1 EnvironmentRepository (org.springframework.cloud.config.server.environment.EnvironmentRepository)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1