package org.killbill.billing.util.config.tenant;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.killbill.billing.callcontext.InternalTenantContext;
import org.killbill.billing.util.config.definition.KillbillConfig;
import org.skife.config.Config;
import org.skife.config.Separator;
import org.skife.config.TimeSpan;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
public abstract class MultiTenantConfigBase {
private final static Function<String, Integer> INT_CONVERTER = new Function<String, Integer>() {
@Override
public Integer apply(final String input) {
return Integer.valueOf(input);
}
};
private final static Function<String, TimeSpan> TIME_SPAN_CONVERTER = new Function<String, TimeSpan>() {
@Override
public TimeSpan apply(final String input) {
return new TimeSpan(input);
}
};
public MultiTenantConfigBase() {
}
protected List<String> convertToListString(final String value, final String methodName) {
final Method method = getConfigStaticMethodWithChecking(methodName);
final Iterable<String> tokens = getTokens(method, value);
return ImmutableList.copyOf(tokens);
}
protected List<TimeSpan> convertToListTimeSpan(final String value, final String methodName) {
final Method method = getConfigStaticMethodWithChecking(methodName);
final Iterable<String> tokens = getTokens(method, value);
return ImmutableList.copyOf(Iterables.transform(tokens, TIME_SPAN_CONVERTER));
}
protected List<Integer> convertToListInteger(final String value, final String methodName) {
final Method method = getConfigStaticMethodWithChecking(methodName);
final Iterable<String> tokens = getTokens(method, value);
return ImmutableList.copyOf(Iterables.transform(tokens, INT_CONVERTER));
}
protected String getStringTenantConfig(final String methodName, final InternalTenantContext tenantContext) {
if (tenantContext == null) {
return null;
}
final Method method = getConfigStaticMethodWithChecking(methodName);
return getCachedValue(method.getAnnotation(Config.class), tenantContext);
}
private String getCachedValue(final Config annotation, final InternalTenantContext tenantContext) {
return null;
}
private Method getConfigStaticMethodWithChecking(final String methodName) {
final Method method = getConfigStaticMethod(methodName);
if (!method.isAnnotationPresent(Config.class)) {
throw new RuntimeException("Missing @Config annotation to skife config method " + method.getName());
}
return method;
}
private List<String> getTokens(final Method method, final String value) {
final Separator separator = method.getAnnotation(Separator.class);
if (value == null || value.isEmpty()) {
return ImmutableList.of();
} else {
return ImmutableList.copyOf(value.split(separator == null ? Separator.DEFAULT : separator.value()));
}
}
protected Method getConfigStaticMethod(final String methodName) {
Method method = null;
try {
method = getConfigClass().getMethod(methodName, InternalTenantContext.class);
} catch (final NoSuchMethodException e) {
throw new RuntimeException(e);
}
return method;
}
protected abstract Class<? extends KillbillConfig> getConfigClass();
}