Skip to content

Commit 79de339

Browse files
authored
Support application configuration file by default (#1190)
1 parent e3c72a1 commit 79de339

File tree

11 files changed

+279
-12
lines changed

11 files changed

+279
-12
lines changed

documentation/src/main/docs/config-sources/yaml.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# YAML Config Source
22

3-
This Config Source allows to use a `yaml` file to load configuration values. The YAML Config Source loads the
4-
configuration from the file `META-INF/microprofile-config.yaml`. It has a higher ordinal (`110`) than the
5-
`microprofile-config.properties`.
3+
This Config Source allows to use a `yaml` file to load configuration values. The YAML Config Source loads the configuration from the following files:
4+
5+
1. (`265`) `application.yaml|yml` in `config` folder, located in the current working directory
6+
2. (`255`) `application.yaml|yml` in the classpath
7+
3. (`110`) MicroProfile Config configuration file `META-INF/microprofile-config.yaml|yml` in the classpath
68

79
The following dependency is required in the classpath to use the YAML Config Source:
810

documentation/src/main/docs/config/getting-started.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ By default, SmallRye Config reads configuration properties from multiple configu
66

77
1. (`400`) System properties
88
2. (`300`) Environment variables
9-
3. (`100`) MicroProfile Config configuration file `META-INF/microprofile-config.properties` in the classpath
9+
3. (`260`) `application.properties` in `config` folder, located in the current working directory
10+
3. (`250`) `application.properties` in the classpath
11+
4. (`100`) MicroProfile Config configuration file `META-INF/microprofile-config.properties` in the classpath
1012

1113
A configuration source is handled by a `ConfigSource`. A `ConfigSource` provides configuration values from a specific
1214
place.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.smallrye.config;
2+
3+
import static java.util.Collections.emptyList;
4+
5+
import java.io.IOException;
6+
import java.net.URI;
7+
import java.net.URL;
8+
import java.util.List;
9+
10+
import org.eclipse.microprofile.config.spi.ConfigSource;
11+
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
12+
13+
public class PropertiesConfigSourceLoader extends AbstractLocationConfigSourceLoader {
14+
protected final String path;
15+
protected final int ordinal;
16+
17+
PropertiesConfigSourceLoader(final String path, final int ordinal) {
18+
this.path = path;
19+
this.ordinal = ordinal;
20+
}
21+
22+
@Override
23+
protected String[] getFileExtensions() {
24+
return new String[] { "properties" };
25+
}
26+
27+
@Override
28+
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
29+
return new PropertiesConfigSource(url, ordinal);
30+
}
31+
32+
public static List<ConfigSource> inClassPath(final String path, final int ordinal, final ClassLoader loader) {
33+
return new InClassPath(path, ordinal).getConfigSources(loader);
34+
}
35+
36+
public static List<ConfigSource> inFileSystem(final String path, final int ordinal, final ClassLoader loader) {
37+
return new InFileSystem(path, ordinal).getConfigSources(loader);
38+
}
39+
40+
private static class InClassPath extends PropertiesConfigSourceLoader implements ConfigSourceProvider {
41+
public InClassPath(final String path, final int ordinal) {
42+
super(path, ordinal);
43+
}
44+
45+
@Override
46+
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
47+
return loadConfigSources(path, ordinal, classLoader);
48+
}
49+
50+
@Override
51+
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
52+
return emptyList();
53+
}
54+
}
55+
56+
private static class InFileSystem extends PropertiesConfigSourceLoader implements ConfigSourceProvider {
57+
public InFileSystem(final String path, final int ordinal) {
58+
super(path, ordinal);
59+
}
60+
61+
@Override
62+
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
63+
return loadConfigSources(path, ordinal, classLoader);
64+
}
65+
66+
@Override
67+
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
68+
return emptyList();
69+
}
70+
}
71+
}

