Skip to content

Commit

Permalink
#59 - Added a ConfigValueConfigSource so it can be combined with inte…
Browse files Browse the repository at this point in the history
…rceptors and trace the origin of a configuration key.
  • Loading branch information
radcortez committed Mar 24, 2020
1 parent 03c8ba0 commit 2a3d542
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 9 deletions.
20 changes: 19 additions & 1 deletion implementation/src/main/java/io/smallrye/config/ConfigValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ public class ConfigValue {
private final String configSourceName;
private final int configSourceOrdinal;

private final int lineNumber;

private ConfigValue(final ConfigValueBuilder builder) {
this.name = builder.name;
this.value = builder.value;
this.configSourceName = builder.configSourceName;
this.configSourceOrdinal = builder.configSourceOrdinal;
this.lineNumber = builder.lineNumber;
}

public String getName() {
Expand All @@ -29,6 +32,10 @@ public int getConfigSourceOrdinal() {
return configSourceOrdinal;
}

public int getLineNumber() {
return lineNumber;
}

public ConfigValue withName(final String name) {
return from().withName(name).build();
}
Expand All @@ -45,12 +52,17 @@ public ConfigValue withConfigSourceOrdinal(final int configSourceOrdinal) {
return from().withConfigSourceOrdinal(configSourceOrdinal).build();
}

public ConfigValue withLineNumber(final int lineNumber) {
return from().withLineNumber(lineNumber).build();
}

ConfigValueBuilder from() {
return new ConfigValueBuilder()
.withName(name)
.withValue(value)
.withConfigSourceName(configSourceName)
.withConfigSourceOrdinal(configSourceOrdinal);
.withConfigSourceOrdinal(configSourceOrdinal)
.withLineNumber(lineNumber);
}

public static ConfigValueBuilder builder() {
Expand All @@ -62,6 +74,7 @@ public static class ConfigValueBuilder {
private String value;
private String configSourceName;
private int configSourceOrdinal;
private int lineNumber;

public ConfigValueBuilder withName(final String name) {
this.name = name;
Expand All @@ -83,6 +96,11 @@ public ConfigValueBuilder withConfigSourceOrdinal(final int configSourceOrdinal)
return this;
}

public ConfigValueBuilder withLineNumber(final int lineNumber) {
this.lineNumber = lineNumber;
return this;
}

public ConfigValue build() {
return new ConfigValue(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.smallrye.config;

import static java.util.stream.Collectors.toMap;

import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import org.eclipse.microprofile.config.spi.ConfigSource;

public interface ConfigValueConfigSource extends ConfigSource {
ConfigValue getConfigValue(String propertyName);

Map<String, ConfigValue> getConfigValueProperties();

@Override
default Map<String, String> getProperties() {
return getConfigValueProperties().entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
new Function<Map.Entry<String, ConfigValue>, String>() {
@Override
public String apply(final Map.Entry<String, ConfigValue> entry) {
return entry.getValue() != null ? entry.getValue().getValue() : null;
}
}));
}

@Override
default Set<String> getPropertyNames() {
return getProperties().keySet();
}

@Override
default String getValue(String propertyName) {
final ConfigValue value = getConfigValue(propertyName);
return value != null ? value.getValue() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.smallrye.config;

import javax.annotation.Priority;

import org.jboss.logging.Logger;

@Priority(100)
public class ConfigValueConfigSourceInterceptor implements ConfigSourceInterceptor {
private static final Logger LOG = Logger.getLogger("io.smallrye.config");

@Override
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) {
final ConfigValue configValue = context.proceed(name);

LOG.infof("The configuration %s with the value %s was loaded from %s:%d",
configValue.getName(),
configValue.getValue(),
configValue.getConfigSourceName(),
configValue.getLineNumber());

return configValue;
}
}
Loading

0 comments on commit 2a3d542

Please sign in to comment.