Skip to content

Instantly share code, notes, and snippets.

@struberg
Created October 4, 2016 11:48
Show Gist options
  • Save struberg/23224d402cdbb9801573e9bf994377bd to your computer and use it in GitHub Desktop.
Save struberg/23224d402cdbb9801573e9bf994377bd to your computer and use it in GitHub Desktop.
import org.apache.deltaspike.core.api.config.ConfigResolver;
import java.util.concurrent.TimeUnit;
/**
* A value taken from DeltaSpike configuration.
* But with configurable expiry time
*
* Use the {@link #get()} method to resolve the cached or re-loaded value
*
* @author Mark Struberg
*/
public final class UrlConfigValue {
public static final int DEFAULT_RELOAD_TIME = 300;
private final ConfigResolver.TypedResolver<String> resolver;
/**
* This will reload after {@link #DEFAULT_RELOAD_TIME}
* @param configKey the name of the configuration key
*/
public UrlConfigValue(String configKey) {
this(configKey, null, DEFAULT_RELOAD_TIME);
}
/**
* @param configKey the name of the configuration key
* @param reloadAfterSeconds after how many seconds the value should get resolved from the underlying config system again
*/
public UrlConfigValue(String configKey, int reloadAfterSeconds) {
this(configKey, null, reloadAfterSeconds);
}
/**
* @param configKey the name of the configuration key
* @param defaultValue the default value
* @param reloadAfterSeconds after how many seconds the value should get resolved from the underlying config system again
*/
public UrlConfigValue(String configKey, String defaultValue, int reloadAfterSeconds) {
this.resolver = ConfigResolver.resolve(configKey)
.withDefault(defaultValue)
.cacheFor(TimeUnit.SECONDS, reloadAfterSeconds)
.evaluateVariables(true)
.withCurrentProjectStage(true)
.logChanges(true);
}
public String get() {
return resolver.getValue();
}
public String configKey() {
return resolver.getKey();
}
@Override
public String toString() {
return get();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment