From 9846847a61d73235e483f2ff9868e065f2d73fea Mon Sep 17 00:00:00 2001 From: kimchy Date: Sun, 11 Apr 2010 10:26:02 +0300 Subject: [PATCH] allow for settings loader to load data from byte[], not just string --- .../util/settings/loader/JsonSettingsLoader.java | 10 ++++++++++ .../loader/PropertiesSettingsLoader.java | 11 +++++++++++ .../util/settings/loader/SettingsLoader.java | 7 ++++++- .../util/settings/loader/YamlSettingsLoader.java | 16 +++++++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/JsonSettingsLoader.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/JsonSettingsLoader.java index 8ef5c7b23ab..a3d0b3381d7 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/JsonSettingsLoader.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/JsonSettingsLoader.java @@ -22,6 +22,7 @@ package org.elasticsearch.util.settings.loader; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; +import org.elasticsearch.util.io.FastByteArrayInputStream; import org.elasticsearch.util.io.FastStringReader; import org.elasticsearch.util.json.Jackson; @@ -51,6 +52,15 @@ public class JsonSettingsLoader implements SettingsLoader { } } + @Override public Map load(byte[] source) throws IOException { + JsonParser jp = jsonFactory.createJsonParser(new FastByteArrayInputStream(source)); + try { + return load(jp); + } finally { + jp.close(); + } + } + public Map load(JsonParser jp) throws IOException { StringBuilder sb = new StringBuilder(); Map settings = newHashMap(); diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/PropertiesSettingsLoader.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/PropertiesSettingsLoader.java index 6d75f97ff85..c95b3fdd11b 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/PropertiesSettingsLoader.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/PropertiesSettingsLoader.java @@ -19,6 +19,7 @@ package org.elasticsearch.util.settings.loader; +import org.elasticsearch.util.io.FastByteArrayInputStream; import org.elasticsearch.util.io.FastStringReader; import java.io.IOException; @@ -43,4 +44,14 @@ public class PropertiesSettingsLoader implements SettingsLoader { } return result; } + + @Override public Map load(byte[] source) throws IOException { + Properties props = new Properties(); + props.load(new FastByteArrayInputStream(source)); + Map result = newHashMap(); + for (Map.Entry entry : props.entrySet()) { + result.put((String) entry.getKey(), (String) entry.getValue()); + } + return result; + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/SettingsLoader.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/SettingsLoader.java index 934a7fca4dd..baa4c7b3b7e 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/SettingsLoader.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/SettingsLoader.java @@ -26,7 +26,7 @@ import java.util.Map; * Provides the ability to load settings (in the form of a simple Map) from * the actual source content that represents them. * - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public interface SettingsLoader { @@ -34,4 +34,9 @@ public interface SettingsLoader { * Loads (parses) the settings from a source string. */ Map load(String source) throws IOException; + + /** + * Loads (parses) the settings from a source bytes. + */ + Map load(byte[] source) throws IOException; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/YamlSettingsLoader.java b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/YamlSettingsLoader.java index c918d62d012..ebd50944c4c 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/YamlSettingsLoader.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/util/settings/loader/YamlSettingsLoader.java @@ -19,6 +19,7 @@ package org.elasticsearch.util.settings.loader; +import org.elasticsearch.util.io.FastByteArrayInputStream; import org.elasticsearch.util.yaml.snakeyaml.Yaml; import java.io.IOException; @@ -32,7 +33,7 @@ import static com.google.common.collect.Maps.*; * Settings loader that loads (parses) the settings in a yaml format by flattening them * into a map. * - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class YamlSettingsLoader implements SettingsLoader { @@ -51,6 +52,19 @@ public class YamlSettingsLoader implements SettingsLoader { return settings; } + @Override public Map load(byte[] source) throws IOException { + Yaml yaml = new Yaml(); + Map yamlMap = (Map) yaml.load(new FastByteArrayInputStream(source)); + StringBuilder sb = new StringBuilder(); + Map settings = newHashMap(); + if (yamlMap == null) { + return settings; + } + List path = newArrayList(); + serializeMap(settings, sb, path, yamlMap); + return settings; + } + private void serializeMap(Map settings, StringBuilder sb, List path, Map yamlMap) { for (Map.Entry entry : yamlMap.entrySet()) { if (entry.getValue() instanceof Map) {