fix unspecified string settings load
This commit is contained in:
parent
7b220eac39
commit
54b000429d
16 changed files with 65 additions and 107 deletions
|
@ -13,8 +13,8 @@ public interface Settings extends AutoCloseable {
|
|||
|
||||
private static SettingsBuilder createBuilder() {
|
||||
ServiceLoader<SettingsBuilder> serviceLoader = ServiceLoader.load(SettingsBuilder.class);
|
||||
Optional<SettingsBuilder> optionalConfigLogger = serviceLoader.findFirst();
|
||||
return optionalConfigLogger.orElse(null);
|
||||
Optional<SettingsBuilder> optionalSettingsBuilder = serviceLoader.findFirst();
|
||||
return optionalSettingsBuilder.orElse(null);
|
||||
}
|
||||
|
||||
private static final Settings emptySettings = createBuilder().build();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.xbib.settings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
|
@ -36,9 +37,9 @@ public interface SettingsBuilder {
|
|||
|
||||
SettingsBuilder put(Map<String, String> settings);
|
||||
|
||||
SettingsBuilder loadFromString(String source);
|
||||
SettingsBuilder loadFromString(String resourceName, String content);
|
||||
|
||||
SettingsBuilder loadFromResource(String resourceName, InputStream inputStream) throws SettingsException;
|
||||
SettingsBuilder loadFromResource(String resourceName, InputStream inputStream);
|
||||
|
||||
default SettingsBuilder fromJdbc(Connection connection, String statement, String[] params) throws SQLException {
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement(statement, params);
|
||||
|
|
|
@ -20,11 +20,14 @@ public interface SettingsLoader {
|
|||
* Loads the settings from a source string.
|
||||
* @param source the source
|
||||
* @return a Map
|
||||
* @throws IOException if load fails
|
||||
*/
|
||||
Map<String, String> load(String source) throws IOException;
|
||||
|
||||
/**
|
||||
* Loads the settings from a map.
|
||||
* @param source the map with the source
|
||||
* @return a Map
|
||||
*/
|
||||
Map<String, String> load(Map<String, Object> source) throws IOException;
|
||||
|
||||
boolean canLoad(String source);
|
||||
}
|
||||
|
|
|
@ -44,20 +44,6 @@ public final class SettingsLoaderService {
|
|||
throw new IllegalArgumentException("no settings loader for " + resourceName + " in " + settingsLoaderMap.keySet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link SettingsLoader} based on the actual source.
|
||||
* @param source the source
|
||||
* @return the settings loader
|
||||
*/
|
||||
public SettingsLoader loaderFromString(String source) {
|
||||
for (SettingsLoader loader : settingsLoaderMap.values()) {
|
||||
if (loader.canLoad(source)) {
|
||||
return loader;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("no settings loader");
|
||||
}
|
||||
|
||||
public Set<String> getSuffixes() {
|
||||
Set<String> suffixes = new HashSet<>();
|
||||
for (Set<String> set : settingsLoaderMap.keySet()) {
|
||||
|
|
|
@ -21,8 +21,4 @@ public class JsonSettingsLoader extends AbstractSettingsLoader {
|
|||
return Set.of("json");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source.indexOf('{') != -1 && source.indexOf('}') != -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,9 +47,9 @@ public class JsonSettingsTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testLoadSettingsFromString() {
|
||||
public void testLoadSettingsFromString() throws IOException {
|
||||
String json = "{\"Hello\":\"World\"}";
|
||||
Settings settings = Settings.settingsBuilder().loadFromString(json).build();
|
||||
Settings settings = Settings.settingsBuilder().loadFromString("json", json).build();
|
||||
assertEquals("{Hello=World}", settings.getAsMap().toString());
|
||||
}
|
||||
|
||||
|
|
|
@ -30,9 +30,4 @@ public class YamlSettingsLoader extends AbstractSettingsLoader {
|
|||
// replace tabs with whitespace (yaml does not accept tabs, but many users might use it still...)
|
||||
return super.load(source.replace("\t", " "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source.indexOf(':') != -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ import java.util.function.Function;
|
|||
*/
|
||||
public class ContentSettingsBuilder implements SettingsBuilder {
|
||||
|
||||
private final SettingsLoaderService settingsLoaderService = SettingsLoaderService.getInstance();
|
||||
private final SettingsLoaderService settingsLoaderService;
|
||||
|
||||
private final Map<String, String> map;
|
||||
|
||||
|
@ -41,7 +41,8 @@ public class ContentSettingsBuilder implements SettingsBuilder {
|
|||
private TimeUnit timeUnit;
|
||||
|
||||
public ContentSettingsBuilder() {
|
||||
map = TinyMap.builder();
|
||||
this.settingsLoaderService = SettingsLoaderService.getInstance();
|
||||
this.map = TinyMap.builder();
|
||||
}
|
||||
|
||||
public String remove(String key) {
|
||||
|
@ -227,24 +228,6 @@ public class ContentSettingsBuilder implements SettingsBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from the actual string content that represents them using the
|
||||
* {@link SettingsLoaderService#loaderFromString(String)}.
|
||||
*
|
||||
* @param source source
|
||||
* @return builder
|
||||
*/
|
||||
public ContentSettingsBuilder loadFromString(String source) {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromString(source);
|
||||
try {
|
||||
Map<String, String> loadedSettings = settingsLoader.load(source);
|
||||
put(loadedSettings);
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("Failed to load settings from [" + source + "]", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from an URL.
|
||||
*
|
||||
|
@ -267,16 +250,31 @@ public class ContentSettingsBuilder implements SettingsBuilder {
|
|||
* @return builder
|
||||
*/
|
||||
@Override
|
||||
public ContentSettingsBuilder loadFromResource(String resourceName, InputStream inputStream) throws SettingsException {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromResource(resourceName);
|
||||
public ContentSettingsBuilder loadFromResource(String resourceName, InputStream inputStream) {
|
||||
try {
|
||||
Map<String, String> loadedSettings = settingsLoader
|
||||
.load(ContentSettings.copyToString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)));
|
||||
put(loadedSettings);
|
||||
return loadFromString(resourceName, ContentSettings.copyToString(new InputStreamReader(inputStream, StandardCharsets.UTF_8)));
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("Failed to load settings from [" + resourceName + "]", e);
|
||||
throw new SettingsException("failed to load settings from [" + resourceName + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from the actual string content that represents them using the
|
||||
* {@link SettingsLoaderService#loaderFromResource(String)} (String)}.
|
||||
*
|
||||
* @param resourceName the resource name ("json", "yaml")
|
||||
* @param content the content
|
||||
* @return builder
|
||||
*/
|
||||
@Override
|
||||
public ContentSettingsBuilder loadFromString(String resourceName, String content) {
|
||||
try {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromResource(resourceName);
|
||||
put(settingsLoader.load(content));
|
||||
return this;
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("failed to load settings from [" + resourceName + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -41,7 +41,7 @@ public class PropertiesSettingsLoader implements SettingsLoader {
|
|||
|
||||
|
||||
@Override
|
||||
public Map<String, String> load(Map<String, Object> source) throws IOException {
|
||||
public Map<String, String> load(Map<String, Object> source) {
|
||||
Properties props = new Properties();
|
||||
props.putAll(source);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
|
@ -51,11 +51,6 @@ public class PropertiesSettingsLoader implements SettingsLoader {
|
|||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source != null && source.charAt(0) == '#';
|
||||
}
|
||||
|
||||
public Map<String, String> load(BytesReference ref) throws IOException {
|
||||
Properties props = new Properties();
|
||||
try (Reader reader = new InputStreamReader(ref.streamInput(), StandardCharsets.UTF_8)) {
|
||||
|
|
|
@ -153,7 +153,7 @@ public class SettingsTest {
|
|||
@Test
|
||||
public void testPropertiesLoaderFromString() {
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.loadFromString("#\na.b=c")
|
||||
.loadFromString("properties", "#\na.b=c")
|
||||
.build();
|
||||
assertEquals("{a.b=c}", settings.getAsMap().toString());
|
||||
}
|
||||
|
|
|
@ -16,9 +16,4 @@ public class JsonSettingsLoader extends AbstractSettingsLoader {
|
|||
public Set<String> suffixes() {
|
||||
return Set.of("json");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source.indexOf('{') != -1 && source.indexOf('}') != -1;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,6 @@
|
|||
package org.xbib.settings.datastructures.json.test;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.xbib.datastructures.json.tiny.JsonBuilder;
|
||||
import org.xbib.settings.Settings;
|
||||
import org.xbib.settings.SettingsLoader;
|
||||
import org.xbib.settings.datastructures.json.JsonSettingsLoader;
|
||||
|
@ -53,7 +49,7 @@ public class JsonSettingsTest {
|
|||
@Test
|
||||
public void testLoadSettingsFromString() {
|
||||
String json = "{\"Hello\":\"World\"}";
|
||||
Settings settings = Settings.settingsBuilder().loadFromString(json).build();
|
||||
Settings settings = Settings.settingsBuilder().loadFromString("json", json).build();
|
||||
assertEquals("{Hello=World}", settings.getAsMap().toString());
|
||||
}
|
||||
|
||||
|
|
|
@ -20,11 +20,6 @@ public class YamlSettingsLoader extends AbstractSettingsLoader {
|
|||
return Set.of("yml", "yaml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source.indexOf(':') != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> load(String source) throws IOException {
|
||||
// replace tabs with whitespace (yaml does not accept tabs, but many users might use it still...)
|
||||
|
|
|
@ -28,12 +28,13 @@ import java.util.stream.Collectors;
|
|||
*/
|
||||
public class DatastructureSettingsBuilder implements SettingsBuilder {
|
||||
|
||||
private final SettingsLoaderService settingsLoaderService = SettingsLoaderService.getInstance();
|
||||
private final SettingsLoaderService settingsLoaderService;
|
||||
|
||||
private final TinyMap.Builder<String, String> map;
|
||||
|
||||
public DatastructureSettingsBuilder() {
|
||||
map = TinyMap.builder();
|
||||
this.settingsLoaderService = SettingsLoaderService.getInstance();
|
||||
this.map = TinyMap.builder();
|
||||
}
|
||||
|
||||
public String remove(String key) {
|
||||
|
@ -218,24 +219,6 @@ public class DatastructureSettingsBuilder implements SettingsBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from the actual string content that represents them using the
|
||||
* {@link SettingsLoaderService#loaderFromString(String)}.
|
||||
*
|
||||
* @param source source
|
||||
* @return builder
|
||||
*/
|
||||
@Override
|
||||
public DatastructureSettingsBuilder loadFromString(String source) {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromString(source);
|
||||
try {
|
||||
put(settingsLoader.load(source));
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("failed to load settings from [" + source + "]", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from a resource.
|
||||
*
|
||||
|
@ -244,7 +227,7 @@ public class DatastructureSettingsBuilder implements SettingsBuilder {
|
|||
* @return builder
|
||||
*/
|
||||
@Override
|
||||
public DatastructureSettingsBuilder loadFromResource(String resourceName, InputStream inputStream) throws SettingsException {
|
||||
public DatastructureSettingsBuilder loadFromResource(String resourceName, InputStream inputStream) {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromResource(resourceName);
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||
Map<String, String> loadedSettings = settingsLoader.load(bufferedReader.lines().collect(Collectors.joining()));
|
||||
|
@ -255,6 +238,25 @@ public class DatastructureSettingsBuilder implements SettingsBuilder {
|
|||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads settings from the actual string content that represents them using the
|
||||
* {@link SettingsLoaderService#loaderFromResource(String)} (String)}.
|
||||
*
|
||||
* @param resourceName the resource name
|
||||
* @param source the source
|
||||
* @return builder
|
||||
*/
|
||||
@Override
|
||||
public DatastructureSettingsBuilder loadFromString(String resourceName, String source) {
|
||||
SettingsLoader settingsLoader = settingsLoaderService.loaderFromResource(resourceName);
|
||||
try {
|
||||
put(settingsLoader.load(source));
|
||||
} catch (Exception e) {
|
||||
throw new SettingsException("failed to load settings from [" + source + "]", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load system properties to this settings.
|
||||
*
|
||||
|
|
|
@ -41,9 +41,4 @@ public class PropertiesSettingsLoader implements SettingsLoader {
|
|||
}
|
||||
return result.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad(String source) {
|
||||
return source != null && source.charAt(0) == '#';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
|
|||
import org.xbib.settings.Settings;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
|
@ -126,7 +127,7 @@ public class SettingsTest {
|
|||
@Test
|
||||
public void testPropertiesLoaderFromString() {
|
||||
Settings settings = Settings.settingsBuilder()
|
||||
.loadFromString("#\na.b=c")
|
||||
.loadFromString("properties", "#\na.b=c")
|
||||
.build();
|
||||
assertEquals("{a.b=c}", settings.getAsMap().toString());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue