diff --git a/debugShadowJar.sh b/debugShadowJar.sh index dc04cdb9e..311d4143b 100755 --- a/debugShadowJar.sh +++ b/debugShadowJar.sh @@ -1,3 +1,3 @@ #!/bin/sh -java -jar -D@appId=cms -D@environment=local $* -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 build/libs/*.jar +java -jar -D@appId=cms -D@environment=local $* -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 build/libs/*.jar diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index d65533b1e..d9060803f 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -26,7 +26,7 @@ repositories { def riposteVersion = '0.13.0' def logbackVersion = '1.1.7' // Request version 4 for full java 8 support -def guiceVersion = '4.0' +def guiceVersion = '4.2.2' def awsSdkVersion = '1.11.160' def groovyVersion = '2.3.9' diff --git a/src/main/java/com/nike/cerberus/server/config/CmsConfig.java b/src/main/java/com/nike/cerberus/server/config/CmsConfig.java index e4ba71d86..2f01893ef 100644 --- a/src/main/java/com/nike/cerberus/server/config/CmsConfig.java +++ b/src/main/java/com/nike/cerberus/server/config/CmsConfig.java @@ -20,18 +20,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.SerializationFeature; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Module; +import com.google.inject.*; +import com.google.inject.util.Modules; import com.nike.backstopper.handler.riposte.config.guice.BackstopperRiposteConfigGuiceModule; -import com.nike.cerberus.server.config.guice.AwsStsGuiceModule; -import com.nike.cerberus.server.config.guice.CerberusBackstopperRiposteGuiceModule; -import com.nike.cerberus.server.config.guice.CmsFlywayModule; -import com.nike.cerberus.server.config.guice.CmsGuiceModule; -import com.nike.cerberus.server.config.guice.CmsMyBatisModule; -import com.nike.cerberus.server.config.guice.GuiceProvidedServerConfigValues; -import com.nike.cerberus.server.config.guice.MetricsGuiceModule; -import com.nike.cerberus.server.config.guice.OneLoginGuiceModule; +import com.nike.cerberus.server.config.guice.*; +import com.nike.cerberus.service.ConfigService; import com.nike.cerberus.util.ArchaiusUtils; import com.nike.cerberus.util.JobsInitializerUtils; import com.nike.guice.PropertiesRegistrationGuiceModule; @@ -48,12 +41,12 @@ import com.nike.riposte.server.http.filter.RequestAndResponseFilter; import com.nike.riposte.server.logging.AccessLogger; import com.typesafe.config.Config; +import com.typesafe.config.ConfigException; import io.netty.handler.ssl.SslContext; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import javax.inject.Named; +import javax.inject.Singleton; +import java.util.*; import java.util.concurrent.CompletableFuture; /** @@ -96,7 +89,10 @@ protected CmsConfig(Config appConfig, PropertiesRegistrationGuiceModule properti // Create a Guice Injector for this app. List appGuiceModules = new ArrayList<>(); - appGuiceModules.add(propertiesRegistrationGuiceModule); + appGuiceModules.add( + Modules.override(propertiesRegistrationGuiceModule) + .with(new JdbcPropertiesModule()) // Allow us to dynamically set JDBC.url + ); appGuiceModules.addAll(Arrays.asList( new CmsMyBatisModule(), new BackstopperRiposteConfigGuiceModule(), diff --git a/src/main/java/com/nike/cerberus/server/config/guice/JdbcPropertiesModule.java b/src/main/java/com/nike/cerberus/server/config/guice/JdbcPropertiesModule.java new file mode 100644 index 000000000..b898ee3dd --- /dev/null +++ b/src/main/java/com/nike/cerberus/server/config/guice/JdbcPropertiesModule.java @@ -0,0 +1,34 @@ +package com.nike.cerberus.server.config.guice; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.nike.cerberus.service.ConfigService; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigException; + +import javax.inject.Named; +import javax.inject.Singleton; +import java.util.Optional; + +public class JdbcPropertiesModule extends AbstractModule { + + /** + * Attempts to fetch the region specific JDBC URL first, if not present fall back to the globally defined + * JDBC url (The write cluster for the primary region). + * + * @return The resolved jdbc url + */ + @Provides + @Singleton + @Named("JDBC.url") + public String jdbcUrl() { + ConfigService configService = ConfigService.getInstance(); + Config config = configService.getAppConfigMergedWithCliGeneratedProperties(); + String regionKey = Optional.ofNullable(configService.getSetRegion()).orElse("region_env_var_not_set"); + try { + return config.getString(String.format("JDBC.%s.url", regionKey)); + } catch (ConfigException e) { + return config.getString("JDBC.url"); + } + } +} diff --git a/src/main/java/com/nike/cerberus/service/ConfigService.java b/src/main/java/com/nike/cerberus/service/ConfigService.java index 3e3f6cac7..3a23530ea 100644 --- a/src/main/java/com/nike/cerberus/service/ConfigService.java +++ b/src/main/java/com/nike/cerberus/service/ConfigService.java @@ -135,4 +135,8 @@ public Set getEnabledEventProcessors() { } return new HashSet<>(); } + + public String getSetRegion() { + return System.getenv(REGION_KEY); + } }