kz.greetgo.conf.hot.AbstractConfigFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of greetgo.conf Show documentation
Show all versions of greetgo.conf Show documentation
Configuration library using in greetgo!
package kz.greetgo.conf.hot;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;
import static java.util.Collections.unmodifiableMap;
import static kz.greetgo.conf.hot.ConfigDataLoader.loadConfigDataTo;
/**
* Factory for hot config implementations
*/
public abstract class AbstractConfigFactory {
/**
* Gets config storage
*
* @return config storage
*/
protected abstract ConfigStorage getConfigStorage();
/**
* Calculates config location for config interface
*
* @param configInterface config interface
* @return config location
*/
protected abstract String configLocationFor(Class configInterface);
/**
* Marks all configs to reread from storage
*/
public void resetAll() {
resetIf(config -> true);
}
/**
* Marks specified configs to reread from storage
*/
public void resetIf(Predicate predicate) {
for (HotConfigImpl mediator : workingConfigs.values()) {
if (predicate.test(mediator)) {
mediator.reset();
}
}
}
private static final Object ABSENT_ENV = new Object();
/**
* Defines auto reset timeout. It is a time interval in milliseconds to check last config modification date and time.
* And if the date and time was changed, then it calls method `reset` for this config.
*
* @return auto reset timeout. Zero - auto resetting is off
*/
protected long autoResetTimeout() {
return 500;
}
private final Map workingConfigs = new ConcurrentHashMap<>();
/**
* Creates config storage with default values if it is absent
*/
public void sync() {
workingConfigs.values().forEach(HotConfigImpl::getData);
}
private class HotConfigImpl implements HotConfig {
private final AtomicReference