Skip to content

Commit

Permalink
Support dynamic configs of different kinds, not just String
Browse files Browse the repository at this point in the history
This is very useful for remote TypesafeConfigLoader, which I’m going to
add in a different pull request. It makes it possible to enhance the
config with objects of type
Duration/Date/ConfigList/ConfigMemorySize/etc.
  • Loading branch information
Constantin Muraru committed Apr 13, 2016
1 parent 25a18b8 commit 7f2a8dc
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class PollingDynamicConfig extends AbstractConfig {
private static final Logger LOG = LoggerFactory.getLogger(PollingDynamicConfig.class);

private volatile Map<String, String> current = new HashMap<String, String>();
private volatile Map<String, Object> current = new HashMap<String, Object>();
private final AtomicBoolean busy = new AtomicBoolean();
private final Callable<PollingResponse> reader;
private final AtomicLong updateCounter = new AtomicLong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.util.Map;

public abstract class PollingResponse {
public static PollingResponse forSnapshot(final Map<String, String> values) {
public static PollingResponse forSnapshot(final Map<String, Object> values) {
return new PollingResponse() {
@Override
public Map<String, String> getToAdd() {
public Map<String, Object> getToAdd() {
return values;
}

Expand All @@ -28,7 +28,7 @@ public boolean hasData() {
public static PollingResponse noop() {
return new PollingResponse() {
@Override
public Map<String, String> getToAdd() {
public Map<String, Object> getToAdd() {
return Collections.emptyMap();
}

Expand All @@ -44,7 +44,7 @@ public boolean hasData() {
};
}

public abstract Map<String, String> getToAdd();
public abstract Map<String, Object> getToAdd();
public abstract Collection<String> getToRemove();
public abstract boolean hasData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ private void internalLoad(Properties props, Set<String> seenUrls, ClassLoader lo
try {
// Load properties into the single Properties object overriding any property
// that may already exist
Map<String, String> p = new URLConfigReader(url).call().getToAdd();
Map<String, Object> p = new URLConfigReader(url).call().getToAdd();
LOG.debug("Loaded : {}", url.toExternalForm());
props.putAll(p);

// Recursively load any files referenced by an @next property in the file
// Only one @next property is expected and the value may be a list of files
String next = p.get(INCLUDE_KEY);
if (next != null) {
Object next = p.get(INCLUDE_KEY);
if (next != null && next instanceof String) {
p.remove(INCLUDE_KEY);
for (String urlString : next.split(",")) {
for (String urlString : ((String) next).split(",")) {
URL nextUrl = getResource(loader, strInterpolator.create(lookup).resolve(urlString));
if (nextUrl != null) {
internalLoad(props, seenUrls, loader, nextUrl, strInterpolator, lookup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static URL[] createUrls(String... urlStrings) {

@Override
public PollingResponse call() throws IOException {
final Map<String, String> map = new HashMap<String, String>();
final Map<String, Object> map = new HashMap<String, Object>();
for (URL url: configUrls) {
Properties props = new Properties();
InputStream fin = url.openStream();
Expand All @@ -95,7 +95,7 @@ public PollingResponse call() throws IOException {
}
return new PollingResponse() {
@Override
public Map<String, String> getToAdd() {
public Map<String, Object> getToAdd() {
return map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testBasicRead() throws Exception {
server.getServerPathURI("/prop1").toURL()
);

Map<String, String> result;
Map<String, Object> result;

prop1.setProperty("a", "a_value");
result = reader.call().getToAdd();
Expand All @@ -72,7 +72,7 @@ public void testCombineSources() throws Exception {
prop1.setProperty("a", "A");
prop2.setProperty("b", "B");

Map<String, String> result = reader.call().getToAdd();
Map<String, Object> result = reader.call().getToAdd();

Assert.assertEquals(2, result.size());
Assert.assertEquals("A", result.get("a"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public PollingResponse call() throws Exception {
}

// Resolve to a single property value
final Map<String, String> result = new HashMap<String, String>();
final Map<String, Object> result = new HashMap<String, Object>();
for (Entry<String, List<ScopedValue>> entry : props.entrySet()) {
result.put(entry.getKey(), valueResolver.resolve(entry.getKey(), entry.getValue()));
}
Expand Down

0 comments on commit 7f2a8dc

Please sign in to comment.