Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
Expand All @@ -45,12 +45,23 @@ public AugmentedConfigurationObjectFactory(final ConfigSource configSource) {
public <T> T build(final Class<T> configClass) {
final T instance = super.build(configClass);

collectConfigValues(configClass, instance);
collectConfigValues(configClass, instance, null);

return instance;
}

private <T> void collectConfigValues(final Class<T> configClass, final T instance) {
@Override
public <T> T buildWithReplacements(final Class<T> configClass, final Map<String, String> mappedReplacements) {
final T instance = super.buildWithReplacements(configClass, mappedReplacements);

collectConfigValues(configClass, instance, mappedReplacements);

return instance;
}

private <T> void collectConfigValues(final Class<T> configClass,
final T instance,
final Map<String, String> mappedReplacements) {
final String configSource = configClass.getSimpleName();

for (final Method method : configClass.getMethods()) {
Expand All @@ -60,11 +71,14 @@ private <T> void collectConfigValues(final Class<T> configClass, final T instanc
try {
final Object value = method.invoke(instance);
final String[] keys = configAnnotation.value();
Arrays.stream(keys)
.forEach(key -> RuntimeConfigRegistry.put(key, value));

Arrays.stream(keys)
.forEach(key -> RuntimeConfigRegistry.putWithSource(configSource, key, value));
for (String key : keys) {
if (mappedReplacements != null) {
key = applyReplacements(key, mappedReplacements);
}

RuntimeConfigRegistry.putWithSource(configSource, key, value);
}
} catch (final IllegalAccessException | InvocationTargetException e) {
log.warn("Failed to resolve config method: {}", method.getName(), e);
}
Expand All @@ -73,4 +87,14 @@ private <T> void collectConfigValues(final Class<T> configClass, final T instanc
}
}
}

private String applyReplacements(String propertyName, final Map<String, String> mappedReplacements) {
for (final Map.Entry<String, String> entry : mappedReplacements.entrySet()) {
final String token = "${" + entry.getKey() + "}";
final String replacement = entry.getValue();
propertyName = propertyName.replace(token, replacement);
}

return propertyName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.skife.config;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
Expand All @@ -27,14 +28,8 @@
*/
public class RuntimeConfigRegistry {

private static final Map<String, String> RUNTIME_CONFIGS = new ConcurrentHashMap<>();

private static final Map<String, Map<String, String>> RUNTIME_CONFIGS_BY_SOURCE = new ConcurrentHashMap<>();

public static void put(final String key, final Object value) {
RUNTIME_CONFIGS.put(key, value == null ? "" : value.toString());
}

public static void putWithSource(final String configSource, final String key, final Object value) {
RUNTIME_CONFIGS_BY_SOURCE
.computeIfAbsent(configSource, k -> new ConcurrentHashMap<>())
Expand All @@ -55,23 +50,32 @@ public static void putAllWithSource(final String configSource, final Map<String,
}

public static String get(final String key) {
return RUNTIME_CONFIGS.getOrDefault(key, "");
for (final Map<String, String> sourceMap : RUNTIME_CONFIGS_BY_SOURCE.values()) {
final String value = sourceMap.get(key);
if (value != null) {
return value;
}
}

return "";
}

public static Map<String, String> getBySource(final String source) {
return Collections.unmodifiableMap(RUNTIME_CONFIGS_BY_SOURCE.getOrDefault(source, Map.of()));
}

public static Map<String, String> getAll() {
return Collections.unmodifiableMap(RUNTIME_CONFIGS);
final Map<String, String> allConfigs = new LinkedHashMap<>();
RUNTIME_CONFIGS_BY_SOURCE.values().forEach(allConfigs::putAll);

return Collections.unmodifiableMap(allConfigs);
}

public static Map<String, Map<String, String>> getAllBySource() {
return Collections.unmodifiableMap(RUNTIME_CONFIGS_BY_SOURCE);
}

public static void clear() {
RUNTIME_CONFIGS.clear();
RUNTIME_CONFIGS_BY_SOURCE.clear();
}
}
Loading