Skip to content

Commit

Permalink
Extract Properties Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Feb 2, 2024
1 parent ca4d59c commit f5db568
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
16 changes: 13 additions & 3 deletions avaje-config/src/main/java/io/avaje/config/CoreConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static io.avaje.config.Constants.SYSTEM_PROPS;
import static io.avaje.config.Constants.USER_PROVIDED_DEFAULT;
import static java.lang.System.Logger.Level.ERROR;
import static java.util.Objects.requireNonNull;

/**
Expand Down Expand Up @@ -384,12 +385,12 @@ private void applyChangesAndPublish(CoreEventBuilder eventBuilder) {

@Override
public void onChange(Consumer<ModificationEvent> eventListener, String... keys) {
listeners.add(new CoreListener(eventListener, keys));
listeners.add(new CoreListener(log, eventListener, keys));
}

private OnChangeListener onChange(String key) {
requireNonNull(key, "key is required");
return callbacks.computeIfAbsent(key, s -> new OnChangeListener());
return callbacks.computeIfAbsent(key, s -> new OnChangeListener(log));
}

@Override
Expand Down Expand Up @@ -433,15 +434,24 @@ public void clearProperty(String key) {

private static class OnChangeListener {

private final ConfigurationLog log;
private final List<Consumer<String>> callbacks = new ArrayList<>();

OnChangeListener(ConfigurationLog log) {
this.log = log;
}

void register(Consumer<String> callback) {
callbacks.add(callback);
}

void fireOnChange(String value) {
for (Consumer<String> callback : callbacks) {
callback.accept(value);
try {
callback.accept(value);
} catch (Exception e) {
log.log(ERROR, "Error during onChange notification", e);
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions avaje-config/src/main/java/io/avaje/config/CoreListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@

import java.util.function.Consumer;

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

/**
* Wraps the listener taking the interesting keys into account.
*/
final class CoreListener {

private final ConfigurationLog log;
private final Consumer<ModificationEvent> listener;
private final String[] keys;

CoreListener(Consumer<ModificationEvent> listener, String[] keys) {
CoreListener(ConfigurationLog log, Consumer<ModificationEvent> listener, String[] keys) {
this.log = log;
this.listener = listener;
this.keys = keys;
}

void accept(CoreModificationEvent event) {
if (keys == null || keys.length == 0 || containsKey(event)) {
listener.accept(event);
try {
listener.accept(event);
} catch (Exception e) {
log.log(ERROR, "Error during onChange notification", e);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions avaje-config/src/main/java/io/avaje/config/Parsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class Parsers {
private final Map<String, ConfigParser> parserMap = new HashMap<>();

Parsers() {
parserMap.put("properties", new PropertiesParser());
if (!"true".equals(System.getProperty("skipYaml"))) {
initYamlParser();
}
Expand Down
54 changes: 54 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/PropertiesParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.avaje.config;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

final class PropertiesParser implements ConfigParser {

private static final String[] extensions = new String[]{"properties"};

@Override
public String[] supportedExtensions() {
return extensions;
}

@Override
public Map<String, String> load(Reader reader) {
try {
Properties p = new Properties();
p.load(reader);
return toMap(p);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public Map<String, String> load(InputStream is) {
try {
Properties p = new Properties();
p.load(is);
return toMap(p);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static Map<String, String> toMap(Properties p) {
Map<String, String> result = new LinkedHashMap<>();
Set<Map.Entry<Object, Object>> entries = p.entrySet();
for (Map.Entry<Object, Object> entry : entries) {
Object value = entry.getValue();
if (value != null) {
result.put(entry.getKey().toString(), entry.getValue().toString());
}
}
return result;
}
}

0 comments on commit f5db568

Please sign in to comment.