Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ buildscript {
def jacksonV = '2.8.5'
def okhttpV = '3.4.1'
def slf4jV = '1.7.21'
def logbackV = '1.1.8'
def undertowV = '1.3.22.Final'
def metricsV = '3.1.2'
def guavaV = '19.0'

def typesafeConfigV = '1.3.1'
// Test
def junitV = '4.12'

Expand All @@ -31,8 +32,13 @@ ext {
, [group: 'io.dropwizard.metrics', name: 'metrics-json', version: metricsV]
]
undertowLib = [[group: 'io.undertow', name: 'undertow-core', version: undertowV]]
slf4jLib = [[group: 'org.slf4j', name: 'slf4j-api', version: slf4jV]]
loggingLib = [
[group: 'org.slf4j', name: 'slf4j-api', version: slf4jV]
, [group: 'ch.qos.logback', name: 'logback-classic', version: logbackV]

]
guavaLib = [[group: 'com.google.guava', name: 'guava', version: guavaV]]
typesafeConfigLib = [[group: 'com.typesafe', name: 'config', version: typesafeConfigV]]

junitLib = [[group: 'junit', name: 'junit', version: junitV]]
}
Expand Down
9 changes: 4 additions & 5 deletions stubbornjava-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ dependencies {
runtime project(':stubbornjava-undertow')

// The production code uses the SLF4J logging API at compile time
compile slf4jLib
compile loggingLib

// Jackson libs
compile jacksonLib
Expand All @@ -14,9 +14,8 @@ dependencies {
// Guava
compile guavaLib

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
// Configuration
compile typesafeConfigLib

testCompile junitLib
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.stubbornjava.common;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigRenderOptions;

//{{start:config}}
public class Configs {
private static final Logger logger = LoggerFactory.getLogger(Configs.class);
/*
* I am letting the typesafe configs bleed out on purpose here.
* We could abstract out and delegate but its not worth it.
* I am gambling on the fact that I will not switch out the config library.
*/
private static final Config system = ConfigFactory.systemProperties();
private static final Config properties = new Builder().envAwareApp().build();

public static Config system() {
return system;
}

public static Config properties() {
return properties;
}

public static class Builder {
private final List<String> configs;

public Builder() {
this.configs = Lists.newLinkedList();
}

public Builder withResource(String resource) {
configs.add(resource);
return this;
}

public Builder envAwareApp() {
String env = system.hasPath("env") ? system.getString("env") : "local";
String envFile = "application." + env + ".conf";
configs.add(envFile);
configs.add("application.conf");
return this;
}

public Config build() {
logger.info("Loading configs first row is highest priority, second row is fallback and so on");
configs.forEach(logger::info);
Preconditions.checkArgument(configs.size() > 0, "No config resources specified!");
Config appConfig = ConfigFactory.parseResources(configs.remove(0));
for (String resource : configs) {
appConfig = appConfig.withFallback(ConfigFactory.parseResources(resource));
}
logger.debug("Logging properties. Make sure sensitive data such as passwords or secrets are not logged!");
logger.debug(appConfig.root().render(ConfigRenderOptions.concise().setFormatted(true)));
return appConfig;
}
}

public static void main(String[] args) {
Configs.properties();
}
}
//{{end:config}}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# See it in action

## Configuration
* [JVM configuration with Typesafe Config](https://www.stubbornjava.com/posts/environment-aware-configuration-with-typesafe-config)

## JSON Serialization
* [Jackson configuration / usage](https://www.stubbornjava.com/posts/practical-jackson-objectmapper-configuration)
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.stubbornjava.common;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.typesafe.config.Config;

public class ConfigsTest {

@Test(expected=IllegalArgumentException.class)
public void emptyConfigShouldFail() {
new Configs.Builder().build();
}

public void configShouldLoadResource() {
Config conf = new Configs.Builder()
.withResource("other.conf")
.build();
assertEquals("other", conf.getString("name"));
}

public void configShouldLoadAppConfig() {
Config conf = new Configs.Builder()
.envAwareApp()
.build();
assertEquals("StubbornJava Common", conf.getString("app"));
// 2 minutes is the override local config.
assertEquals(2, conf.getDuration("someTimeout", TimeUnit.MINUTES));
}
}
3 changes: 3 additions & 0 deletions stubbornjava-common/src/test/resources/application.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
app = "StubbornJava Common"

someTimeout = 5 minutes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
someTimeout = 2 minutes
1 change: 1 addition & 0 deletions stubbornjava-common/src/test/resources/other.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "other"