Refactor PropertiesSettingsLoader
This commit refactors PropertiesSettingsLoader to remove some duplicate code.
This commit is contained in:
parent
bb364cc793
commit
7323c37339
|
@ -24,10 +24,12 @@ import org.elasticsearch.ElasticsearchParseException;
|
||||||
import org.elasticsearch.common.io.FastStringReader;
|
import org.elasticsearch.common.io.FastStringReader;
|
||||||
import org.elasticsearch.common.io.stream.StreamInput;
|
import org.elasticsearch.common.io.stream.StreamInput;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Settings loader that loads (parses) the settings in a properties format.
|
* Settings loader that loads (parses) the settings in a properties format.
|
||||||
|
@ -36,36 +38,38 @@ public class PropertiesSettingsLoader implements SettingsLoader {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> load(String source) throws IOException {
|
public Map<String, String> load(String source) throws IOException {
|
||||||
Properties props = new NoDuplicatesProperties();
|
return load(() -> new FastStringReader(source), (reader, props) -> props.load(reader));
|
||||||
FastStringReader reader = new FastStringReader(source);
|
|
||||||
try {
|
|
||||||
props.load(reader);
|
|
||||||
Map<String, String> result = new HashMap<>();
|
|
||||||
for (Map.Entry entry : props.entrySet()) {
|
|
||||||
result.put((String) entry.getKey(), (String) entry.getValue());
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
} finally {
|
|
||||||
IOUtils.closeWhileHandlingException(reader);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<String, String> load(byte[] source) throws IOException {
|
public Map<String, String> load(byte[] source) throws IOException {
|
||||||
Properties props = new NoDuplicatesProperties();
|
return load(() -> StreamInput.wrap(source), (inStream, props) -> props.load(inStream));
|
||||||
StreamInput stream = StreamInput.wrap(source);
|
}
|
||||||
|
|
||||||
|
private final <T extends Closeable> Map<String, String> load(
|
||||||
|
Supplier<T> supplier,
|
||||||
|
IOExceptionThrowingBiConsumer<T, Properties> properties
|
||||||
|
) throws IOException {
|
||||||
|
T t = null;
|
||||||
try {
|
try {
|
||||||
props.load(stream);
|
t = supplier.get();
|
||||||
Map<String, String> result = new HashMap<>();
|
final Properties props = new NoDuplicatesProperties();
|
||||||
|
properties.accept(t, props);
|
||||||
|
final Map<String, String> result = new HashMap<>();
|
||||||
for (Map.Entry entry : props.entrySet()) {
|
for (Map.Entry entry : props.entrySet()) {
|
||||||
result.put((String) entry.getKey(), (String) entry.getValue());
|
result.put((String) entry.getKey(), (String) entry.getValue());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
} finally {
|
} finally {
|
||||||
IOUtils.closeWhileHandlingException(stream);
|
IOUtils.closeWhileHandlingException(t);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
private interface IOExceptionThrowingBiConsumer<T, U> {
|
||||||
|
void accept(T t, U u) throws IOException;
|
||||||
|
}
|
||||||
|
|
||||||
class NoDuplicatesProperties extends Properties {
|
class NoDuplicatesProperties extends Properties {
|
||||||
@Override
|
@Override
|
||||||
public synchronized Object put(Object key, Object value) {
|
public synchronized Object put(Object key, Object value) {
|
||||||
|
|
Loading…
Reference in New Issue