From e6a80e1470eda7c31b48226fc3fc15b7db0bdd2b Mon Sep 17 00:00:00 2001 From: Mikko Kortelainen Date: Fri, 8 Dec 2023 13:34:43 +0200 Subject: [PATCH] implement config source (#17) * implement config source, configureable with -Dconfig.source=environemnt or -Dconfig.source=properties * add tests, configuration type instantiation from main --- .gitignore | 3 + pom.xml | 3 + .../teragrep/aer_01/EventContextConsumer.java | 7 +- src/main/java/com/teragrep/aer_01/Main.java | 26 ++++++- .../teragrep/aer_01/config/AzureConfig.java | 15 ++-- .../teragrep/aer_01/config/RelpConfig.java | 19 +++-- .../teragrep/aer_01/config/SyslogConfig.java | 14 ++-- .../config/source/EnvironmentSource.java | 73 ++++++++++++++++++ .../aer_01/config/source/PropertySource.java | 67 ++++++++++++++++ .../aer_01/config/source/Sourceable.java | 50 ++++++++++++ .../java/com/teragrep/aer_01/ConfigTest.java | 77 +++++++++++++++++++ 11 files changed, 330 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/teragrep/aer_01/config/source/EnvironmentSource.java create mode 100644 src/main/java/com/teragrep/aer_01/config/source/PropertySource.java create mode 100644 src/main/java/com/teragrep/aer_01/config/source/Sourceable.java create mode 100644 src/test/java/com/teragrep/aer_01/ConfigTest.java diff --git a/.gitignore b/.gitignore index 2f43530..04fb8d9 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ buildNumber.properties .project # JDT-specific (Eclipse Java Development Tools) .classpath +.flattened-pom.xml +.idea/ +aer_01.iml diff --git a/pom.xml b/pom.xml index c895446..0dbaa7a 100644 --- a/pom.xml +++ b/pom.xml @@ -325,6 +325,9 @@ all true + + azure_namespace_from_env + diff --git a/src/main/java/com/teragrep/aer_01/EventContextConsumer.java b/src/main/java/com/teragrep/aer_01/EventContextConsumer.java index 778bdcb..21d9ba7 100644 --- a/src/main/java/com/teragrep/aer_01/EventContextConsumer.java +++ b/src/main/java/com/teragrep/aer_01/EventContextConsumer.java @@ -49,6 +49,7 @@ import com.codahale.metrics.MetricRegistry; import com.teragrep.aer_01.config.RelpConfig; import com.teragrep.aer_01.config.SyslogConfig; +import com.teragrep.aer_01.config.source.Sourceable; import com.teragrep.rlo_14.SDElement; import com.teragrep.rlo_14.SyslogMessage; @@ -63,8 +64,8 @@ final class EventContextConsumer implements AutoCloseable, Consumer.servicebus.windows.net"); + return configSource.source("azure.namespace", ".servicebus.windows.net"); } private String getEventHubName() { - return System.getProperty("azure.eventhub", ""); + return configSource.source("azure.eventhub", ""); } private String getBlobStorageEndpoint() { - return System.getProperty("azure.blobstorage.endpoint", "https://.blob.core.windows.net"); + return configSource.source("azure.blobstorage.endpoint", "https://.blob.core.windows.net"); } private String getBlobStorageContainerName() { - return System.getProperty("azure.blobstorage.container", ""); + return configSource.source("azure.blobstorage.container", ""); } } diff --git a/src/main/java/com/teragrep/aer_01/config/RelpConfig.java b/src/main/java/com/teragrep/aer_01/config/RelpConfig.java index 3bb703b..b58ea1f 100644 --- a/src/main/java/com/teragrep/aer_01/config/RelpConfig.java +++ b/src/main/java/com/teragrep/aer_01/config/RelpConfig.java @@ -46,9 +46,11 @@ package com.teragrep.aer_01.config; +import com.teragrep.aer_01.config.source.Sourceable; + // copy from snw_01 with fixes final public class RelpConfig { - + public final Sourceable configSource; public final int connectionTimeout; public final int readTimeout; public final int writeTimeout; @@ -56,7 +58,8 @@ final public class RelpConfig { public final int destinationPort; public final String destinationAddress; - public RelpConfig() { + public RelpConfig(Sourceable configSource) { + this.configSource = configSource; this.connectionTimeout = getConnectTimeout(); this.readTimeout = getReadTimeout(); this.writeTimeout = getWriteTimeout(); @@ -69,7 +72,7 @@ public RelpConfig() { * @return relp.connection.timeout */ private int getConnectTimeout() { - String connectTimeout = System.getProperty("relp.connection.timeout", "5000"); + String connectTimeout = configSource.source("relp.connection.timeout", "5000"); return Integer.parseInt(connectTimeout); } @@ -77,7 +80,7 @@ private int getConnectTimeout() { * @return relp.transaction.read.timeout */ private int getReadTimeout() { - String readTimeout = System.getProperty("relp.transaction.read.timeout", "5000"); + String readTimeout = configSource.source("relp.transaction.read.timeout", "5000"); return Integer.parseInt(readTimeout); } @@ -85,7 +88,7 @@ private int getReadTimeout() { * @return relp.transaction.write.timeout */ private int getWriteTimeout() { - String writeTimeout = System.getProperty("relp.transaction.write.timeout", "5000"); + String writeTimeout = configSource.source("relp.transaction.write.timeout", "5000"); return Integer.parseInt(writeTimeout); } @@ -93,7 +96,7 @@ private int getWriteTimeout() { * @return relp.connection.retry.interval */ private int getReconnectInterval() { - String reconnectString = System.getProperty("relp.connection.retry.interval", "5000"); + String reconnectString = configSource.source("relp.connection.retry.interval", "5000"); return Integer.parseInt(reconnectString); } @@ -101,7 +104,7 @@ private int getReconnectInterval() { * @return relp.connection.port */ private int getRelpPort() { - String relpPort = System.getProperty("relp.connection.port", "601"); + String relpPort = configSource.source("relp.connection.port", "601"); return Integer.parseInt(relpPort); } @@ -109,6 +112,6 @@ private int getRelpPort() { * @return relp.connection.address */ private String getRelpAddress() { - return System.getProperty("relp.connection.address", "localhost"); + return configSource.source("relp.connection.address", "localhost"); } } \ No newline at end of file diff --git a/src/main/java/com/teragrep/aer_01/config/SyslogConfig.java b/src/main/java/com/teragrep/aer_01/config/SyslogConfig.java index 82fef6c..fb9d1b2 100644 --- a/src/main/java/com/teragrep/aer_01/config/SyslogConfig.java +++ b/src/main/java/com/teragrep/aer_01/config/SyslogConfig.java @@ -45,14 +45,16 @@ */ package com.teragrep.aer_01.config; -final public class SyslogConfig { - - // TODO copy from snw_01 +import com.teragrep.aer_01.config.source.Sourceable; +final public class SyslogConfig { + // TODO this is a copy from snw_01, with configSource + public final Sourceable configSource; public final String hostname; public final String appName; - public SyslogConfig() { + public SyslogConfig(Sourceable configSource) { + this.configSource = configSource; this.hostname = getHostname(); this.appName = getAppName(); } @@ -60,14 +62,14 @@ public SyslogConfig() { * @return syslog.appname */ private String getAppName() { - return System.getProperty("syslog.appname","aer-01"); + return configSource.source("syslog.appname","aer-01"); } /** * @return syslog.hostname */ private String getHostname() { - return System.getProperty("syslog.hostname","localhost.localdomain"); + return configSource.source("syslog.hostname","localhost.localdomain"); } } diff --git a/src/main/java/com/teragrep/aer_01/config/source/EnvironmentSource.java b/src/main/java/com/teragrep/aer_01/config/source/EnvironmentSource.java new file mode 100644 index 0000000..3ed617d --- /dev/null +++ b/src/main/java/com/teragrep/aer_01/config/source/EnvironmentSource.java @@ -0,0 +1,73 @@ +/* + * Teragrep Azure Eventhub Reader + * Copyright (C) 2023 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.aer_01.config.source; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +public class EnvironmentSource implements Sourceable { + private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentSource.class); + + private final Map envValues = System.getenv(); + + @Override + public String source(String name, String defaultValue) { + String variable = name.toUpperCase().replace(".", "_"); + LOGGER.debug("sourcing name <[{}]> as environment variable <[{}]>", name, variable); + String rv = envValues.getOrDefault(variable, defaultValue); + LOGGER.debug("sourced value <[{}]> for variable <[{}]>", rv, variable); + return rv; + } + + @Override + public String toString() { + return "EnvironmentSource{" + + "envValues=" + envValues + + '}'; + } +} diff --git a/src/main/java/com/teragrep/aer_01/config/source/PropertySource.java b/src/main/java/com/teragrep/aer_01/config/source/PropertySource.java new file mode 100644 index 0000000..e9fe105 --- /dev/null +++ b/src/main/java/com/teragrep/aer_01/config/source/PropertySource.java @@ -0,0 +1,67 @@ +/* + * Teragrep Azure Eventhub Reader + * Copyright (C) 2023 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.aer_01.config.source; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Properties; + +public final class PropertySource implements Sourceable { + private static final Logger LOGGER = LoggerFactory.getLogger(PropertySource.class); + private final Properties properties; + + public PropertySource() { + this.properties = System.getProperties(); + } + @Override + public String source(String name, String defaultValue) { + LOGGER.debug("sourcing property name <[{}]>", name); + String rv = properties.getProperty(name, defaultValue); + LOGGER.debug("sourced value <[{}]> for property name <[{}]>", rv, name); + return rv; + } +} diff --git a/src/main/java/com/teragrep/aer_01/config/source/Sourceable.java b/src/main/java/com/teragrep/aer_01/config/source/Sourceable.java new file mode 100644 index 0000000..f48c0ed --- /dev/null +++ b/src/main/java/com/teragrep/aer_01/config/source/Sourceable.java @@ -0,0 +1,50 @@ +/* + * Teragrep Azure Eventhub Reader + * Copyright (C) 2023 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.aer_01.config.source; + +public interface Sourceable { + String source(String name, String defaultValue); +} diff --git a/src/test/java/com/teragrep/aer_01/ConfigTest.java b/src/test/java/com/teragrep/aer_01/ConfigTest.java new file mode 100644 index 0000000..e61e96c --- /dev/null +++ b/src/test/java/com/teragrep/aer_01/ConfigTest.java @@ -0,0 +1,77 @@ +/* + * Teragrep Azure Eventhub Reader + * Copyright (C) 2023 Suomen Kanuuna Oy + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * + * Additional permission under GNU Affero General Public License version 3 + * section 7 + * + * If you modify this Program, or any covered work, by linking or combining it + * with other code, such other code is not for that reason alone subject to any + * of the requirements of the GNU Affero GPL version 3 as long as this Program + * is the same Program as licensed from Suomen Kanuuna Oy without any additional + * modifications. + * + * Supplemented terms under GNU Affero General Public License version 3 + * section 7 + * + * Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified + * versions must be marked as "Modified version of" The Program. + * + * Names of the licensors and authors may not be used for publicity purposes. + * + * No rights are granted for use of trade names, trademarks, or service marks + * which are in The Program if any. + * + * Licensee must indemnify licensors and authors for any liability that these + * contractual assumptions impose on licensors and authors. + * + * To the extent this program is licensed as part of the Commercial versions of + * Teragrep, the applicable Commercial License may apply to this file if you as + * a licensee so wish it. + */ +package com.teragrep.aer_01; + +import com.teragrep.aer_01.config.AzureConfig; +import com.teragrep.aer_01.config.RelpConfig; +import com.teragrep.aer_01.config.SyslogConfig; +import com.teragrep.aer_01.config.source.EnvironmentSource; +import com.teragrep.aer_01.config.source.PropertySource; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ConfigTest { + @Test + public void testConfigFromEnv() { + AzureConfig azureConfig = new AzureConfig(new EnvironmentSource()); // AZURE_NAMESPACE comes from maven + Assertions.assertEquals("azure_namespace_from_env", azureConfig.namespaceName, "Expected to get config from environment variable"); + } + + @Test + public void testConfigFromProperty() { + String expected = "testing.hostname.example.com"; + System.setProperty("syslog.hostname", expected); + SyslogConfig syslogConfig = new SyslogConfig(new PropertySource()); + Assertions.assertEquals(expected, syslogConfig.hostname, "Expected to get config from property"); + System.clearProperty("syslog.hostname"); + } + + @Test + public void testConfigFallback() { + RelpConfig relpConfig = new RelpConfig(new EnvironmentSource()); + Assertions.assertEquals(601, relpConfig.destinationPort, "Expected to get fallback value"); + } +}