Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemConfigurationProvider can't find ints, longs, and booleans #77

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
/**
* @return name/key of the config to assign
*/
String value();
String value() default "";

/**
* @return user displayable description of this configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,139 +16,22 @@

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<String, String> variableValues;

public SystemConfigurationProvider()
{
this(Maps.<String, String>newHashMap());
}

public SystemConfigurationProvider(Map<String, String> 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<Boolean> getBooleanSupplier(final ConfigurationKey key, final Boolean defaultValue)
{
return new Supplier<Boolean>()
{
@Override
public Boolean get()
{
Boolean value = Boolean.getBoolean(System.getProperty(key.getKey(variableValues)));
if ( value == null )
{
return defaultValue;
}
return value;
}
};
}

@Override
public Supplier<Integer> getIntegerSupplier(final ConfigurationKey key, final Integer defaultValue)
{
return new Supplier<Integer>()
{
@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<Long> getLongSupplier(final ConfigurationKey key, final Long defaultValue)
{
return new Supplier<Long>()
{
@Override
public Long get()
{
Long value = Long.getLong(System.getProperty(key.getKey(variableValues)));
if ( value == null )
{
return defaultValue;
}
return value;
}
};
}

@Override
public Supplier<Double> getDoubleSupplier(final ConfigurationKey key, final Double defaultValue)
{
return new Supplier<Double>()
{
@Override
public Double get()
{
Double value = Double.parseDouble(System.getProperty(key.getKey(variableValues)));
if ( value == null )
{
return defaultValue;
}
return value;
}
};
}

@Override
public Supplier<String> getStringSupplier(final ConfigurationKey key, final String defaultValue)
{
return new Supplier<String>()
{
@Override
public String get()
{
String value = System.getProperty(key.getKey(variableValues));
if ( value == null )
{
return defaultValue;
}
return value;
}
};
}

@Override
public Supplier<Date> getDateSupplier(ConfigurationKey key, Date defaultValue)
{
return new DateWithDefaultSupplier(getStringSupplier(key, null), defaultValue);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ public class ObjectWithConfig

@Configuration("test.dt")
public Date aDate = null;

@Configuration("test.auto")
public String auto;
}