allow for settings loader to load data from byte[], not just string

This commit is contained in:
kimchy 2010-04-11 10:26:02 +03:00
parent 442e0e87b0
commit 9846847a61
4 changed files with 42 additions and 2 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.util.settings.loader;
import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.JsonToken;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.io.FastStringReader; import org.elasticsearch.util.io.FastStringReader;
import org.elasticsearch.util.json.Jackson; import org.elasticsearch.util.json.Jackson;
@ -51,6 +52,15 @@ public class JsonSettingsLoader implements SettingsLoader {
} }
} }
@Override public Map<String, String> load(byte[] source) throws IOException {
JsonParser jp = jsonFactory.createJsonParser(new FastByteArrayInputStream(source));
try {
return load(jp);
} finally {
jp.close();
}
}
public Map<String, String> load(JsonParser jp) throws IOException { public Map<String, String> load(JsonParser jp) throws IOException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Map<String, String> settings = newHashMap(); Map<String, String> settings = newHashMap();

View File

@ -19,6 +19,7 @@
package org.elasticsearch.util.settings.loader; package org.elasticsearch.util.settings.loader;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.io.FastStringReader; import org.elasticsearch.util.io.FastStringReader;
import java.io.IOException; import java.io.IOException;
@ -43,4 +44,14 @@ public class PropertiesSettingsLoader implements SettingsLoader {
} }
return result; return result;
} }
@Override public Map<String, String> load(byte[] source) throws IOException {
Properties props = new Properties();
props.load(new FastByteArrayInputStream(source));
Map<String, String> result = newHashMap();
for (Map.Entry entry : props.entrySet()) {
result.put((String) entry.getKey(), (String) entry.getValue());
}
return result;
}
} }

View File

@ -26,7 +26,7 @@ import java.util.Map;
* Provides the ability to load settings (in the form of a simple Map) from * Provides the ability to load settings (in the form of a simple Map) from
* the actual source content that represents them. * the actual source content that represents them.
* *
* @author kimchy (Shay Banon) * @author kimchy (shay.banon)
*/ */
public interface SettingsLoader { public interface SettingsLoader {
@ -34,4 +34,9 @@ public interface SettingsLoader {
* Loads (parses) the settings from a source string. * Loads (parses) the settings from a source string.
*/ */
Map<String, String> load(String source) throws IOException; Map<String, String> load(String source) throws IOException;
/**
* Loads (parses) the settings from a source bytes.
*/
Map<String, String> load(byte[] source) throws IOException;
} }

View File

@ -19,6 +19,7 @@
package org.elasticsearch.util.settings.loader; package org.elasticsearch.util.settings.loader;
import org.elasticsearch.util.io.FastByteArrayInputStream;
import org.elasticsearch.util.yaml.snakeyaml.Yaml; import org.elasticsearch.util.yaml.snakeyaml.Yaml;
import java.io.IOException; 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 * Settings loader that loads (parses) the settings in a yaml format by flattening them
* into a map. * into a map.
* *
* @author kimchy (Shay Banon) * @author kimchy (shay.banon)
*/ */
public class YamlSettingsLoader implements SettingsLoader { public class YamlSettingsLoader implements SettingsLoader {
@ -51,6 +52,19 @@ public class YamlSettingsLoader implements SettingsLoader {
return settings; return settings;
} }
@Override public Map<String, String> load(byte[] source) throws IOException {
Yaml yaml = new Yaml();
Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(new FastByteArrayInputStream(source));
StringBuilder sb = new StringBuilder();
Map<String, String> settings = newHashMap();
if (yamlMap == null) {
return settings;
}
List<String> path = newArrayList();
serializeMap(settings, sb, path, yamlMap);
return settings;
}
private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) { private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) {
for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) { for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) {
if (entry.getValue() instanceof Map) { if (entry.getValue() instanceof Map) {