com.netflix.fabricator.jackson.JacksonComponentConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fabricator-jackson Show documentation
Show all versions of fabricator-jackson Show documentation
fabricator-jackson developed by Netflix
package com.netflix.fabricator.jackson;
import java.util.Properties;
import java.util.Set;
import org.codehaus.jackson.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.Lists;
import com.netflix.fabricator.ComponentConfiguration;
import com.netflix.fabricator.jackson.JacksonComponentConfiguration;
import com.netflix.fabricator.supplier.ListenableSupplier;
public class JacksonComponentConfiguration implements ComponentConfiguration {
private static Logger LOG = LoggerFactory.getLogger(JacksonComponentConfiguration.class);
public static abstract class StaticListenableSupplier implements ListenableSupplier {
StaticListenableSupplier() {
}
@Override
public void onChange(Function func) {
throw new IllegalStateException("Change notification not supported");
}
}
private final String id;
private final String type;
private final JsonNode node;
public JacksonComponentConfiguration(String id, String type, JsonNode node) {
super();
this.id = id;
this.type = type;
this.node = node;
}
@Override
public String getId() {
return id;
}
@Override
public String getType() {
return type;
}
@Override
public ComponentConfiguration getChild(String name) {
return new JacksonComponentConfiguration(null, null, node.get(name));
}
@Override
public boolean isSimpleProperty(String propertyName) {
JsonNode child = node.get(propertyName);
if (child == null)
return true;
return !child.isObject();
}
@Override
public boolean hasProperty(String propertyName) {
return node.get(propertyName) != null;
}
@Override
public Set getUnknownProperties(Set supportedProperties) {
return null;
}
@Override
public T getValue(String propertyName, Class type) {
Supplier supplier = this.getDynamicValue(propertyName, type);
if (supplier != null)
return supplier.get();
return null;
}
@SuppressWarnings("unchecked")
@Override
public ListenableSupplier getDynamicValue(final String propertyName, Class type) {
if ( String.class.isAssignableFrom(type) ) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public String get() {
JsonNode child = node.get(propertyName);
if (child == null)
return null;
return child.asText();
}
};
}
else if ( Boolean.class.isAssignableFrom(type)
|| Boolean.TYPE.isAssignableFrom(type)
|| boolean.class.equals(type)) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public Boolean get() {
JsonNode child = node.get(propertyName);
if (child == null)
return null;
return child.asBoolean();
}
};
}
else if ( Integer.class.isAssignableFrom(type)
|| Integer.TYPE.isAssignableFrom(type)
|| int.class.equals(type)) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public Integer get() {
JsonNode child = node.get(propertyName);
if (child == null)
return null;
return child.asInt();
}
};
}
else if ( Long.class.isAssignableFrom(type)
|| Long.TYPE.isAssignableFrom(type)
|| long.class.equals(type)) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public Long get() {
JsonNode child = node.get(propertyName);
if (child == null)
return null;
return child.asLong();
}
};
}
else if ( Double.class.isAssignableFrom(type)
|| Double.TYPE.isAssignableFrom(type)
|| double.class.equals(type)) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public Double get() {
JsonNode child = node.get(propertyName);
if (child == null)
return null;
return child.asDouble();
}
};
}
else if ( Properties.class.isAssignableFrom(type)) {
return (ListenableSupplier) new StaticListenableSupplier() {
@Override
public Properties get() {
JsonNode child = node.get(propertyName);
Properties result = new Properties();
for (String prop : Lists.newArrayList(child.getFieldNames())) {
result.setProperty(prop, child.get(prop).asText());
}
return result;
}
};
}
else {
LOG.warn(String.format("Unknown type '%s' for property '%s'", type.getCanonicalName(), propertyName));
return null;
}
}
}