Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide MP config profile support for application.yaml (#5565) #5586

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;
import io.helidon.config.mp.spi.MpConfigFilter;
import io.helidon.config.mp.spi.MpConfigSourceProvider;
import io.helidon.config.mp.spi.MpMetaConfigProvider;

import org.eclipse.microprofile.config.Config;
Expand Down Expand Up @@ -382,6 +383,10 @@ private void addDiscoveredSources(List<OrdinalSource> targetConfigSources) {
ServiceLoader.load(ConfigSourceProvider.class)
.forEach(it -> it.getConfigSources(classLoader)
.forEach(source -> targetConfigSources.add(new OrdinalSource(source))));

ServiceLoader.load(MpConfigSourceProvider.class)
.forEach(it -> (profile == null ? it.getConfigSources(classLoader) : it.getConfigSources(classLoader, profile))
.forEach(source -> targetConfigSources.add(new OrdinalSource(source))));
}

private void addDiscoveredConverters(List<OrdinalConverter> targetConverters) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.config.mp.spi;

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

/**
* Java Service loader interface for MP ConfigSource Providers that adds configuration profile support.
*/
public interface MpConfigSourceProvider extends ConfigSourceProvider {
/**
* Returns a list of configuration sources.
*
* @param classLoader classloader where resource will be retrieved from
* @param profile configuration profile to use, must not be null
*
* @return list of config sources
*/
Iterable<ConfigSource> getConfigSources(ClassLoader classLoader, String profile);
}
1 change: 1 addition & 0 deletions config/config-mp/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
uses org.eclipse.microprofile.config.spi.ConfigSourceProvider;
uses org.eclipse.microprofile.config.spi.Converter;
uses io.helidon.config.mp.spi.MpConfigFilter;
uses io.helidon.config.mp.spi.MpConfigSourceProvider;
uses io.helidon.config.mp.spi.MpMetaConfigProvider;
uses io.helidon.config.spi.ConfigParser;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,30 +21,46 @@
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import io.helidon.config.ConfigException;
import io.helidon.config.mp.spi.MpConfigSourceProvider;

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

/**
* YAML config source provider for MicroProfile config that supports file {@code application.yaml}.
* This class should not be used directly - it is loaded automatically by Java service loader.
*/
public class YamlConfigSourceProvider implements ConfigSourceProvider {
public class YamlConfigSourceProvider implements MpConfigSourceProvider {
private static final String RESOURCE_NAME = "application.yaml";

@Override
public Iterable<ConfigSource> getConfigSources(ClassLoader classLoader) {
Enumeration<URL> resources;
try {
resources = classLoader.getResources("application.yaml");
} catch (IOException e) {
throw new ConfigException("Failed to read resources from classpath", e);
}
return configSources(classLoader, null);
}

@Override
public Iterable<ConfigSource> getConfigSources(ClassLoader classLoader, String profile) {
Objects.requireNonNull(profile, "Profile must not be null");
return configSources(classLoader, profile);
}

private Iterable<ConfigSource> configSources(ClassLoader classLoader, String profile) {
List<ConfigSource> result = new LinkedList<>();
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
result.add(YamlMpConfigSource.create(url));
if (profile == null) {
Enumeration<URL> resources;
try {
resources = classLoader.getResources(RESOURCE_NAME);
} catch (IOException e) {
throw new ConfigException("Failed to read resources from classpath", e);
}
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
result.add(YamlMpConfigSource.create(url));
}
} else {
result.addAll(YamlMpConfigSource.classPath(RESOURCE_NAME, profile, classLoader));
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,31 @@ public static List<ConfigSource> classPath(String resource) {
* Create from YAML file(s) on classpath with profile support.
*
* @param resource resource name to locate on classpath (looks for all instances)
* @param profile name of the profile to use
* @param profile configuration profile to use, must not be null
* @return list of config sources discovered (may be zero length)
*/
public static List<ConfigSource> classPath(String resource, String profile) {
return classPathConfigSources(resource, profile, Thread.currentThread().getContextClassLoader());
}

/**
* Create from YAML file(s) on classpath from specified classloader with profile support.
*
* @param resource resource name to locate on classpath (looks for all instances)
* @param profile configuration profile to use, must not be null
* @param classLoader classloader where resource will be retrieved from
* @return list of config sources discovered (may be zero length)
*/
public static List<ConfigSource> classPath(String resource, String profile, ClassLoader classLoader) {
Objects.requireNonNull(profile, "Profile must be defined");
Objects.requireNonNull(classLoader, "ClassLoader must be defined");

return classPathConfigSources(resource, profile, classLoader);
}

private static List<ConfigSource> classPathConfigSources(String resource, String profile, ClassLoader classLoader) {

List<ConfigSource> sources = new LinkedList<>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

try {
Enumeration<URL> baseResources = classLoader.getResources(resource);
Expand Down
2 changes: 1 addition & 1 deletion config/yaml-mp/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@

exports io.helidon.config.yaml.mp;

provides org.eclipse.microprofile.config.spi.ConfigSourceProvider with io.helidon.config.yaml.mp.YamlConfigSourceProvider;
provides io.helidon.config.mp.spi.MpConfigSourceProvider with io.helidon.config.yaml.mp.YamlConfigSourceProvider;
provides io.helidon.config.mp.spi.MpMetaConfigProvider with io.helidon.config.yaml.mp.YamlMetaConfigProvider;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.config.yaml.mp;

import java.util.Map;
import java.util.NoSuchElementException;

import io.helidon.config.mp.MpConfigSources;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class YamlMpConfigSourceProviderTest {
private static ConfigProviderResolver configResolver;
private Config config;
private static final String MP_CONFIG_PROFILE = "mp.config.profile";
public static final String PROFILE_TYPE_CONFIG_KEY = "profile.type";
public static final String PROFILE_VALUE_CONFIG_KEY = "profile.value";
public static final String DEFAULT_PROFILE = "default";
public static final String DEV_PROFILE = "dev";
public static final String TEST_PROFILE = "test";
public static final String DEFAULT_PROFILE_VALUE = "Production";
public static final Map<String, String> profileConfigValues = Map.of(
DEFAULT_PROFILE, DEFAULT_PROFILE_VALUE,
DEV_PROFILE, "Development",
TEST_PROFILE, "Test"
);

@BeforeAll
static void init() {
configResolver = ConfigProviderResolver.instance();
}

@AfterAll
static void reset() {
System.clearProperty(MP_CONFIG_PROFILE);
}

@BeforeEach
void resetConfig() {
if (config == null) {
System.clearProperty(MP_CONFIG_PROFILE);
configResolver.releaseConfig(ConfigProvider.getConfig());
} else {
configResolver.releaseConfig(config);
config = null;
}
}

@Test
void testNoProfileFromSystemPropertyWithOverride() {
// if using DEFAULT_PROFILE, mp.config.profile system property will not be set
validateUsingSystemProperty(DEFAULT_PROFILE, PROFILE_TYPE_CONFIG_KEY, profileConfigValues.get(DEFAULT_PROFILE));
}

@Test
void testDevProfileFromSystemPropertyWithOverride() {
validateUsingSystemProperty(DEV_PROFILE, PROFILE_TYPE_CONFIG_KEY, profileConfigValues.get(DEV_PROFILE));
}

@Test
void testTestProfileFromSystemPropertyWithOverride() {
validateUsingSystemProperty(TEST_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testNoProfileFromSystemPropertyWithNoOverride() {
// if using DEFAULT_PROFILE, mp.config.profile will not be added in the config source
validateUsingSystemProperty(DEFAULT_PROFILE, PROFILE_TYPE_CONFIG_KEY, profileConfigValues.get(DEFAULT_PROFILE));
}

@Test
void testDevProfileFromSystemPropertyWithNoOverride() {
validateUsingSystemProperty(TEST_PROFILE, PROFILE_TYPE_CONFIG_KEY, profileConfigValues.get(TEST_PROFILE));
}

@Test
void testTestProfileFromSystemPropertyWithNoOverride() {
validateUsingSystemProperty(DEV_PROFILE, PROFILE_TYPE_CONFIG_KEY, profileConfigValues.get(DEV_PROFILE));
}

@Test
void testNoProfileFromFromConfigSourceWithOverride() {
// if using DEFAULT_PROFILE, mp.config.profile will not be added in the config source
validateUsingConfigSource(DEFAULT_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testDevProfileFromFromConfigSourceWithOverride() {
validateUsingConfigSource(DEV_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testTestProfileFromFromConfigSourceWithOverride() {
validateUsingConfigSource(TEST_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testNoProfileFromFromConfigSourceWithNoOverride() {
// if using DEFAULT_PROFILE, mp.config.profile will not be added in the config source
validateUsingConfigSource(DEFAULT_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testDevProfileFromFromConfigSourceWithNoOverride() {
validateUsingConfigSource(DEV_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

@Test
void testTestProfileFromFromConfigSourceWithNoOverride() {
validateUsingConfigSource(TEST_PROFILE, PROFILE_VALUE_CONFIG_KEY, DEFAULT_PROFILE_VALUE);
}

private void validateUsingSystemProperty(String profile, String profileConfigKey, String profileConfigValue) {
// Don't set mp.config.profile system property if using DEFAULT_PROFILE
if (!profile.equals(DEFAULT_PROFILE)) {
System.setProperty(MP_CONFIG_PROFILE, profile);
}
validateConfigValues(profile, profileConfigKey, profileConfigValue);
}

private void validateUsingConfigSource(String profile, String profileConfigKey, String profileConfigValue){
// Don't set mp.config.profile in config source if using DEFAULT_PROFILE
Map<String, String> configMap = profile == DEFAULT_PROFILE ? Map.of() : Map.of(MP_CONFIG_PROFILE, profile);
Config mpConfigProfile = configResolver.getBuilder()
.addDiscoveredSources()
.withSources(MpConfigSources.create(configMap))
.build();
configResolver.registerConfig(mpConfigProfile, Thread.currentThread().getContextClassLoader());
validateConfigValues(profile, profileConfigKey, profileConfigValue);
}

private void validateConfigValues(String profile, String profileConfigKey, String profileConfigValue) {
config = ConfigProvider.getConfig();
// If using DEFAULT_PROFILE, mp.config.profile should not exist in the Config
if (profile == DEFAULT_PROFILE) {
assertThrows(NoSuchElementException.class, () -> {
config.getValue(MP_CONFIG_PROFILE, String.class);
});
} else {
assertThat(config.getValue(MP_CONFIG_PROFILE, String.class), is(profile));
}
assertThat(config.getValue(profileConfigKey, String.class), is(profileConfigValue));
}
}
18 changes: 18 additions & 0 deletions config/yaml-mp/src/test/resources/application-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2022 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

profile:
type: "Development"
18 changes: 18 additions & 0 deletions config/yaml-mp/src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Copyright (c) 2022 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

profile:
type: "Test"
6 changes: 5 additions & 1 deletion config/yaml-mp/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
#

---
yaml:
string: String
number: 10
Expand All @@ -23,3 +22,8 @@ yaml:
- "Array 2"
- "Array 3"
boolean: true

# Will be used for config profile testing
profile:
type: "Production"
value: "Production"