Skip to content

Commit 6b4d4d5

Browse files
authored
F/config (StubbornJava#2)
Added a config builder
1 parent 017eb71 commit 6b4d4d5

8 files changed

Lines changed: 124 additions & 7 deletions

File tree

‎build.gradle‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ buildscript {
1010
def jacksonV = '2.8.5'
1111
def okhttpV = '3.4.1'
1212
def slf4jV = '1.7.21'
13+
def logbackV = '1.1.8'
1314
def undertowV = '1.3.22.Final'
1415
def metricsV = '3.1.2'
1516
def guavaV = '19.0'
16-
17+
def typesafeConfigV = '1.3.1'
1718
// Test
1819
def junitV = '4.12'
1920

@@ -31,8 +32,13 @@ ext {
3132
, [group: 'io.dropwizard.metrics', name: 'metrics-json', version: metricsV]
3233
]
3334
undertowLib = [[group: 'io.undertow', name: 'undertow-core', version: undertowV]]
34-
slf4jLib = [[group: 'org.slf4j', name: 'slf4j-api', version: slf4jV]]
35+
loggingLib = [
36+
[group: 'org.slf4j', name: 'slf4j-api', version: slf4jV]
37+
, [group: 'ch.qos.logback', name: 'logback-classic', version: logbackV]
38+
39+
]
3540
guavaLib = [[group: 'com.google.guava', name: 'guava', version: guavaV]]
41+
typesafeConfigLib = [[group: 'com.typesafe', name: 'config', version: typesafeConfigV]]
3642

3743
junitLib = [[group: 'junit', name: 'junit', version: junitV]]
3844
}

‎stubbornjava-common/build.gradle‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ dependencies {
33
runtime project(':stubbornjava-undertow')
44

55
// The production code uses the SLF4J logging API at compile time
6-
compile slf4jLib
6+
compile loggingLib
77

88
// Jackson libs
99
compile jacksonLib
@@ -14,9 +14,8 @@ dependencies {
1414
// Guava
1515
compile guavaLib
1616

17-
// Declare the dependency for your favourite test framework you want to use in your tests.
18-
// TestNG is also supported by the Gradle Test task. Just change the
19-
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
20-
// 'test.useTestNG()' to your build script.
17+
// Configuration
18+
compile typesafeConfigLib
19+
2120
testCompile junitLib
2221
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.stubbornjava.common;
2+
3+
import java.util.List;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.google.common.base.Preconditions;
9+
import com.google.common.collect.Lists;
10+
import com.typesafe.config.Config;
11+
import com.typesafe.config.ConfigFactory;
12+
import com.typesafe.config.ConfigRenderOptions;
13+
14+
//{{start:config}}
15+
public class Configs {
16+
private static final Logger logger = LoggerFactory.getLogger(Configs.class);
17+
/*
18+
* I am letting the typesafe configs bleed out on purpose here.
19+
* We could abstract out and delegate but its not worth it.
20+
* I am gambling on the fact that I will not switch out the config library.
21+
*/
22+
private static final Config system = ConfigFactory.systemProperties();
23+
private static final Config properties = new Builder().envAwareApp().build();
24+
25+
public static Config system() {
26+
return system;
27+
}
28+
29+
public static Config properties() {
30+
return properties;
31+
}
32+
33+
public static class Builder {
34+
private final List<String> configs;
35+
36+
public Builder() {
37+
this.configs = Lists.newLinkedList();
38+
}
39+
40+
public Builder withResource(String resource) {
41+
configs.add(resource);
42+
return this;
43+
}
44+
45+
public Builder envAwareApp() {
46+
String env = system.hasPath("env") ? system.getString("env") : "local";
47+
String envFile = "application." + env + ".conf";
48+
configs.add(envFile);
49+
configs.add("application.conf");
50+
return this;
51+
}
52+
53+
public Config build() {
54+
logger.info("Loading configs first row is highest priority, second row is fallback and so on");
55+
configs.forEach(logger::info);
56+
Preconditions.checkArgument(configs.size() > 0, "No config resources specified!");
57+
Config appConfig = ConfigFactory.parseResources(configs.remove(0));
58+
for (String resource : configs) {
59+
appConfig = appConfig.withFallback(ConfigFactory.parseResources(resource));
60+
}
61+
logger.debug("Logging properties. Make sure sensitive data such as passwords or secrets are not logged!");
62+
logger.debug(appConfig.root().render(ConfigRenderOptions.concise().setFormatted(true)));
63+
return appConfig;
64+
}
65+
}
66+
67+
public static void main(String[] args) {
68+
Configs.properties();
69+
}
70+
}
71+
//{{end:config}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# See it in action
22

3+
## Configuration
4+
* [JVM configuration with Typesafe Config](https://www.stubbornjava.com/posts/environment-aware-configuration-with-typesafe-config)
5+
36
## JSON Serialization
47
* [Jackson configuration / usage](https://www.stubbornjava.com/posts/practical-jackson-objectmapper-configuration)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.stubbornjava.common;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.concurrent.TimeUnit;
6+
7+
import org.junit.Test;
8+
9+
import com.typesafe.config.Config;
10+
11+
public class ConfigsTest {
12+
13+
@Test(expected=IllegalArgumentException.class)
14+
public void emptyConfigShouldFail() {
15+
new Configs.Builder().build();
16+
}
17+
18+
public void configShouldLoadResource() {
19+
Config conf = new Configs.Builder()
20+
.withResource("other.conf")
21+
.build();
22+
assertEquals("other", conf.getString("name"));
23+
}
24+
25+
public void configShouldLoadAppConfig() {
26+
Config conf = new Configs.Builder()
27+
.envAwareApp()
28+
.build();
29+
assertEquals("StubbornJava Common", conf.getString("app"));
30+
// 2 minutes is the override local config.
31+
assertEquals(2, conf.getDuration("someTimeout", TimeUnit.MINUTES));
32+
}
33+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
app = "StubbornJava Common"
2+
3+
someTimeout = 5 minutes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
someTimeout = 2 minutes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name = "other"

0 commit comments

Comments
 (0)