From f193a2165d9261c2bd3413339c954a8bba829a93 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Thu, 16 May 2019 10:01:43 +0200 Subject: Refactor and Test ConfigProfileGenerator Refactor - Add Builder to ConfigProfileGenerator. Reason: Constructor had too many arguments. - Move Conversion from Map to Config from ConfigProfileGenerator into dedicated 'ConvertMapToConfig' Class; Reason: I expect additional configuration properties and I don't want those changes to affect the ConfigProfileGenerator (or it's test cases) - Move Access to Spring's env into facade to simplify ConfigProfileGenerator. --- .../moazs/preprocess/SpringPropertiesFacade.java | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/at/gv/egiz/moazs/preprocess/SpringPropertiesFacade.java (limited to 'src/main/java/at/gv/egiz/moazs/preprocess/SpringPropertiesFacade.java') diff --git a/src/main/java/at/gv/egiz/moazs/preprocess/SpringPropertiesFacade.java b/src/main/java/at/gv/egiz/moazs/preprocess/SpringPropertiesFacade.java new file mode 100644 index 0000000..4cce7e0 --- /dev/null +++ b/src/main/java/at/gv/egiz/moazs/preprocess/SpringPropertiesFacade.java @@ -0,0 +1,39 @@ +package at.gv.egiz.moazs.preprocess; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +@Component +public class SpringPropertiesFacade { + + @Autowired + private ConfigurableEnvironment env; + + /** + * Collect all properties from Spring environment and return them as a stream. + * + * inspired by https://stackoverflow.com/questions/23506471/spring-access-all-environment-properties-as-a-map-or-properties-object/42521523#42521523 + * @author pedorro + * @return + */ + public Stream getPropertyNames() { + + MutablePropertySources propSrcs = env.getPropertySources(); + + return StreamSupport.stream(propSrcs.spliterator(), false) + .filter(ps -> ps instanceof EnumerablePropertySource) + .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames()) + .flatMap(Arrays::stream); + } + + public String getProperty(String name) { + return env.getProperty(name); + } +} -- cgit v1.2.3