Skip to content

Commit

Permalink
Support application configuration file by default (#1190)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jul 4, 2024
1 parent e3c72a1 commit 79de339
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 12 deletions.
8 changes: 5 additions & 3 deletions documentation/src/main/docs/config-sources/yaml.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# YAML Config Source

This Config Source allows to use a `yaml` file to load configuration values. The YAML Config Source loads the
configuration from the file `META-INF/microprofile-config.yaml`. It has a higher ordinal (`110`) than the
`microprofile-config.properties`.
This Config Source allows to use a `yaml` file to load configuration values. The YAML Config Source loads the configuration from the following files:

1. (`265`) `application.yaml|yml` in `config` folder, located in the current working directory
2. (`255`) `application.yaml|yml` in the classpath
3. (`110`) MicroProfile Config configuration file `META-INF/microprofile-config.yaml|yml` in the classpath

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

Expand Down
4 changes: 3 additions & 1 deletion documentation/src/main/docs/config/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ By default, SmallRye Config reads configuration properties from multiple configu

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

A configuration source is handled by a `ConfigSource`. A `ConfigSource` provides configuration values from a specific
place.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package io.smallrye.config;

import static java.util.Collections.emptyList;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

public class PropertiesConfigSourceLoader extends AbstractLocationConfigSourceLoader {
protected final String path;
protected final int ordinal;

PropertiesConfigSourceLoader(final String path, final int ordinal) {
this.path = path;
this.ordinal = ordinal;
}

@Override
protected String[] getFileExtensions() {
return new String[] { "properties" };
}

@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
return new PropertiesConfigSource(url, ordinal);
}

public static List<ConfigSource> inClassPath(final String path, final int ordinal, final ClassLoader loader) {
return new InClassPath(path, ordinal).getConfigSources(loader);
}

public static List<ConfigSource> inFileSystem(final String path, final int ordinal, final ClassLoader loader) {
return new InFileSystem(path, ordinal).getConfigSources(loader);
}

private static class InClassPath extends PropertiesConfigSourceLoader implements ConfigSourceProvider {
public InClassPath(final String path, final int ordinal) {
super(path, ordinal);
}

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
return loadConfigSources(path, ordinal, classLoader);
}

@Override
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
return emptyList();
}
}

private static class InFileSystem extends PropertiesConfigSourceLoader implements ConfigSourceProvider {
public InFileSystem(final String path, final int ordinal) {
super(path, ordinal);
}

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
return loadConfigSources(path, ordinal, classLoader);
}

