-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
implementation/src/main/java/io/smallrye/config/DotEnvConfigSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package io.smallrye.config; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.eclipse.microprofile.config.spi.ConfigSourceProvider; | ||
|
||
import io.smallrye.config.common.utils.ConfigSourceUtil; | ||
|
||
public class DotEnvConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider { | ||
private final String location; | ||
|
||
public DotEnvConfigSourceProvider() { | ||
this(".env"); | ||
} | ||
|
||
public DotEnvConfigSourceProvider(final String location) { | ||
this.location = location; | ||
} | ||
|
||
@Override | ||
protected String[] getFileExtensions() { | ||
return new String[] { "" }; | ||
} | ||
|
||
@Override | ||
protected ConfigSource loadConfigSource(final URL url) throws IOException { | ||
return loadConfigSource(url, 295); | ||
} | ||
|
||
@Override | ||
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException { | ||
return new EnvConfigSource(ConfigSourceUtil.urlToMap(url), ordinal) { | ||
@Override | ||
public String getName() { | ||
return super.getName() + "[source=" + url + "]"; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public List<ConfigSource> getConfigSources(final ClassLoader forClassLoader) { | ||
return loadConfigSources(location, forClassLoader); | ||
} | ||
|
||
public static List<ConfigSource> dotEnvSources(final ClassLoader classLoader) { | ||
return new DotEnvConfigSourceProvider().getConfigSources(classLoader); | ||
} | ||
|
||
public static List<ConfigSource> dotEnvSources(final String location, final ClassLoader classLoader) { | ||
return new DotEnvConfigSourceProvider(location).getConfigSources(classLoader); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
implementation/src/test/java/io/smallrye/config/DotEnvConfigSourceProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.smallrye.config; | ||
|
||
import static io.smallrye.config.DotEnvConfigSourceProvider.dotEnvSources; | ||
import static io.smallrye.config.SecuritySupport.getContextClassLoader; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.FileOutputStream; | ||
import java.nio.file.Path; | ||
import java.util.Properties; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
class DotEnvConfigSourceProviderTest { | ||
@Test | ||
void dotEnvSource(@TempDir Path tempDir) throws Exception { | ||
Properties envProperties = new Properties(); | ||
envProperties.setProperty("MY_PROP", "1234"); | ||
try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env").toFile())) { | ||
envProperties.store(out, null); | ||
} | ||
|
||
SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDiscoveredSources() | ||
.addDefaultInterceptors() | ||
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader())) | ||
.build(); | ||
|
||
assertEquals("1234", config.getRawValue("my.prop")); | ||
assertEquals("1234", config.getRawValue("MY_PROP")); | ||
} | ||
|
||
@Test | ||
void dotEnvSourceProfiles(@TempDir Path tempDir) throws Exception { | ||
Properties mainProperties = new Properties(); | ||
mainProperties.setProperty("MY_PROP_MAIN", "main"); | ||
mainProperties.setProperty("MY_PROP_COMMON", "main"); | ||
mainProperties.setProperty("MY_PROP_PROFILE", "main"); | ||
try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env").toFile())) { | ||
mainProperties.store(out, null); | ||
} | ||
|
||
Properties commonProperties = new Properties(); | ||
commonProperties.setProperty("MY_PROP_COMMON", "common"); | ||
commonProperties.setProperty("MY_PROP_PROFILE", "common"); | ||
try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env-common").toFile())) { | ||
commonProperties.store(out, null); | ||
} | ||
|
||
Properties devProperties = new Properties(); | ||
devProperties.setProperty("MY_PROP_DEV", "dev"); | ||
devProperties.setProperty("MY_PROP_PROFILE", "dev"); | ||
try (FileOutputStream out = new FileOutputStream(tempDir.resolve(".env-dev").toFile())) { | ||
devProperties.store(out, null); | ||
} | ||
|
||
SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDiscoveredSources() | ||
.addDefaultInterceptors() | ||
.withProfile("common,dev") | ||
.withSources(dotEnvSources(tempDir.resolve(".env").toFile().toURI().toString(), getContextClassLoader())) | ||
.build(); | ||
|
||
assertEquals("main", config.getRawValue("my.prop.main")); | ||
assertEquals("main", config.getRawValue("MY_PROP_MAIN")); | ||
assertEquals("common", config.getRawValue("my.prop.common")); | ||
assertEquals("common", config.getRawValue("MY_PROP_COMMON")); | ||
assertEquals("dev", config.getRawValue("my.prop.profile")); | ||
assertEquals("dev", config.getRawValue("MY_PROP_PROFILE")); | ||
} | ||
} |