Skip to content

Commit

Permalink
Allow loading config from environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
rchodava committed May 5, 2016
1 parent bf47791 commit a29e9b7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 14 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.16.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -45,6 +46,11 @@ public ConfigurationBuilder<T> ifSystemPropertyExists(String name,
Consumer<ConfigurationBuilder<T>> configuration,
Consumer<ConfigurationBuilder<T>> elseConfiguration) {
String value = System.getProperty(name);

if (value == null) {
value = System.getenv(name);
}

if (value != null) {
if (configuration != null) {
configuration.accept(this);
Expand All @@ -60,6 +66,11 @@ public ConfigurationBuilder<T> ifSystemPropertyExists(String name,

private String getSystemProperty(String name, boolean required) {
String value = System.getProperty(name);

if (value == null) {
value = System.getenv(name);
}

if (value == null && required) {
throw new IllegalStateException("Expected " + name + " to be found in the system properties list!");
}
Expand Down Expand Up @@ -206,4 +217,24 @@ public <P> ConfigurationBuilder<T> fromLocalAddress(Consumer<T> propertyInvoker,
bean.set(propertyInvoker, derivation.call(localAddress));
return this;
}

public ConfigurationBuilder<T> printSystemProperties() {
StringBuilder properties = new StringBuilder();
for (Map.Entry<Object, Object> property : System.getProperties().entrySet()) {
properties.append(property.getKey() != null ? property.getKey().toString() : "<null>");
properties.append("=");
properties.append(property.getValue() != null ? property.getValue().toString() : "<null>");
properties.append("; ");
}

for (Map.Entry<String, String> property : System.getenv().entrySet()) {
properties.append(property.getKey() != null ? property.getKey() : "<null>");
properties.append("=");
properties.append(property.getValue() != null ? property.getValue() : "<null>");
properties.append("; ");
}

logger.debug("System Properties: { {} }", properties);
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.chodavarapu.datamill.configuration;

import org.chodavarapu.datamill.reflection.OutlineBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

import java.net.InetAddress;

Expand All @@ -23,13 +25,38 @@ public void setProperty(String property) {
}
}

@Rule public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

@Test
public void build() throws Exception {
System.setProperty("test", "value");
System.setProperty("test2", "_value2");
System.setProperty("test3", "_value3");
System.setProperty("test4", "_value4");
System.setProperty("test5", "_value5");
public void buildFromMiscellaneous() throws Exception {
Configuration configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure(c -> c.setProperty("set"))
.get();
assertEquals("set", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure((b, c) -> c.setProperty("builder_set"))
.get();
assertEquals("builder_set", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromLocalAddress(c -> c.getProperty())
.get();
assertEquals(InetAddress.getLocalHost().getHostAddress(), configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromLocalAddress(c -> c.getProperty(), a -> a + "_derived")
.get();
assertEquals(InetAddress.getLocalHost().getHostAddress() + "_derived", configuration.getProperty());
}

@Test
public void buildFromEnvironmentVariables() throws Exception {
environmentVariables.set("test", "value");
environmentVariables.set("test2", "_value2");
environmentVariables.set("test3", "_value3");
environmentVariables.set("test4", "_value4");
environmentVariables.set("test5", "_value5");

Configuration configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperty(c -> c.getProperty(), "test")
Expand Down Expand Up @@ -70,29 +97,68 @@ public void build() throws Exception {
assertEquals("value_value2_value3_value4_value5_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure(c -> c.setProperty("set"))
.ifSystemPropertyExists("test", b -> b.configure(c -> c.setProperty("exists")))
.get();
assertEquals("set", configuration.getProperty());
assertEquals("exists", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure((b, c) -> c.setProperty("builder_set"))
.ifSystemPropertyExists("test_non_existent",
b -> b.configure(c -> c.setProperty("exists")),
b -> b.configure(c -> c.setProperty("non_existent")))
.get();
assertEquals("builder_set", configuration.getProperty());
assertEquals("non_existent", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure((b, c) -> c.setProperty(b.getRequiredSystemProperty("test").asString()))
.get();
assertEquals("value", configuration.getProperty());
}

@Test
public void buildFromSystemProperties() throws Exception {
System.setProperty("test", "value");
System.setProperty("test2", "_value2");
System.setProperty("test3", "_value3");
System.setProperty("test4", "_value4");
System.setProperty("test5", "_value5");

Configuration configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperty(c -> c.getProperty(), "test")
.get();
assertEquals("value", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromLocalAddress(c -> c.getProperty())
.fromOptionalSystemProperty(c -> c.getProperty(), "test_unknown", "default")
.get();
assertEquals(InetAddress.getLocalHost().getHostAddress(), configuration.getProperty());
assertEquals("default", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromLocalAddress(c -> c.getProperty(), a -> a + "_derived")
.fromRequiredSystemProperty(c -> c.getProperty(), "test", v -> v + "_derived")
.get();
assertEquals(InetAddress.getLocalHost().getHostAddress() + "_derived", configuration.getProperty());
assertEquals("value_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperties(c -> c.getProperty(), "test", "test2", (v1, v2) -> v1 + v2 + "_derived")
.get();
assertEquals("value_value2_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperties(c -> c.getProperty(), "test", "test2", "test3",
(v1, v2, v3) -> v1 + v2 + v3 + "_derived")
.get();
assertEquals("value_value2_value3_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperties(c -> c.getProperty(), "test", "test2", "test3", "test4",
(v1, v2, v3, v4) -> v1 + v2 + v3 + v4 + "_derived")
.get();
assertEquals("value_value2_value3_value4_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.fromRequiredSystemProperties(c -> c.getProperty(), "test", "test2", "test3", "test4", "test5",
(v1, v2, v3, v4, v5) -> v1 + v2 + v3 + v4 + v5 + "_derived")
.get();
assertEquals("value_value2_value3_value4_value5_derived", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.ifSystemPropertyExists("test", b -> b.configure(c -> c.setProperty("exists")))
Expand All @@ -105,5 +171,10 @@ public void build() throws Exception {
b -> b.configure(c -> c.setProperty("non_existent")))
.get();
assertEquals("non_existent", configuration.getProperty());

configuration = new ConfigurationBuilder<>(new OutlineBuilder().wrap(new Configuration()))
.configure((b, c) -> c.setProperty(b.getRequiredSystemProperty("test").asString()))
.get();
assertEquals("value", configuration.getProperty());
}
}

0 comments on commit a29e9b7

Please sign in to comment.