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; public static final int BUFFER_SIZE = 1024 * 8;
private final Map<String, String> settings; private final Map<String, String> settings;
public Settings() { private Settings(Map<String, String> settings) {
this(new HashMap<>());
}
public Settings(Map<String, String> settings) {
this.settings = new HashMap<>(settings); this.settings = new HashMap<>(settings);
} }
@ -666,6 +662,27 @@ public class Settings {
return this; 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 * Runs across all the settings set on this builder and replaces <tt>${...}</tt> elements in the
* each setting value according to the following logic: * each setting value according to the following logic:
@ -679,14 +696,17 @@ public class Settings {
public Builder replacePropertyPlaceholders() { public Builder replacePropertyPlaceholders() {
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false); PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
PropertyPlaceholder.PlaceholderResolver placeholderResolver = placeholderName -> { PropertyPlaceholder.PlaceholderResolver placeholderResolver = placeholderName -> {
// system property
String value = System.getProperty(placeholderName); String value = System.getProperty(placeholderName);
if (value != null) { if (value != null) {
return value; return value;
} }
// environment
value = System.getenv(placeholderName); value = System.getenv(placeholderName);
if (value != null) { if (value != null) {
return value; return value;
} }
// current date
try { try {
return DateTimeFormatter.ofPattern(placeholderName).format(LocalDate.now()); return DateTimeFormatter.ofPattern(placeholderName).format(LocalDate.now());
} catch (IllegalArgumentException | DateTimeException e) { } catch (IllegalArgumentException | DateTimeException e) {

View file

@ -10,6 +10,10 @@ import java.util.Set;
*/ */
public interface SettingsLoader { public interface SettingsLoader {
/**
* Suffices for file names to load from.
* @return a set of suffices
*/
Set<String> suffixes(); 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 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 { try {
ServiceLoader<SettingsLoader> serviceLoader = ServiceLoader.load(SettingsLoader.class); ServiceLoader<SettingsLoader> serviceLoader = ServiceLoader.load(SettingsLoader.class);
for (SettingsLoader settingsLoader : serviceLoader) { for (SettingsLoader settingsLoader : serviceLoader) {
@ -30,6 +31,10 @@ public final class SettingsLoaderService {
logger.log(Level.SEVERE, e.getMessage(), e); logger.log(Level.SEVERE, e.getMessage(), e);
} }
} }
private SettingsLoaderService() {
}
/** /**
* Returns a {@link SettingsLoader} based on the resource name. * Returns a {@link SettingsLoader} based on the resource name.
* @param resourceName the resource * @param resourceName the resource

View file

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

View file

@ -1,12 +1,13 @@
package org.xbib.content.settings; package org.xbib.content.settings;
import static org.xbib.content.settings.Settings.settingsBuilder;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.xbib.content.XContentHelper; import org.xbib.content.XContentHelper;
import java.io.ByteArrayInputStream;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -16,6 +17,12 @@ import java.util.Map;
*/ */
public class SettingsTest extends Assert { public class SettingsTest extends Assert {
@Test
public void testEmpty() {
Settings settings = Settings.EMPTY_SETTINGS;
assertTrue(settings.getAsMap().isEmpty());
}
@Test @Test
public void testArray() { public void testArray() {
Settings settings = Settings.settingsBuilder() Settings settings = Settings.settingsBuilder()
@ -51,7 +58,7 @@ public class SettingsTest extends Assert {
map.put("hello", "world"); map.put("hello", "world");
Map<String, Object> settingsMap = new HashMap<>(); Map<String, Object> settingsMap = new HashMap<>();
settingsMap.put("map", map); settingsMap.put("map", map);
Settings settings = settingsBuilder().loadFromMap(settingsMap).build(); Settings settings = Settings.settingsBuilder().loadFromMap(settingsMap).build();
assertEquals("{map.hello=world}", settings.getAsMap().toString()); assertEquals("{map.hello=world}", settings.getAsMap().toString());
} }
@ -59,7 +66,7 @@ public class SettingsTest extends Assert {
public void testMapSettingsFromReader() { public void testMapSettingsFromReader() {
StringReader reader = new StringReader("{\"map\":{\"hello\":\"world\"}}"); StringReader reader = new StringReader("{\"map\":{\"hello\":\"world\"}}");
Map<String, Object> spec = XContentHelper.convertFromJsonToMap(reader); 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()); assertEquals("{map.hello=world}", settings.getAsMap().toString());
} }
@ -72,4 +79,27 @@ public class SettingsTest extends Assert {
assertTrue(Integer.parseInt(settings.get("date")) > 2000); 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"); QName root = new QName("root");
XContentBuilder builder = XmlXContent.contentBuilder(new XmlXParams(root)); XContentBuilder builder = XmlXContent.contentBuilder(new XmlXParams(root));
builder.startObject().field("Hello", "World\u001b").endObject(); 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 @Test

View file

@ -17,6 +17,6 @@ public class XMLUtilTest extends Assert {
@Test @Test
public void testWhitespaceCleanerWithReplacementCharacter() { public void testWhitespaceCleanerWithReplacementCharacter() {
String s = "Hello World\u001b"; 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 group = org.xbib
version = 1.0.0 version = 1.0.1
org.gradle.daemon = true org.gradle.daemon = true