implementation/src/main/java/io/smallrye/config/PropertiesConfigSourceProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
/**
2828
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
2929
*/
30+
@Deprecated(forRemoval = true)
3031
public class PropertiesConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider {
3132
private final List<ConfigSource> configSources = new ArrayList<>();
3233
private final boolean includeFileSystem;

implementation/src/main/java/io/smallrye/config/SmallRyeConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,13 @@ private static List<ConfigSource> buildSources(final SmallRyeConfigBuilder build
839839
}
840840
if (builder.isAddDefaultSources()) {
841841
sourcesToBuild.addAll(builder.getDefaultSources());
842+
} else {
843+
if (builder.isAddSystemSources()) {
844+
sourcesToBuild.addAll(builder.getSystemSources());
845+
}
846+
if (builder.isAddPropertiesSources()) {
847+
sourcesToBuild.addAll(builder.getPropertiesSources());
848+
}
842849
}
843850

844851
return sourcesToBuild;

implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import static io.smallrye.config.Converters.newCollectionConverter;
2424
import static io.smallrye.config.Converters.newTrimmingConverter;
2525
import static io.smallrye.config.ProfileConfigSourceInterceptor.convertProfile;
26-
import static io.smallrye.config.PropertiesConfigSourceProvider.classPathSources;
26+
import static io.smallrye.config.PropertiesConfigSourceLoader.inClassPath;
27+
import static io.smallrye.config.PropertiesConfigSourceLoader.inFileSystem;
2728
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_LOG_VALUES;
2829
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
2930
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT;
3031
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_SECRET_HANDLERS;
3132

3233
import java.lang.reflect.Type;
34+
import java.nio.file.Paths;
3335
import java.util.ArrayList;
3436
import java.util.Collection;
3537
import java.util.Collections;
@@ -61,8 +63,6 @@
6163
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
6264
*/
6365
public class SmallRyeConfigBuilder implements ConfigBuilder {
64-
public static final String META_INF_MICROPROFILE_CONFIG_PROPERTIES = "META-INF/microprofile-config.properties";
65-
6666
private final List<SmallRyeConfigBuilderCustomizer> customizers = new ArrayList<>();
6767
// sources are not sorted by their ordinals
6868
private final List<ConfigSource> sources = new ArrayList<>();
@@ -78,6 +78,8 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
7878
private ClassLoader classLoader = SecuritySupport.getContextClassLoader();
7979
private boolean addDiscoveredCustomizers = false;
8080
private boolean addDefaultSources = false;
81+
private boolean addSystemSources = false;
82+
private boolean addPropertiesSources = false;
8183
private boolean addDefaultInterceptors = false;
8284
private boolean addDiscoveredSources = false;
8385
private boolean addDiscoveredConverters = false;
@@ -182,14 +184,38 @@ public SmallRyeConfigBuilder addDefaultSources() {
182184
return this;
183185
}
184186

187+
public SmallRyeConfigBuilder addSystemSources() {
188+
addSystemSources = true;
189+
return this;
190+
}
191+
192+
public SmallRyeConfigBuilder addPropertiesSources() {
193+
addPropertiesSources = true;
194+
return this;
195+
}
196+
185197
protected List<ConfigSource> getDefaultSources() {
186198
List<ConfigSource> defaultSources = new ArrayList<>();
199+
defaultSources.addAll(getSystemSources());
200+
defaultSources.addAll(getPropertiesSources());
201+
return defaultSources;
202+
}
187203

188-
defaultSources.add(new EnvConfigSource());
189-
defaultSources.add(new SysPropConfigSource());
190-
defaultSources.addAll(classPathSources(META_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader));
204+
protected List<ConfigSource> getSystemSources() {
205+
List<ConfigSource> sources = new ArrayList<>();
206+
sources.add(new EnvConfigSource());
207+
sources.add(new SysPropConfigSource());
208+
return sources;
209+
}
191210

192-
return defaultSources;
211+
protected List<ConfigSource> getPropertiesSources() {
212+
List<ConfigSource> sources = new ArrayList<>();
213+
sources.addAll(
214+
inFileSystem(Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(),
215+
260, classLoader));
216+
sources.addAll(inClassPath("application.properties", 250, classLoader));
217+
sources.addAll(inClassPath("META-INF/microprofile-config.properties", 100, classLoader));
218+
return sources;
193219
}
194220

195221
public SmallRyeConfigBuilder addDefaultInterceptors() {
@@ -616,6 +642,14 @@ public boolean isAddDefaultSources() {
616642
return addDefaultSources;
617643
}
618644

645+
public boolean isAddSystemSources() {
646+
return addSystemSources;
647+
}
648+
649+
public boolean isAddPropertiesSources() {
650+
return addPropertiesSources;
651+
}
652+
619653
public boolean isAddDefaultInterceptors() {
620654
return addDefaultInterceptors;
621655
}
@@ -645,6 +679,16 @@ public SmallRyeConfigBuilder setAddDefaultSources(final boolean addDefaultSource
645679
return this;
646680
}
647681

682+
public SmallRyeConfigBuilder setAddSystemSources(final boolean addSystemSources) {
683+
this.addSystemSources = addSystemSources;
684+
return this;
685+
}
686+
687+
public SmallRyeConfigBuilder setAddPropertiesSources(final boolean addPropertiesSources) {
688+
this.addPropertiesSources = addPropertiesSources;
689+
return this;
690+
}
691+
648692
public SmallRyeConfigBuilder setAddDefaultInterceptors(final boolean addDefaultInterceptors) {
649693
this.addDefaultInterceptors = addDefaultInterceptors;
650694
return this;

implementation/src/test/java/io/smallrye/config/SmallRyeConfigTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
import static org.junit.jupiter.api.Assertions.assertThrows;
1313
import static org.junit.jupiter.api.Assertions.assertTrue;
1414

15+
import java.io.File;
16+
import java.io.FileOutputStream;
17+
import java.net.URL;
18+
import java.net.URLClassLoader;
19+
import java.nio.file.Path;
1520
import java.util.ArrayList;
1621
import java.util.Arrays;
1722
import java.util.Collections;
1823
import java.util.List;
1924
import java.util.Map;
2025
import java.util.NoSuchElementException;
2126
import java.util.Optional;
27+
import java.util.Properties;
2228
import java.util.Set;
2329
import java.util.TreeMap;
2430
import java.util.function.IntFunction;
@@ -27,6 +33,7 @@
2733
import org.eclipse.microprofile.config.spi.ConfigSource;
2834
import org.eclipse.microprofile.config.spi.Converter;
2935
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.io.TempDir;
3037

3138
import io.smallrye.config.common.AbstractConfigSource;
3239
import io.smallrye.config.common.MapBackedConfigSource;
@@ -530,4 +537,33 @@ void emptyPropertyNames() {
530537

531538
assertEquals("value", config.getRawValue(""));
532539
}
540+
541+
@Test
542+
void systemSources() {
543+
SmallRyeConfig config = new SmallRyeConfigBuilder()
544+
.addSystemSources()
545+
.build();
546+
547+
assertTrue(config.getConfigSources(SysPropConfigSource.class).iterator().hasNext());
548+
assertTrue(config.getConfigSources(EnvConfigSource.class).iterator().hasNext());
549+
}
550+
551+
@Test
552+
void propertiesSources(@TempDir Path tempDir) throws Exception {
553+
File file = tempDir.resolve("application.properties").toFile();
554+
Properties properties = new Properties();
555+
properties.setProperty("my.prop", "1234");
556+
try (FileOutputStream out = new FileOutputStream(file)) {
557+
properties.store(out, null);
558+
}
559+
560+
SmallRyeConfig config = new SmallRyeConfigBuilder()
561+
.forClassLoader(new URLClassLoader(new URL[] { tempDir.toUri().toURL() }))
562+
.addPropertiesSources()
563+
.build();
564+
565+
assertFalse(config.getConfigSources(SysPropConfigSource.class).iterator().hasNext());
566+
assertFalse(config.getConfigSources(EnvConfigSource.class).iterator().hasNext());
567+
assertEquals("1234", config.getRawValue("my.prop"));
568+
}
533569
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package io.smallrye.config.source.yaml;
2+
3+
import static java.util.Collections.emptyList;
4+
5+
import java.io.IOException;
6+
import java.net.URI;
7+
import java.net.URL;
8+
import java.nio.file.Paths;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
import org.eclipse.microprofile.config.spi.ConfigSource;
13+
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
14+
15+
import io.smallrye.config.AbstractLocationConfigSourceLoader;
16+
17+
public class YamlConfigSourceLoader extends AbstractLocationConfigSourceLoader {
18+
@Override
19+
protected String[] getFileExtensions() {
20+
return new String[] {
21+
"yaml",
22+
"yml"
23+
};
24+
}
25+
26+
@Override
27+
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
28+
return new YamlConfigSource(url, ordinal);
29+
}
30+
31+
public static class InClassPath extends YamlConfigSourceLoader implements ConfigSourceProvider {
32+
@Override
33+
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
34+
List<ConfigSource> configSources = new ArrayList<>();
35+
configSources.addAll(loadConfigSources("application.yaml", 255, classLoader));
36+
configSources.addAll(loadConfigSources("application.yml", 255, classLoader));
37+
configSources.addAll(loadConfigSources("META-INF/microprofile-config.yaml", 110, classLoader));
38+
configSources.addAll(loadConfigSources("META-INF/microprofile-config.yml", 110, classLoader));
39+
return configSources;
40+
}
41+
42+
@Override
43+
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
44+
return emptyList();
45+
}
46+
}
47+
48+
public static class InFileSystem extends YamlConfigSourceLoader implements ConfigSourceProvider {
49+
@Override
50+
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
51+
List<ConfigSource> configSources = new ArrayList<>();
52+
configSources.addAll(loadConfigSources(
53+
Paths.get(System.getProperty("user.dir"), "config", "application.yaml").toUri().toString(), 265,
54+
classLoader));
55+
configSources.addAll(
56+
loadConfigSources(Paths.get(System.getProperty("user.dir"), "config", "application.yml").toUri().toString(),
57+
265, classLoader));
58+
return configSources;
59+
}
60+
61+
@Override
62+
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
63+
return emptyList();
64+
}
65+
}
66+
}

sources/yaml/src/main/java/io/smallrye/config/source/yaml/YamlConfigSourceProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import io.smallrye.config.AbstractLocationConfigSourceLoader;
1212

13+
@Deprecated(forRemoval = true)
1314
public class YamlConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider {
1415
@Override
1516
public String[] getFileExtensions() {
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
io.smallrye.config.source.yaml.YamlConfigSourceProvider
1+
io.smallrye.config.source.yaml.YamlConfigSourceLoader$InClassPath
2+
io.smallrye.config.source.yaml.YamlConfigSourceLoader$InFileSystem

0 commit comments

Comments
 (0)