add override by system properties, reduce config logger API
This commit is contained in:
parent
8774fcc1dd
commit
135bf78e58
5 changed files with 50 additions and 62 deletions
|
@ -15,6 +15,7 @@ import java.nio.file.Paths;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
@ -40,27 +41,39 @@ public class ConfigLoader {
|
|||
for (String fileNameWithoutSuffix : fileNamesWithoutSuffix) {
|
||||
Settings.Builder settings = createSettingsFromFile(createListOfLocations(applicationName, fileNameWithoutSuffix));
|
||||
if (settings != null) {
|
||||
return settings;
|
||||
return overrideFromProperties(applicationName, settings);
|
||||
}
|
||||
if (classLoader != null) {
|
||||
settings = createClasspathSettings(classLoader, applicationName, fileNameWithoutSuffix);
|
||||
if (settings != null) {
|
||||
return settings;
|
||||
for (ClassLoader cl : List.of(classLoader,
|
||||
Thread.currentThread().getContextClassLoader(),
|
||||
ConfigLoader.class.getClassLoader(),
|
||||
ClassLoader.getSystemClassLoader())) {
|
||||
if (cl != null) {
|
||||
settings = createClasspathSettings(cl, applicationName, fileNameWithoutSuffix);
|
||||
if (settings != null) {
|
||||
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 + " " +
|
||||
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,
|
||||
String applicationName,
|
||||
String fileNameWithoutSuffix)
|
||||
|
@ -79,21 +92,6 @@ public class ConfigLoader {
|
|||
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 {
|
||||
if (inputStream == null) {
|
||||
logger.error("unable to open input stream");
|
||||
|
@ -114,6 +112,18 @@ public class ConfigLoader {
|
|||
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) {
|
||||
return List.of(
|
||||
applicationName + '-' + fileNameWithoutSuffix + YML,
|
||||
|
|
|
@ -1,18 +1,10 @@
|
|||
package org.xbib.content.config;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
public interface ConfigLogger {
|
||||
|
||||
void setLevel(String level);
|
||||
|
||||
void info(String string);
|
||||
|
||||
void warn(String string);
|
||||
|
||||
void error(String message);
|
||||
|
||||
void error(String message, Throwable throwable);
|
||||
|
||||
OutputStream getStderrOutputStream();
|
||||
|
||||
OutputStream getStdoutOutputStream();
|
||||
}
|
||||
|
|
|
@ -1,37 +1,19 @@
|
|||
package org.xbib.content.config;
|
||||
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class SystemConfigLogger implements ConfigLogger {
|
||||
|
||||
@Override
|
||||
public void setLevel(String level) {
|
||||
// d nothing
|
||||
public void info(String string) {
|
||||
System.out.println("info: " + string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void info(String string) {
|
||||
System.out.println(string);
|
||||
public void warn(String message) {
|
||||
System.out.println("warning: " + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void error(String message) {
|
||||
System.err.println(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;
|
||||
System.out.println("error: " + message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -424,6 +424,10 @@ public class Settings implements AutoCloseable {
|
|||
map = TinyMap.builder();
|
||||
}
|
||||
|
||||
public Map<String, String> map() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public String remove(String key) {
|
||||
return map.remove(key);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
group = org.xbib
|
||||
name = content
|
||||
version = 2.5.5
|
||||
version = 2.6.0
|
||||
|
||||
gradle.wrapper.version = 6.6.1
|
||||
xbib.net.version = 2.1.0
|
||||
|
|
Loading…
Reference in a new issue