Skip to content

Commit

Permalink
Merge pull request #182 from kenfinnigan/config-source-extraction
Browse files Browse the repository at this point in the history
Fixes #178
  • Loading branch information
kenfinnigan authored Nov 11, 2019
2 parents ec29fd0 + 91c8850 commit dbd4c8e
Show file tree
Hide file tree
Showing 23 changed files with 244 additions and 189 deletions.
57 changes: 57 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config-parent</artifactId>
<version>1.3.12-SNAPSHOT</version>
</parent>

<artifactId>smallrye-config-common</artifactId>

<name>SmallRye: Common classes</name>

<dependencies>
<dependency>
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>coverage</id>
<properties>
<argLine>@{jacocoArgLine}</argLine>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{jacocoArgLine} -Xmx512m</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
32 changes: 32 additions & 0 deletions common/src/main/java/io/smallrye/config/AbstractConfigSource.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
33 changes: 33 additions & 0 deletions common/src/main/java/io/smallrye/config/MapBackedConfigSource.java
Original file line number Diff line number Diff line change
@@ -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<String, String> properties;

public MapBackedConfigSource(String name, Map<String, String> propertyMap) {
super(name, Integer.parseInt(propertyMap.getOrDefault(CONFIG_ORDINAL_KEY, CONFIG_ORDINAL_100)));
properties = propertyMap;
}

public MapBackedConfigSource(String name, Map<String, String> propertyMap, int defaultOrdinal) {
super(name, Integer.parseInt(propertyMap.getOrDefault(CONFIG_ORDINAL_KEY, String.valueOf(defaultOrdinal))));
properties = propertyMap;
}

@Override
public Map<String, String> getProperties() {
return Collections.unmodifiableMap(properties);
}

@Override
public String getValue(String propertyName) {
return properties.get(propertyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

package io.smallrye.config.utils;

import java.io.IOException;
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;
Expand Down Expand Up @@ -44,4 +48,12 @@ public static Map<String, String> propertiesToMap(Properties properties) {
return properties.entrySet().stream().collect(Collectors.toMap(e -> String.valueOf(e.getKey()),
e -> String.valueOf(e.getValue())));
}

public static Map<String, String> urlToMap(URL locationOfProperties) throws IOException {
try (InputStreamReader reader = new InputStreamReader(locationOfProperties.openStream(), StandardCharsets.UTF_8)) {
Properties p = new Properties();
p.load(reader);
return ConfigSourceUtil.propertiesToMap(p);
}
}
}
5 changes: 5 additions & 0 deletions implementation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config-common</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://jmesnil.net/">Jeff Mesnil</a> (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
Expand All @@ -40,11 +40,6 @@ public Map<String, String> getProperties() {
.unmodifiableMap(AccessController.doPrivileged((PrivilegedAction<Map<String, String>>) System::getenv));
}

@Override
public int getOrdinal() {
return 300;
}

@Override
public String getValue(String name) {
if (name == null) {
Expand All @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
public class PropertiesConfigSource implements ConfigSource, Serializable {

private final Map<String, String> properties;
private final String source;
private final int ordinal;
public class PropertiesConfigSource extends MapBackedConfigSource {
private static final long serialVersionUID = 1866835565147832432L;

/**
* Construct a new instance
Expand All @@ -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<String, String> 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<String, String> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="http://jmesnil.net/">Jeff Mesnil</a> (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<String, String> getProperties() {

return Collections.unmodifiableMap(
propertiesToMap(AccessController.doPrivileged((PrivilegedAction<Properties>) System::getProperties)));
}

@Override
public int getOrdinal() {
return 400;
}

@Override
public String getValue(String s) {
return AccessController.doPrivileged((PrivilegedAction<String>) () -> System.getProperty(s));
}

@Override
public String getName() {
return "SysPropConfigSource";
}
}
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
</scm>

<modules>
<module>common</module>
<module>implementation</module>
<module>testsuite</module>
<module>docs</module>
<module>sources/hocon</module>
<module>sources/file-system</module>
<module>sources/zookeeper</module>
<module>converters/json</module>
<module>utils/events</module>
Expand All @@ -80,6 +82,7 @@
<artifactId>microprofile-config-tck</artifactId>
<version>${version.eclipse.microprofile.config}</version>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.testng</groupId>
<artifactId>arquillian-testng-container</artifactId>
Expand All @@ -100,6 +103,11 @@
</dependency>

<!-- Dependencies provided by the project -->
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-config</artifactId>
Expand All @@ -114,6 +122,11 @@
</dependency>

<!-- Config Sources -->
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-file-system</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-zookeeper</artifactId>
Expand Down
Loading

0 comments on commit dbd4c8e

Please sign in to comment.