diff --git a/src/main/java/com/netflix/governator/annotations/Configuration.java b/src/main/java/com/netflix/governator/annotations/Configuration.java index 67d26584..d675895b 100644 --- a/src/main/java/com/netflix/governator/annotations/Configuration.java +++ b/src/main/java/com/netflix/governator/annotations/Configuration.java @@ -34,7 +34,7 @@ /** * @return name/key of the config to assign */ - String value(); + String value() default ""; /** * @return user displayable description of this configuration diff --git a/src/main/java/com/netflix/governator/configuration/SystemConfigurationProvider.java b/src/main/java/com/netflix/governator/configuration/SystemConfigurationProvider.java index 14e5ab8d..69886dc3 100644 --- a/src/main/java/com/netflix/governator/configuration/SystemConfigurationProvider.java +++ b/src/main/java/com/netflix/governator/configuration/SystemConfigurationProvider.java @@ -16,18 +16,15 @@ package com.netflix.governator.configuration; -import com.google.common.base.Supplier; import com.google.common.collect.Maps; -import java.util.Date; + import java.util.Map; /** * ConfigurationProvider backed by the system properties */ -public class SystemConfigurationProvider implements ConfigurationProvider +public class SystemConfigurationProvider extends PropertiesConfigurationProvider implements ConfigurationProvider { - private final Map variableValues; - public SystemConfigurationProvider() { this(Maps.newHashMap()); @@ -35,120 +32,6 @@ public SystemConfigurationProvider() public SystemConfigurationProvider(Map variableValues) { - this.variableValues = Maps.newHashMap(variableValues); - } - - /** - * Change a variable value - * - * @param name name - * @param value value - */ - public void setVariable(String name, String value) - { - variableValues.put(name, value); - } - - @Override - public boolean has(ConfigurationKey key) - { - return System.getProperty(key.getKey(variableValues), null) != null; - } - - @Override - public Supplier getBooleanSupplier(final ConfigurationKey key, final Boolean defaultValue) - { - return new Supplier() - { - @Override - public Boolean get() - { - Boolean value = Boolean.getBoolean(System.getProperty(key.getKey(variableValues))); - if ( value == null ) - { - return defaultValue; - } - return value; - } - }; - } - - @Override - public Supplier getIntegerSupplier(final ConfigurationKey key, final Integer defaultValue) - { - return new Supplier() - { - @Override - public Integer get() - { - Integer value = Integer.getInteger(System.getProperty(key.getKey(variableValues))); - if ( value == null ) - { - return defaultValue; - } - return value; - } - }; + super(System.getProperties(), variableValues); } - - @Override - public Supplier getLongSupplier(final ConfigurationKey key, final Long defaultValue) - { - return new Supplier() - { - @Override - public Long get() - { - Long value = Long.getLong(System.getProperty(key.getKey(variableValues))); - if ( value == null ) - { - return defaultValue; - } - return value; - } - }; - } - - @Override - public Supplier getDoubleSupplier(final ConfigurationKey key, final Double defaultValue) - { - return new Supplier() - { - @Override - public Double get() - { - Double value = Double.parseDouble(System.getProperty(key.getKey(variableValues))); - if ( value == null ) - { - return defaultValue; - } - return value; - } - }; - } - - @Override - public Supplier getStringSupplier(final ConfigurationKey key, final String defaultValue) - { - return new Supplier() - { - @Override - public String get() - { - String value = System.getProperty(key.getKey(variableValues)); - if ( value == null ) - { - return defaultValue; - } - return value; - } - }; - } - - @Override - public Supplier getDateSupplier(ConfigurationKey key, Date defaultValue) - { - return new DateWithDefaultSupplier(getStringSupplier(key, null), defaultValue); - } - } diff --git a/src/main/java/com/netflix/governator/lifecycle/ConfigurationProcessor.java b/src/main/java/com/netflix/governator/lifecycle/ConfigurationProcessor.java index 128a44c9..21323bb9 100644 --- a/src/main/java/com/netflix/governator/lifecycle/ConfigurationProcessor.java +++ b/src/main/java/com/netflix/governator/lifecycle/ConfigurationProcessor.java @@ -47,6 +47,11 @@ void assignConfiguration(Object obj, Field field) throws Exception { Configuration configuration = field.getAnnotation(Configuration.class); String configurationName = configuration.value(); + + if (configurationName.equals("") || configurationName.endsWith(".")) { + configurationName += field.getName(); + } + ConfigurationKey key = new ConfigurationKey(configurationName, KeyParser.parse(configurationName)); Object value = null; @@ -84,12 +89,12 @@ void assignConfiguration(Object obj, Field field) throws Exception } catch ( IllegalArgumentException e ) { - ignoreTypeMismtachIfConfigured(configuration, configurationName, e); + ignoreTypeMismatchIfConfigured(configuration, configurationName, e); field = null; } catch ( ConversionException e ) { - ignoreTypeMismtachIfConfigured(configuration, configurationName, e); + ignoreTypeMismatchIfConfigured(configuration, configurationName, e); field = null; } } @@ -162,7 +167,7 @@ else if ( Date.class.isAssignableFrom(type) ) } } - private void ignoreTypeMismtachIfConfigured(Configuration configuration, String configurationName, Exception e) + private void ignoreTypeMismatchIfConfigured(Configuration configuration, String configurationName, Exception e) { if ( configuration.ignoreTypeMismatch() ) { diff --git a/src/test/java/com/netflix/governator/lifecycle/TestConfiguration.java b/src/test/java/com/netflix/governator/lifecycle/TestConfiguration.java index 825fbb80..26182985 100644 --- a/src/test/java/com/netflix/governator/lifecycle/TestConfiguration.java +++ b/src/test/java/com/netflix/governator/lifecycle/TestConfiguration.java @@ -92,6 +92,7 @@ public void testConfig() throws Exception properties.setProperty("test.d", "300.4"); properties.setProperty("test.s", "a is a"); properties.setProperty("test.dt", "1964-10-06"); + properties.setProperty("test.auto", "bindByName"); LifecycleManagerArguments arguments = new LifecycleManagerArguments(); arguments.getConfigurationProvider().add(new PropertiesConfigurationProvider(properties)); @@ -107,6 +108,7 @@ public void testConfig() throws Exception Assert.assertEquals(obj.aLong, 200); Assert.assertEquals(obj.aDouble, 300.4); Assert.assertEquals(obj.aString, "a is a"); + Assert.assertEquals(obj.auto, "bindByName"); } @Test diff --git a/src/test/java/com/netflix/governator/lifecycle/mocks/ObjectWithConfig.java b/src/test/java/com/netflix/governator/lifecycle/mocks/ObjectWithConfig.java index ad8d0da1..e1fe1169 100644 --- a/src/test/java/com/netflix/governator/lifecycle/mocks/ObjectWithConfig.java +++ b/src/test/java/com/netflix/governator/lifecycle/mocks/ObjectWithConfig.java @@ -38,4 +38,7 @@ public class ObjectWithConfig @Configuration("test.dt") public Date aDate = null; + + @Configuration("test.auto") + public String auto; }