Skip to content

Commit

Permalink
implement config source (#17)
Browse files Browse the repository at this point in the history
* implement config source, configureable with -Dconfig.source=environemnt or -Dconfig.source=properties

* add tests, configuration type instantiation from main
  • Loading branch information
kortemik authored Dec 8, 2023
1 parent 30219f2 commit e6a80e1
Show file tree
Hide file tree
Showing 11 changed files with 330 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ buildNumber.properties
.project
# JDT-specific (Eclipse Java Development Tools)
.classpath
.flattened-pom.xml
.idea/
aer_01.iml
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@
<configuration>
<parallel>all</parallel>
<useUnlimitedThreads>true</useUnlimitedThreads>
<environmentVariables>
<AZURE_NAMESPACE>azure_namespace_from_env</AZURE_NAMESPACE>
</environmentVariables>
</configuration>
</plugin>
<plugin>
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/teragrep/aer_01/EventContextConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -63,8 +64,8 @@ final class EventContextConsumer implements AutoCloseable, Consumer<EventContext
private final Output output;
private final String realHostName;
private final SyslogConfig syslogConfig;
EventContextConsumer() {
RelpConfig relpConfig = new RelpConfig();
EventContextConsumer(Sourceable configSource) {
RelpConfig relpConfig = new RelpConfig(configSource);

this.output = new Output(
"defaultOutput",
Expand All @@ -78,7 +79,7 @@ final class EventContextConsumer implements AutoCloseable, Consumer<EventContext
);

this.realHostName = getRealHostName();
this.syslogConfig = new SyslogConfig();
this.syslogConfig = new SyslogConfig(configSource);
}

private String getRealHostName() {
Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/teragrep/aer_01/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.BlobContainerClientBuilder;
import com.teragrep.aer_01.config.AzureConfig;
import com.teragrep.aer_01.config.source.EnvironmentSource;
import com.teragrep.aer_01.config.source.PropertySource;
import com.teragrep.aer_01.config.source.Sourceable;

import java.io.IOException;

Expand All @@ -64,8 +67,10 @@
public final class Main {

public static void main(String[] args) throws IOException, InterruptedException {
try (final EventContextConsumer PARTITION_PROCESSOR = new EventContextConsumer()) {
AzureConfig azureConfig = new AzureConfig();
final Sourceable configSource = getConfigSource();

try (final EventContextConsumer PARTITION_PROCESSOR = new EventContextConsumer(configSource)) {
AzureConfig azureConfig = new AzureConfig(configSource);
final ErrorContextConsumer ERROR_HANDLER = new ErrorContextConsumer();

// create a token using the default Azure credential
Expand Down Expand Up @@ -98,4 +103,21 @@ public static void main(String[] args) throws IOException, InterruptedException
eventProcessorClient.stop();
}
}

private static Sourceable getConfigSource() {
String type = System.getProperty("config.source", "properties");

Sourceable rv;
if ("properties".equals(type)) {
rv = new PropertySource();
}
else if ("environment".equals(type)) {
rv = new EnvironmentSource();
}
else {
throw new IllegalArgumentException("config.source not within supported types: [properties, environment]");
}

return rv;
}
}
15 changes: 10 additions & 5 deletions src/main/java/com/teragrep/aer_01/config/AzureConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,36 @@
*/
package com.teragrep.aer_01.config;

import com.teragrep.aer_01.config.source.Sourceable;

final public class AzureConfig {
public final Sourceable configSource;
public final String namespaceName;
public final String eventHubName;
public final String blobStorageEndpoint;
public final String blobStorageContainerName;
public AzureConfig() {

public AzureConfig(Sourceable configSource) {
this.configSource = configSource;
this.namespaceName = getNamespaceName();
this.eventHubName = getEventHubName();
this.blobStorageEndpoint = getBlobStorageEndpoint();
this.blobStorageContainerName = getBlobStorageContainerName();
}

private String getNamespaceName() {
return System.getProperty("azure.namespace", "<NAMESPACE NAME>.servicebus.windows.net");
return configSource.source("azure.namespace", "<NAMESPACE NAME>.servicebus.windows.net");
}

private String getEventHubName() {
return System.getProperty("azure.eventhub", "<EVENT HUB NAME>");
return configSource.source("azure.eventhub", "<EVENT HUB NAME>");
}

private String getBlobStorageEndpoint() {
return System.getProperty("azure.blobstorage.endpoint", "https://<STORAGE ACCOUNT NAME>.blob.core.windows.net");
return configSource.source("azure.blobstorage.endpoint", "https://<STORAGE ACCOUNT NAME>.blob.core.windows.net");
}

private String getBlobStorageContainerName() {
return System.getProperty("azure.blobstorage.container", "<CONTAINER NAME>");
return configSource.source("azure.blobstorage.container", "<CONTAINER NAME>");
}
}
19 changes: 11 additions & 8 deletions src/main/java/com/teragrep/aer_01/config/RelpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@
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;
public final int reconnectInterval;
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();
Expand All @@ -69,46 +72,46 @@ 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);
}

/**
* @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);
}

/**
* @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);
}

/**
* @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);
}

/**
* @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);
}

/**
* @return relp.connection.address
*/
private String getRelpAddress() {
return System.getProperty("relp.connection.address", "localhost");
return configSource.source("relp.connection.address", "localhost");
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/teragrep/aer_01/config/SyslogConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,31 @@
*/
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();
}
/**
* @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");
}
}

Original file line number Diff line number Diff line change
@@ -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 <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* 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<String, String> 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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* 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;
}
}
Loading

0 comments on commit e6a80e1

Please sign in to comment.