From 3fa55cf76d758c1314d47dce1dc7b883ed3f81f1 Mon Sep 17 00:00:00 2001 From: jnorm1 Date: Mon, 26 Feb 2018 14:02:09 -0800 Subject: [PATCH] allow the appid and environment to be overriden when loading the typesafe config files --- .../typesafeconfig/TypesafeConfigServer.java | 15 ++++++++++- .../TypesafeConfigServerTest.java | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/riposte-typesafe-config/src/main/java/com/nike/riposte/typesafeconfig/TypesafeConfigServer.java b/riposte-typesafe-config/src/main/java/com/nike/riposte/typesafeconfig/TypesafeConfigServer.java index c3ae9856..5cf2af0a 100644 --- a/riposte-typesafe-config/src/main/java/com/nike/riposte/typesafeconfig/TypesafeConfigServer.java +++ b/riposte-typesafe-config/src/main/java/com/nike/riposte/typesafeconfig/TypesafeConfigServer.java @@ -81,7 +81,7 @@ public void launchServer(String[] args) throws Exception { protected void infrastructureInit() { MainClassUtils.setupJbossLoggingToUseSlf4j(); - Pair appIdAndEnvironmentPair = MainClassUtils.getAppIdAndEnvironmentFromSystemProperties(); + Pair appIdAndEnvironmentPair = getAppIdAndEnvironmentPair(); Config appConfig = TypesafeConfigUtil .loadConfigForAppIdAndEnvironment(appIdAndEnvironmentPair.getLeft(), appIdAndEnvironmentPair.getRight()); @@ -97,6 +97,19 @@ protected void infrastructureInit() { setAppConfig(appConfig); } + /** + * Allow the appId and Environment to be overridden when loading TypesafeConfig files. + * This will support resolution of different TypesafeConfig files without globally modifying the + * appId or environment. This would allow region specific property files + * while still maintaining the same appId or environment variables. + * + * @return {@code Pair} with the {@code Left} the appId of the TypesafeConfig to load + * and the {@code Right} the Environment. + */ + protected Pair getAppIdAndEnvironmentPair(){ + return MainClassUtils.getAppIdAndEnvironmentFromSystemProperties(); + } + /** * Creates the {@link Server} instance using {@link #getServerConfig(Config)} and {@link #getAppConfig()}, then * calls {@link Server#startup()}. DO NOT CALL THIS DIRECTLY. Use {@link #launchServer(String[])} when you're ready diff --git a/riposte-typesafe-config/src/test/java/com/nike/riposte/typesafeconfig/TypesafeConfigServerTest.java b/riposte-typesafe-config/src/test/java/com/nike/riposte/typesafeconfig/TypesafeConfigServerTest.java index a112ed3b..1c587c94 100644 --- a/riposte-typesafe-config/src/test/java/com/nike/riposte/typesafeconfig/TypesafeConfigServerTest.java +++ b/riposte-typesafe-config/src/test/java/com/nike/riposte/typesafeconfig/TypesafeConfigServerTest.java @@ -1,5 +1,6 @@ package com.nike.riposte.typesafeconfig; +import com.nike.internal.util.Pair; import com.nike.riposte.server.config.ServerConfig; import com.nike.riposte.server.http.Endpoint; import com.nike.riposte.server.http.RequestInfo; @@ -21,6 +22,7 @@ import java.util.Collections; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicBoolean; import io.netty.channel.ChannelHandlerContext; import io.netty.util.ResourceLeakDetector; @@ -115,6 +117,30 @@ public void verify_essential_behavior() throws Exception { assertThat(ResourceLeakDetector.getLevel()).isEqualTo(ResourceLeakDetector.Level.PARANOID); } + @Test + public void getAppIdAndEnvironmentPair(){ + // given + setAppAndEnvironment("typesafeconfigserver", "compiletimetest"); + + AtomicBoolean called = new AtomicBoolean(false); + // when + TypesafeConfigServer server = new TypesafeConfigServer(){ + protected ServerConfig getServerConfig(Config appConfig){ + return null; + } + + @Override + protected Pair getAppIdAndEnvironmentPair() { + called.set(true); + return super.getAppIdAndEnvironmentPair(); + } + }; + server.infrastructureInit(); + + // then (verify that getAppIdAndEnvironmentPair was called from infrastructureInit) + assertThat(called.get()).isTrue(); + } + private static class SomeEndpoint extends StandardEndpoint { public static final String MATCHING_PATH = "/typesafeConfigValue";