From 4ee9e7a747fbc7d60fd19994754f4b1e1182cbcb Mon Sep 17 00:00:00 2001 From: Jarrod Ribble Date: Sat, 12 Sep 2020 18:19:17 -0600 Subject: [PATCH 1/4] Expand config variables from system properties if not found as environment variables. Allow '.' in config variable names: REDIS.HOST as well as REDIS_HOST Signed-off-by: Jarrod Ribble --- .../org/redisson/config/ConfigSupport.java | 5 +- .../redisson/config/ConfigSupportTest.java | 92 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/redisson/src/main/java/org/redisson/config/ConfigSupport.java b/redisson/src/main/java/org/redisson/config/ConfigSupport.java index 5107dbf37..a5d5948bf 100644 --- a/redisson/src/main/java/org/redisson/config/ConfigSupport.java +++ b/redisson/src/main/java/org/redisson/config/ConfigSupport.java @@ -104,11 +104,14 @@ public class ConfigSupport { } private String resolveEnvParams(String content) { - Pattern pattern = Pattern.compile("\\$\\{(\\w+(:-.+?)?)\\}"); + Pattern pattern = Pattern.compile("\\$\\{([\\w\\.]+(:-.+?)?)\\}"); Matcher m = pattern.matcher(content); while (m.find()) { String[] parts = m.group(1).split(":-"); String v = System.getenv(parts[0]); + if (v == null) { + v = System.getProperty(parts[0]); + } if (v != null) { content = content.replace(m.group(), v); } else if (parts.length == 2) { diff --git a/redisson/src/test/java/org/redisson/config/ConfigSupportTest.java b/redisson/src/test/java/org/redisson/config/ConfigSupportTest.java index 5d3ad9747..b1597768c 100644 --- a/redisson/src/test/java/org/redisson/config/ConfigSupportTest.java +++ b/redisson/src/test/java/org/redisson/config/ConfigSupportTest.java @@ -25,6 +25,14 @@ public class ConfigSupportTest { assertEquals("redis://1.1.1.1", config.getAddress()); } + + @Test + public void testParsingProperty() throws IOException { + mockHostProperty("1.1.1.1"); + SingleServerConfig config = mkConfig("${REDIS_URI}"); + + assertEquals("redis://1.1.1.1", config.getAddress()); + } @Test public void testParsingEnv2() throws IOException { @@ -34,9 +42,18 @@ public class ConfigSupportTest { assertEquals("redis://1.1.1.1:6379", config.getAddress()); } + @Test + public void testParsingProperty2() throws IOException { + mockHostPortProperty("1.1.1.1", "6379"); + SingleServerConfig config = mkConfig("${REDIS_HOST}:${REDIS_PORT}"); + + assertEquals("redis://1.1.1.1:6379", config.getAddress()); + } + @Test public void testParsingEnv_envMissing() throws IOException { mockHostEnv(null); + mockHostProperty(null); final SingleServerConfig config = mkConfig("${REDIS_URI}"); assertEquals("redis://${REDIS_URI}", config.getAddress()); @@ -49,6 +66,14 @@ public class ConfigSupportTest { assertEquals("redis://11.0.0.1", config.getAddress()); } + + @Test + public void testParsingDefault_propertyPresent() throws IOException { + mockHostProperty("11.0.0.1"); + SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}"); + + assertEquals("redis://11.0.0.1", config.getAddress()); + } @Test public void testParsingDefault_envPresent2() throws IOException { @@ -57,10 +82,19 @@ public class ConfigSupportTest { assertEquals("redis://11.0.0.1:1234", config.getAddress()); } + + @Test + public void testParsingDefault_propertyPresent2() throws IOException { + mockHostPortProperty("11.0.0.1", "1234"); + SingleServerConfig config = mkConfig("${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}"); + + assertEquals("redis://11.0.0.1:1234", config.getAddress()); + } @Test public void testParsingDefault_envMissing() throws IOException { mockHostEnv(null); + mockHostProperty(null); SingleServerConfig config = mkConfig("${REDIS_URI:-10.0.0.1}"); assertEquals("redis://10.0.0.1", config.getAddress()); @@ -69,10 +103,27 @@ public class ConfigSupportTest { @Test public void testParsingDefault_envMissing2() throws IOException { mockHostPortEnv(null, null); + mockHostPortProperty(null, null); SingleServerConfig config = mkConfig("${REDIS_HOST:-127.0.0.1}:${REDIS_PORT:-6379}"); assertEquals("redis://127.0.0.1:6379", config.getAddress()); } + + @Test + public void testParsingDefaultPeriod_propertyPresent2() throws IOException { + mockHostPortPropertyPeriod("11.0.0.1", "1234"); + SingleServerConfig config = mkConfig("${REDIS.HOST:-127.0.0.1}:${REDIS.PORT:-6379}"); + + assertEquals("redis://11.0.0.1:1234", config.getAddress()); + } + + @Test + public void testParsingDefaultPeriod_envMissing() throws IOException { + mockHostPortProperty(null, null); + SingleServerConfig config = mkConfig("${REDIS.HOST:-127.0.0.1}:${REDIS.PORT:-6379}"); + + assertEquals("redis://127.0.0.1:6379", config.getAddress()); + } private SingleServerConfig mkConfig(String authorityValue) throws IOException { String config = "singleServerConfig:\n address: redis://" + authorityValue; @@ -87,6 +138,15 @@ public class ConfigSupportTest { } }; } + + private void mockHostProperty(String value) { + new MockUp() { + @Mock + String getProperty(String name) { + return value; + } + }; + } private void mockHostPortEnv(String host, String port) { new MockUp() { @@ -104,4 +164,36 @@ public class ConfigSupportTest { }; } + private void mockHostPortProperty(String host, String port) { + new MockUp() { + @Mock + String getProperty(String name) { + switch (name) { + case "REDIS_HOST": + return host; + case "REDIS_PORT": + return port; + default: + return null; + } + } + }; + } + + private void mockHostPortPropertyPeriod(String host, String port) { + new MockUp() { + @Mock + String getProperty(String name) { + switch (name) { + case "REDIS.HOST": + return host; + case "REDIS.PORT": + return port; + default: + return null; + } + } + }; + } + } From f105674a0d0b8aa17ab8913dcdada4d82c772561 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 14 Sep 2020 14:21:11 +0300 Subject: [PATCH 2/4] Update README.md --- redisson-tomcat/README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/redisson-tomcat/README.md b/redisson-tomcat/README.md index 3072b01f7..aca4e0409 100644 --- a/redisson-tomcat/README.md +++ b/redisson-tomcat/README.md @@ -6,10 +6,6 @@ Supports Apache Tomcat 7.x, 8.x, 9.x Consider __[Redisson PRO](https://redisson.pro)__ version for advanced features and support by SLA. -## Advantages - -Current implementation differs from any other Redis based Tomcat Session Manager in terms of efficient storage and optimized writes. Each session attribute is written into Redis during each `HttpSession.setAttribute` invocation. While other solutions serialize whole session each time. - ## Usage ### 1. Add `RedissonSessionManager` From 8bbe102f82a41af0958f100f797871b5dc0f3808 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 14 Sep 2020 14:22:19 +0300 Subject: [PATCH 3/4] Update README.md --- redisson-tomcat/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redisson-tomcat/README.md b/redisson-tomcat/README.md index aca4e0409..b07d9040d 100644 --- a/redisson-tomcat/README.md +++ b/redisson-tomcat/README.md @@ -8,7 +8,7 @@ Supports Apache Tomcat 7.x, 8.x, 9.x ## Usage -### 1. Add `RedissonSessionManager` +### 1. Define session manager Add `RedissonSessionManager` in `tomcat/conf/context.xml` or per context in `tomcat/conf/server.xml` From b33e70ba48bbb4e342128fbf57337acce8bc81ab Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 14 Sep 2020 14:22:33 +0300 Subject: [PATCH 4/4] Update README.md --- redisson-tomcat/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redisson-tomcat/README.md b/redisson-tomcat/README.md index b07d9040d..744a1738c 100644 --- a/redisson-tomcat/README.md +++ b/redisson-tomcat/README.md @@ -8,7 +8,7 @@ Supports Apache Tomcat 7.x, 8.x, 9.x ## Usage -### 1. Define session manager +### 1. Add session manager Add `RedissonSessionManager` in `tomcat/conf/context.xml` or per context in `tomcat/conf/server.xml`