Skip to content

Commit

Permalink
Merge pull request #20385 from radcortez/fix-19549
Browse files Browse the repository at this point in the history
Add build steps to access the config builder at runtime
  • Loading branch information
radcortez authored Nov 11, 2021
2 parents 5483799 + 4ecbdc7 commit 4c45e79
Show file tree
Hide file tree
Showing 16 changed files with 309 additions and 18 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<smallrye-reactive-types-converter.version>2.6.0</smallrye-reactive-types-converter.version>
<smallrye-mutiny-vertx-binding.version>2.15.1</smallrye-mutiny-vertx-binding.version>
<smallrye-reactive-messaging.version>3.11.0</smallrye-reactive-messaging.version>
<smallrye-stork.version>1.0.0.Beta1</smallrye-stork.version>
<smallrye-stork.version>1.0.0.Beta1</smallrye-stork.version>
<jakarta.activation.version>1.2.1</jakarta.activation.version>
<jakarta.annotation-api.version>1.3.5</jakarta.annotation-api.version>
<jakarta.el-impl.version>3.0.4</jakarta.el-impl.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.MultiBuildItem;

public final class RunTimeConfigBuilderBuildItem extends MultiBuildItem {
private String builderClassName;

public RunTimeConfigBuilderBuildItem(final String builderClassName) {
this.builderClassName = builderClassName;
}

public String getBuilderClassName() {
return builderClassName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.MultiBuildItem;

public final class StaticInitConfigBuilderBuildItem extends MultiBuildItem {
private String builderClassName;

public StaticInitConfigBuilderBuildItem(final String builderClassName) {
this.builderClassName = builderClassName;
}

public String getBuilderClassName() {
return builderClassName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public final class RunTimeConfigurationGenerator {
ConfigUtils.class,
"configBuilder",
SmallRyeConfigBuilder.class, boolean.class, boolean.class, boolean.class, LaunchMode.class);
static final MethodDescriptor CU_CONFIG_BUILDER_LIST = MethodDescriptor.ofMethod(ConfigUtils.class, "configBuilder",
SmallRyeConfigBuilder.class, SmallRyeConfigBuilder.class, List.class);
static final MethodDescriptor CU_ADD_SOURCE_PROVIDER = MethodDescriptor.ofMethod(ConfigUtils.class, "addSourceProvider",
void.class, SmallRyeConfigBuilder.class, ConfigSourceProvider.class);
static final MethodDescriptor CU_ADD_SOURCE_PROVIDERS = MethodDescriptor.ofMethod(ConfigUtils.class, "addSourceProviders",
Expand Down Expand Up @@ -304,11 +306,13 @@ public static final class GenerateOperation implements AutoCloseable {
final Set<String> staticConfigSources;
final Set<String> staticConfigSourceProviders;
final Set<String> staticConfigSourceFactories;
final Set<String> staticConfigBuilders;
final Set<String> runtimeConfigSources;
final Set<String> runtimeConfigSourceProviders;
final Set<String> runtimeConfigSourceFactories;
final Set<ConfigClassWithPrefix> staticConfigMappings;
final Set<ConfigClassWithPrefix> runtimeConfigMappings;
final Set<String> runtimeConfigBuilders;
/**
* Regular converters organized by type. Each converter is stored in a separate field. Some are used
* only at build time, some only at run time, and some at both times.
Expand Down Expand Up @@ -342,11 +346,13 @@ public static final class GenerateOperation implements AutoCloseable {
staticConfigSources = builder.getStaticConfigSources();
staticConfigSourceProviders = builder.getStaticConfigSourceProviders();
staticConfigSourceFactories = builder.getStaticConfigSourceFactories();
staticConfigBuilders = builder.getStaticConfigBuilders();
runtimeConfigSources = builder.getRuntimeConfigSources();
runtimeConfigSourceProviders = builder.getRuntimeConfigSourceProviders();
runtimeConfigSourceFactories = builder.getRuntimeConfigSourceFactories();
staticConfigMappings = builder.getStaticConfigMappings();
runtimeConfigMappings = builder.getRuntimeConfigMappings();
runtimeConfigBuilders = builder.getRuntimeConfigBuilders();
cc = ClassCreator.builder().classOutput(classOutput).className(CONFIG_CLASS_NAME).setFinal(true).build();
generateEmptyParsers(cc);
// not instantiable
Expand Down Expand Up @@ -440,6 +446,14 @@ public static final class GenerateOperation implements AutoCloseable {
clinit.load(configMapping.getKlass().getName()), clinit.load(configMapping.getPrefix()));
}

// additional config builders
ResultHandle staticConfigBuilderInstances = clinit.newInstance(AL_NEW);
for (String configBuilder : staticConfigBuilders) {
ResultHandle staticConfigBuilderInstance = clinit.newInstance(MethodDescriptor.ofConstructor(configBuilder));
clinit.invokeVirtualMethod(AL_ADD, staticConfigBuilderInstances, staticConfigBuilderInstance);
}
clinit.invokeStaticMethod(CU_CONFIG_BUILDER_LIST, buildTimeBuilder, staticConfigBuilderInstances);

clinitConfig = clinit.checkCast(clinit.invokeVirtualMethod(SRCB_BUILD, buildTimeBuilder),
SmallRyeConfig.class);

Expand Down Expand Up @@ -700,6 +714,15 @@ public void run() {
readConfig.load(configMapping.getKlass().getName()), readConfig.load(configMapping.getPrefix()));
}

// additional config builders
ResultHandle runtimeConfigBuilderInstances = readConfig.newInstance(AL_NEW);
for (String configBuilder : runtimeConfigBuilders) {
ResultHandle runtimeConfigBuilderInstance = readConfig
.newInstance(MethodDescriptor.ofConstructor(configBuilder));
readConfig.invokeVirtualMethod(AL_ADD, runtimeConfigBuilderInstances, runtimeConfigBuilderInstance);
}
readConfig.invokeStaticMethod(CU_CONFIG_BUILDER_LIST, runTimeBuilder, runtimeConfigBuilderInstances);

ResultHandle bootstrapConfig = null;
if (bootstrapConfigSetupNeeded()) {
bootstrapConfig = readBootstrapConfig.invokeVirtualMethod(SRCB_BUILD, bootstrapBuilder);
Expand Down Expand Up @@ -1712,9 +1735,11 @@ public static final class Builder {
private Set<String> staticConfigSources;
private Set<String> staticConfigSourceProviders;
private Set<String> staticConfigSourceFactories;
private Set<String> staticConfigBuilders;
private Set<String> runtimeConfigSources;
private Set<String> runtimeConfigSourceProviders;
private Set<String> runtimeConfigSourceFactories;
private Set<String> runtimeConfigBuilders;

private Set<ConfigClassWithPrefix> staticConfigMappings;
private Set<ConfigClassWithPrefix> runtimeConfigMappings;
Expand Down Expand Up @@ -1799,6 +1824,15 @@ public Builder setStaticConfigSourceFactories(final Set<String> staticConfigSour
return this;
}

Set<String> getStaticConfigBuilders() {
return staticConfigBuilders;
}

public Builder setStaticConfigBuilders(final Set<String> staticConfigBuilders) {
this.staticConfigBuilders = staticConfigBuilders;
return this;
}

Set<String> getRuntimeConfigSources() {
return runtimeConfigSources;
}
Expand Down Expand Up @@ -1844,6 +1878,15 @@ public Builder setRuntimeConfigMappings(final Set<ConfigClassWithPrefix> runtime
return this;
}

Set<String> getRuntimeConfigBuilders() {
return runtimeConfigBuilders;
}

public Builder setRuntimeConfigBuilders(final Set<String> runtimeConfigBuilders) {
this.runtimeConfigBuilders = runtimeConfigBuilders;
return this;
}

public GenerateOperation build() {
return new GenerateOperation(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import io.quarkus.deployment.builditem.HotDeploymentWatchedFileBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LiveReloadBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigSourceFactoryBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigSourceProviderBuildItem;
import io.quarkus.deployment.builditem.SuppressNonRuntimeConfigChangedWarningBuildItem;
Expand Down Expand Up @@ -120,7 +122,9 @@ void generateConfigClass(
List<AdditionalBootstrapConfigSourceProviderBuildItem> additionalBootstrapConfigSourceProviders,
List<StaticInitConfigSourceProviderBuildItem> staticInitConfigSourceProviders,
List<StaticInitConfigSourceFactoryBuildItem> staticInitConfigSourceFactories,
List<ConfigMappingBuildItem> configMappings)
List<ConfigMappingBuildItem> configMappings,
List<StaticInitConfigBuilderBuildItem> staticInitConfigBuilders,
List<RunTimeConfigBuilderBuildItem> runTimeConfigBuilders)
throws IOException {

if (liveReloadBuildItem.isLiveReload()) {
Expand All @@ -134,7 +138,7 @@ void generateConfigClass(
Set<String> staticConfigSourceProviders = new HashSet<>();
staticConfigSourceProviders.addAll(staticSafeServices(discoveredConfigSourceProviders));
staticConfigSourceProviders.addAll(staticInitConfigSourceProviders.stream()
.map(StaticInitConfigSourceProviderBuildItem::getProviderClassName).collect(Collectors.toSet()));
.map(StaticInitConfigSourceProviderBuildItem::getProviderClassName).collect(toSet()));
Set<String> staticConfigSourceFactories = new HashSet<>();
staticConfigSourceFactories.addAll(staticSafeServices(discoveredConfigSourceFactories));
staticConfigSourceFactories.addAll(staticInitConfigSourceFactories.stream()
Expand All @@ -153,11 +157,15 @@ void generateConfigClass(
.setStaticConfigSources(staticSafeServices(discoveredConfigSources))
.setStaticConfigSourceProviders(staticConfigSourceProviders)
.setStaticConfigSourceFactories(staticConfigSourceFactories)
.setStaticConfigMappings(staticSafeConfigMappings(configMappings))
.setStaticConfigBuilders(staticInitConfigBuilders.stream()
.map(StaticInitConfigBuilderBuildItem::getBuilderClassName).collect(toSet()))
.setRuntimeConfigSources(discoveredConfigSources)
.setRuntimeConfigSourceProviders(discoveredConfigSourceProviders)
.setRuntimeConfigSourceFactories(discoveredConfigSourceFactories)
.setStaticConfigMappings(staticSafeConfigMappings(configMappings))
.setRuntimeConfigMappings(runtimeConfigMappings(configMappings))
.setRuntimeConfigBuilders(
runTimeConfigBuilders.stream().map(RunTimeConfigBuilderBuildItem::getBuilderClassName).collect(toSet()))
.build()
.run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.runtime.configuration;

import io.smallrye.config.SmallRyeConfigBuilder;

public interface ConfigBuilder {
SmallRyeConfigBuilder configBuilder(SmallRyeConfigBuilder builder);

default int priority() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.quarkus.runtime.configuration;

import static io.smallrye.config.DotEnvConfigSourceProvider.dotEnvSources;
import static io.smallrye.config.PropertiesConfigSourceProvider.classPathSources;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_LOCATIONS;
import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE;
Expand All @@ -13,6 +12,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -70,7 +70,7 @@ public static <T> IntFunction<SortedSet<T>> sortedSetFactory() {
return size -> new TreeSet<>();
}

public static SmallRyeConfigBuilder configBuilder(final boolean runTime, LaunchMode launchMode) {
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final LaunchMode launchMode) {
return configBuilder(runTime, true, launchMode);
}

Expand All @@ -82,7 +82,7 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, LaunchM
* @return the configuration builder
*/
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final boolean addDiscovered,
LaunchMode launchMode) {
final LaunchMode launchMode) {
return configBuilder(runTime, false, addDiscovered, launchMode);
}

Expand All @@ -94,22 +94,22 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b
* @return the configuration builder
*/
public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final boolean bootstrap,
final boolean addDiscovered,
LaunchMode launchMode) {
final SmallRyeConfigBuilder builder = emptyConfigBuilder();
final boolean addDiscovered, final LaunchMode launchMode) {
SmallRyeConfigBuilder builder = emptyConfigBuilder();

final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InFileSystem().getConfigSources(classLoader));
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InClassPath().getConfigSources(classLoader));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
builder.forClassLoader(classLoader);
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InFileSystem());
builder.withSources(new ApplicationPropertiesConfigSourceLoader.InClassPath());
if (launchMode.isDevOrTest() && (runTime || bootstrap)) {
builder.withSources(new RuntimeOverrideConfigSource(classLoader));
}
if (runTime) {
builder.addDefaultSources();
builder.withDefaultValue(UUID_KEY, UUID.randomUUID().toString());
builder.withSources(dotEnvSources(classLoader));
builder.withSources(new DotEnvConfigSourceProvider());
} else {
final List<ConfigSource> sources = new ArrayList<>();
List<ConfigSource> sources = new ArrayList<>();
sources.addAll(classPathSources(META_INF_MICROPROFILE_CONFIG_PROPERTIES, classLoader));
sources.addAll(new BuildTimeDotEnvConfigSourceProvider().getConfigSources(classLoader));
sources.add(new BuildTimeEnvConfigSource());
Expand All @@ -127,10 +127,10 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b
}

public static SmallRyeConfigBuilder emptyConfigBuilder() {
final SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withDefaultValue(SMALLRYE_CONFIG_PROFILE, ProfileManager.getActiveProfile());

final Map<String, String> relocations = new HashMap<>();
Map<String, String> relocations = new HashMap<>();
relocations.put(SMALLRYE_CONFIG_LOCATIONS, "quarkus.config.locations");
relocations.put(SMALLRYE_CONFIG_PROFILE_PARENT, "quarkus.config.profile.parent");
// Override the priority, because of the ProfileConfigSourceInterceptor and profile.parent.
Expand All @@ -146,7 +146,7 @@ public OptionalInt getPriority() {
}
});

final Map<String, String> fallbacks = new HashMap<>();
Map<String, String> fallbacks = new HashMap<>();
fallbacks.put("quarkus.config.locations", SMALLRYE_CONFIG_LOCATIONS);
fallbacks.put("quarkus.config.profile.parent", SMALLRYE_CONFIG_PROFILE_PARENT);
builder.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
Expand All @@ -168,6 +168,20 @@ public OptionalInt getPriority() {
return builder;
}

@SuppressWarnings("unchecked")
public static SmallRyeConfigBuilder configBuilder(SmallRyeConfigBuilder builder, List<ConfigBuilder> configBuilders) {
configBuilders.sort(Comparator.comparing(ConfigBuilder::priority));

for (ConfigBuilder configBuilder : configBuilders) {
builder = configBuilder.configBuilder(builder);
if (builder == null) {
throw new ConfigurationException(configBuilder.getClass().getName() + " returned a null builder");
}
}

return builder;
}

/**
* Add a configuration source provider to the builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LogHandlerBuildItem;
import io.quarkus.deployment.builditem.ObjectSubstitutionBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.StaticInitConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedPackageBuildItem;
Expand All @@ -51,6 +53,8 @@
import io.quarkus.extest.runtime.beans.PublicKeyProducer;
import io.quarkus.extest.runtime.config.ObjectOfValue;
import io.quarkus.extest.runtime.config.ObjectValueOf;
import io.quarkus.extest.runtime.config.RunTimeConfigBuilder;
import io.quarkus.extest.runtime.config.StaticInitConfigBuilder;
import io.quarkus.extest.runtime.config.TestBuildAndRunTimeConfig;
import io.quarkus.extest.runtime.config.TestBuildTimeConfig;
import io.quarkus.extest.runtime.config.TestConfigRoot;
Expand Down Expand Up @@ -408,6 +412,16 @@ void deprecatedStaticInitBuildItem(
AdditionalStaticInitConfigSourceProvider.class.getName()));
}

@BuildStep
void staticInitConfigBuilder(BuildProducer<StaticInitConfigBuilderBuildItem> configBuilders) {
configBuilders.produce(new StaticInitConfigBuilderBuildItem(StaticInitConfigBuilder.class.getName()));
}

@BuildStep
void runTimeConfigBuilder(BuildProducer<RunTimeConfigBuilderBuildItem> configBuilders) {
configBuilders.produce(new RunTimeConfigBuilderBuildItem(RunTimeConfigBuilder.class.getName()));
}

@BuildStep(onlyIf = Never.class)
void neverRunThisOne() {
throw new IllegalStateException("Not supposed to run!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.config;

import static org.junit.jupiter.api.Assertions.assertEquals;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.extest.runtime.config.StaticInitConfigBuilder;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.config.SmallRyeConfig;

public class ConfigBuilderTest {
@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class));

@Inject
SmallRyeConfig config;

@Test
void staticConfigBuilder() {
assertEquals(1, StaticInitConfigBuilder.counter.get());
}

@Test
void runTimeConfigBuilder() {
assertEquals("1234", config.getRawValue("additional.builder.property"));
}
}
Loading

0 comments on commit 4c45e79

Please sign in to comment.