Skip to content

Commit

Permalink
Add support for injecting List/Date/etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Constantin Muraru committed Apr 15, 2016
1 parent 462734e commit 09064b4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 49 deletions.
60 changes: 28 additions & 32 deletions archaius2-core/src/main/java/com/netflix/archaius/ConfigMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@
*/
package com.netflix.archaius;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.text.StrSubstitutor;

import com.netflix.archaius.api.Config;
import com.netflix.archaius.api.IoCContainer;
import com.netflix.archaius.api.annotations.Configuration;
import com.netflix.archaius.exceptions.MappingException;
import com.netflix.archaius.interpolate.ConfigStrLookup;
import org.apache.commons.lang3.text.StrSubstitutor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class ConfigMapper {
private static final IoCContainer NULL_IOC_CONTAINER = new IoCContainer() {
Expand All @@ -43,7 +42,6 @@ public <T> T getInstance(String name, Class<T> type) {
*
* @param injectee
* @param config
* @param ioc
* @throws MappingException
*/
public <T> void mapConfig(T injectee, Config config) throws MappingException {
Expand Down Expand Up @@ -111,18 +109,8 @@ public <T> void mapConfig(T injectee, final Config config, IoCContainer ioc) thr

String name = field.getName();
Class<?> type = field.getType();
Object value = null;
if (type.isInterface()) {
// TODO: Do Class.newInstance() if objName is a classname
String objName = config.getString(prefix + name, null);
if (objName != null) {
value = ioc.getInstance(objName, type);
}
}
else {
value = config.get(type, prefix + name, null);
}

Object value = getValue(config, ioc, prefix, name, type);

if (value != null) {
try {
field.setAccessible(true);
Expand Down Expand Up @@ -160,17 +148,8 @@ else if (name.startsWith("with") && name.length() > 4) {

method.setAccessible(true);
Class<?> type = method.getParameterTypes()[0];
Object value = null;
if (type.isInterface()) {
String objName = config.getString(prefix + name, null);
if (objName != null) {
value = ioc.getInstance(objName, type);
}
}
else {
value = config.get(type, prefix + name, null);
}

Object value = getValue(config, ioc, prefix, name, type);

if (value != null) {
try {
method.invoke(injectee, value);
Expand All @@ -190,4 +169,21 @@ else if (name.startsWith("with") && name.length() > 4) {
}
}
}

private Object getValue(Config config, IoCContainer ioc, String prefix, String name, Class<?> type) {
if (type.isInterface()) {
Object rawProperty = config.getRawProperty(prefix + name);
if (rawProperty != null && type.isAssignableFrom(rawProperty.getClass())) {
return rawProperty;
}

// TODO: Do Class.newInstance() if objName is a classname
String objName = config.getString(prefix + name, null);
if (objName != null) {
return ioc.getInstance(objName, type);
}
}

return config.get(type, prefix + name, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class MapConfig extends AbstractConfig {
* @author elandau
*/
public static class Builder {
Map<String, String> map = new HashMap<String, String>();
Map<String, Object> map = new HashMap<String, Object>();

public <T> Builder put(String key, T value) {
map.put(key, value.toString());
map.put(key, value);
return this;
}

Expand All @@ -60,19 +60,18 @@ public static Builder builder() {
public static MapConfig from(Properties props) {
return new MapConfig(props);
}
public static MapConfig from(Map<String, String> props) {

public static MapConfig from(Map<String, Object> props) {
return new MapConfig(props);
}
private Map<String, String> props = new HashMap<String, String>();

private Map<String, Object> props = new HashMap<String, Object>();

/**
* Construct a MapConfig as a copy of the provided Map
* @param name
* @param props
*/
public MapConfig(Map<String, String> props) {
public MapConfig(Map<String, Object> props) {
this.props.putAll(props);
this.props = Collections.unmodifiableMap(this.props);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
*/
package com.netflix.archaius.guice;

import java.util.Properties;

import javax.inject.Inject;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
Expand All @@ -45,6 +39,13 @@
import com.netflix.archaius.config.MapConfig;
import com.netflix.archaius.exceptions.MappingException;
import com.netflix.archaius.visitor.PrintStreamVisitor;
import org.junit.Assert;
import org.junit.Test;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;
import java.util.Properties;

public class ArchaiusModuleTest {

Expand All @@ -63,6 +64,8 @@ public static class MyServiceConfig {
private Boolean bool_value;
private Double double_value;
private Property<Integer> fast_int;
private List<Integer> int_list;
private Date date;
private Named named;

public void setStr_value(String value) {
Expand Down Expand Up @@ -342,4 +345,27 @@ public TestProxyConfig getProxyConfig(ConfigProxyFactory factory) {
Assert.assertArrayEquals(new String[]{"foo", "bar"}, configProxy.getStringArray());
Assert.assertArrayEquals(new Integer[]{1,2}, configProxy.getIntArray());
}

@Test
public void testObjectInjection() throws MappingException {
Date date = new Date(100);
final Config config = MapConfig.builder()
.put("env", "prod")
.put("prefix-prod.int_list", Lists.newArrayList(1, 2, 3))
.put("prefix-prod.date", date)
.build();

Injector injector = Guice.createInjector(
new ArchaiusModule() {
@Override
protected void configureArchaius() {
bindApplicationConfigurationOverride().toInstance(config);
}
});

MyServiceConfig serviceConfig = injector.getInstance(MyServiceConfig.class);
Assert.assertEquals(Lists.newArrayList(1, 2, 3), serviceConfig.int_list);
Assert.assertEquals(date, serviceConfig.date);
}

}

0 comments on commit 09064b4

Please sign in to comment.