From cb95ee0650d3bbc3fe619af0a687f09d39a0a07f Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Wed, 15 May 2019 10:21:41 +0200 Subject: Refactor ConfigProfileGenerator - Add Source for Code Snippet. - Break down generate() into smaller chunks. --- .../moazs/preprocess/ConfigProfileGenerator.java | 45 ++++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'src/main/java/at/gv/egiz/moazs/preprocess') diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java index a0cf766..21d97fc 100644 --- a/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java +++ b/src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java @@ -2,10 +2,7 @@ package at.gv.egiz.moazs.preprocess; import at.gv.zustellung.app2mzs.xsd.ConfigType; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.env.AbstractEnvironment; -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.Environment; -import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.*; import org.springframework.stereotype.Component; import java.util.Arrays; @@ -13,6 +10,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Stream; import java.util.stream.StreamSupport; import static at.gv.zustellung.app2mzs.xsd.ConfigType.configTypeBuilder; @@ -27,33 +25,40 @@ public class ConfigProfileGenerator { private static final String TNVZ_REQUEST_KEY = "perform-query-person-request"; private static final String MSG_URL_KEY = "msg.url"; - private final Environment env; + private final ConfigurableEnvironment env; private final ConfigProfileMerger merger; @Autowired - public ConfigProfileGenerator(Environment env, ConfigProfileMerger merger) { + public ConfigProfileGenerator(ConfigurableEnvironment env, ConfigProfileMerger merger) { this.env = env; this.merger = merger; } public Map generate() { - MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); - Map> groupedKeys = StreamSupport.stream(propSrcs.spliterator(), false) - .filter(ps -> ps instanceof EnumerablePropertySource) - .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) - .flatMap(Arrays::stream) + var groupedKeys = getStreamOfPropertyKeysFromEnv() .filter(this::isConfigurationProfileProperty) .map(this::removePrefix) .collect(groupingBy(this::keepPrefix, mapping(this::removePrefix, toSet()))); - Map profiles = groupedKeys.entrySet().stream() - .collect(toMap(Entry::getKey, this::createConfig)); + var profiles = groupedKeys.entrySet().stream() + .collect(toMap(Entry::getKey, this::createConfigFromEnv)); var defaultProfile = profiles.get("default"); - return profiles.entrySet().stream() - .collect(toUnmodifiableMap(Entry::getKey, e -> merger.merge(e.getValue(), defaultProfile))); + return mergeProfiles(profiles, defaultProfile); + } + + // @author pedorro + // inspired by https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object/42521523#42521523 + private Stream getStreamOfPropertyKeysFromEnv() { + + MutablePropertySources propSrcs = env.getPropertySources(); + + return StreamSupport.stream(propSrcs.spliterator(), false) + .filter(ps -> ps instanceof EnumerablePropertySource) + .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) + .flatMap(Arrays::stream); } private boolean isConfigurationProfileProperty(String propName) { @@ -68,7 +73,7 @@ public class ConfigProfileGenerator { return name.substring(name.indexOf('.') + 1); } - private ConfigType createConfig(Entry> entry) { + private ConfigType createConfigFromEnv(Entry> entry) { var profile = entry.getKey(); @@ -94,4 +99,12 @@ public class ConfigProfileGenerator { .build(); } + private Map mergeProfiles(Map profiles, ConfigType defaultProfile) { + return profiles.entrySet().stream() + .collect(toUnmodifiableMap( + Entry::getKey, + e -> merger.merge(e.getValue(), defaultProfile))); + } + + } -- cgit v1.2.3