From 54b000429de330c7bc54ee0838ecf7537825085f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Thu, 6 Jan 2022 14:12:24 +0100 Subject: [PATCH] fix unspecified string settings load --- .../main/java/org/xbib/settings/Settings.java | 4 +- .../org/xbib/settings/SettingsBuilder.java | 5 +- .../org/xbib/settings/SettingsLoader.java | 7 ++- .../xbib/settings/SettingsLoaderService.java | 14 ----- .../content/json/JsonSettingsLoader.java | 4 -- .../content/json/JsonSettingsTest.java | 4 +- .../content/yaml/YamlSettingsLoader.java | 5 -- .../content/ContentSettingsBuilder.java | 52 +++++++++---------- .../content/PropertiesSettingsLoader.java | 7 +-- .../settings/content/test/SettingsTest.java | 2 +- .../json/JsonSettingsLoader.java | 5 -- .../json/test/JsonSettingsTest.java | 6 +-- .../yaml/YamlSettingsLoader.java | 5 -- .../DatastructureSettingsBuilder.java | 44 ++++++++-------- .../PropertiesSettingsLoader.java | 5 -- .../datastructures/test/SettingsTest.java | 3 +- 16 files changed, 65 insertions(+), 107 deletions(-) diff --git a/settings-api/src/main/java/org/xbib/settings/Settings.java b/settings-api/src/main/java/org/xbib/settings/Settings.java index bdc64b5..24f2778 100644 --- a/settings-api/src/main/java/org/xbib/settings/Settings.java +++ b/settings-api/src/main/java/org/xbib/settings/Settings.java @@ -13,8 +13,8 @@ public interface Settings extends AutoCloseable { private static SettingsBuilder createBuilder() { ServiceLoader serviceLoader = ServiceLoader.load(SettingsBuilder.class); - Optional optionalConfigLogger = serviceLoader.findFirst(); - return optionalConfigLogger.orElse(null); + Optional optionalSettingsBuilder = serviceLoader.findFirst(); + return optionalSettingsBuilder.orElse(null); } private static final Settings emptySettings = createBuilder().build(); diff --git a/settings-api/src/main/java/org/xbib/settings/SettingsBuilder.java b/settings-api/src/main/java/org/xbib/settings/SettingsBuilder.java index 8c97ad8..1666e37 100644 --- a/settings-api/src/main/java/org/xbib/settings/SettingsBuilder.java +++ b/settings-api/src/main/java/org/xbib/settings/SettingsBuilder.java @@ -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 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); diff --git a/settings-api/src/main/java/org/xbib/settings/SettingsLoader.java b/settings-api/src/main/java/org/xbib/settings/SettingsLoader.java index dbbe866..69cbb09 100644 --- a/settings-api/src/main/java/org/xbib/settings/SettingsLoader.java +++ b/settings-api/src/main/java/org/xbib/settings/SettingsLoader.java @@ -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 load(String source) throws IOException; + /** + * Loads the settings from a map. + * @param source the map with the source + * @return a Map + */ Map load(Map source) throws IOException; - boolean canLoad(String source); } diff --git a/settings-api/src/main/java/org/xbib/settings/SettingsLoaderService.java b/settings-api/src/main/java/org/xbib/settings/SettingsLoaderService.java index bb23481..54a2d02 100644 --- a/settings-api/src/main/java/org/xbib/settings/SettingsLoaderService.java +++ b/settings-api/src/main/java/org/xbib/settings/SettingsLoaderService.java @@ -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 getSuffixes() { Set suffixes = new HashSet<>(); for (Set set : settingsLoaderMap.keySet()) { diff --git a/settings-content-json/src/main/java/org/xbib/settings/content/json/JsonSettingsLoader.java b/settings-content-json/src/main/java/org/xbib/settings/content/json/JsonSettingsLoader.java index 712773d..cfd4280 100644 --- a/settings-content-json/src/main/java/org/xbib/settings/content/json/JsonSettingsLoader.java +++ b/settings-content-json/src/main/java/org/xbib/settings/content/json/JsonSettingsLoader.java @@ -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; - } } diff --git a/settings-content-json/src/test/java/org/xbib/settings/content/json/JsonSettingsTest.java b/settings-content-json/src/test/java/org/xbib/settings/content/json/JsonSettingsTest.java index cc2e8a2..c4c390a 100644 --- a/settings-content-json/src/test/java/org/xbib/settings/content/json/JsonSettingsTest.java +++ b/settings-content-json/src/test/java/org/xbib/settings/content/json/JsonSettingsTest.java @@ -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()); } diff --git a/settings-content-yaml/src/main/java/org/xbib/settings/content/yaml/YamlSettingsLoader.java b/settings-content-yaml/src/main/java/org/xbib/settings/content/yaml/YamlSettingsLoader.java index 500c3f8..5e07789 100644 --- a/settings-content-yaml/src/main/java/org/xbib/settings/content/yaml/YamlSettingsLoader.java +++ b/settings-content-yaml/src/main/java/org/xbib/settings/content/yaml/YamlSettingsLoader.java @@ -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; - } } diff --git a/settings-content/src/main/java/org/xbib/settings/content/ContentSettingsBuilder.java b/settings-content/src/main/java/org/xbib/settings/content/ContentSettingsBuilder.java index 4057bb8..91643b9 100644 --- a/settings-content/src/main/java/org/xbib/settings/content/ContentSettingsBuilder.java +++ b/settings-content/src/main/java/org/xbib/settings/content/ContentSettingsBuilder.java @@ -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 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 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 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); } - return this; } /** diff --git a/settings-content/src/main/java/org/xbib/settings/content/PropertiesSettingsLoader.java b/settings-content/src/main/java/org/xbib/settings/content/PropertiesSettingsLoader.java index ed2e83a..f101291 100644 --- a/settings-content/src/main/java/org/xbib/settings/content/PropertiesSettingsLoader.java +++ b/settings-content/src/main/java/org/xbib/settings/content/PropertiesSettingsLoader.java @@ -41,7 +41,7 @@ public class PropertiesSettingsLoader implements SettingsLoader { @Override - public Map load(Map source) throws IOException { + public Map load(Map source) { Properties props = new Properties(); props.putAll(source); Map 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 load(BytesReference ref) throws IOException { Properties props = new Properties(); try (Reader reader = new InputStreamReader(ref.streamInput(), StandardCharsets.UTF_8)) { diff --git a/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java b/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java index 58e6c63..540b71b 100644 --- a/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java +++ b/settings-content/src/test/java/org/xbib/settings/content/test/SettingsTest.java @@ -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()); } diff --git a/settings-datastructures-json/src/main/java/org/xbib/settings/datastructures/json/JsonSettingsLoader.java b/settings-datastructures-json/src/main/java/org/xbib/settings/datastructures/json/JsonSettingsLoader.java index eb12297..987e33a 100644 --- a/settings-datastructures-json/src/main/java/org/xbib/settings/datastructures/json/JsonSettingsLoader.java +++ b/settings-datastructures-json/src/main/java/org/xbib/settings/datastructures/json/JsonSettingsLoader.java @@ -16,9 +16,4 @@ public class JsonSettingsLoader extends AbstractSettingsLoader { public Set suffixes() { return Set.of("json"); } - - @Override - public boolean canLoad(String source) { - return source.indexOf('{') != -1 && source.indexOf('}') != -1; - } } diff --git a/settings-datastructures-json/src/test/java/org/xbib/settings/datastructures/json/test/JsonSettingsTest.java b/settings-datastructures-json/src/test/java/org/xbib/settings/datastructures/json/test/JsonSettingsTest.java index b6033b1..2622986 100644 --- a/settings-datastructures-json/src/test/java/org/xbib/settings/datastructures/json/test/JsonSettingsTest.java +++ b/settings-datastructures-json/src/test/java/org/xbib/settings/datastructures/json/test/JsonSettingsTest.java @@ -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()); } diff --git a/settings-datastructures-yaml/src/main/java/org/xbib/settings/datastructures/yaml/YamlSettingsLoader.java b/settings-datastructures-yaml/src/main/java/org/xbib/settings/datastructures/yaml/YamlSettingsLoader.java index d96ca84..3716cc3 100644 --- a/settings-datastructures-yaml/src/main/java/org/xbib/settings/datastructures/yaml/YamlSettingsLoader.java +++ b/settings-datastructures-yaml/src/main/java/org/xbib/settings/datastructures/yaml/YamlSettingsLoader.java @@ -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 load(String source) throws IOException { // replace tabs with whitespace (yaml does not accept tabs, but many users might use it still...) diff --git a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettingsBuilder.java b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettingsBuilder.java index e1cac3e..6179506 100644 --- a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettingsBuilder.java +++ b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/DatastructureSettingsBuilder.java @@ -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 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 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. * diff --git a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/PropertiesSettingsLoader.java b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/PropertiesSettingsLoader.java index b41cde5..bed385e 100644 --- a/settings-datastructures/src/main/java/org/xbib/settings/datastructures/PropertiesSettingsLoader.java +++ b/settings-datastructures/src/main/java/org/xbib/settings/datastructures/PropertiesSettingsLoader.java @@ -41,9 +41,4 @@ public class PropertiesSettingsLoader implements SettingsLoader { } return result.build(); } - - @Override - public boolean canLoad(String source) { - return source != null && source.charAt(0) == '#'; - } } diff --git a/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java b/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java index af7eac9..52005f5 100644 --- a/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java +++ b/settings-datastructures/src/test/java/org/xbib/settings/datastructures/test/SettingsTest.java @@ -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()); }