fix automatic interpolation, move content-config to config

This commit is contained in:
Jörg Prante 2021-10-15 11:21:34 +02:00
parent 5df5a3e2cc
commit 39b8da629c
33 changed files with 86 additions and 43 deletions

View file

@ -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;
}

View file

@ -1,4 +1,4 @@
package org.xbib.content.config; package org.xbib.config;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ConfigException extends RuntimeException { public class ConfigException extends RuntimeException {

View file

@ -1,4 +1,4 @@
package org.xbib.content.config; package org.xbib.config;
import java.io.IOException; import java.io.IOException;
import java.nio.file.FileSystem; import java.nio.file.FileSystem;

View file

@ -1,4 +1,4 @@
package org.xbib.content.config; package org.xbib.config;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
import org.xbib.settings.SettingsBuilder; import org.xbib.settings.SettingsBuilder;
@ -57,7 +57,9 @@ public class ConfigLoader {
} }
public synchronized Settings load(ConfigParams configParams) throws ConfigException { 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); return map.get(configParams);
} }
@ -80,7 +82,7 @@ public class ConfigLoader {
if (readerSettings != null) { if (readerSettings != null) {
settings.put(readerSettings.build()); settings.put(readerSettings.build());
if (!params.includeAll) { if (!params.includeAll) {
return settings; return overrideFromProperties(params, settings);
} }
} }
} }
@ -90,7 +92,7 @@ public class ConfigLoader {
if (argsSettings != null) { if (argsSettings != null) {
settings.put(argsSettings.build()); settings.put(argsSettings.build());
if (!params.includeAll) { if (!params.includeAll) {
return settings; return overrideFromProperties(params, settings);
} }
} }
} }
@ -150,7 +152,10 @@ public class ConfigLoader {
if (params.includeAll) { if (params.includeAll) {
return overrideFromProperties(params, settings); 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 { private SettingsBuilder createSettingsFromArgs(ConfigParams params) throws ConfigException {

View file

@ -1,4 +1,4 @@
package org.xbib.content.config; package org.xbib.config;
public interface ConfigLogger { public interface ConfigLogger {

View file

@ -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 org.xbib.settings.datastructures.DatastructureSettings;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
@ -18,6 +19,8 @@ public class ConfigParams implements Comparable<ConfigParams> {
boolean withSystemProperties = false; boolean withSystemProperties = false;
boolean failIfEmpty = false;
boolean includeAll = false; boolean includeAll = false;
boolean withStdin = false; boolean withStdin = false;
@ -63,6 +66,11 @@ public class ConfigParams implements Comparable<ConfigParams> {
return this; return this;
} }
public ConfigParams failIfEmpty() {
this.failIfEmpty = true;
return this;
}
public ConfigParams withStdin(boolean withStdin) { public ConfigParams withStdin(boolean withStdin) {
this.withStdin = withStdin; this.withStdin = withStdin;
return this; return this;
@ -86,8 +94,8 @@ public class ConfigParams implements Comparable<ConfigParams> {
return this; return this;
} }
public ConfigParams withSettings(DatastructureSettings settings) { public ConfigParams withSettings(Settings settings) {
this.settings.add(settings); this.settings.add(DatastructureSettings.builder().put(settings.getAsMap()).build());
return this; return this;
} }

View file

@ -1,4 +1,4 @@
package org.xbib.content.config; package org.xbib.config;
public class SystemConfigLogger implements ConfigLogger { public class SystemConfigLogger implements ConfigLogger {

View file

@ -0,0 +1,4 @@
/**
* Classes for configuration setup.
*/
package org.xbib.config;

View file

@ -0,0 +1 @@
org.xbib.config.SystemConfigLogger

View file

@ -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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.settings.Settings; import org.xbib.settings.Settings;
import org.xbib.content.config.ConfigLoader; import org.xbib.config.ConfigLoader;
import org.xbib.content.config.ConfigParams; import org.xbib.config.ConfigParams;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
@ -12,7 +14,37 @@ import java.io.StringReader;
public class ConfigLoaderTest { public class ConfigLoaderTest {
@Test @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"); Reader reader = new StringReader("a=b");
Settings settings = ConfigLoader.getInstance() Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams() .load(new ConfigParams()
@ -30,7 +62,7 @@ public class ConfigLoaderTest {
} }
@Test @Test
public void testOverride() throws IOException { public void testSystemPropertiesOverride() throws IOException {
System.setProperty("hello", "override"); System.setProperty("hello", "override");
Settings settings = ConfigLoader.getInstance() Settings settings = ConfigLoader.getInstance()
.load(new ConfigParams() .load(new ConfigParams()

View file

@ -0,0 +1,4 @@
/**
* Test classes for config.
*/
package org.xbib.config.test;

View file

@ -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;
}

View file

@ -1,4 +0,0 @@
/**
* Classes for configurations.
*/
package org.xbib.content.config;

View file

@ -1 +0,0 @@
org.xbib.content.config.SystemConfigLogger

View file

@ -1,4 +0,0 @@
/**
* Test classes for content config.
*/
package org.xbib.content.config.test;

View file

@ -1,6 +1,3 @@
#
# BSD-style license; for more info see http://pmd.sourceforge.net/license.html
#
rulesets.filenames=\ rulesets.filenames=\
category/java/bestpractices.xml,\ category/java/bestpractices.xml,\

View file

@ -4,7 +4,7 @@ checkstyle {
toolVersion '8.33' toolVersion '8.33'
showViolations = true showViolations = true
ignoreFailures = true ignoreFailures = true
configFile rootProject.file("config/checkstyle/checkstyle.xml") configFile rootProject.file("gradle/config/checkstyle/checkstyle.xml")
} }
checkstyleMain { checkstyleMain {

View file

@ -5,5 +5,5 @@ pmd {
consoleOutput = false consoleOutput = false
toolVersion = "6.24.0" toolVersion = "6.24.0"
rulePriority = 5 rulePriority = 5
ruleSets = ["category/java/bestpractices.xml"] ruleSetFiles = rootProject.files('gradle/config/pmd/category/java/bestpractices.xml')
} }

View file

@ -1,5 +1,4 @@
include 'content-api' include 'content-api'
include 'content-config'
include 'content-core' include 'content-core'
include 'content-csv' include 'content-csv'
include 'content-language' include 'content-language'
@ -16,3 +15,4 @@ include 'settings-content-yaml'
include 'settings-datastructures' include 'settings-datastructures'
include 'settings-datastructures-json' include 'settings-datastructures-json'
include 'settings-datastructures-yaml' include 'settings-datastructures-yaml'
include 'config'