package com.netflix.hystrix.strategy.properties;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.hystrix.strategy.HystrixPlugins;
public abstract class HystrixPropertiesChainedProperty {
private static final Logger logger = LoggerFactory.getLogger(HystrixPropertiesChainedProperty.class);
private static abstract class ChainLink<T> {
private final AtomicReference<ChainLink<T>> pReference;
private final ChainLink<T> next;
private final List<Runnable> callbacks;
public abstract String getName();
protected abstract T getValue();
public abstract boolean isValueAcceptable();
public ChainLink() {
next = null;
pReference = new AtomicReference<ChainLink<T>>(this);
callbacks = new ArrayList<Runnable>();
}
public ChainLink(ChainLink<T> nextProperty) {
next = nextProperty;
pReference = new AtomicReference<ChainLink<T>>(next);
callbacks = new ArrayList<Runnable>();
}
protected void checkAndFlip() {
if (next == null) {
pReference.set(this);
return;
}
if (this.isValueAcceptable()) {
logger.debug("Flipping property: {} to use its current value: {}", getName(), getValue());
pReference.set(this);
} else {
logger.debug("Flipping property: {} to use NEXT property: {}", getName(), next);
pReference.set(next);
}
for (Runnable r : callbacks) {
r.run();
}
}
public T get() {
if (pReference.get() == this) {
return this.getValue();
} else {
return pReference.get().get();
}
}
public void addCallback(Runnable r) {
callbacks.add(r);
}
public String toString() {
return getName() + " = " + get();
}
}
public static abstract class ChainBuilder<T> {
private ChainBuilder() {
super();
}
private List<HystrixDynamicProperty<T>> properties =
new ArrayList<HystrixDynamicProperty<T>>();
public ChainBuilder<T> add(HystrixDynamicProperty<T> property) {
properties.add(property);
return this;
}
public ChainBuilder<T> add(String name, T defaultValue) {
properties.add(getDynamicProperty(name, defaultValue, getType()));
return this;
}
public HystrixDynamicProperty<T> build() {
if (properties.size() < 1) throw new IllegalArgumentException();
if (properties.size() == 1) return properties.get(0);
List<HystrixDynamicProperty<T>> reversed =
new ArrayList<HystrixDynamicProperty<T>>(properties);
Collections.reverse(reversed);
ChainProperty<T> current = null;
for (HystrixDynamicProperty<T> p : reversed) {
if (current == null) {
current = new ChainProperty<T>(p);
}
else {
current = new ChainProperty<T>(p, current);
}
}
return new ChainHystrixProperty<T>(current);
}
protected abstract Class<T> getType();
}
private static <T> ChainBuilder<T> forType(final Class<T> type) {
return new ChainBuilder<T>() {
@Override
protected Class<T> getType() {
return type;
}
};
}
public static ChainBuilder<String> forString() {
return forType(String.class);
}
public static ChainBuilder<Integer> forInteger() {
return forType(Integer.class);
}
public static ChainBuilder<Boolean> forBoolean() {
return forType(Boolean.class);
}
public static ChainBuilder<Long> forLong() {
return forType(Long.class);
}
private static class ChainHystrixProperty<T> implements HystrixDynamicProperty<T> {
private final ChainProperty<T> property;
public ChainHystrixProperty(ChainProperty<T> property) {
super();
this.property = property;
}
@Override
public String getName() {
return property.getName();
}
@Override
public T get() {
return property.get();
}
@Override
public void addCallback(Runnable callback) {
property.addCallback(callback);
}
}
private static class ChainProperty<T> extends ChainLink<T> {
private final HystrixDynamicProperty<T> sProp;
public ChainProperty(HystrixDynamicProperty<T> sProperty) {
super();
sProp = sProperty;
}
public ChainProperty(HystrixDynamicProperty<T> sProperty, ChainProperty<T> next) {
super(next);
sProp = sProperty;
sProp.addCallback(new Runnable() {
@Override
public void run() {
logger.debug("Property changed: '{} = {}'", getName(), getValue());
checkAndFlip();
}
});
checkAndFlip();
}
@Override
public boolean isValueAcceptable() {
return (sProp.get() != null);
}
@Override
protected T getValue() {
return sProp.get();
}
@Override
public String getName() {
return sProp.getName();
}
}
private static <T> HystrixDynamicProperty<T>
getDynamicProperty(String propName, T defaultValue, Class<T> type) {
HystrixDynamicProperties properties = HystrixPlugins.getInstance().getDynamicProperties();
HystrixDynamicProperty<T> p =
HystrixDynamicProperties.Util.getProperty(properties, propName, defaultValue, type);
return p;
}
}