diff options
Diffstat (limited to 'src/main/java/at')
-rw-r--r-- | src/main/java/at/gv/egiz/moazs/preprocess/ConfigProfileGenerator.java | 45 |
1 files changed, 29 insertions, 16 deletions
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<String, ConfigType> generate() { - MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); - Map<String, Set<String>> 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<String, ConfigType> 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<String> 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<String, Set<String>> entry) { + private ConfigType createConfigFromEnv(Entry<String, Set<String>> entry) { var profile = entry.getKey(); @@ -94,4 +99,12 @@ public class ConfigProfileGenerator { .build(); } + private Map<String, ConfigType> mergeProfiles(Map<String, ConfigType> profiles, ConfigType defaultProfile) { + return profiles.entrySet().stream() + .collect(toUnmodifiableMap( + Entry::getKey, + e -> merger.merge(e.getValue(), defaultProfile))); + } + + } |