From b9ac7f87bd7016ba276c5ffcbf76f1777c3fb332 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 18 Oct 2016 00:43:00 +0200 Subject: [PATCH] adding properties loading, fixes settings loader service --- .../settings/PropertiesSettingsLoader.java | 41 +++++++++++++++++++ .../org/xbib/content/settings/Settings.java | 30 +++++++++++--- .../xbib/content/settings/SettingsLoader.java | 4 ++ .../settings/SettingsLoaderService.java | 9 +++- .../org.xbib.content.settings.SettingsLoader | 3 +- .../xbib/content/settings/SettingsTest.java | 36 ++++++++++++++-- .../content/xml/XContentXmlBuilderTest.java | 2 +- .../xbib/content/xml/util/XMLUtilTest.java | 2 +- gradle.properties | 2 +- 9 files changed, 115 insertions(+), 14 deletions(-) create mode 100644 content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java diff --git a/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java b/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java new file mode 100644 index 0000000..2fac5bf --- /dev/null +++ b/content-core/src/main/java/org/xbib/content/settings/PropertiesSettingsLoader.java @@ -0,0 +1,41 @@ +package org.xbib.content.settings; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +/** + * Settings loader that loads (parses) the settings in a properties format. + */ +public class PropertiesSettingsLoader implements SettingsLoader { + + private static final Set PROPERTIES_SUFFIXES = new HashSet<>(Collections.singletonList("properties")); + + @Override + public Set suffixes() { + return PROPERTIES_SUFFIXES; + } + + @Override + public Map load(String source) throws IOException { + Properties props = new Properties(); + try (StringReader reader = new StringReader(source)) { + props.load(reader); + Map result = new HashMap<>(); + for (Map.Entry entry : props.entrySet()) { + result.put((String) entry.getKey(), (String) entry.getValue()); + } + return result; + } + } + + @Override + public boolean canLoad(String source) { + return true; + } +} diff --git a/content-core/src/main/java/org/xbib/content/settings/Settings.java b/content-core/src/main/java/org/xbib/content/settings/Settings.java index bebbead..d477a71 100644 --- a/content-core/src/main/java/org/xbib/content/settings/Settings.java +++ b/content-core/src/main/java/org/xbib/content/settings/Settings.java @@ -43,11 +43,7 @@ public class Settings { public static final int BUFFER_SIZE = 1024 * 8; private final Map settings; - public Settings() { - this(new HashMap<>()); - } - - public Settings(Map settings) { + private Settings(Map settings) { this.settings = new HashMap<>(settings); } @@ -666,6 +662,27 @@ public class Settings { return this; } + /** + * Load system properties to this settings. + * @return builder + */ + public Builder loadFromSystemProperties() { + for (Map.Entry entry : System.getProperties().entrySet()) { + put((String)entry.getKey(), (String)entry.getValue()); + } + return this; + } + + /** + * Load system environment to this settings. + * @return builder + */ + public Builder loadFromSystemEnvironment() { + for (Map.Entry entry : System.getenv().entrySet()) { + put(entry.getKey(), entry.getValue()); + } + return this; + } /** * Runs across all the settings set on this builder and replaces ${...} elements in the * each setting value according to the following logic: @@ -679,14 +696,17 @@ public class Settings { public Builder replacePropertyPlaceholders() { PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false); PropertyPlaceholder.PlaceholderResolver placeholderResolver = placeholderName -> { + // system property String value = System.getProperty(placeholderName); if (value != null) { return value; } + // environment value = System.getenv(placeholderName); if (value != null) { return value; } + // current date try { return DateTimeFormatter.ofPattern(placeholderName).format(LocalDate.now()); } catch (IllegalArgumentException | DateTimeException e) { diff --git a/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java b/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java index bfe8eda..210885c 100644 --- a/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java +++ b/content-core/src/main/java/org/xbib/content/settings/SettingsLoader.java @@ -10,6 +10,10 @@ import java.util.Set; */ public interface SettingsLoader { + /** + * Suffices for file names to load from. + * @return a set of suffices + */ Set suffixes(); /** diff --git a/content-core/src/main/java/org/xbib/content/settings/SettingsLoaderService.java b/content-core/src/main/java/org/xbib/content/settings/SettingsLoaderService.java index 03c7927..60c4208 100644 --- a/content-core/src/main/java/org/xbib/content/settings/SettingsLoaderService.java +++ b/content-core/src/main/java/org/xbib/content/settings/SettingsLoaderService.java @@ -16,9 +16,10 @@ public final class SettingsLoaderService { private static final Logger logger = Logger.getLogger(SettingsLoaderService.class.getName()); - private static final Map, SettingsLoader> settingsLoaderMap = new HashMap<>(); + private static final Map, SettingsLoader> settingsLoaderMap; - private SettingsLoaderService() { + static { + settingsLoaderMap = new HashMap<>(); try { ServiceLoader serviceLoader = ServiceLoader.load(SettingsLoader.class); for (SettingsLoader settingsLoader : serviceLoader) { @@ -30,6 +31,10 @@ public final class SettingsLoaderService { logger.log(Level.SEVERE, e.getMessage(), e); } } + + private SettingsLoaderService() { + } + /** * Returns a {@link SettingsLoader} based on the resource name. * @param resourceName the resource diff --git a/content-core/src/main/resources/META-INF/services/org.xbib.content.settings.SettingsLoader b/content-core/src/main/resources/META-INF/services/org.xbib.content.settings.SettingsLoader index 4d22e56..bda82af 100644 --- a/content-core/src/main/resources/META-INF/services/org.xbib.content.settings.SettingsLoader +++ b/content-core/src/main/resources/META-INF/services/org.xbib.content.settings.SettingsLoader @@ -1 +1,2 @@ -org.xbib.content.json.JsonSettingsLoader \ No newline at end of file +org.xbib.content.json.JsonSettingsLoader +org.xbib.content.settings.PropertiesSettingsLoader \ No newline at end of file diff --git a/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java b/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java index a354c56..0c9b42f 100644 --- a/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java +++ b/content-core/src/test/java/org/xbib/content/settings/SettingsTest.java @@ -1,12 +1,13 @@ package org.xbib.content.settings; -import static org.xbib.content.settings.Settings.settingsBuilder; import org.junit.Assert; import org.junit.Test; import org.xbib.content.XContentHelper; +import java.io.ByteArrayInputStream; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -16,6 +17,12 @@ import java.util.Map; */ public class SettingsTest extends Assert { + @Test + public void testEmpty() { + Settings settings = Settings.EMPTY_SETTINGS; + assertTrue(settings.getAsMap().isEmpty()); + } + @Test public void testArray() { Settings settings = Settings.settingsBuilder() @@ -51,7 +58,7 @@ public class SettingsTest extends Assert { map.put("hello", "world"); Map settingsMap = new HashMap<>(); settingsMap.put("map", map); - Settings settings = settingsBuilder().loadFromMap(settingsMap).build(); + Settings settings = Settings.settingsBuilder().loadFromMap(settingsMap).build(); assertEquals("{map.hello=world}", settings.getAsMap().toString()); } @@ -59,7 +66,7 @@ public class SettingsTest extends Assert { public void testMapSettingsFromReader() { StringReader reader = new StringReader("{\"map\":{\"hello\":\"world\"}}"); Map spec = XContentHelper.convertFromJsonToMap(reader); - Settings settings = settingsBuilder().loadFromMap(spec).build(); + Settings settings = Settings.settingsBuilder().loadFromMap(spec).build(); assertEquals("{map.hello=world}", settings.getAsMap().toString()); } @@ -72,4 +79,27 @@ public class SettingsTest extends Assert { assertTrue(Integer.parseInt(settings.get("date")) > 2000); } + @Test + public void testSystemEnvironment() { + Settings settings = Settings.settingsBuilder() + .loadFromSystemEnvironment() + .build(); + assertTrue(!settings.getAsMap().isEmpty()); + } + + @Test + public void testSystemProperties() { + Settings settings = Settings.settingsBuilder() + .loadFromSystemProperties() + .build(); + assertTrue(!settings.getAsMap().isEmpty()); + } + + @Test + public void testPropertiesLoader() { + Settings settings = Settings.settingsBuilder() + .loadFromStream(".properties", new ByteArrayInputStream("a.b=c".getBytes(StandardCharsets.UTF_8))) + .build(); + assertEquals("{a.b=c}", settings.getAsMap().toString()); + } } diff --git a/content-xml/src/test/java/org/xbib/content/xml/XContentXmlBuilderTest.java b/content-xml/src/test/java/org/xbib/content/xml/XContentXmlBuilderTest.java index 78b57ea..28653cf 100644 --- a/content-xml/src/test/java/org/xbib/content/xml/XContentXmlBuilderTest.java +++ b/content-xml/src/test/java/org/xbib/content/xml/XContentXmlBuilderTest.java @@ -189,7 +189,7 @@ public class XContentXmlBuilderTest extends Assert { QName root = new QName("root"); XContentBuilder builder = XmlXContent.contentBuilder(new XmlXParams(root)); builder.startObject().field("Hello", "World\u001b").endObject(); - assertEquals("World�", builder.string()); + assertEquals("World\ufffd", builder.string()); } @Test diff --git a/content-xml/src/test/java/org/xbib/content/xml/util/XMLUtilTest.java b/content-xml/src/test/java/org/xbib/content/xml/util/XMLUtilTest.java index fb4d331..84ba2be 100644 --- a/content-xml/src/test/java/org/xbib/content/xml/util/XMLUtilTest.java +++ b/content-xml/src/test/java/org/xbib/content/xml/util/XMLUtilTest.java @@ -17,6 +17,6 @@ public class XMLUtilTest extends Assert { @Test public void testWhitespaceCleanerWithReplacementCharacter() { String s = "Hello World\u001b"; - assertEquals(XMLUtil.sanitizeXml10(s), "Hello World�"); + assertEquals("Hello World\ufffd", XMLUtil.sanitizeXml10(s)); } } diff --git a/gradle.properties b/gradle.properties index 5b041cc..e26553b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib -version = 1.0.0 +version = 1.0.1 org.gradle.daemon = true