Skip to content

Commit

Permalink
Move avaje-aws-appconfig package
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Feb 2, 2024
1 parent f5db568 commit 5181f13
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 45 deletions.
3 changes: 2 additions & 1 deletion avaje-aws-appconfig/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<relativePath/>
</parent>

<artifactId>avaje-aws-appconfig</artifactId>
<groupId>io.avaje</groupId>
<artifactId>avaje-config-aws-appconfig</artifactId>
<version>0.3-SNAPSHOT</version>

<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.avaje.aws.appconfig;
package io.avaje.config.awsappconfig;

public interface AppConfigFetcher {
interface AppConfigFetcher {

static AppConfigFetcher.Builder builder() {
return new DAppConfigFetcher.Builder();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
package io.avaje.aws.appconfig;
package io.avaje.config.awsappconfig;

import io.avaje.config.ConfigParser;
import io.avaje.config.Configuration;
import io.avaje.config.ConfigurationSource;

import java.io.IOException;
import java.io.StringReader;
import java.util.Map;
import java.util.Properties;

import static java.lang.System.Logger.Level.*;

public final class AppConfigPlugin implements ConfigurationSource {
/**
* Plugin that loads AWS AppConfig as Yaml or Properties.
* <p>
* By default, will periodically reload the configuration if it has changed.
*/
public final class AwsAppConfigPlugin implements ConfigurationSource {

private static final System.Logger log = System.getLogger("io.avaje.config.AppConfigPlugin");
private static final System.Logger log = System.getLogger("io.avaje.config.AwsAppConfig");

@Override
public void load(Configuration configuration) {
if (!configuration.getBool("aws.appconfig.enabled", true)) {
log.log(INFO, "AppConfigPlugin is not enabled");
if (!configuration.enabled("aws.appconfig.enabled", true)) {
log.log(INFO, "AwsAppConfig plugin is disabled");
return;
}

var loader = new Loader(configuration);
loader.schedule();
loader.reload();
}


static final class Loader {

private final Configuration configuration;
private final AppConfigFetcher fetcher;
private final ConfigParser yamlParser;
private final ConfigParser propertiesParser;
private final long frequency;

private String currentVersion = "";
private String currentVersion = "none";

Loader(Configuration configuration) {
this.configuration = configuration;
this.frequency = configuration.getLong("aws.appconfig.frequency", 60L);
this.propertiesParser = configuration.parser("properties").orElseThrow();
this.yamlParser = configuration.parser("yaml").orElse(null);
if (yamlParser == null) {
log.log(WARNING, "No Yaml parser registered");
}

var app = configuration.get("aws.appconfig.application");
var env = configuration.get("aws.appconfig.environment");
var con = configuration.get("aws.appconfig.configuration");
var con = configuration.get("aws.appconfig.configuration", env + "-" + app);

this.fetcher = AppConfigFetcher.builder()
.application(app)
Expand All @@ -61,37 +67,35 @@ void reload() {
try {
AppConfigFetcher.Result result = fetcher.fetch();
if (currentVersion.equals(result.version())) {
log.log(TRACE, "AwsAppConfig unchanged, version", currentVersion);
log.log(TRACE, "AwsAppConfig unchanged, version {0}", currentVersion);
} else {
String contentType = result.contentType();
if (log.isLoggable(TRACE)) {
log.log(TRACE, "AwsAppConfig fetched version:{0} contentType:{1} body:{2}", result.version(), contentType, result.body());
}
if (contentType.endsWith("yaml")) {
if (yamlParser == null) {
log.log(ERROR, "No Yaml Parser registered to parse AWS AppConfig");
} else {
Map<String, String> keyValues = yamlParser.load(new StringReader(result.body()));
configuration.eventBuilder("AwsAppConfig")
.putAll(keyValues)
.publish();
currentVersion = result.version();
debugLog(result, keyValues.size());
}
} else {
// assuming properties content
Properties properties = new Properties();
properties.load(new StringReader(result.body()));
configuration.eventBuilder("AwsAppConfig")
.putAll(properties)
Map<String, String> keyValues = parse(result);
configuration.eventBuilder("AwsAppConfig")
.putAll(keyValues)
.publish();
currentVersion = result.version();
debugLog(result, properties.size());
}
currentVersion = result.version();
debugLog(result, keyValues.size());
}

} catch (AppConfigFetcher.FetchException | IOException e) {
log.log(ERROR, "Error fetching or processing AppConfig", e);
} catch (Exception e) {
log.log(ERROR, "Error fetching or processing AwsAppConfig", e);
}
}

private Map<String, String> parse(AppConfigFetcher.Result result) {
ConfigParser parser = parser(result.contentType());
return parser.load(new StringReader(result.body()));
}

private ConfigParser parser(String contentType) {
if (contentType.endsWith("yaml")) {
return yamlParser;
} else {
return propertiesParser;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.avaje.aws.appconfig;
package io.avaje.config.awsappconfig;

import java.io.IOException;
import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.avaje.aws.appconfig;
package io.avaje.config.awsappconfig;

final class DResult implements AppConfigFetcher.Result {

private final String version;
private final String contentType;
private final String body;
public DResult(String version, String contentType, String body) {

DResult(String version, String contentType, String body) {
this.version = version;
this.contentType = contentType;
this.body = body;
Expand Down
8 changes: 4 additions & 4 deletions avaje-aws-appconfig/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io.avaje.aws.appconfig.AppConfigPlugin;
import io.avaje.config.awsappconfig.AwsAppConfigPlugin;

module io.avaje.aws.appconfig {
module io.avaje.config.awsappconfig {

exports io.avaje.aws.appconfig;
exports io.avaje.config.awsappconfig;

requires io.avaje.config;
requires java.net.http;
provides io.avaje.config.ConfigurationSource with AppConfigPlugin;
provides io.avaje.config.ConfigurationSource with AwsAppConfigPlugin;
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
io.avaje.aws.appconfig.AppConfigPlugin
io.avaje.config.awsappconfig.AwsAppConfigPlugin

0 comments on commit 5181f13

Please sign in to comment.