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

Meta-Configuration support for Helidon MP. #2767

Merged
merged 4 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion config/config-mp/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020 Oracle and/or its affiliates.
~ Copyright (c) 2020, 2021 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 Down Expand Up @@ -50,6 +50,10 @@
<groupId>org.eclipse.microprofile.config</groupId>
<artifactId>microprofile-config-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
* Copyright (c) 2020, 2021 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 @@ -24,6 +24,7 @@
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
Expand All @@ -50,19 +51,25 @@
import java.util.SimpleTimeZone;
import java.util.TimeZone;
import java.util.UUID;
import java.util.function.Function;
import java.util.regex.Pattern;

import io.helidon.common.serviceloader.HelidonServiceLoader;
import io.helidon.common.serviceloader.Priorities;
import io.helidon.config.ConfigException;
import io.helidon.config.ConfigMappers;
import io.helidon.config.ConfigValue;
import io.helidon.config.mp.spi.MpConfigFilter;
import io.helidon.config.yaml.YamlMpConfigSource;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.eclipse.microprofile.config.spi.ConfigSourceProvider;
import org.eclipse.microprofile.config.spi.Converter;

import static io.helidon.config.mp.MpMetaConfig.MetaConfigSource;

/**
* Configuration builder.
*/
Expand Down Expand Up @@ -125,6 +132,142 @@ public ConfigBuilder withConverters(Converter<?>... converters) {
return this;
}

void mpMetaConfig(io.helidon.config.Config meta) {
meta.get("add-discovered-sources").asBoolean().ifPresent(it -> {
if (it) {
addDiscoveredSources();
}
});
meta.get("add-discovered-converters").asBoolean().ifPresent(it -> {
if (it) {
addDiscoveredConverters();
}
});
meta.get("add-default-sources").asBoolean().ifPresent(it -> {
if (it) {
addDefaultSources();
}
});
tomas-langer marked this conversation as resolved.
Show resolved Hide resolved

meta.get("sources")
.asNodeList()
.ifPresent(this::processMetaSources);
}

private void processMetaSources(List<io.helidon.config.Config> configs) {
for (io.helidon.config.Config config : configs) {
String type = config.get("type").asString()
.orElseThrow(() -> new ConfigException("Meta configuration sources must have a \"type\" property defined"));
// in MP, we have a hardcoded list of supported configuration source types
List<ConfigSource> delegates;
switch (type) {
case "system-properties":
delegates = List.of(MpConfigSources.systemProperties());
break;
case "environment-variables":
delegates = List.of(MpConfigSources.environmentVariables());
break;
case "properties":
delegates = propertiesSource(config);
break;
case "yaml":
delegates = yamlSource(config);
break;
default:
throw new ConfigException("Meta configuration source type \"" + type + "\" is not supported. Use on of: "
+ "system-properties, environment-variables, properties, yaml");
}
boolean shouldCount = delegates.size() > 1;
int counter = 0;

for (ConfigSource delegate : delegates) {
MetaConfigSource.Builder builder = MetaConfigSource.builder()
.delegate(delegate);

config.get("ordinal").asInt().ifPresent(builder::ordinal);
ConfigValue<String> name = config.get("name").asString();
if (shouldCount) {
if (name.isPresent()) {
// multiple instances - count them
builder.name(name.get() + "_" + counter++);
}
} else {
name.ifPresent(builder::name);
}

withSources(builder.build());
}
}
}

private List<ConfigSource> propertiesSource(io.helidon.config.Config config) {
return sourceFromMeta(config,
MpConfigSources::create,
MpConfigSources::classPath,
MpConfigSources::create);
}

private List<ConfigSource> yamlSource(io.helidon.config.Config config) {
return sourceFromMeta(config,
YamlMpConfigSource::create,
YamlMpConfigSource::classPath,
YamlMpConfigSource::create);
}

private List<ConfigSource> sourceFromMeta(io.helidon.config.Config config,
Function<Path, ConfigSource> fromPath,
Function<String, List<ConfigSource>> fromClasspath,
Function<URL, ConfigSource> fromUrl) {

boolean optional = config.get("optional").asBoolean().orElse(false);

String location;
Exception cause = null;

ConfigValue<Path> pathConfig = config.get("path").as(Path.class);
if (pathConfig.isPresent()) {
Path path = pathConfig.get();
if (Files.exists(path) && Files.isRegularFile(path)) {
return List.of(fromPath.apply(path));
}
location = "path " + path.toAbsolutePath();
} else {
ConfigValue<String> classpathConfig = config.get("classpath").as(String.class);
if (classpathConfig.isPresent()) {
String classpath = classpathConfig.get();
List<ConfigSource> sources = fromClasspath.apply(classpath);
if (!sources.isEmpty()) {
return sources;
}
location = "classpath " + classpath;
} else {
ConfigValue<URL> urlConfig = config.get("url").as(URL.class);
if (urlConfig.isPresent()) {
URL url = urlConfig.get();
try {
return List.of(fromUrl.apply(url));
} catch (ConfigException e) {
location = "url " + url;
cause = e;
}
} else {
throw new ConfigException("MP meta configuration does not contain config source location. Node: " + config
.key());
}
}
}

if (optional) {
return List.of();
}
String message = "Meta configuration could not find non-optional config source on " + location;
if (cause == null) {
throw new ConfigException(message);
} else {
throw new ConfigException(message, cause);
}
}

@Override
public Config build() {
if (useDefaultSources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.stream.Stream;

import io.helidon.common.GenericType;
Expand All @@ -44,6 +45,7 @@
* This class is an implementation of a java service obtained through ServiceLoader.
*/
public class MpConfigProviderResolver extends ConfigProviderResolver {
private static final Logger LOGGER = Logger.getLogger(MpConfigProviderResolver.class.getName());
private static final Map<ClassLoader, ConfigDelegate> CONFIGS = new IdentityHashMap<>();
private static final ReadWriteLock RW_LOCK = new ReentrantReadWriteLock();
// specific for native image - we want to replace config provided during build with runtime configuration
Expand Down Expand Up @@ -83,20 +85,31 @@ public Config getConfig(ClassLoader classLoader) {

private Config buildConfig(ClassLoader loader) {
MpConfigBuilder builder = getBuilder();
builder.forClassLoader(loader);

Optional<io.helidon.config.Config> meta = MetaConfig.metaConfig();
// MP Meta Configuration
Optional<io.helidon.config.Config> meta = MpMetaConfig.metaConfig();

meta.ifPresent(builder::metaConfig);
if (meta.isEmpty()){
meta = MetaConfig.metaConfig();

builder.forClassLoader(loader);
if (meta.isPresent()) {
builder.metaConfig(meta.get());
LOGGER.warning("You are using Helidon SE meta configuration in a Helidon MP application. Some features "
+ "work differently, such as environment variable resolving, and mutability");
}
} else {
builder.mpMetaConfig(meta.get());
}

if (meta.isEmpty()) {
// no meta configuration, use defaults
builder.addDefaultSources();
builder.addDiscoveredSources();
builder.addDiscoveredConverters();
}

return builder.addDiscoveredSources()
.addDiscoveredConverters()
.build();
return builder.build();
}

@Override
Expand Down
Loading