add override by system properties, reduce config logger API

This commit is contained in:
Jörg Prante 2020-10-13 10:05:54 +02:00
parent 8774fcc1dd
commit 135bf78e58
5 changed files with 50 additions and 62 deletions

View file

@ -15,6 +15,7 @@ import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
@ -40,27 +41,39 @@ public class ConfigLoader {
for (String fileNameWithoutSuffix : fileNamesWithoutSuffix) { for (String fileNameWithoutSuffix : fileNamesWithoutSuffix) {
Settings.Builder settings = createSettingsFromFile(createListOfLocations(applicationName, fileNameWithoutSuffix)); Settings.Builder settings = createSettingsFromFile(createListOfLocations(applicationName, fileNameWithoutSuffix));
if (settings != null) { if (settings != null) {
return settings; return overrideFromProperties(applicationName, settings);
} }
if (classLoader != null) { for (ClassLoader cl : List.of(classLoader,
settings = createClasspathSettings(classLoader, applicationName, fileNameWithoutSuffix); Thread.currentThread().getContextClassLoader(),
ConfigLoader.class.getClassLoader(),
ClassLoader.getSystemClassLoader())) {
if (cl != null) {
settings = createClasspathSettings(cl, applicationName, fileNameWithoutSuffix);
if (settings != null) { if (settings != null) {
return settings; return overrideFromProperties(applicationName, settings);
} }
} }
settings = createClasspathSettings(ConfigLoader.class.getClassLoader(), applicationName, fileNameWithoutSuffix);
if (settings != null) {
return settings;
}
settings = createClasspathSettings(ClassLoader.getSystemClassLoader(), applicationName, fileNameWithoutSuffix);
if (settings != null) {
return settings;
} }
} }
throw new IllegalArgumentException("no config found for " + applicationName + " " + throw new IllegalArgumentException("no config found for " + applicationName + " " +
Arrays.asList(fileNamesWithoutSuffix)); Arrays.asList(fileNamesWithoutSuffix));
} }
private Settings.Builder createSettingsFromFile(List<String> settingsFileNames) throws IOException {
for (String settingsFileName: settingsFileNames) {
int pos = settingsFileName.lastIndexOf('.');
String suffix = (pos > 0 ? settingsFileName.substring(pos) : "").toLowerCase(Locale.ROOT);
Path path = Paths.get(settingsFileName);
logger.info("trying " + path.toString());
if (Files.exists(path)) {
logger.info("found path: " + path);
System.setProperty("config.path", path.getParent().toString());
return createSettingsFromStream(Files.newInputStream(path), suffix);
}
}
return null;
}
private Settings.Builder createClasspathSettings(ClassLoader classLoader, private Settings.Builder createClasspathSettings(ClassLoader classLoader,
String applicationName, String applicationName,
String fileNameWithoutSuffix) String fileNameWithoutSuffix)
@ -79,21 +92,6 @@ public class ConfigLoader {
return null; return null;
} }
private Settings.Builder createSettingsFromFile(List<String> settingsFileNames) throws IOException {
for (String settingsFileName: settingsFileNames) {
int pos = settingsFileName.lastIndexOf('.');
String suffix = (pos > 0 ? settingsFileName.substring(pos) : "").toLowerCase(Locale.ROOT);
Path path = Paths.get(settingsFileName);
logger.info("trying " + path.toString());
if (Files.exists(path)) {
logger.info("found path: " + path);
System.setProperty("config.path", path.getParent().toString());
return createSettingsFromStream(Files.newInputStream(path), suffix);
}
}
return null;
}
private Settings.Builder createSettingsFromStream(InputStream inputStream, String suffix) throws IOException { private Settings.Builder createSettingsFromStream(InputStream inputStream, String suffix) throws IOException {
if (inputStream == null) { if (inputStream == null) {
logger.error("unable to open input stream"); logger.error("unable to open input stream");
@ -114,6 +112,18 @@ public class ConfigLoader {
return null; return null;
} }
private Settings.Builder overrideFromProperties(String applicationName, Settings.Builder settings) {
for (Map.Entry<String, String> entry : settings.map().entrySet()) {
String key = entry.getKey();
String value = System.getProperty(applicationName + '.' + key);
if (value != null) {
logger.warn("overriding " + key + " with " + value);
settings.put(key, value);
}
}
return settings;
}
private static List<String> createListOfLocations(String applicationName, String fileNameWithoutSuffix) { private static List<String> createListOfLocations(String applicationName, String fileNameWithoutSuffix) {
return List.of( return List.of(
applicationName + '-' + fileNameWithoutSuffix + YML, applicationName + '-' + fileNameWithoutSuffix + YML,

View file

@ -1,18 +1,10 @@
package org.xbib.content.config; package org.xbib.content.config;
import java.io.OutputStream;
public interface ConfigLogger { public interface ConfigLogger {
void setLevel(String level);
void info(String string); void info(String string);
void warn(String string);
void error(String message); void error(String message);
void error(String message, Throwable throwable);
OutputStream getStderrOutputStream();
OutputStream getStdoutOutputStream();
} }

View file

@ -1,37 +1,19 @@
package org.xbib.content.config; package org.xbib.content.config;
import java.io.OutputStream;
public class SystemConfigLogger implements ConfigLogger { public class SystemConfigLogger implements ConfigLogger {
@Override @Override
public void setLevel(String level) { public void info(String string) {
// d nothing System.out.println("info: " + string);
} }
@Override @Override
public void info(String string) { public void warn(String message) {
System.out.println(string); System.out.println("warning: " + message);
} }
@Override @Override
public void error(String message) { public void error(String message) {
System.err.println(message); System.out.println("error: " + message);
}
@Override
public void error(String message, Throwable throwable) {
System.err.println(message);
System.err.println(ExceptionFormatter.format(throwable));
}
@Override
public OutputStream getStderrOutputStream() {
return System.err;
}
@Override
public OutputStream getStdoutOutputStream() {
return System.out;
} }
} }

View file

@ -424,6 +424,10 @@ public class Settings implements AutoCloseable {
map = TinyMap.builder(); map = TinyMap.builder();
} }
public Map<String, String> map() {
return map;
}
public String remove(String key) { public String remove(String key) {
return map.remove(key); return map.remove(key);
} }

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = content name = content
version = 2.5.5 version = 2.6.0
gradle.wrapper.version = 6.6.1 gradle.wrapper.version = 6.6.1
xbib.net.version = 2.1.0 xbib.net.version = 2.1.0