From 154f1609f8825a09f569b7187acca8cdb42732c3 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Tue, 12 Apr 2022 17:28:16 +0200 Subject: refact(config): split IConfigurationWithSP into two interfaces --- .../config/BasicSpringBootConfigurationImpl.java | 174 +++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/config/BasicSpringBootConfigurationImpl.java (limited to 'eaaf_core_utils/src/main') diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/config/BasicSpringBootConfigurationImpl.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/config/BasicSpringBootConfigurationImpl.java new file mode 100644 index 00000000..8f3c558c --- /dev/null +++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/config/BasicSpringBootConfigurationImpl.java @@ -0,0 +1,174 @@ +package at.gv.egiz.eaaf.core.impl.config; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; + +import at.gv.egiz.eaaf.core.api.idp.IExtendedConfiguration; +import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; +import lombok.extern.slf4j.Slf4j; + +/** + * Basic Spring based configuration implementation. + * + * @author tlenz + * + */ +@Slf4j +public abstract class BasicSpringBootConfigurationImpl implements IExtendedConfiguration { + + public static final String PROP_CONFIG_ROOT_DIR = "core.configRootDir"; + + @Autowired + private Environment env; + + @Override + public String getBasicConfiguration(final String key) { + if (StringUtils.isNotEmpty(key)) { + final String value = env.getProperty(addPrefixToKey(key)); + if (value != null) { + return value.trim(); + } + } + + return null; + } + + @Override + public String getBasicConfiguration(final String key, final String defaultValue) { + if (StringUtils.isNotEmpty(key)) { + final String value = env.getProperty(addPrefixToKey(key), defaultValue); + if (value != null) { + return value.trim(); + } + } + + return defaultValue; + } + + @Override + public Map getBasicConfigurationWithPrefix(final String prefix) { + final Map configProps = getPropertiesStartingWith((ConfigurableEnvironment) env, + addPrefixToKey(prefix)); + return KeyValueUtils.removePrefixFromKeys(configProps, addPrefixToKey(prefix) + "."); + + } + + @Override + public boolean getBasicConfigurationBoolean(final String key) { + return getBasicConfigurationBoolean(key, false); + + } + + @Override + public boolean getBasicConfigurationBoolean(final String key, final boolean defaultValue) { + final String value = getBasicConfiguration(key); + if (StringUtils.isNotEmpty(value)) { + return Boolean.valueOf(value.trim()); + } else { + return defaultValue; + } + } + + @Override + public URI getConfigurationRootDirectory() { + try { + return new URI(env.getRequiredProperty(addPrefixToKey(PROP_CONFIG_ROOT_DIR))); + + } catch (IllegalStateException | URISyntaxException e) { + log.warn("ConfigRootDirectory is NOT set", e); + return null; + + } + } + + /** + * Get a specific configuration-key prefix for this software implementation. + * + * @return + */ + public abstract String getApplicationSpecificKeyPrefix(); + + protected String addPrefixToKey(final String key) { + if (StringUtils.isNotEmpty(getApplicationSpecificKeyPrefix())) { + if (getApplicationSpecificKeyPrefix().endsWith(KeyValueUtils.KEY_DELIMITER)) { + return getApplicationSpecificKeyPrefix() + key; + } else { + return getApplicationSpecificKeyPrefix() + KeyValueUtils.KEY_DELIMITER + key; + } + + } + + return key; + } + + private static Map getPropertiesStartingWith(final ConfigurableEnvironment aenv, + final String akeyPrefix) { + final Map result = new HashMap<>(); + final Map map = getAllProperties(aenv); + + for (final Entry entry : map.entrySet()) { + final String key = entry.getKey(); + + if (key.startsWith(akeyPrefix)) { + result.put(key, (String) entry.getValue()); + } + } + + return result; + } + + private static Map getAllProperties(final ConfigurableEnvironment aenv) { + final Map result = new HashMap<>(); + aenv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); + return result; + + } + + private static Map getAllProperties(final PropertySource apropSource) { + final Map result = new HashMap<>(); + + if (apropSource instanceof CompositePropertySource) { + final CompositePropertySource cps = (CompositePropertySource) apropSource; + cps.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); + return result; + } + + if (apropSource instanceof EnumerablePropertySource) { + final EnumerablePropertySource ps = (EnumerablePropertySource) apropSource; + Arrays.asList(ps.getPropertyNames()).forEach(key -> result.put(key, ps.getProperty(key))); + return result; + } + + // note: Most descendants of PropertySource are EnumerablePropertySource. There + // are some + // few others like JndiPropertySource or StubPropertySource + log.trace("Given PropertySource is instanceof " + apropSource.getClass().getName() + + " and cannot be iterated"); + + return result; + + } + + private static void addAll(final Map abase, final Map atoBeAdded) { + for (final Entry entry : atoBeAdded.entrySet()) { + if (abase.containsKey(entry.getKey())) { + continue; + } + + abase.put(entry.getKey(), entry.getValue()); + } + } + +} -- cgit v1.2.3