diff options
Diffstat (limited to 'eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java')
-rw-r--r-- | eaaf_core/src/main/java/at/gv/egiz/eaaf/core/impl/idp/conf/AbstractConfigurationImpl.java | 437 |
1 files changed, 222 insertions, 215 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 55662326..e661a2a8 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 @@ -1,29 +1,22 @@ -/******************************************************************************* - * Copyright 2017 Graz University of Technology - * EAAF-Core Components has been developed in a cooperation between EGIZ, - * A-SIT Plus, A-SIT, and Graz University of Technology. +/* + * Copyright 2017 Graz University of Technology EAAF-Core Components has been developed in a + * cooperation between EGIZ, A-SIT Plus, A-SIT, and Graz University of Technology. * - * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by - * the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: + * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European + * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in + * compliance with the Licence. You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - * - * This product combines work with different licenses. See the "NOTICE" text - * file for details on the various modules and licenses. - * The "NOTICE" text file is part of the distribution. Any derivative works - * that you distribute must include a readable copy of the "NOTICE" text file. - *******************************************************************************/ -/******************************************************************************* - *******************************************************************************/ -/******************************************************************************* - *******************************************************************************/ + * Unless required by applicable law or agreed to in writing, software distributed under the Licence + * is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the Licence for the specific language governing permissions and limitations under + * the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text file for details on the + * various modules and licenses. The "NOTICE" text file is part of the distribution. Any derivative + * works that you distribute must include a readable copy of the "NOTICE" text file. +*/ + package at.gv.egiz.eaaf.core.impl.idp.conf; import java.io.File; @@ -34,201 +27,215 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Map; import java.util.Properties; - +import at.gv.egiz.eaaf.core.api.idp.IExtendedConfiguration; +import at.gv.egiz.eaaf.core.exceptions.EaafConfigurationException; +import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; 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.exceptions.EAAFConfigurationException; -import at.gv.egiz.eaaf.core.impl.utils.KeyValueUtils; - public abstract class AbstractConfigurationImpl implements IExtendedConfiguration { - private static final Logger log = LoggerFactory.getLogger(AbstractConfigurationImpl.class); - - private static final String URI_SCHEME_CLASSPATH = "classpath"; - private static final String URI_SCHEME_FILESYSTEM = "file"; - - private final URI internalConfigPath; - private final URI configRootDirectory; - private final Properties properties; - - public AbstractConfigurationImpl(final String configPath) throws EAAFConfigurationException { - InputStream is = null; - try { - log.debug("Starting EAAFCore initialization process .... "); - - if (StringUtils.isEmpty(configPath)) { - log.debug("Primary configuration is empty. Search for backup configuration .... "); - final String backupConfigPath = getBackupConfigPath(); - if (StringUtils.isEmpty(backupConfigPath)) { - log.error("No configuration file found."); - throw new EAAFConfigurationException("config.01", null); - - } - - internalConfigPath = new URI(getBackupConfigPath()); - - } else - internalConfigPath = new URI(configPath); - - log.info("Load EAAFCore configuration from " + internalConfigPath); - - - //extract configuration root directory - //TODO: check if it works with classpath - final File propertiesFile = new File(internalConfigPath); - if (!propertiesFile.exists()) { - log.error("Configuration file: " + internalConfigPath + " is NOT found on filesystem"); - throw new EAAFConfigurationException("config.18", null); - - } - - final String configDir = propertiesFile.getParent(); - configRootDirectory = new File(configDir).toURI(); - log.debug("Set EAAFCore configuration root directory to " + configRootDirectory.toString()); - - - //get input stream from configuration path - if (internalConfigPath.getScheme().equals(URI_SCHEME_FILESYSTEM)) { - log.trace("Load config from filesystem"); - is = new FileInputStream(propertiesFile); - - } else if (internalConfigPath.getScheme().equals(URI_SCHEME_CLASSPATH)) { - log.trace("Load config from classpath"); - is = this.getClass().getResourceAsStream(internalConfigPath.toString()); - - } else { - log.error("Can not load EAAFCore configuration. Unsupported prefix! (Only 'file:' and 'classpath:') "); - throw new EAAFConfigurationException("config.24", new Object[] {internalConfigPath, "'file:'"}); - - } - - if (is == null) { - log.error("Can NOT load EAAFCore configuration from file " + internalConfigPath.toString()); - throw new EAAFConfigurationException("config.03", - new Object[] {internalConfigPath.toString()}); - - } - - - //load EAAF core configuration into properties object - properties = new Properties(); - properties.load(is); - - log.info("EAAFCore configuration loaded"); - - } catch (URISyntaxException | IOException e) { - log.error("Can not parse configuration path " + configPath + " or " + getBackupConfigPath()); - throw new EAAFConfigurationException("config.03", new Object[]{configPath + " or " + getBackupConfigPath()}, e); - - } finally { - if (is != null) { - try { - is.close(); - - } catch (final IOException e) { - log.warn("Can not close inputstream from configuration loader!"); - - } - } - } - - } - - @Override - public String getBasicConfiguration(String key) { - if (StringUtils.isNotEmpty(key)) { - final String value = properties.getProperty(addPrefixToKey(key)); - if (value != null) - return value.trim(); - } - - return null; - } - - @Override - public String getBasicConfiguration(String key, String defaultValue) { - if (StringUtils.isNotEmpty(key)) { - final String value = properties.getProperty(addPrefixToKey(key), defaultValue); - if (value != null) - return value.trim(); - } - - return defaultValue; - } - - - @Override - public Boolean getBasicConfigurationBoolean(String key) { - final String value = getBasicConfiguration(key); - if (value != null) - return Boolean.parseBoolean(value); - else - return null; - - } - - - @Override - public boolean getBasicConfigurationBoolean(String key, boolean defaultValue) { - final Boolean result = getBasicConfigurationBoolean(key); - if (result != null) - return result; - else - return defaultValue; - - } - - @Override - public Map<String, String> getBasicConfigurationWithPrefix(String prefix) { - return KeyValueUtils.getSubSetWithPrefix(KeyValueUtils.convertPropertiesToMap(properties), addPrefixToKey(prefix)); - - } - - @Override - public Properties getFullConfigurationProperties() { - return properties; - - } - - @Override - public URI getConfigurationRootDirectory() { - return configRootDirectory; - - } - - @Override - public URI getConfigurationFilePath() { - return internalConfigPath; - - } - - /** - * Get the path to backup configuration - * - * @return A filepath file: or a classpath classpath: - */ - abstract protected String getBackupConfigPath(); - - /** - * Get a specific configuration-key prefix for this software implementation - * - * @return - */ - abstract public String getApplicationSpecificKeyPrefix(); - - - private String addPrefixToKey(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 final Logger log = LoggerFactory.getLogger(AbstractConfigurationImpl.class); + + private static final String URI_SCHEME_CLASSPATH = "classpath"; + private static final String URI_SCHEME_FILESYSTEM = "file"; + + private final URI internalConfigPath; + private final URI configRootDirectory; + private final Properties properties; + + /** + * Basic configuration loader implementation. + * + * @param configPath Path to configuration + * @throws EaafConfigurationException In case of a configuration error + */ + public AbstractConfigurationImpl(final String configPath) throws EaafConfigurationException { + InputStream is = null; + try { + log.debug("Starting EAAFCore initialization process .... "); + + if (StringUtils.isEmpty(configPath)) { + log.debug("Primary configuration is empty. Search for backup configuration .... "); + final String backupConfigPath = getBackupConfigPath(); + if (StringUtils.isEmpty(backupConfigPath)) { + log.error("No configuration file found."); + throw new EaafConfigurationException("config.01", null); + + } + + internalConfigPath = new URI(getBackupConfigPath()); + + } else { + internalConfigPath = new URI(configPath); + } + + log.info("Load EAAFCore configuration from " + internalConfigPath); + + + // extract configuration root directory + // TODO: check if it works with classpath + final File propertiesFile = new File(internalConfigPath); + if (!propertiesFile.exists()) { + log.error("Configuration file: " + internalConfigPath + " is NOT found on filesystem"); + throw new EaafConfigurationException("config.18", null); + + } + + final String configDir = propertiesFile.getParent(); + configRootDirectory = new File(configDir).toURI(); + log.debug("Set EAAFCore configuration root directory to " + configRootDirectory.toString()); + + + // get input stream from configuration path + if (internalConfigPath.getScheme().equals(URI_SCHEME_FILESYSTEM)) { + log.trace("Load config from filesystem"); + is = new FileInputStream(propertiesFile); + + } else if (internalConfigPath.getScheme().equals(URI_SCHEME_CLASSPATH)) { + log.trace("Load config from classpath"); + is = this.getClass().getResourceAsStream(internalConfigPath.toString()); + + } else { + log.error( + "Can not load EAAFCore configuration. Unsupported prefix! (Only 'file:' and 'classpath:') "); + throw new EaafConfigurationException("config.24", + new Object[] {internalConfigPath, "'file:'"}); + + } + + if (is == null) { + log.error("Can NOT load EAAFCore configuration from file " + internalConfigPath.toString()); + throw new EaafConfigurationException("config.03", + new Object[] {internalConfigPath.toString()}); + + } + + + // load EAAF core configuration into properties object + properties = new Properties(); + properties.load(is); + + log.info("EAAFCore configuration loaded"); + + } catch (URISyntaxException | IOException e) { + log.error("Can not parse configuration path " + configPath + " or " + getBackupConfigPath()); + throw new EaafConfigurationException("config.03", + new Object[] {configPath + " or " + getBackupConfigPath()}, e); + + } finally { + if (is != null) { + try { + is.close(); + + } catch (final IOException e) { + log.warn("Can not close inputstream from configuration loader!"); + + } + } + } + + } + + @Override + public String getBasicConfiguration(final String key) { + if (StringUtils.isNotEmpty(key)) { + final String value = properties.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 = properties.getProperty(addPrefixToKey(key), defaultValue); + if (value != null) { + return value.trim(); + } + } + + return defaultValue; + } + + + @Override + public boolean getBasicConfigurationBoolean(final String key) { + final String value = getBasicConfiguration(key); + if (value != null) { + return Boolean.parseBoolean(value); + } else { + return false; + } + + } + + + @Override + public boolean getBasicConfigurationBoolean(final String key, final boolean defaultValue) { + final Boolean result = getBasicConfigurationBoolean(key); + if (result != null) { + return result; + } else { + return defaultValue; + } + + } + + @Override + public Map<String, String> getBasicConfigurationWithPrefix(final String prefix) { + return KeyValueUtils.getSubSetWithPrefix(KeyValueUtils.convertPropertiesToMap(properties), + addPrefixToKey(prefix)); + + } + + @Override + public Properties getFullConfigurationProperties() { + return properties; + + } + + @Override + public URI getConfigurationRootDirectory() { + return configRootDirectory; + + } + + @Override + public URI getConfigurationFilePath() { + return internalConfigPath; + + } + + /** + * Get the path to backup configuration. + * + * @return A filepath file: or a classpath classpath: + */ + 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; + + } } |