From 39b8da629c1a4dc8c8092df69a66c5f310b21740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Fri, 15 Oct 2021 11:21:34 +0200 Subject: [PATCH] fix automatic interpolation, move content-config to config --- {content-config => config}/build.gradle | 0 config/src/main/java/module-info.java | 12 ++++++ .../org/xbib}/config/ConfigException.java | 2 +- .../java/org/xbib}/config/ConfigFinder.java | 2 +- .../java/org/xbib}/config/ConfigLoader.java | 15 ++++--- .../java/org/xbib}/config/ConfigLogger.java | 2 +- .../java/org/xbib}/config/ConfigParams.java | 14 +++++-- .../org/xbib}/config/SystemConfigLogger.java | 2 +- .../java/org/xbib/config/package-info.java | 4 ++ .../services/org.xbib.config.ConfigLogger | 1 + .../xbib}/config/test/ConfigLoaderTest.java | 42 ++++++++++++++++--- .../org/xbib/config/test/package-info.java | 4 ++ .../services/org.xbib.settings.SettingsLoader | 0 .../src/test/resources/config.json | 0 .../src/test/resources/config.yaml | 0 content-config/src/main/java/module-info.java | 11 ----- .../org/xbib/content/config/package-info.java | 4 -- .../org.xbib.content.config.ConfigLogger | 1 - .../content/config/test/package-info.java | 4 -- .../config}/checkstyle/checkstyle.xml | 0 .../config}/checkstyle/suppressions.xml | 0 .../pmd/category/java/bestpractices.xml | 0 .../pmd/category/java/categories.properties | 3 -- .../config}/pmd/category/java/codestyle.xml | 0 .../config}/pmd/category/java/design.xml | 0 .../pmd/category/java/documentation.xml | 0 .../config}/pmd/category/java/errorprone.xml | 0 .../pmd/category/java/multithreading.xml | 0 .../config}/pmd/category/java/performance.xml | 0 .../config}/pmd/category/java/security.xml | 0 gradle/quality/checkstyle.gradle | 2 +- gradle/quality/pmd.gradle | 2 +- settings.gradle | 2 +- 33 files changed, 86 insertions(+), 43 deletions(-) rename {content-config => config}/build.gradle (100%) create mode 100644 config/src/main/java/module-info.java rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/ConfigException.java (90%) rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/ConfigFinder.java (99%) rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/ConfigLoader.java (96%) rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/ConfigLogger.java (79%) rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/ConfigParams.java (92%) rename {content-config/src/main/java/org/xbib/content => config/src/main/java/org/xbib}/config/SystemConfigLogger.java (92%) create mode 100644 config/src/main/java/org/xbib/config/package-info.java create mode 100644 config/src/main/resources/META-INF/services/org.xbib.config.ConfigLogger rename {content-config/src/test/java/org/xbib/content => config/src/test/java/org/xbib}/config/test/ConfigLoaderTest.java (53%) create mode 100644 config/src/test/java/org/xbib/config/test/package-info.java rename {content-config => config}/src/test/resources/META-INF/services/org.xbib.settings.SettingsLoader (100%) rename {content-config => config}/src/test/resources/config.json (100%) rename {content-config => config}/src/test/resources/config.yaml (100%) delete mode 100644 content-config/src/main/java/module-info.java delete mode 100644 content-config/src/main/java/org/xbib/content/config/package-info.java delete mode 100644 content-config/src/main/resources/META-INF/services/org.xbib.content.config.ConfigLogger delete mode 100644 content-config/src/test/java/org/xbib/content/config/test/package-info.java rename {config => gradle/config}/checkstyle/checkstyle.xml (100%) rename {config => gradle/config}/checkstyle/suppressions.xml (100%) rename {config => gradle/config}/pmd/category/java/bestpractices.xml (100%) rename {config => gradle/config}/pmd/category/java/categories.properties (78%) rename {config => gradle/config}/pmd/category/java/codestyle.xml (100%) rename {config => gradle/config}/pmd/category/java/design.xml (100%) rename {config => gradle/config}/pmd/category/java/documentation.xml (100%) rename {config => gradle/config}/pmd/category/java/errorprone.xml (100%) rename {config => gradle/config}/pmd/category/java/multithreading.xml (100%) rename {config => gradle/config}/pmd/category/java/performance.xml (100%) rename {config => gradle/config}/pmd/category/java/security.xml (100%) diff --git a/content-config/build.gradle b/config/build.gradle similarity index 100% rename from content-config/build.gradle rename to config/build.gradle diff --git a/config/src/main/java/module-info.java b/config/src/main/java/module-info.java new file mode 100644 index 0000000..ef13722 --- /dev/null +++ b/config/src/main/java/module-info.java @@ -0,0 +1,12 @@ +import org.xbib.config.ConfigLogger; +import org.xbib.config.SystemConfigLogger; +import org.xbib.settings.SettingsLoader; + +module org.xbib.config { + exports org.xbib.config; + uses ConfigLogger; + uses SettingsLoader; + provides ConfigLogger with SystemConfigLogger; + requires org.xbib.settings.api; + requires transitive org.xbib.settings.datastructures; +} diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigException.java b/config/src/main/java/org/xbib/config/ConfigException.java similarity index 90% rename from content-config/src/main/java/org/xbib/content/config/ConfigException.java rename to config/src/main/java/org/xbib/config/ConfigException.java index a5a887e..d089fa0 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigException.java +++ b/config/src/main/java/org/xbib/config/ConfigException.java @@ -1,4 +1,4 @@ -package org.xbib.content.config; +package org.xbib.config; @SuppressWarnings("serial") public class ConfigException extends RuntimeException { diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigFinder.java b/config/src/main/java/org/xbib/config/ConfigFinder.java similarity index 99% rename from content-config/src/main/java/org/xbib/content/config/ConfigFinder.java rename to config/src/main/java/org/xbib/config/ConfigFinder.java index 8520fb3..a3cb674 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigFinder.java +++ b/config/src/main/java/org/xbib/config/ConfigFinder.java @@ -1,4 +1,4 @@ -package org.xbib.content.config; +package org.xbib.config; import java.io.IOException; import java.nio.file.FileSystem; diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java b/config/src/main/java/org/xbib/config/ConfigLoader.java similarity index 96% rename from content-config/src/main/java/org/xbib/content/config/ConfigLoader.java rename to config/src/main/java/org/xbib/config/ConfigLoader.java index 2fd3fac..6089082 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigLoader.java +++ b/config/src/main/java/org/xbib/config/ConfigLoader.java @@ -1,4 +1,4 @@ -package org.xbib.content.config; +package org.xbib.config; import org.xbib.settings.Settings; import org.xbib.settings.SettingsBuilder; @@ -57,7 +57,9 @@ public class ConfigLoader { } public synchronized Settings load(ConfigParams configParams) throws ConfigException { - map.computeIfAbsent(configParams, p -> internalLoad(p).build()); + map.computeIfAbsent(configParams, p -> internalLoad(p) + .replacePropertyPlaceholders() + .build()); return map.get(configParams); } @@ -80,7 +82,7 @@ public class ConfigLoader { if (readerSettings != null) { settings.put(readerSettings.build()); if (!params.includeAll) { - return settings; + return overrideFromProperties(params, settings); } } } @@ -90,7 +92,7 @@ public class ConfigLoader { if (argsSettings != null) { settings.put(argsSettings.build()); if (!params.includeAll) { - return settings; + return overrideFromProperties(params, settings); } } } @@ -150,7 +152,10 @@ public class ConfigLoader { if (params.includeAll) { return overrideFromProperties(params, settings); } - throw new ConfigException("no config found"); + if (params.failIfEmpty) { + throw new ConfigException("no config found"); + } + return settings; } private SettingsBuilder createSettingsFromArgs(ConfigParams params) throws ConfigException { diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigLogger.java b/config/src/main/java/org/xbib/config/ConfigLogger.java similarity index 79% rename from content-config/src/main/java/org/xbib/content/config/ConfigLogger.java rename to config/src/main/java/org/xbib/config/ConfigLogger.java index 37f4ebb..ca600db 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigLogger.java +++ b/config/src/main/java/org/xbib/config/ConfigLogger.java @@ -1,4 +1,4 @@ -package org.xbib.content.config; +package org.xbib.config; public interface ConfigLogger { diff --git a/content-config/src/main/java/org/xbib/content/config/ConfigParams.java b/config/src/main/java/org/xbib/config/ConfigParams.java similarity index 92% rename from content-config/src/main/java/org/xbib/content/config/ConfigParams.java rename to config/src/main/java/org/xbib/config/ConfigParams.java index fc3a797..9e810b8 100644 --- a/content-config/src/main/java/org/xbib/content/config/ConfigParams.java +++ b/config/src/main/java/org/xbib/config/ConfigParams.java @@ -1,5 +1,6 @@ -package org.xbib.content.config; +package org.xbib.config; +import org.xbib.settings.Settings; import org.xbib.settings.datastructures.DatastructureSettings; import java.io.IOException; import java.io.Reader; @@ -18,6 +19,8 @@ public class ConfigParams implements Comparable { boolean withSystemProperties = false; + boolean failIfEmpty = false; + boolean includeAll = false; boolean withStdin = false; @@ -63,6 +66,11 @@ public class ConfigParams implements Comparable { return this; } + public ConfigParams failIfEmpty() { + this.failIfEmpty = true; + return this; + } + public ConfigParams withStdin(boolean withStdin) { this.withStdin = withStdin; return this; @@ -86,8 +94,8 @@ public class ConfigParams implements Comparable { return this; } - public ConfigParams withSettings(DatastructureSettings settings) { - this.settings.add(settings); + public ConfigParams withSettings(Settings settings) { + this.settings.add(DatastructureSettings.builder().put(settings.getAsMap()).build()); return this; } diff --git a/content-config/src/main/java/org/xbib/content/config/SystemConfigLogger.java b/config/src/main/java/org/xbib/config/SystemConfigLogger.java similarity index 92% rename from content-config/src/main/java/org/xbib/content/config/SystemConfigLogger.java rename to config/src/main/java/org/xbib/config/SystemConfigLogger.java index a5afa06..fd6db17 100644 --- a/content-config/src/main/java/org/xbib/content/config/SystemConfigLogger.java +++ b/config/src/main/java/org/xbib/config/SystemConfigLogger.java @@ -1,4 +1,4 @@ -package org.xbib.content.config; +package org.xbib.config; public class SystemConfigLogger implements ConfigLogger { diff --git a/config/src/main/java/org/xbib/config/package-info.java b/config/src/main/java/org/xbib/config/package-info.java new file mode 100644 index 0000000..675b377 --- /dev/null +++ b/config/src/main/java/org/xbib/config/package-info.java @@ -0,0 +1,4 @@ +/** + * Classes for configuration setup. + */ +package org.xbib.config; diff --git a/config/src/main/resources/META-INF/services/org.xbib.config.ConfigLogger b/config/src/main/resources/META-INF/services/org.xbib.config.ConfigLogger new file mode 100644 index 0000000..b57351d --- /dev/null +++ b/config/src/main/resources/META-INF/services/org.xbib.config.ConfigLogger @@ -0,0 +1 @@ +org.xbib.config.SystemConfigLogger \ No newline at end of file diff --git a/content-config/src/test/java/org/xbib/content/config/test/ConfigLoaderTest.java b/config/src/test/java/org/xbib/config/test/ConfigLoaderTest.java similarity index 53% rename from content-config/src/test/java/org/xbib/content/config/test/ConfigLoaderTest.java rename to config/src/test/java/org/xbib/config/test/ConfigLoaderTest.java index 9e1182a..9ce3c5c 100644 --- a/content-config/src/test/java/org/xbib/content/config/test/ConfigLoaderTest.java +++ b/config/src/test/java/org/xbib/config/test/ConfigLoaderTest.java @@ -1,10 +1,12 @@ -package org.xbib.content.config.test; +package org.xbib.config.test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; import org.xbib.settings.Settings; -import org.xbib.content.config.ConfigLoader; -import org.xbib.content.config.ConfigParams; +import org.xbib.config.ConfigLoader; +import org.xbib.config.ConfigParams; import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -12,7 +14,37 @@ import java.io.StringReader; public class ConfigLoaderTest { @Test - public void configTest() { + public void configEmptyTest() { + Settings settings = ConfigLoader.getInstance() + .load(new ConfigParams()); + assertTrue(settings.isEmpty()); + } + + @Test + public void configSettingsTest() { + Settings configSettings = Settings.settingsBuilder() + .put("hello", "world") + .build(); + Settings settings = ConfigLoader.getInstance() + .load(new ConfigParams().withSettings(configSettings)); + assertEquals("world", settings.get("hello")); + } + + @Test + public void configArgsTest() { + String[] args = new String[] { + "--config.yaml", + "hello: world" + }; + Settings settings = ConfigLoader.getInstance() + .load(new ConfigParams() + .withArgs(args) + .withFileNamesWithoutSuffix("config")); + assertEquals("world", settings.get("hello")); + } + + @Test + public void configPropertiesTest() { Reader reader = new StringReader("a=b"); Settings settings = ConfigLoader.getInstance() .load(new ConfigParams() @@ -30,7 +62,7 @@ public class ConfigLoaderTest { } @Test - public void testOverride() throws IOException { + public void testSystemPropertiesOverride() throws IOException { System.setProperty("hello", "override"); Settings settings = ConfigLoader.getInstance() .load(new ConfigParams() diff --git a/config/src/test/java/org/xbib/config/test/package-info.java b/config/src/test/java/org/xbib/config/test/package-info.java new file mode 100644 index 0000000..b83dd48 --- /dev/null +++ b/config/src/test/java/org/xbib/config/test/package-info.java @@ -0,0 +1,4 @@ +/** + * Test classes for config. + */ +package org.xbib.config.test; diff --git a/content-config/src/test/resources/META-INF/services/org.xbib.settings.SettingsLoader b/config/src/test/resources/META-INF/services/org.xbib.settings.SettingsLoader similarity index 100% rename from content-config/src/test/resources/META-INF/services/org.xbib.settings.SettingsLoader rename to config/src/test/resources/META-INF/services/org.xbib.settings.SettingsLoader diff --git a/content-config/src/test/resources/config.json b/config/src/test/resources/config.json similarity index 100% rename from content-config/src/test/resources/config.json rename to config/src/test/resources/config.json diff --git a/content-config/src/test/resources/config.yaml b/config/src/test/resources/config.yaml similarity index 100% rename from content-config/src/test/resources/config.yaml rename to config/src/test/resources/config.yaml diff --git a/content-config/src/main/java/module-info.java b/content-config/src/main/java/module-info.java deleted file mode 100644 index 554a363..0000000 --- a/content-config/src/main/java/module-info.java +++ /dev/null @@ -1,11 +0,0 @@ -import org.xbib.content.config.ConfigLogger; -import org.xbib.settings.SettingsLoader; - -module org.xbib.content.config { - exports org.xbib.content.config; - uses ConfigLogger; - uses SettingsLoader; - provides ConfigLogger with org.xbib.content.config.SystemConfigLogger; - requires org.xbib.settings.api; - requires transitive org.xbib.settings.datastructures; -} diff --git a/content-config/src/main/java/org/xbib/content/config/package-info.java b/content-config/src/main/java/org/xbib/content/config/package-info.java deleted file mode 100644 index 8a294fe..0000000 --- a/content-config/src/main/java/org/xbib/content/config/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Classes for configurations. - */ -package org.xbib.content.config; diff --git a/content-config/src/main/resources/META-INF/services/org.xbib.content.config.ConfigLogger b/content-config/src/main/resources/META-INF/services/org.xbib.content.config.ConfigLogger deleted file mode 100644 index 1c7eb79..0000000 --- a/content-config/src/main/resources/META-INF/services/org.xbib.content.config.ConfigLogger +++ /dev/null @@ -1 +0,0 @@ -org.xbib.content.config.SystemConfigLogger \ No newline at end of file diff --git a/content-config/src/test/java/org/xbib/content/config/test/package-info.java b/content-config/src/test/java/org/xbib/content/config/test/package-info.java deleted file mode 100644 index 3527751..0000000 --- a/content-config/src/test/java/org/xbib/content/config/test/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Test classes for content config. - */ -package org.xbib.content.config.test; diff --git a/config/checkstyle/checkstyle.xml b/gradle/config/checkstyle/checkstyle.xml similarity index 100% rename from config/checkstyle/checkstyle.xml rename to gradle/config/checkstyle/checkstyle.xml diff --git a/config/checkstyle/suppressions.xml b/gradle/config/checkstyle/suppressions.xml similarity index 100% rename from config/checkstyle/suppressions.xml rename to gradle/config/checkstyle/suppressions.xml diff --git a/config/pmd/category/java/bestpractices.xml b/gradle/config/pmd/category/java/bestpractices.xml similarity index 100% rename from config/pmd/category/java/bestpractices.xml rename to gradle/config/pmd/category/java/bestpractices.xml diff --git a/config/pmd/category/java/categories.properties b/gradle/config/pmd/category/java/categories.properties similarity index 78% rename from config/pmd/category/java/categories.properties rename to gradle/config/pmd/category/java/categories.properties index 3189fd3..8ef5eac 100644 --- a/config/pmd/category/java/categories.properties +++ b/gradle/config/pmd/category/java/categories.properties @@ -1,6 +1,3 @@ -# -# BSD-style license; for more info see http://pmd.sourceforge.net/license.html -# rulesets.filenames=\ category/java/bestpractices.xml,\ diff --git a/config/pmd/category/java/codestyle.xml b/gradle/config/pmd/category/java/codestyle.xml similarity index 100% rename from config/pmd/category/java/codestyle.xml rename to gradle/config/pmd/category/java/codestyle.xml diff --git a/config/pmd/category/java/design.xml b/gradle/config/pmd/category/java/design.xml similarity index 100% rename from config/pmd/category/java/design.xml rename to gradle/config/pmd/category/java/design.xml diff --git a/config/pmd/category/java/documentation.xml b/gradle/config/pmd/category/java/documentation.xml similarity index 100% rename from config/pmd/category/java/documentation.xml rename to gradle/config/pmd/category/java/documentation.xml diff --git a/config/pmd/category/java/errorprone.xml b/gradle/config/pmd/category/java/errorprone.xml similarity index 100% rename from config/pmd/category/java/errorprone.xml rename to gradle/config/pmd/category/java/errorprone.xml diff --git a/config/pmd/category/java/multithreading.xml b/gradle/config/pmd/category/java/multithreading.xml similarity index 100% rename from config/pmd/category/java/multithreading.xml rename to gradle/config/pmd/category/java/multithreading.xml diff --git a/config/pmd/category/java/performance.xml b/gradle/config/pmd/category/java/performance.xml similarity index 100% rename from config/pmd/category/java/performance.xml rename to gradle/config/pmd/category/java/performance.xml diff --git a/config/pmd/category/java/security.xml b/gradle/config/pmd/category/java/security.xml similarity index 100% rename from config/pmd/category/java/security.xml rename to gradle/config/pmd/category/java/security.xml diff --git a/gradle/quality/checkstyle.gradle b/gradle/quality/checkstyle.gradle index 01a0c63..e42e014 100644 --- a/gradle/quality/checkstyle.gradle +++ b/gradle/quality/checkstyle.gradle @@ -4,7 +4,7 @@ checkstyle { toolVersion '8.33' showViolations = true ignoreFailures = true - configFile rootProject.file("config/checkstyle/checkstyle.xml") + configFile rootProject.file("gradle/config/checkstyle/checkstyle.xml") } checkstyleMain { diff --git a/gradle/quality/pmd.gradle b/gradle/quality/pmd.gradle index 1201b01..d20868b 100644 --- a/gradle/quality/pmd.gradle +++ b/gradle/quality/pmd.gradle @@ -5,5 +5,5 @@ pmd { consoleOutput = false toolVersion = "6.24.0" rulePriority = 5 - ruleSets = ["category/java/bestpractices.xml"] + ruleSetFiles = rootProject.files('gradle/config/pmd/category/java/bestpractices.xml') } diff --git a/settings.gradle b/settings.gradle index aaeaae9..457a20f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,4 @@ include 'content-api' -include 'content-config' include 'content-core' include 'content-csv' include 'content-language' @@ -16,3 +15,4 @@ include 'settings-content-yaml' include 'settings-datastructures' include 'settings-datastructures-json' include 'settings-datastructures-yaml' +include 'config'