@Override
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
return emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/**
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
@Deprecated(forRemoval = true)
public class PropertiesConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider {
private final List<ConfigSource> configSources = new ArrayList<>();
private final boolean includeFileSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,13 @@ private static List<ConfigSource> buildSources(final SmallRyeConfigBuilder build
}
if (builder.isAddDefaultSources()) {
sourcesToBuild.addAll(builder.getDefaultSources());
} else {
if (builder.isAddSystemSources()) {
sourcesToBuild.addAll(builder.getSystemSources());
}
if (builder.isAddPropertiesSources()) {
sourcesToBuild.addAll(builder.getPropertiesSources());
}
}

return sourcesToBuild;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import static io.smallrye.config.Converters.newCollectionConverter;
import static io.smallrye.config.Converters.newTrimmingConverter;
import static io.smallrye.config.ProfileConfigSourceInterceptor.convertProfile;
import static io.smallrye.config.PropertiesConfigSourceProvider.classPathSources;
import static io.smallrye.config.PropertiesConfigSourceLoader.inClassPath;
import static io.smallrye.config.PropertiesConfigSourceLoader.inFileSystem;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_LOG_VALUES;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_SECRET_HANDLERS;

import java.lang.reflect.Type;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -61,8 +63,6 @@
* @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2017 Red Hat inc.
*/
public class SmallRyeConfigBuilder implements ConfigBuilder {
public static final String META_INF_MICROPROFILE_CONFIG_PROPERTIES = "META-INF/microprofile-config.properties";

private final List<SmallRyeConfigBuilderCustomizer> customizers = new ArrayList<>();
// sources are not sorted by their ordinals
private final List<ConfigSource> sources = new ArrayList<>();
Expand All @@ -78,6 +78,8 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
private ClassLoader classLoader = SecuritySupport.getContextClassLoader();
private boolean addDiscoveredCustomizers = false;
private boolean addDefaultSources = false;
private boolean addSystemSources = false;
private boolean addPropertiesSources = false;
private boolean addDefaultInterceptors = false;
private boolean addDiscoveredSources = false;
private boolean addDiscoveredConverters = false;
Expand Down Expand Up @@ -182,14 +184,38 @@ public SmallRyeConfigBuilder addDefaultSources() {
return this;
}

public SmallRyeConfigBuilder addSystemSources() {
addSystemSources = true;
return this;
}

public SmallRyeConfigBuilder addPropertiesSources() {
addPropertiesSources = true;
return this;
}

protected List<ConfigSource> getDefaultSources() {
List<ConfigSource> defaultSources = new ArrayList<>();
defaultSources.addAll(getSystemSources());
defaultSources.addAll(getPropertiesSources());
return defaultSources;
}

defaultSources.add(new EnvConfigSource());
defaultSources.add(new SysPropConfigSource());
defaultSources.addAll(classPathSources(META_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader));
protected List<ConfigSource> getSystemSources() {
List<ConfigSource> sources = new ArrayList<>();
sources.add(new EnvConfigSource());
sources.add(new SysPropConfigSource());
return sources;
}

return defaultSources;
protected List<ConfigSource> getPropertiesSources() {
List<ConfigSource> sources = new ArrayList<>();
sources.addAll(
inFileSystem(Paths.get(System.getProperty("user.dir"), "config", "application.properties").toUri().toString(),
260, classLoader));
sources.addAll(inClassPath("application.properties", 250, classLoader));
sources.addAll(inClassPath("META-INF/microprofile-config.properties", 100, classLoader));
return sources;
}

public SmallRyeConfigBuilder addDefaultInterceptors() {
Expand Down Expand Up @@ -616,6 +642,14 @@ public boolean isAddDefaultSources() {
return addDefaultSources;
}

public boolean isAddSystemSources() {
return addSystemSources;
}

public boolean isAddPropertiesSources() {
return addPropertiesSources;
}

public boolean isAddDefaultInterceptors() {
return addDefaultInterceptors;
}
Expand Down Expand Up @@ -645,6 +679,16 @@ public SmallRyeConfigBuilder setAddDefaultSources(final boolean addDefaultSource
return this;
}

public SmallRyeConfigBuilder setAddSystemSources(final boolean addSystemSources) {
this.addSystemSources = addSystemSources;
return this;
}

public SmallRyeConfigBuilder setAddPropertiesSources(final boolean addPropertiesSources) {
this.addPropertiesSources = addPropertiesSources;
return this;
}

public SmallRyeConfigBuilder setAddDefaultInterceptors(final boolean addDefaultInterceptors) {
this.addDefaultInterceptors = addDefaultInterceptors;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.IntFunction;
Expand All @@ -27,6 +33,7 @@
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.Converter;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.smallrye.config.common.AbstractConfigSource;
import io.smallrye.config.common.MapBackedConfigSource;
Expand Down Expand Up @@ -530,4 +537,33 @@ void emptyPropertyNames() {

assertEquals("value", config.getRawValue(""));
}

@Test
void systemSources() {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.addSystemSources()
.build();

assertTrue(config.getConfigSources(SysPropConfigSource.class).iterator().hasNext());
assertTrue(config.getConfigSources(EnvConfigSource.class).iterator().hasNext());
}

@Test
void propertiesSources(@TempDir Path tempDir) throws Exception {
File file = tempDir.resolve("application.properties").toFile();
Properties properties = new Properties();
properties.setProperty("my.prop", "1234");
try (FileOutputStream out = new FileOutputStream(file)) {
properties.store(out, null);
}

SmallRyeConfig config = new SmallRyeConfigBuilder()
.forClassLoader(new URLClassLoader(new URL[] { tempDir.toUri().toURL() }))
.addPropertiesSources()
.build();

assertFalse(config.getConfigSources(SysPropConfigSource.class).iterator().hasNext());
assertFalse(config.getConfigSources(EnvConfigSource.class).iterator().hasNext());
assertEquals("1234", config.getRawValue("my.prop"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.smallrye.config.source.yaml;

import static java.util.Collections.emptyList;

import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;

import io.smallrye.config.AbstractLocationConfigSourceLoader;

public class YamlConfigSourceLoader extends AbstractLocationConfigSourceLoader {
@Override
protected String[] getFileExtensions() {
return new String[] {
"yaml",
"yml"
};
}

@Override
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException {
return new YamlConfigSource(url, ordinal);
}

public static class InClassPath extends YamlConfigSourceLoader implements ConfigSourceProvider {
@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
List<ConfigSource> configSources = new ArrayList<>();
configSources.addAll(loadConfigSources("application.yaml", 255, classLoader));
configSources.addAll(loadConfigSources("application.yml", 255, classLoader));
configSources.addAll(loadConfigSources("META-INF/microprofile-config.yaml", 110, classLoader));
configSources.addAll(loadConfigSources("META-INF/microprofile-config.yml", 110, classLoader));
return configSources;
}

@Override
protected List<ConfigSource> tryFileSystem(final URI uri, final int ordinal) {
return emptyList();
}
}

public static class InFileSystem extends YamlConfigSourceLoader implements ConfigSourceProvider {
@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
List<ConfigSource> configSources = new ArrayList<>();
configSources.addAll(loadConfigSources(
Paths.get(System.getProperty("user.dir"), "config", "application.yaml").toUri().toString(), 265,
classLoader));
configSources.addAll(
loadConfigSources(Paths.get(System.getProperty("user.dir"), "config", "application.yml").toUri().toString(),
265, classLoader));
return configSources;
}

@Override
protected List<ConfigSource> tryClassPath(final URI uri, final int ordinal, final ClassLoader classLoader) {
return emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.smallrye.config.AbstractLocationConfigSourceLoader;

@Deprecated(forRemoval = true)
public class YamlConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider {
@Override
public String[] getFileExtensions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.smallrye.config.source.yaml.YamlConfigSourceProvider
io.smallrye.config.source.yaml.YamlConfigSourceLoader$InClassPath
io.smallrye.config.source.yaml.YamlConfigSourceLoader$InFileSystem
Loading

0 comments on commit 79de339

Please sign in to comment.