From 3e791ef3f5eef2179b2a71aa7b104a984a95d9a4 Mon Sep 17 00:00:00 2001 From: Ken Finnigan Date: Fri, 8 Nov 2019 14:07:21 -0500 Subject: [PATCH 1/2] Fixes #178 --- common/pom.xml | 57 +++++++++++++++++ .../smallrye/config/AbstractConfigSource.java | 32 ++++++++++ .../config/MapBackedConfigSource.java | 33 ++++++++++ .../config/utils/ConfigSourceUtil.java | 11 ++++ .../io/smallrye/config/utils/StringUtil.java | 0 .../config/utils/ConfigSourceUtilTest.java | 0 .../smallrye/config/utils/StringUtilTest.java | 0 implementation/pom.xml | 5 ++ .../io/smallrye/config/EnvConfigSource.java | 18 ++---- .../config/PropertiesConfigSource.java | 61 ++----------------- .../smallrye/config/SysPropConfigSource.java | 18 +----- pom.xml | 13 ++++ sources/file-system/pom.xml | 44 +++++++++++++ .../config/FileSystemConfigSource.java | 49 +++------------ .../FileSystemConfigSourceTestCase.java | 4 +- .../smallrye/config/configDir/config_ordinal | 0 .../io/smallrye/config/configDir/myKey1 | 0 .../io/smallrye/config/configDir/myKey2 | 0 sources/hocon/pom.xml | 2 +- .../configsource/HoconConfigSource.java | 46 ++------------ sources/zookeeper/pom.xml | 20 +++--- .../configsource/ZooKeeperConfigSource.java | 17 ++---- .../tests/ZooKeeperConfigSourceTest.java | 2 + 23 files changed, 243 insertions(+), 189 deletions(-) create mode 100644 common/pom.xml create mode 100644 common/src/main/java/io/smallrye/config/AbstractConfigSource.java create mode 100644 common/src/main/java/io/smallrye/config/MapBackedConfigSource.java rename {implementation => common}/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java (79%) rename {implementation => common}/src/main/java/io/smallrye/config/utils/StringUtil.java (100%) rename {implementation => common}/src/test/java/io/smallrye/config/utils/ConfigSourceUtilTest.java (100%) rename {implementation => common}/src/test/java/io/smallrye/config/utils/StringUtilTest.java (100%) create mode 100644 sources/file-system/pom.xml rename implementation/src/main/java/io/smallrye/config/DirConfigSource.java => sources/file-system/src/main/java/io/smallrye/config/FileSystemConfigSource.java (66%) rename implementation/src/test/java/io/smallrye/config/DirConfigSourceTestCase.java => sources/file-system/src/test/java/io/smallrye/config/FileSystemConfigSourceTestCase.java (91%) rename {implementation => sources/file-system}/src/test/resources/io/smallrye/config/configDir/config_ordinal (100%) rename {implementation => sources/file-system}/src/test/resources/io/smallrye/config/configDir/myKey1 (100%) rename {implementation => sources/file-system}/src/test/resources/io/smallrye/config/configDir/myKey2 (100%) diff --git a/common/pom.xml b/common/pom.xml new file mode 100644 index 000000000..d1150c0f9 --- /dev/null +++ b/common/pom.xml @@ -0,0 +1,57 @@ + + + + 4.0.0 + + + io.smallrye + smallrye-config-parent + 1.3.12-SNAPSHOT + + + smallrye-config-common + + SmallRye: Common classes + + + + org.eclipse.microprofile.config + microprofile-config-api + + + org.jboss.logging + jboss-logging + + + + junit + junit + test + + + + + + coverage + + @{jacocoArgLine} + + + + + org.jacoco + jacoco-maven-plugin + + + org.apache.maven.plugins + maven-surefire-plugin + + @{jacocoArgLine} -Xmx512m + + + + + + + + diff --git a/common/src/main/java/io/smallrye/config/AbstractConfigSource.java b/common/src/main/java/io/smallrye/config/AbstractConfigSource.java new file mode 100644 index 000000000..27d2e33a9 --- /dev/null +++ b/common/src/main/java/io/smallrye/config/AbstractConfigSource.java @@ -0,0 +1,32 @@ +package io.smallrye.config; + +import java.io.Serializable; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +public abstract class AbstractConfigSource implements ConfigSource, Serializable { + private static final long serialVersionUID = 9018847720072978115L; + + private final int ordinal; + private final String name; + + public AbstractConfigSource(String name, int ordinal) { + this.name = name; + this.ordinal = ordinal; + } + + @Override + public int getOrdinal() { + return ordinal; + } + + @Override + public String getName() { + return name; + } + + @Override + public String toString() { + return getName(); + } +} diff --git a/common/src/main/java/io/smallrye/config/MapBackedConfigSource.java b/common/src/main/java/io/smallrye/config/MapBackedConfigSource.java new file mode 100644 index 000000000..83edac625 --- /dev/null +++ b/common/src/main/java/io/smallrye/config/MapBackedConfigSource.java @@ -0,0 +1,33 @@ +package io.smallrye.config; + +import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_100; +import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY; + +import java.util.Collections; +import java.util.Map; + +public abstract class MapBackedConfigSource extends AbstractConfigSource { + private static final long serialVersionUID = -7159956218217228877L; + + private final Map properties; + + public MapBackedConfigSource(String name, Map propertyMap) { + super(name, Integer.parseInt(propertyMap.getOrDefault(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100))); + properties = propertyMap; + } + + public MapBackedConfigSource(String name, Map propertyMap, int defaultOrdinal) { + super(name, Integer.parseInt(propertyMap.getOrDefault(CONFIG_ORDINAL_KEY, String.valueOf(defaultOrdinal)))); + properties = propertyMap; + } + + @Override + public Map getProperties() { + return Collections.unmodifiableMap(properties); + } + + @Override + public String getValue(String propertyName) { + return properties.get(propertyName); + } +} diff --git a/implementation/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java b/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java similarity index 79% rename from implementation/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java rename to common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java index 6e9f701cf..d566d1f7d 100644 --- a/implementation/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java +++ b/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java @@ -16,6 +16,9 @@ package io.smallrye.config.utils; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; @@ -44,4 +47,12 @@ public static Map propertiesToMap(Properties properties) { return properties.entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()), e -> String.valueOf(e.getValue()))); } + + public static Map urlToMap(URL locationOfProperties) throws IOException { + try (InputStream in = locationOfProperties.openStream()) { + Properties p = new Properties(); + p.load(in); + return ConfigSourceUtil.propertiesToMap(p); + } + } } diff --git a/implementation/src/main/java/io/smallrye/config/utils/StringUtil.java b/common/src/main/java/io/smallrye/config/utils/StringUtil.java similarity index 100% rename from implementation/src/main/java/io/smallrye/config/utils/StringUtil.java rename to common/src/main/java/io/smallrye/config/utils/StringUtil.java diff --git a/implementation/src/test/java/io/smallrye/config/utils/ConfigSourceUtilTest.java b/common/src/test/java/io/smallrye/config/utils/ConfigSourceUtilTest.java similarity index 100% rename from implementation/src/test/java/io/smallrye/config/utils/ConfigSourceUtilTest.java rename to common/src/test/java/io/smallrye/config/utils/ConfigSourceUtilTest.java diff --git a/implementation/src/test/java/io/smallrye/config/utils/StringUtilTest.java b/common/src/test/java/io/smallrye/config/utils/StringUtilTest.java similarity index 100% rename from implementation/src/test/java/io/smallrye/config/utils/StringUtilTest.java rename to common/src/test/java/io/smallrye/config/utils/StringUtilTest.java diff --git a/implementation/pom.xml b/implementation/pom.xml index 608e02c56..f6c882270 100644 --- a/implementation/pom.xml +++ b/implementation/pom.xml @@ -42,10 +42,15 @@ javax.annotation javax.annotation-api + + io.smallrye + smallrye-config-common + org.jboss.logging jboss-logging + junit junit diff --git a/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java b/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java index 7a418e46e..3033aa935 100644 --- a/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/EnvConfigSource.java @@ -16,22 +16,22 @@ package io.smallrye.config; -import java.io.Serializable; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collections; import java.util.Map; import java.util.regex.Pattern; -import org.eclipse.microprofile.config.spi.ConfigSource; - /** * @author Jeff Mesnil (c) 2017 Red Hat inc. */ -public class EnvConfigSource implements ConfigSource, Serializable { +public class EnvConfigSource extends AbstractConfigSource { + private static final long serialVersionUID = -4525015934376795496L; + private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]"); EnvConfigSource() { + super("EnvConfigSource", 300); } @Override @@ -40,11 +40,6 @@ public Map getProperties() { .unmodifiableMap(AccessController.doPrivileged((PrivilegedAction>) System::getenv)); } - @Override - public int getOrdinal() { - return 300; - } - @Override public String getValue(String name) { if (name == null) { @@ -70,9 +65,4 @@ public String getValue(String name) { // replace non-alphanumeric characters by underscores and convert to uppercase return properties.get(sanitizedName.toUpperCase()); } - - @Override - public String getName() { - return "EnvConfigSource"; - } } diff --git a/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java b/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java index 06e501156..3550fb51d 100644 --- a/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/PropertiesConfigSource.java @@ -16,30 +16,18 @@ package io.smallrye.config; -import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_100; -import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY; - import java.io.IOException; -import java.io.InputStream; -import java.io.Serializable; import java.net.URL; -import java.util.Collections; -import java.util.HashMap; import java.util.Map; import java.util.Properties; -import org.eclipse.microprofile.config.spi.ConfigSource; - import io.smallrye.config.utils.ConfigSourceUtil; /** * @author Jeff Mesnil (c) 2017 Red Hat inc. */ -public class PropertiesConfigSource implements ConfigSource, Serializable { - - private final Map properties; - private final String source; - private final int ordinal; +public class PropertiesConfigSource extends MapBackedConfigSource { + private static final long serialVersionUID = 1866835565147832432L; /** * Construct a new instance @@ -48,53 +36,14 @@ public class PropertiesConfigSource implements ConfigSource, Serializable { * @throws IOException if an error occurred when reading from the input stream */ public PropertiesConfigSource(URL url) throws IOException { - this.source = url.toString(); - try (InputStream in = url.openStream()) { - Properties p = new Properties(); - p.load(in); - properties = ConfigSourceUtil.propertiesToMap(p); - } - this.ordinal = Integer.parseInt(properties.getOrDefault(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100)); + super("PropertiesConfigSource[source=" + url.toString() + "]", ConfigSourceUtil.urlToMap(url)); } public PropertiesConfigSource(Properties properties, String source) { - this.properties = ConfigSourceUtil.propertiesToMap(properties); - this.source = source; - this.ordinal = Integer.parseInt(properties.getProperty(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100)); + super("PropertiesConfigSource[source=" + source + "]", ConfigSourceUtil.propertiesToMap(properties)); } public PropertiesConfigSource(Map properties, String source, int ordinal) { - this.properties = new HashMap<>(properties); - this.source = source; - if (properties.containsKey(CONFIG_ORDINAL_KEY)) { - this.ordinal = Integer.parseInt(properties.getOrDefault(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100)); - } else { - this.ordinal = ordinal; - } - } - - @Override - public Map getProperties() { - return Collections.unmodifiableMap(properties); - } - - @Override - public int getOrdinal() { - return ordinal; - } - - @Override - public String getValue(String s) { - return properties.get(s); - } - - @Override - public String getName() { - return "PropertiesConfigSource[source=" + source + "]"; - } - - @Override - public String toString() { - return getName(); + super("PropertiesConfigSource[source=" + source + "]", properties, ordinal); } } diff --git a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java index 983c308a0..9a49f4c1c 100644 --- a/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java +++ b/implementation/src/main/java/io/smallrye/config/SysPropConfigSource.java @@ -18,42 +18,30 @@ import static io.smallrye.config.utils.ConfigSourceUtil.propertiesToMap; -import java.io.Serializable; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Collections; import java.util.Map; import java.util.Properties; -import org.eclipse.microprofile.config.spi.ConfigSource; - /** * @author Jeff Mesnil (c) 2017 Red Hat inc. */ -class SysPropConfigSource implements ConfigSource, Serializable { +class SysPropConfigSource extends AbstractConfigSource { + private static final long serialVersionUID = 9167738611308785403L; SysPropConfigSource() { + super("SysPropConfigSource", 400); } @Override public Map getProperties() { - return Collections.unmodifiableMap( propertiesToMap(AccessController.doPrivileged((PrivilegedAction) System::getProperties))); } - @Override - public int getOrdinal() { - return 400; - } - @Override public String getValue(String s) { return AccessController.doPrivileged((PrivilegedAction) () -> System.getProperty(s)); } - - @Override - public String getName() { - return "SysPropConfigSource"; - } } diff --git a/pom.xml b/pom.xml index 805219c7b..b62e93fc3 100644 --- a/pom.xml +++ b/pom.xml @@ -58,10 +58,12 @@ + common implementation testsuite docs sources/hocon + sources/file-system sources/zookeeper converters/json utils/events @@ -80,6 +82,7 @@ microprofile-config-tck ${version.eclipse.microprofile.config} + org.jboss.arquillian.testng arquillian-testng-container @@ -100,6 +103,11 @@ + + io.smallrye + smallrye-config-common + ${project.version} + io.smallrye smallrye-config @@ -114,6 +122,11 @@ + + io.smallrye.config + smallrye-config-source-file-system + ${project.version} + io.smallrye.config smallrye-config-source-zookeeper diff --git a/sources/file-system/pom.xml b/sources/file-system/pom.xml new file mode 100644 index 000000000..17a8e5dc1 --- /dev/null +++ b/sources/file-system/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + smallrye-config-parent + io.smallrye + 1.3.12-SNAPSHOT + ../../pom.xml + + + io.smallrye.config + smallrye-config-source-file-system + SmallRye: FileSystem ConfigSource + + + + io.smallrye + smallrye-config-common + + + + junit + junit + test + + + + + + coverage + + @{jacocoArgLine} + + + + + org.jacoco + jacoco-maven-plugin + + + + + + \ No newline at end of file diff --git a/implementation/src/main/java/io/smallrye/config/DirConfigSource.java b/sources/file-system/src/main/java/io/smallrye/config/FileSystemConfigSource.java similarity index 66% rename from implementation/src/main/java/io/smallrye/config/DirConfigSource.java rename to sources/file-system/src/main/java/io/smallrye/config/FileSystemConfigSource.java index ce369fd8a..72eb8cd6d 100644 --- a/implementation/src/main/java/io/smallrye/config/DirConfigSource.java +++ b/sources/file-system/src/main/java/io/smallrye/config/FileSystemConfigSource.java @@ -16,9 +16,6 @@ package io.smallrye.config; -import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_100; -import static io.smallrye.config.utils.ConfigSourceUtil.CONFIG_ORDINAL_KEY; - import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; @@ -29,7 +26,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import org.eclipse.microprofile.config.spi.ConfigSource; import org.jboss.logging.Logger; /** @@ -58,15 +54,12 @@ * * @author Jeff Mesnil (c) 2017 Red Hat inc. */ -public class DirConfigSource implements ConfigSource { +public class FileSystemConfigSource extends MapBackedConfigSource { + private static final long serialVersionUID = 654034634846856045L; private static final Logger LOG = Logger.getLogger("io.smallrye.config"); - private final File dir; - private final int ordinal; - private final Map props; - - DirConfigSource(File dir) { + FileSystemConfigSource(File dir) { this(dir, DEFAULT_ORDINAL); } @@ -76,42 +69,16 @@ public class DirConfigSource implements ConfigSource { * @param dir the directory, containing configuration files * @param ordinal the ordinal value */ - public DirConfigSource(File dir, int ordinal) { - this.dir = dir; - this.props = scan(dir); - if (props.containsKey(CONFIG_ORDINAL_KEY)) { - this.ordinal = Integer.parseInt(props.getOrDefault(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100)); - } else { - this.ordinal = ordinal; - } - } - - @Override - public Map getProperties() { - return Collections.unmodifiableMap(props); - } - - @Override - public String getValue(String key) { - return props.get(key); - } - - @Override - public String getName() { - return "DirConfigSource[dir=" + dir.getAbsolutePath() + "]"; - } - - @Override - public int getOrdinal() { - return ordinal; + public FileSystemConfigSource(File dir, int ordinal) { + super("DirConfigSource[dir=" + dir.getAbsolutePath() + "]", scan(dir), ordinal); } - private Map scan(File directory) { + private static Map scan(File directory) { if (directory != null && directory.isDirectory()) { try (Stream stream = Files.walk(directory.toPath())) { return stream.filter(p -> p.toFile().isFile()) - .collect(Collectors.toMap(it -> it.getFileName().toString(), this::readContent)); + .collect(Collectors.toMap(it -> it.getFileName().toString(), FileSystemConfigSource::readContent)); } catch (Throwable t) { LOG.warnf("Unable to read content from file %s", directory.getAbsolutePath()); } @@ -119,7 +86,7 @@ private Map scan(File directory) { return Collections.emptyMap(); } - private String readContent(Path file) { + private static String readContent(Path file) { try (Stream stream = Files.lines(file)) { return stream.collect(Collectors.joining()); } catch (IOException e) { diff --git a/implementation/src/test/java/io/smallrye/config/DirConfigSourceTestCase.java b/sources/file-system/src/test/java/io/smallrye/config/FileSystemConfigSourceTestCase.java similarity index 91% rename from implementation/src/test/java/io/smallrye/config/DirConfigSourceTestCase.java rename to sources/file-system/src/test/java/io/smallrye/config/FileSystemConfigSourceTestCase.java index 55ec33bea..f3a43acec 100644 --- a/implementation/src/test/java/io/smallrye/config/DirConfigSourceTestCase.java +++ b/sources/file-system/src/test/java/io/smallrye/config/FileSystemConfigSourceTestCase.java @@ -28,14 +28,14 @@ /** * @author Jeff Mesnil (c) 2017 Red Hat inc. */ -public class DirConfigSourceTestCase { +public class FileSystemConfigSourceTestCase { @Test public void testConfigSourceFromDir() throws URISyntaxException { URL configDirURL = this.getClass().getResource("configDir"); File dir = new File(configDirURL.toURI()); - ConfigSource configSource = new DirConfigSource(dir); + ConfigSource configSource = new FileSystemConfigSource(dir); assertEquals(4567, configSource.getOrdinal()); diff --git a/implementation/src/test/resources/io/smallrye/config/configDir/config_ordinal b/sources/file-system/src/test/resources/io/smallrye/config/configDir/config_ordinal similarity index 100% rename from implementation/src/test/resources/io/smallrye/config/configDir/config_ordinal rename to sources/file-system/src/test/resources/io/smallrye/config/configDir/config_ordinal diff --git a/implementation/src/test/resources/io/smallrye/config/configDir/myKey1 b/sources/file-system/src/test/resources/io/smallrye/config/configDir/myKey1 similarity index 100% rename from implementation/src/test/resources/io/smallrye/config/configDir/myKey1 rename to sources/file-system/src/test/resources/io/smallrye/config/configDir/myKey1 diff --git a/implementation/src/test/resources/io/smallrye/config/configDir/myKey2 b/sources/file-system/src/test/resources/io/smallrye/config/configDir/myKey2 similarity index 100% rename from implementation/src/test/resources/io/smallrye/config/configDir/myKey2 rename to sources/file-system/src/test/resources/io/smallrye/config/configDir/myKey2 diff --git a/sources/hocon/pom.xml b/sources/hocon/pom.xml index 8b8ad6462..4dfcac6d2 100644 --- a/sources/hocon/pom.xml +++ b/sources/hocon/pom.xml @@ -19,7 +19,7 @@ io.smallrye - smallrye-config + smallrye-config-common com.typesafe diff --git a/sources/hocon/src/main/java/io/smallrye/configsource/HoconConfigSource.java b/sources/hocon/src/main/java/io/smallrye/configsource/HoconConfigSource.java index bafb67cc7..63c5bf9c4 100644 --- a/sources/hocon/src/main/java/io/smallrye/configsource/HoconConfigSource.java +++ b/sources/hocon/src/main/java/io/smallrye/configsource/HoconConfigSource.java @@ -1,54 +1,18 @@ package io.smallrye.configsource; -import java.io.Serializable; import java.util.Collections; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; -import org.eclipse.microprofile.config.spi.ConfigSource; - import com.typesafe.config.Config; -import io.smallrye.config.PropertiesConfigSource; +import io.smallrye.config.MapBackedConfigSource; -public class HoconConfigSource implements ConfigSource, Serializable { - private final String source; - private final PropertiesConfigSource delegate; +public class HoconConfigSource extends MapBackedConfigSource { + private static final long serialVersionUID = -458821383311704657L; public HoconConfigSource(Config config, String source, int ordinal) { - this.source = source; - this.delegate = new PropertiesConfigSource(Collections.unmodifiableMap(config.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, entry -> config.getString(entry.getKey())))), source, ordinal); - } - - @Override - public Map getProperties() { - return this.delegate.getProperties(); - } - - @Override - public Set getPropertyNames() { - return this.delegate.getPropertyNames(); - } - - @Override - public int getOrdinal() { - return delegate.getOrdinal(); - } - - @Override - public String getValue(String s) { - return delegate.getValue(s); - } - - @Override - public String getName() { - return "HoconConfigSource[source=" + source + "]"; - } - - @Override - public String toString() { - return getName(); + super("HoconConfigSource[source=" + source + "]", Collections.unmodifiableMap(config.entrySet().stream() + .collect(Collectors.toMap(Map.Entry::getKey, entry -> config.getString(entry.getKey())))), ordinal); } } diff --git a/sources/zookeeper/pom.xml b/sources/zookeeper/pom.xml index da1abce07..1ce99748a 100644 --- a/sources/zookeeper/pom.xml +++ b/sources/zookeeper/pom.xml @@ -49,13 +49,7 @@ io.smallrye - smallrye-config - - - - junit - junit - test + smallrye-config-common @@ -71,6 +65,18 @@ test + + io.smallrye + smallrye-config + test + + + + junit + junit + test + + org.assertj assertj-core diff --git a/sources/zookeeper/src/main/java/io/smallrye/configsource/ZooKeeperConfigSource.java b/sources/zookeeper/src/main/java/io/smallrye/configsource/ZooKeeperConfigSource.java index 63c8b5fda..dde1166f9 100644 --- a/sources/zookeeper/src/main/java/io/smallrye/configsource/ZooKeeperConfigSource.java +++ b/sources/zookeeper/src/main/java/io/smallrye/configsource/ZooKeeperConfigSource.java @@ -11,7 +11,8 @@ import org.apache.zookeeper.data.Stat; import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; -import org.eclipse.microprofile.config.spi.ConfigSource; + +import io.smallrye.config.AbstractConfigSource; /** * MicroProfile Config Source that is backed by Zookeeper. @@ -21,7 +22,8 @@ *

