diff options
6 files changed, 228 insertions, 195 deletions
diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java index d279446b..8aa29e94 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java @@ -32,11 +32,11 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.gv.egiz.eaaf.core.api.idp.IExtendedConfiguration; +import at.gv.egiz.eaaf.core.api.idp.IExtendedConfigurationWithSP; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; -public abstract class AbstractConfigurationImpl implements IExtendedConfiguration { +public abstract class AbstractConfigurationImpl implements IExtendedConfigurationWithSP { private static final Logger log = LoggerFactory.getLogger(AbstractConfigurationImpl.class); private static final String URI_SCHEME_CLASSPATH = "classpath"; diff --git a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractSpringBootConfigurationImpl.java b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractSpringBootConfigurationImpl.java index 708ef399..ea2cfcd6 100644 --- a/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractSpringBootConfigurationImpl.java +++ b/eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractSpringBootConfigurationImpl.java @@ -19,36 +19,20 @@ package at.gv.egiz.eaaf.core.impl.idp.conf; -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 javax.annotation.PostConstruct; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -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.IConfigurationWithSP; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; -import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; - -public abstract class AbstractSpringBootConfigurationImpl implements IConfigurationWithSP { - private static final Logger log = LoggerFactory.getLogger(AbstractSpringBootConfigurationImpl.class); - - @Autowired - private Environment env; +import at.gv.egiz.eaaf.core.impl.config.BasicSpringBootConfigurationImpl; - public static final String PROP_CONFIG_ROOT_DIR = "core.configRootDir"; +/** + * Basic implementation with SP functionality. + * + * @author tlenz + * + */ +public abstract class AbstractSpringBootConfigurationImpl extends BasicSpringBootConfigurationImpl + implements IConfigurationWithSP { @PostConstruct private void initialize() throws EaafConfigurationException { @@ -57,70 +41,8 @@ public abstract class AbstractSpringBootConfigurationImpl implements IConfigurat PROP_CONFIG_ROOT_DIR) }); } - - } - - @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<String, String> getBasicConfigurationWithPrefix(final String prefix) { - final Map<String, String> 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 the path to backup configuration. * @@ -128,82 +50,6 @@ public abstract class AbstractSpringBootConfigurationImpl implements IConfigurat */ protected abstract String getBackupConfigPath(); - /** - * Get a specific configuration-key prefix for this software implementation. - * - * @return - */ - public abstract String getApplicationSpecificKeyPrefix(); - - private 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<String, String> getPropertiesStartingWith(final ConfigurableEnvironment aenv, - final String akeyPrefix) { - final Map<String, String> result = new HashMap<>(); - final Map<String, Object> map = getAllProperties(aenv); - - for (final Entry<String, Object> entry : map.entrySet()) { - final String key = entry.getKey(); - - if (key.startsWith(akeyPrefix)) { - result.put(key, (String) entry.getValue()); - } - } - - return result; - } - private static Map<String, Object> getAllProperties(final ConfigurableEnvironment aenv) { - final Map<String, Object> result = new HashMap<>(); - aenv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); - return result; - - } - - private static Map<String, Object> getAllProperties(final PropertySource<?> apropSource) { - final Map<String, Object> 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<String, Object> abase, final Map<String, Object> atoBeAdded) { - for (final Entry<String, Object> entry : atoBeAdded.entrySet()) { - if (abase.containsKey(entry.getKey())) { - continue; - } - - abase.put(entry.getKey(), entry.getValue()); - } - } + } diff --git a/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IConfigurationWithSP.java b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IConfigurationWithSP.java index cf8867b3..a38f4ec1 100644 --- a/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IConfigurationWithSP.java +++ b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IConfigurationWithSP.java @@ -1,14 +1,13 @@ package at.gv.egiz.eaaf.core.api.idp; import java.net.URL; -import java.util.Map; import javax.annotation.Nullable; import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; import at.gv.egiz.eaaf.core.exceptions.EaafException; -public interface IConfigurationWithSP extends IConfiguration { +public interface IConfigurationWithSP extends IExtendedConfiguration { String CONFIG_PROPS_AUTH_DEFAULT_COUNTRYCODE = "configuration.auth.default.countrycode"; @@ -42,17 +41,6 @@ public interface IConfigurationWithSP extends IConfiguration { throws EaafConfigurationException; /** - * Get a set of configuration values from file based configuration that starts - * with this prefix. <br> - * <br> - * <b>Important:</b> The configuration values must be of type String! - * - * @param prefix Prefix of the configuration key - * @return Map String/String without prefix, but never null - */ - Map<String, String> getBasicConfigurationWithPrefix(String prefix); - - /** * Validate a URL if it it is allowed by configuration. * * @param authReqUrl URL for validation diff --git a/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfiguration.java b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfiguration.java index ec09995d..fb1c9d04 100644 --- a/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfiguration.java +++ b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfiguration.java @@ -1,24 +1,25 @@ package at.gv.egiz.eaaf.core.api.idp; -import java.net.URI; -import java.util.Properties; +import java.util.Map; -public interface IExtendedConfiguration extends IConfigurationWithSP { +/** + * {@link IConfiguration} with Key/Value pre-fix loader. + * + * @author tlenz + * + */ +public interface IExtendedConfiguration extends IConfiguration { /** - * Get the full configuration properties object. + * Get a set of configuration values from file based configuration that starts + * with this prefix. <br> + * <br> + * <b>Important:</b> The configuration values must be of type String! * - * @return + * @param prefix Prefix of the configuration key + * @return Map String/String without prefix, but never null */ - @Deprecated - Properties getFullConfigurationProperties(); - - /** - * Get the path to EAAFCore configuration that is internally used. - * - * @return - */ - @Deprecated - URI getConfigurationFilePath(); + Map<String, String> getBasicConfigurationWithPrefix(String prefix); + } diff --git a/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfigurationWithSP.java b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfigurationWithSP.java new file mode 100644 index 00000000..0e867c7e --- /dev/null +++ b/eaaf_core_api/src/main/java/at/gv/egiz/eaaf/core/api/idp/IExtendedConfigurationWithSP.java @@ -0,0 +1,24 @@ +package at.gv.egiz.eaaf.core.api.idp; + +import java.net.URI; +import java.util.Properties; + +public interface IExtendedConfigurationWithSP extends IConfigurationWithSP { + + /** + * Get the full configuration properties object. + * + * @return + */ + @Deprecated + Properties getFullConfigurationProperties(); + + /** + * Get the path to EAAFCore configuration that is internally used. + * + * @return + */ + @Deprecated + URI getConfigurationFilePath(); + +} 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<String, String> getBasicConfigurationWithPrefix(final String prefix) { + final Map<String, String> 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<String, String> getPropertiesStartingWith(final ConfigurableEnvironment aenv, + final String akeyPrefix) { + final Map<String, String> result = new HashMap<>(); + final Map<String, Object> map = getAllProperties(aenv); + + for (final Entry<String, Object> entry : map.entrySet()) { + final String key = entry.getKey(); + + if (key.startsWith(akeyPrefix)) { + result.put(key, (String) entry.getValue()); + } + } + + return result; + } + + private static Map<String, Object> getAllProperties(final ConfigurableEnvironment aenv) { + final Map<String, Object> result = new HashMap<>(); + aenv.getPropertySources().forEach(ps -> addAll(result, getAllProperties(ps))); + return result; + + } + + private static Map<String, Object> getAllProperties(final PropertySource<?> apropSource) { + final Map<String, Object> 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<String, Object> abase, final Map<String, Object> atoBeAdded) { + for (final Entry<String, Object> entry : atoBeAdded.entrySet()) { + if (abase.containsKey(entry.getKey())) { + continue; + } + + abase.put(entry.getKey(), entry.getValue()); + } + } + +} |