diff --git a/common/src/main/java/com/kumuluz/ee/configuration/ConfigurationSource.java b/common/src/main/java/com/kumuluz/ee/configuration/ConfigurationSource.java index 5d574360..760b5419 100644 --- a/common/src/main/java/com/kumuluz/ee/configuration/ConfigurationSource.java +++ b/common/src/main/java/com/kumuluz/ee/configuration/ConfigurationSource.java @@ -37,15 +37,77 @@ public interface ConfigurationSource { Optional get(String key); - Optional getBoolean(String key); + default Optional getBoolean(String key) { - Optional getInteger(String key); + Optional value = get(key); - Optional getLong(String key); + return value.map(Boolean::valueOf); + } + + default Optional getInteger(String key) { + + Optional value = get(key); + + if (value.isPresent()) { + + try { + return Optional.of(Integer.valueOf(value.get())); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } else { + return Optional.empty(); + } + } + + default Optional getLong(String key) { + + Optional value = get(key); + + if (value.isPresent()) { + + try { + return Optional.of(Long.valueOf(value.get())); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } else { + return Optional.empty(); + } + } + + default Optional getDouble(String key) { + + Optional value = get(key); + + if (value.isPresent()) { + + try { + return Optional.of(Double.valueOf(value.get())); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } else { + return Optional.empty(); + } + } + + default Optional getFloat(String key) { + + Optional value = get(key); - Optional getDouble(String key); + if (value.isPresent()) { - Optional getFloat(String key); + try { + return Optional.of(Float.valueOf(value.get())); + } catch (NumberFormatException e) { + return Optional.empty(); + } + + } else { + return Optional.empty(); + } + } Optional getListSize(String key); diff --git a/common/src/main/java/com/kumuluz/ee/configuration/sources/FileConfigurationSource.java b/common/src/main/java/com/kumuluz/ee/configuration/sources/FileConfigurationSource.java index ab44b0a3..d81edd5e 100644 --- a/common/src/main/java/com/kumuluz/ee/configuration/sources/FileConfigurationSource.java +++ b/common/src/main/java/com/kumuluz/ee/configuration/sources/FileConfigurationSource.java @@ -17,57 +17,39 @@ * out of or in connection with the software or the use or other dealings in the * software. See the License for the specific language governing permissions and * limitations under the License. -*/ + */ package com.kumuluz.ee.configuration.sources; import com.kumuluz.ee.configuration.ConfigurationSource; -import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher; -import com.kumuluz.ee.configuration.utils.ConfigurationSourceUtils; import com.kumuluz.ee.logs.LogDeferrer; -import org.yaml.snakeyaml.Yaml; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.*; import java.util.logging.Logger; -import java.util.stream.Collectors; /** - * @author Tilen Faganel - * @since 2.1.0 + * Base class for file configuration sources. + * + * @author Urban Malc + * @since 3.6.0 */ -public class FileConfigurationSource implements ConfigurationSource { - - private Logger log; - private LogDeferrer logDeferrer; - - private String ymlFileName; - private String yamlFileName; - private String propertiesFileName; - private String microProfilePropertiesFileName; - private Map config; - private Properties properties; +public abstract class FileConfigurationSource implements ConfigurationSource { - public FileConfigurationSource() { + protected Logger log; + protected LogDeferrer logDeferrer; - this.ymlFileName = "config.yml"; - this.yamlFileName = "config.yaml"; - this.propertiesFileName = "config.properties"; - this.microProfilePropertiesFileName = "META-INF/microprofile-config.properties"; + protected String filename; + protected int defaultOrdinal; - String configurationFileName = System.getProperty("com.kumuluz.ee.configuration.file"); + public FileConfigurationSource(String filename, int defaultOrdinal) { - if (configurationFileName != null && !configurationFileName.isEmpty()) { - this.ymlFileName = configurationFileName; - this.yamlFileName = configurationFileName; - this.propertiesFileName = configurationFileName; - } + this.filename = filename; + this.defaultOrdinal = defaultOrdinal; this.logDeferrer = new LogDeferrer<>(); - - this.logDeferrer.init(() -> Logger.getLogger(FileConfigurationSource.class.getName())); + this.logDeferrer.init(() -> Logger.getLogger(PropertiesConfigurationSource.class.getName())); } public void postInit() { @@ -78,355 +60,13 @@ public void postInit() { log = Logger.getLogger(FileConfigurationSource.class.getName()); } - @Override - @SuppressWarnings("unchecked") - public void init(ConfigurationDispatcher configurationDispatcher) { - - // read yaml file to Map - InputStream file; - Yaml yaml = new Yaml(); - try { - file = getClass().getClassLoader().getResourceAsStream(ymlFileName); - - if (file == null) { - file = getClass().getClassLoader().getResourceAsStream(yamlFileName); - } - - if (file == null) { - try { - file = Files.newInputStream(Paths.get(ymlFileName)); - } catch (IOException ignored) { - } - } - - if (file == null) { - try { - file = Files.newInputStream(Paths.get(yamlFileName)); - } catch (IOException ignored) { - } - } - - if (file != null) { - - logDeferrer.defer(l -> l.info("Loading configuration from YAML file.")); - - Object yamlParsed = yaml.load(file); - - if (yamlParsed instanceof Map) { - config = (Map) yamlParsed; - } else { - - logDeferrer.defer(l -> l.info("Configuration YAML is malformed as it contains an array at the " + - "root level. Skipping.")); - } - - file.close(); - } - } catch (IOException e) { - logDeferrer.defer(l -> - l.info("Couldn't successfully process the YAML configuration file." + - "All your properties may not be correctly loaded")); - } - - // parse properties file - if (config == null) { - loadProperties(propertiesFileName); - if (properties == null) { - loadProperties(microProfilePropertiesFileName); - } - } - - if (config != null || properties != null) { - logDeferrer.defer(l -> l.info("Configuration successfully read.")); - } else { - logDeferrer.defer(l -> l.info("Unable to load configuration from file. No configuration files were found.")); - } - } - - @Override - public Optional get(String key) { - - // get key value from yaml configuration - if (config != null) { - - Object value = getValue(key); - - return (value == null) ? Optional.empty() : Optional.of(value.toString()); - - // get value from .properties configuration - } else if (properties != null) { - - String value = properties.getProperty(key); - if (value != null) { - return Optional.of(value); - } - } - - return Optional.empty(); - } - - @Override - public Optional getBoolean(String key) { - - Optional value = get(key); - - return value.map(Boolean::valueOf); - } - - @Override - public Optional getInteger(String key) { - - Optional value = get(key); - - if (value.isPresent()) { - - try { - return Optional.of(Integer.valueOf(value.get())); - } catch (NumberFormatException e) { - return Optional.empty(); - } - } else { - return Optional.empty(); - } - } - - @Override - public Optional getLong(String key) { - - Optional value = get(key); + protected InputStream getInputStream() throws IOException { + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename); - if (value.isPresent()) { - - try { - return Optional.of(Long.valueOf(value.get())); - } catch (NumberFormatException e) { - return Optional.empty(); - } - } else { - return Optional.empty(); + if (inputStream == null) { + inputStream = Files.newInputStream(Paths.get(filename)); } - } - - @Override - public Optional getDouble(String key) { - - Optional value = get(key); - if (value.isPresent()) { - - try { - return Optional.of(Double.valueOf(value.get())); - } catch (NumberFormatException e) { - return Optional.empty(); - } - } else { - return Optional.empty(); - } - } - - @Override - public Optional getFloat(String key) { - - Optional value = get(key); - - if (value.isPresent()) { - - try { - return Optional.of(Float.valueOf(value.get())); - } catch (NumberFormatException e) { - return Optional.empty(); - } - - } else { - return Optional.empty(); - } - } - - @Override - public Optional getListSize(String key) { - - if (config != null) { - - Object value = getValue(key); - - if (value instanceof List) { - return Optional.of(((List) value).size()); - } - } else if (properties != null) { - return ConfigurationSourceUtils.getListSize(key, properties.stringPropertyNames()); - } - - return Optional.empty(); - - } - - @Override - @SuppressWarnings("unchecked") - public Optional> getMapKeys(String key) { - - if (config != null) { - Object o = (key.equals("")) ? config : getValue(key); - Map map = null; - - if (o instanceof Map) { - map = (Map) o; - } - - if (map == null || map.isEmpty()) { - return Optional.empty(); - } - - return Optional.of(new ArrayList<>(map.keySet())); - - } else if (properties != null) { - return ConfigurationSourceUtils.getMapKeys(key, properties.stringPropertyNames()); - } - - return Optional.empty(); - } - - @Override - public void watch(String key) { - } - - @Override - public void set(String key, String value) { - } - - @Override - public void set(String key, Boolean value) { - } - - @Override - public void set(String key, Integer value) { - } - - @Override - public void set(String key, Double value) { - } - - @Override - public void set(String key, Float value) { - } - - @Override - public Integer getOrdinal() { - return getInteger(CONFIG_ORDINAL).orElse(100); - } - - /** - * Returns true, if key represents an array. - * - * @param key configuration key - * @return true if the config key represents an array, false otherwise. - */ - private boolean representsArray(String key) { - - int openingBracket = key.indexOf("["); - int closingBracket = key.indexOf("]"); - - return closingBracket == key.length() - 1 && openingBracket != -1; - - } - - /** - * Parses configuration map, returns value for given key. - * - * @param key configuration key - * @return Value for given key. - */ - private Object getValue(String key) { - - // iterate over configuration tree - String[] splittedKeys = key.split("\\."); - Object value = config; - - for (int i = 0; i < splittedKeys.length; i++) { - - String splittedKey = splittedKeys[i]; - - if (value == null) { - return null; - } - - // parse arrays - if (representsArray(splittedKey)) { - - // value array support - int arrayIndex; - int openingBracket = splittedKey.indexOf("["); - int closingBracket = splittedKey.indexOf("]"); - - try { - arrayIndex = Integer.parseInt(splittedKey.substring(openingBracket + 1, closingBracket)); - } catch (NumberFormatException e) { - - if (log != null) { - log.severe("Cannot cast array index."); - } - - return null; - } - - splittedKey = splittedKey.substring(0, openingBracket); - - if (value instanceof Map) { - value = ((Map) value).get(splittedKey); - } else { - return null; - } - - if (value instanceof List) { - value = (arrayIndex < ((List) value).size()) ? ((List) value).get(arrayIndex) : null; - } - - } else { - if (value instanceof Map) { - - Object tmpValue = ((Map) value).get(splittedKey); - - if (tmpValue == null && i != splittedKeys.length - 1) { - - String postfixKey = Arrays.stream(splittedKeys).skip(i) - .collect(Collectors.joining(".")); - - return ((Map) value).get(postfixKey); - } else { - - value = tmpValue; - } - } else { - return null; - } - } - } - - return value; - } - - private void loadProperties(String fileName) { - - try { - InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName); - - if (inputStream == null) { - try { - inputStream = Files.newInputStream(Paths.get(fileName)); - } catch (IOException ignored) { - } - } - - if (inputStream != null) { - - logDeferrer.defer(l -> l.info("Loading configuration from .properties file: " + propertiesFileName)); - - properties = new Properties(); - properties.load(inputStream); - - inputStream.close(); - } - } catch (IOException e) { - logDeferrer.defer(l -> l.info("Properties file: " + fileName + " not found.")); - } + return inputStream; } } diff --git a/common/src/main/java/com/kumuluz/ee/configuration/sources/PropertiesConfigurationSource.java b/common/src/main/java/com/kumuluz/ee/configuration/sources/PropertiesConfigurationSource.java new file mode 100644 index 00000000..388dd1b9 --- /dev/null +++ b/common/src/main/java/com/kumuluz/ee/configuration/sources/PropertiesConfigurationSource.java @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2014-2017 Kumuluz and/or its affiliates + * and other contributors as indicated by the @author tags and + * the contributor list. + * + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/MIT + * + * The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or + * implied, including but not limited to the warranties of merchantability, + * fitness for a particular purpose and noninfringement. in no event shall the + * authors or copyright holders be liable for any claim, damages or other + * liability, whether in an action of contract, tort or otherwise, arising from, + * out of or in connection with the software or the use or other dealings in the + * software. See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.kumuluz.ee.configuration.sources; + +import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher; +import com.kumuluz.ee.configuration.utils.ConfigurationSourceUtils; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; +import java.util.Optional; +import java.util.Properties; + +/** + * Configuration source for properties files. + * + * @author Urban Malc + * @since 3.6.0 + */ +public class PropertiesConfigurationSource extends FileConfigurationSource { + + private Properties properties; + + public PropertiesConfigurationSource(String filename, int defaultOrdinal) { + + super(filename, defaultOrdinal); + } + + @Override + public void init(ConfigurationDispatcher configurationDispatcher) { + + try { + InputStream inputStream = getInputStream(); + + logDeferrer.defer(l -> l.info("Loading configuration from .properties file: " + filename)); + + properties = new Properties(); + properties.load(inputStream); + + inputStream.close(); + } catch (IOException e) { + logDeferrer.defer(l -> l.info("Properties file: " + filename + " not found.")); + } + + if (properties != null) { + logDeferrer.defer(l -> l.info("Configuration successfully read.")); + } else { + logDeferrer.defer(l -> l.warning("Unable to load configuration from file. File " + filename + + " is not valid or does not exist.")); + } + } + + @Override + public Optional get(String key) { + + // get value from .properties configuration + if (properties != null) { + + String value = properties.getProperty(key); + if (value != null) { + return Optional.of(value); + } + } + + return Optional.empty(); + } + + @Override + public Optional getListSize(String key) { + + if (properties != null) { + return ConfigurationSourceUtils.getListSize(key, properties.stringPropertyNames()); + } + + return Optional.empty(); + + } + + @Override + public Optional> getMapKeys(String key) { + + if (properties != null) { + return ConfigurationSourceUtils.getMapKeys(key, properties.stringPropertyNames()); + } + + return Optional.empty(); + } + + @Override + public void watch(String key) { + } + + @Override + public void set(String key, String value) { + } + + @Override + public void set(String key, Boolean value) { + } + + @Override + public void set(String key, Integer value) { + } + + @Override + public void set(String key, Double value) { + } + + @Override + public void set(String key, Float value) { + } + + @Override + public Integer getOrdinal() { + return getInteger(CONFIG_ORDINAL).orElse(defaultOrdinal); + } + +} diff --git a/common/src/main/java/com/kumuluz/ee/configuration/sources/YamlConfigurationSource.java b/common/src/main/java/com/kumuluz/ee/configuration/sources/YamlConfigurationSource.java new file mode 100644 index 00000000..58356c68 --- /dev/null +++ b/common/src/main/java/com/kumuluz/ee/configuration/sources/YamlConfigurationSource.java @@ -0,0 +1,255 @@ +/* + * Copyright (c) 2014-2017 Kumuluz and/or its affiliates + * and other contributors as indicated by the @author tags and + * the contributor list. + * + * Licensed under the MIT License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/MIT + * + * The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or + * implied, including but not limited to the warranties of merchantability, + * fitness for a particular purpose and noninfringement. in no event shall the + * authors or copyright holders be liable for any claim, damages or other + * liability, whether in an action of contract, tort or otherwise, arising from, + * out of or in connection with the software or the use or other dealings in the + * software. See the License for the specific language governing permissions and + * limitations under the License. +*/ +package com.kumuluz.ee.configuration.sources; + +import com.kumuluz.ee.configuration.utils.ConfigurationDispatcher; +import org.yaml.snakeyaml.Yaml; + +import java.io.IOException; +import java.io.InputStream; +import java.util.*; +import java.util.stream.Collectors; + +/** + * Configuration source for YAML files. + * + * @author Urban Malc + * @since 3.6.0 + */ +public class YamlConfigurationSource extends FileConfigurationSource { + + private Map config; + + public YamlConfigurationSource(String filename, int defaultOrdinal) { + + super(filename, defaultOrdinal); + } + + @Override + @SuppressWarnings("unchecked") + public void init(ConfigurationDispatcher configurationDispatcher) { + + System.out.println("loading " + filename); + // read yaml file to Map + Yaml yaml = new Yaml(); + try { + + InputStream file = getInputStream(); + + logDeferrer.defer(l -> l.info("Loading configuration from YAML file " + filename)); + + Object yamlParsed = yaml.load(file); + + if (yamlParsed instanceof Map) { + config = (Map) yamlParsed; + } else { + + logDeferrer.defer(l -> l.info("Configuration YAML is malformed as it contains an array at the " + + "root level. Skipping.")); + } + + file.close(); + } catch (IOException e) { + logDeferrer.defer(l -> + l.info("Couldn't successfully process the YAML configuration file." + + "All your properties may not be correctly loaded")); + } + + if (config != null) { + logDeferrer.defer(l -> l.info("Configuration successfully read.")); + } else { + logDeferrer.defer(l -> l.warning("Unable to load configuration from file. File " + filename + + " is not valid or does not exist.")); + } + } + + @Override + public Optional get(String key) { + + // get key value from yaml configuration + if (config != null) { + + Object value = getValue(key); + + return (value == null) ? Optional.empty() : Optional.of(value.toString()); + } + + return Optional.empty(); + } + + @Override + public Optional getListSize(String key) { + + if (config != null) { + + Object value = getValue(key); + + if (value instanceof List) { + return Optional.of(((List) value).size()); + } + } + + return Optional.empty(); + + } + + @Override + @SuppressWarnings("unchecked") + public Optional> getMapKeys(String key) { + + if (config != null) { + Object o = (key.equals("")) ? config : getValue(key); + Map map = null; + + if (o instanceof Map) { + map = (Map) o; + } + + if (map == null || map.isEmpty()) { + return Optional.empty(); + } + + return Optional.of(new ArrayList<>(map.keySet())); + + } + + return Optional.empty(); + } + + @Override + public void watch(String key) { + } + + @Override + public void set(String key, String value) { + } + + @Override + public void set(String key, Boolean value) { + } + + @Override + public void set(String key, Integer value) { + } + + @Override + public void set(String key, Double value) { + } + + @Override + public void set(String key, Float value) { + } + + @Override + public Integer getOrdinal() { + return getInteger(CONFIG_ORDINAL).orElse(defaultOrdinal); + } + + /** + * Returns true, if key represents an array. + * + * @param key configuration key + * @return true if the config key represents an array, false otherwise. + */ + private boolean representsArray(String key) { + + int openingBracket = key.indexOf("["); + int closingBracket = key.indexOf("]"); + + return closingBracket == key.length() - 1 && openingBracket != -1; + + } + + /** + * Parses configuration map, returns value for given key. + * + * @param key configuration key + * @return Value for given key. + */ + private Object getValue(String key) { + + // iterate over configuration tree + String[] splittedKeys = key.split("\\."); + Object value = config; + + for (int i = 0; i < splittedKeys.length; i++) { + + String splittedKey = splittedKeys[i]; + + if (value == null) { + return null; + } + + // parse arrays + if (representsArray(splittedKey)) { + + // value array support + int arrayIndex; + int openingBracket = splittedKey.indexOf("["); + int closingBracket = splittedKey.indexOf("]"); + + try { + arrayIndex = Integer.parseInt(splittedKey.substring(openingBracket + 1, closingBracket)); + } catch (NumberFormatException e) { + + if (log != null) { + log.severe("Cannot cast array index."); + } + + return null; + } + + splittedKey = splittedKey.substring(0, openingBracket); + + if (value instanceof Map) { + value = ((Map) value).get(splittedKey); + } else { + return null; + } + + if (value instanceof List) { + value = (arrayIndex < ((List) value).size()) ? ((List) value).get(arrayIndex) : null; + } + + } else { + if (value instanceof Map) { + + Object tmpValue = ((Map) value).get(splittedKey); + + if (tmpValue == null && i != splittedKeys.length - 1) { + + String postfixKey = Arrays.stream(splittedKeys).skip(i) + .collect(Collectors.joining(".")); + + return ((Map) value).get(postfixKey); + } else { + + value = tmpValue; + } + } else { + return null; + } + } + } + + return value; + } +} diff --git a/common/src/main/java/com/kumuluz/ee/configuration/utils/ConfigurationImpl.java b/common/src/main/java/com/kumuluz/ee/configuration/utils/ConfigurationImpl.java index 2c0eaca3..e4620dab 100644 --- a/common/src/main/java/com/kumuluz/ee/configuration/utils/ConfigurationImpl.java +++ b/common/src/main/java/com/kumuluz/ee/configuration/utils/ConfigurationImpl.java @@ -22,11 +22,12 @@ import com.kumuluz.ee.configuration.ConfigurationDecoder; import com.kumuluz.ee.configuration.ConfigurationSource; -import com.kumuluz.ee.configuration.sources.EnvironmentConfigurationSource; -import com.kumuluz.ee.configuration.sources.FileConfigurationSource; -import com.kumuluz.ee.configuration.sources.SystemPropertyConfigurationSource; +import com.kumuluz.ee.configuration.sources.*; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.ServiceLoader; import java.util.logging.Logger; @@ -37,14 +38,16 @@ */ public class ConfigurationImpl { + private static final String[] YAML_FILE_LOCATIONS = {"config.yml", "config.yaml"}; + private static final String[] PROPERTIES_FILE_LOCATIONS = {"META-INF/microprofile-config.properties", + "config.properties"}; + private Logger utilLogger; private ConfigurationDispatcher dispatcher; private List configurationSources; private ConfigurationDecoder configurationDecoder; - private EnvironmentConfigurationSource environmentConfigurationSource; - private SystemPropertyConfigurationSource systemPropertyConfigurationSource; - private FileConfigurationSource fileConfigurationSource; + private List fileConfigurationSources; public ConfigurationImpl() { init(); @@ -52,15 +55,15 @@ public ConfigurationImpl() { private void init() { - environmentConfigurationSource = new EnvironmentConfigurationSource(); - systemPropertyConfigurationSource = new SystemPropertyConfigurationSource(); - fileConfigurationSource = new FileConfigurationSource(); + EnvironmentConfigurationSource environmentConfigurationSource = new EnvironmentConfigurationSource(); + SystemPropertyConfigurationSource systemPropertyConfigurationSource = new SystemPropertyConfigurationSource(); + fileConfigurationSources = getFileConfigurationSources(); // specify sources configurationSources = new ArrayList<>(); configurationSources.add(environmentConfigurationSource); configurationSources.add(systemPropertyConfigurationSource); - configurationSources.add(fileConfigurationSource); + configurationSources.addAll(fileConfigurationSources); dispatcher = new ConfigurationDispatcher(); @@ -81,9 +84,49 @@ private void init() { } } + private List getFileConfigurationSources() { + + List configurationSources = new LinkedList<>(); + int nextOrdinal = 100; + + // properties should be first so that microprofile properties gets ordinal 100 as per specification + for (String propertiesFile : PROPERTIES_FILE_LOCATIONS) { + if (resourceExists(propertiesFile)) { + configurationSources.add(new PropertiesConfigurationSource(propertiesFile, nextOrdinal)); + nextOrdinal--; + } + } + + for (String yamlFile : YAML_FILE_LOCATIONS) { + if (resourceExists(yamlFile)) { + configurationSources.add(new YamlConfigurationSource(yamlFile, nextOrdinal)); + nextOrdinal--; + } + } + + // legacy additional file from system property + String legacyConfigurationFileName = System.getProperty("com.kumuluz.ee.configuration.file"); + if (legacyConfigurationFileName != null && !legacyConfigurationFileName.isEmpty()) { + configurationSources.add(new PropertiesConfigurationSource(legacyConfigurationFileName, 101)); + configurationSources.add(new YamlConfigurationSource(legacyConfigurationFileName, 102)); + } + + return configurationSources; + } + + public void addFileConfigurationSource(FileConfigurationSource fileConfigurationSource) { + this.configurationSources.add(fileConfigurationSource); + // add to file configuration sources so the postInit logic gets properly executed + this.fileConfigurationSources.add(fileConfigurationSource); + } + + private boolean resourceExists(String locator) { + return this.getClass().getClassLoader().getResource(locator) != null || Files.exists(Paths.get(locator)); + } + public void postInit() { - fileConfigurationSource.postInit(); + fileConfigurationSources.forEach(FileConfigurationSource::postInit); utilLogger = Logger.getLogger(ConfigurationUtil.class.getName()); } diff --git a/core/src/main/java/com/kumuluz/ee/EeApplication.java b/core/src/main/java/com/kumuluz/ee/EeApplication.java index 7fa3180d..bcaaa01c 100644 --- a/core/src/main/java/com/kumuluz/ee/EeApplication.java +++ b/core/src/main/java/com/kumuluz/ee/EeApplication.java @@ -41,6 +41,9 @@ import com.kumuluz.ee.common.wrapper.ExtensionWrapper; import com.kumuluz.ee.common.wrapper.KumuluzServerWrapper; import com.kumuluz.ee.configuration.ConfigurationSource; +import com.kumuluz.ee.configuration.sources.FileConfigurationSource; +import com.kumuluz.ee.configuration.sources.PropertiesConfigurationSource; +import com.kumuluz.ee.configuration.sources.YamlConfigurationSource; import com.kumuluz.ee.configuration.utils.ConfigurationImpl; import com.kumuluz.ee.configuration.utils.ConfigurationUtil; import com.kumuluz.ee.factories.EeConfigFactory; @@ -62,6 +65,8 @@ */ public class EeApplication { + private static final String CONFIG_ADDITIONAL_CONFIG_FILES = "kumuluzee.config.additional-config-files"; + private Logger log; private EeConfig eeConfig; @@ -96,6 +101,38 @@ private void initialize() { ConfigurationUtil.initialize(configImpl); + Optional additionalConfigFilesSize = ConfigurationUtil.getInstance() + .getListSize(CONFIG_ADDITIONAL_CONFIG_FILES); + if (additionalConfigFilesSize.isPresent()) { + for (int i = 0; i < additionalConfigFilesSize.get(); i++) { + + Optional configFile = ConfigurationUtil.getInstance() + .get(CONFIG_ADDITIONAL_CONFIG_FILES + "[" + i + "]"); + + if (configFile.isPresent()) { + + int defaultOrdinal = 100 + additionalConfigFilesSize.get() - i; + FileConfigurationSource configurationSource; + if (configFile.get().endsWith(".yml") || configFile.get().endsWith(".yaml")) { + // ordinal assigned so that the first file is the most important one + configurationSource = new YamlConfigurationSource(configFile.get(), defaultOrdinal); + } else if (configFile.get().endsWith(".properties")) { + configurationSource = new PropertiesConfigurationSource(configFile.get(), defaultOrdinal); + } else { + throw new IllegalArgumentException("Additional configuration file " + configFile.get() + + " does not have any recognised extension."); + } + + configurationSource.init(configImpl.getDispatcher()); + configImpl.addFileConfigurationSource(configurationSource); + } + } + } + + // sort here because even if additional config files are not specified because ordinals of the default + // configuration sources can be overridden using config_ordinal (MicroProfile) + configImpl.getConfigurationSources().sort(Comparator.comparingInt(ConfigurationSource::getOrdinal).reversed()); + if (this.eeConfig == null) { this.eeConfig = EeConfigFactory.buildEeConfig(); } else if (!EeConfigFactory.isEeConfigValid(this.eeConfig)) { diff --git a/pom.xml b/pom.xml index 02a3ed41..afdb9ca2 100644 --- a/pom.xml +++ b/pom.xml @@ -79,7 +79,7 @@ 3.1.1.Final 6.0.16.Final 2.28 - 2.9.9 + 2.9.10 2.4.1 3.2.6 5.4.2.Final