* author: Simon Woodman swoodman@redhat.com */ -public class ZooKeeperConfigSource implements ConfigSource { +public class ZooKeeperConfigSource extends AbstractConfigSource { + private static final long serialVersionUID = 3127679154588598693L; private static final Logger logger = Logger.getLogger(ZooKeeperConfigSource.class.getName()); @@ -44,11 +46,7 @@ public class ZooKeeperConfigSource implements ConfigSource { private static final String ZOOKEEPER_CONFIG_SOURCE_NAME = "io.smallrye.configsource.zookeeper"; public ZooKeeperConfigSource() { - } - - @Override - public int getOrdinal() { - return 150; + super(ZOOKEEPER_CONFIG_SOURCE_NAME, 150); } @Override @@ -108,11 +106,6 @@ public String getValue(final String key) { return null; } - @Override - public String getName() { - return ZOOKEEPER_CONFIG_SOURCE_NAME; - } - private CuratorFramework getCuratorClient() throws ZooKeeperConfigException { CuratorFramework cachedClient = curatorReference.get(); diff --git a/sources/zookeeper/src/test/java/io/smallrye/configsource/zookeeper/tests/ZooKeeperConfigSourceTest.java b/sources/zookeeper/src/test/java/io/smallrye/configsource/zookeeper/tests/ZooKeeperConfigSourceTest.java index 71fd02c60..2b09126b7 100644 --- a/sources/zookeeper/src/test/java/io/smallrye/configsource/zookeeper/tests/ZooKeeperConfigSourceTest.java +++ b/sources/zookeeper/src/test/java/io/smallrye/configsource/zookeeper/tests/ZooKeeperConfigSourceTest.java @@ -33,6 +33,7 @@ import org.junit.Test; import org.junit.runner.RunWith; +import io.smallrye.config.AbstractConfigSource; import io.smallrye.configsource.ZooKeeperConfigSource; /** @@ -76,6 +77,7 @@ public static WebArchive createDeployment() { return ShrinkWrap.create(WebArchive.class, "ZkMicroProfileConfigTest.war") .addPackage(ZooKeeperConfigSource.class.getPackage()) + .addPackage(AbstractConfigSource.class.getPackage()) .addAsLibraries(curatorFiles) .addAsLibraries(swarmMPCFiles) .addAsLibraries(curatorTestFiles) From 91c885041c4c4ee2742965978845f25e6a11a474 Mon Sep 17 00:00:00 2001 From: Ken Finnigan Date: Mon, 11 Nov 2019 13:15:51 -0500 Subject: [PATCH 2/2] Wrap InputStream with Reader and set UTF-8 charset --- .../java/io/smallrye/config/utils/ConfigSourceUtil.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java b/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java index d566d1f7d..74ee55e43 100644 --- a/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java +++ b/common/src/main/java/io/smallrye/config/utils/ConfigSourceUtil.java @@ -17,8 +17,9 @@ package io.smallrye.config.utils; import java.io.IOException; -import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Properties; import java.util.stream.Collectors; @@ -49,9 +50,9 @@ public static Map propertiesToMap(Properties properties) { } public static Map urlToMap(URL locationOfProperties) throws IOException { - try (InputStream in = locationOfProperties.openStream()) { + try (InputStreamReader reader = new InputStreamReader(locationOfProperties.openStream(), StandardCharsets.UTF_8)) { Properties p = new Properties(); - p.load(in); + p.load(reader); return ConfigSourceUtil.propertiesToMap(p); } }