Skip to content

Commit

Permalink
Properties from file system should be search with the same rules used…
Browse files Browse the repository at this point in the history
… for environment variables (#276)

Properties from file system should be search with the same rules used for environment variables.
  • Loading branch information
kifj authored Apr 13, 2020
1 parent 9937dbc commit 91b20f3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ Nested directories are not supported.
This Config Source can be used to read configuration from
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap[Kubernetes ConfigMap].

The same mapping rules as defined for environment variables are applied, so the FileSystemConfigSource will search for a given property name (e.g. com.ACME.size):

* Exact match (i.e. com.ACME.size)
* Replace each character that is neither alphanumeric nor _ with _ (i.e. com_ACME_size)
* Replace each character that is neither alphanumeric nor _ with _; then convert the name to upper case (i.e. COM_ACME_SIZE)

=== Usage

To use the FileSystem Config Source, add the following to your Maven `pom.xml`:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -59,6 +60,7 @@
public class FileSystemConfigSource extends MapBackedConfigSource {
private static final long serialVersionUID = 654034634846856045L;

private static final Pattern PATTERN = Pattern.compile("[^a-zA-Z0-9_]");
private static final Logger LOG = Logger.getLogger("io.smallrye.config");

FileSystemConfigSource(File dir) {
Expand Down Expand Up @@ -96,4 +98,30 @@ private static String readContent(Path file) {
throw new UncheckedIOException(e);
}
}

@Override
public String getValue(String name) {
if (name == null) {
return null;
}

final Map<String, String> properties = getProperties();

// exact match
String value = properties.get(name);
if (value != null) {
return value;
}

// replace non-alphanumeric characters by underscores
String sanitizedName = PATTERN.matcher(name).replaceAll("_");

value = properties.get(sanitizedName);
if (value != null) {
return value;
}

// replace non-alphanumeric characters by underscores and convert to uppercase
return properties.get(sanitizedName.toUpperCase());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.smallrye.config.source.file;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.File;
import java.net.URISyntaxException;
Expand All @@ -42,4 +43,20 @@ public void testConfigSourceFromDir() throws URISyntaxException {
assertEquals("myValue1", configSource.getValue("myKey1"));
assertEquals("true", configSource.getValue("myKey2"));
}

@Test
public void testCharacterReplacement() throws URISyntaxException {
URL configDirURL = this.getClass().getResource("configDir");
File dir = new File(configDirURL.toURI());

ConfigSource configSource = new FileSystemConfigSource(dir);
// the non-alphanumeric chars may be replaced by _
assertEquals("http://localhost:8080/my-service", configSource.getValue("MyService/mp-rest/url"));
// or the file name is uppercased
assertEquals("http://localhost:8080/other-service", configSource.getValue("OtherService/mp-rest/url"));
// but the key is still case sensitive
assertNull(configSource.getValue("myservice/mp-rest/url"));
// you can't rewrite the key, only the file name
assertNull(configSource.getValue("MYSERVICE_MP_REST_URL"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:8080/my-service
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
http://localhost:8080/other-service

0 comments on commit 91b20f3

Please sign in to comment.