adding properties loading, fixes settings loader service

This commit is contained in:
Jörg Prante 2016-10-18 00:43:00 +02:00
parent 6905991002
commit b9ac7f87bd
9 changed files with 115 additions and 14 deletions

View file

@ -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<String> PROPERTIES_SUFFIXES = new HashSet<>(Collections.singletonList("properties"));
@Override
public Set<String> suffixes() {
return PROPERTIES_SUFFIXES;
}
@Override
public Map<String, String> load(String source) throws IOException {
Properties props = new Properties();
try (StringReader reader = new StringReader(source)) {
props.load(reader);
Map<String, String> result = new HashMap<>();
for (Map.Entry<Object, Object> entry : props.entrySet()) {
result.put((String) entry.getKey(), (String) entry.getValue());
}
return result;
}
}
@Override
public boolean canLoad(String source) {
return true;
}
}

View file

@ -43,11 +43,7 @@ public class Settings {
public static final int BUFFER_SIZE = 1024 * 8;
private final Map<String, String> settings;
public Settings() {
this(new HashMap<>());
}
public Settings(Map<String, String> settings) {
private Settings(Map<String, String> 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<Object, Object> 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<String, String> entry : System.getenv().entrySet()) {
put(entry.getKey(), entry.getValue());
}
return this;
}
/**
* Runs across all the settings set on this builder and replaces <tt>${...}</tt> 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) {

View file

@ -10,6 +10,10 @@ import java.util.Set;
*/
public interface SettingsLoader {
/**
* Suffices for file names to load from.
* @return a set of suffices
*/
Set<String> suffixes();
/**

View file

@ -16,9 +16,10 @@ public final class SettingsLoaderService {
private static final Logger logger = Logger.getLogger(SettingsLoaderService.class.getName());
private static final Map<Set<String>, SettingsLoader> settingsLoaderMap = new HashMap<>();
private static final Map<Set<String>, SettingsLoader> settingsLoaderMap;
private SettingsLoaderService() {
static {
settingsLoaderMap = new HashMap<>();
try {
ServiceLoader<SettingsLoader> 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

View file

@ -1 +1,2 @@
org.xbib.content.json.JsonSettingsLoader
org.xbib.content.settings.PropertiesSettingsLoader

View file

@ -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<String, Object> 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<String, Object> 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());
}
}

View file

@ -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("<root><Hello>World<EFBFBD></Hello></root>", builder.string());
assertEquals("<root><Hello>World\ufffd</Hello></root>", builder.string());
}
@Test

View file

@ -17,6 +17,6 @@ public class XMLUtilTest extends Assert {
@Test
public void testWhitespaceCleanerWithReplacementCharacter() {
String s = "Hello World\u001b";
assertEquals(XMLUtil.sanitizeXml10(s), "Hello World<6C>");
assertEquals("Hello World\ufffd", XMLUtil.sanitizeXml10(s));
}
}

View file

@ -1,3 +1,3 @@
group = org.xbib
version = 1.0.0
version = 1.0.1
org.gradle.daemon = true