diff --git a/bom/application/pom.xml b/bom/application/pom.xml index 75f88b0b2c8cc..104c83569ada3 100644 --- a/bom/application/pom.xml +++ b/bom/application/pom.xml @@ -56,7 +56,7 @@ 2.0 3.1.1 2.1.0 - 3.3.3 + 3.3.4-SNAPSHOT 4.0.2 4.0.0 3.5.0 diff --git a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java index b51b14c0481d2..d4076b42abab7 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/ExtensionLoader.java @@ -47,7 +47,6 @@ import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; -import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.jboss.logging.Logger; import org.wildfly.common.function.Functions; @@ -136,16 +135,8 @@ public static Consumer loadStepsFrom(ClassLoader classLoader, final BuildTimeConfigurationReader reader = new BuildTimeConfigurationReader(classLoader); final SmallRyeConfig src = reader.initConfiguration(launchMode, buildSystemProps, appModel.getPlatformProperties()); - // install globally QuarkusConfigFactory.setConfig(src); - final ConfigProviderResolver cpr = ConfigProviderResolver.instance(); - try { - cpr.releaseConfig(cpr.getConfig()); - } catch (IllegalStateException ignored) { - // just means no config was installed, which is fine - } - final BuildTimeConfigurationReader.ReadResult readResult = reader.readConfiguration(src); final BooleanSupplierFactoryBuildItem bsf = new BooleanSupplierFactoryBuildItem(readResult, launchMode, devModeType); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/builditem/ConfigMappingBuildItem.java b/core/deployment/src/main/java/io/quarkus/deployment/builditem/ConfigMappingBuildItem.java index 78639aaccc718..45274f49eb20a 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/builditem/ConfigMappingBuildItem.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/builditem/ConfigMappingBuildItem.java @@ -4,6 +4,7 @@ import io.quarkus.builder.item.MultiBuildItem; import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix; public final class ConfigMappingBuildItem extends MultiBuildItem { private final Class configClass; @@ -26,6 +27,10 @@ public boolean isStaticInitSafe() { return configClass.isAnnotationPresent(StaticInitSafe.class); } + public ConfigClassWithPrefix toConfigClassWithPrefix() { + return new ConfigClassWithPrefix(configClass, prefix); + } + @Override public boolean equals(final Object o) { if (this == o) { diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java index fd33610c90ca0..d45a1b7f398bf 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/BuildTimeConfigurationReader.java @@ -470,7 +470,6 @@ final class ReadOperation { final SmallRyeConfig config; final ConfigTrackingInterceptor buildConfigTracker; final Set processedNames = new HashSet<>(); - final Map, Object> objectsByClass = new HashMap<>(); final Map allBuildTimeValues = new TreeMap<>(); final Map buildTimeRunTimeValues = new TreeMap<>(); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigMappingUtils.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigMappingUtils.java index 65a31c9132ad3..3617c0a0a1b31 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigMappingUtils.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/ConfigMappingUtils.java @@ -1,5 +1,6 @@ package io.quarkus.deployment.configuration; +import static io.smallrye.config.ConfigMappings.ConfigClassWithPrefix.configClassWithPrefix; import static org.jboss.jandex.AnnotationTarget.Kind.CLASS; import java.util.ArrayList; @@ -32,6 +33,7 @@ import io.smallrye.config.ConfigMappingInterface.Property; import io.smallrye.config.ConfigMappingLoader; import io.smallrye.config.ConfigMappingMetadata; +import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix; public class ConfigMappingUtils { @@ -58,33 +60,42 @@ public static void processConfigClasses( Class configClass = toClass(target.asClass().name()); String prefix = Optional.ofNullable(annotationPrefix).map(AnnotationValue::asString).orElse(""); Kind configClassKind = getConfigClassType(instance); - processConfigClass(configClass, configClassKind, prefix, true, combinedIndex, generatedClasses, reflectiveClasses, - configClasses); + processConfigClass(configClassWithPrefix(configClass, prefix), configClassKind, true, combinedIndex, + generatedClasses, reflectiveClasses, configClasses); } } + public static void processConfigMapping( + CombinedIndexBuildItem combinedIndex, + BuildProducer generatedClasses, + BuildProducer reflectiveClasses, + BuildProducer configClasses) { + processConfigClasses(combinedIndex, generatedClasses, reflectiveClasses, configClasses, CONFIG_MAPPING_NAME); + } + public static void processExtensionConfigMapping( - Class configClass, - String prefix, + ConfigClassWithPrefix configClass, CombinedIndexBuildItem combinedIndex, BuildProducer generatedClasses, BuildProducer reflectiveClasses, BuildProducer configClasses) { - processConfigClass(configClass, Kind.MAPPING, prefix, false, combinedIndex, generatedClasses, reflectiveClasses, + processConfigClass(configClass, Kind.MAPPING, false, combinedIndex, generatedClasses, reflectiveClasses, configClasses); } private static void processConfigClass( - Class configClass, + ConfigClassWithPrefix configClassWithPrefix, Kind configClassKind, - String prefix, boolean isApplicationClass, CombinedIndexBuildItem combinedIndex, BuildProducer generatedClasses, BuildProducer reflectiveClasses, BuildProducer configClasses) { + Class configClass = configClassWithPrefix.getKlass(); + String prefix = configClassWithPrefix.getPrefix(); + List configMappingsMetadata = ConfigMappingLoader.getConfigMappingsMetadata(configClass); Set generatedClassesNames = new HashSet<>(); configMappingsMetadata.forEach(mappingMetadata -> { diff --git a/core/deployment/src/main/java/io/quarkus/deployment/configuration/RunTimeConfigurationGenerator.java b/core/deployment/src/main/java/io/quarkus/deployment/configuration/RunTimeConfigurationGenerator.java index a14c1d0c61470..6e396c8d5f190 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/configuration/RunTimeConfigurationGenerator.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/configuration/RunTimeConfigurationGenerator.java @@ -7,7 +7,6 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -26,7 +25,6 @@ import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; -import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import org.eclipse.microprofile.config.spi.Converter; import org.objectweb.asm.Opcodes; import org.wildfly.common.Assert; @@ -61,18 +59,15 @@ import io.quarkus.gizmo.TryBlock; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.annotations.ConfigPhase; +import io.quarkus.runtime.configuration.AbstractConfigBuilder; import io.quarkus.runtime.configuration.AbstractRawDefaultConfigSource; import io.quarkus.runtime.configuration.ConfigDiagnostic; -import io.quarkus.runtime.configuration.ConfigSourceFactoryProvider; import io.quarkus.runtime.configuration.ConfigUtils; import io.quarkus.runtime.configuration.ConfigurationException; import io.quarkus.runtime.configuration.HyphenateEnumConverter; import io.quarkus.runtime.configuration.NameIterator; import io.quarkus.runtime.configuration.PropertiesUtil; import io.quarkus.runtime.configuration.QuarkusConfigFactory; -import io.quarkus.runtime.configuration.RuntimeConfigSource; -import io.quarkus.runtime.configuration.RuntimeConfigSourceFactory; -import io.quarkus.runtime.configuration.RuntimeConfigSourceProvider; import io.smallrye.config.ConfigMappings; import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix; import io.smallrye.config.Converters; @@ -86,6 +81,8 @@ public final class RunTimeConfigurationGenerator { public static final String CONFIG_CLASS_NAME = "io.quarkus.runtime.generated.Config"; + public static final String CONFIG_BUILDER_STATIC_NAME = "io.quarkus.runtime.generated.StaticInitConfigBuilder"; + public static final String CONFIG_BUILDER_RUNTIME_NAME = "io.quarkus.runtime.generated.RunTimeConfigBuilder"; static final String RTDVCS_CLASS_NAME = "io.quarkus.runtime.generated.RunTimeDefaultValuesConfigSource"; public static final MethodDescriptor C_CREATE_RUN_TIME_CONFIG = MethodDescriptor.ofMethod(CONFIG_CLASS_NAME, @@ -150,19 +147,6 @@ public final class RunTimeConfigurationGenerator { static final MethodDescriptor CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED = MethodDescriptor.ofMethod(ConfigUtils.class, "configBuilder", SmallRyeConfigBuilder.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", - void.class, SmallRyeConfigBuilder.class, Collection.class); - static final MethodDescriptor CU_ADD_SOURCE_FACTORY_PROVIDER = MethodDescriptor.ofMethod(ConfigUtils.class, - "addSourceFactoryProvider", - void.class, SmallRyeConfigBuilder.class, ConfigSourceFactoryProvider.class); - - static final MethodDescriptor RCS_NEW = MethodDescriptor.ofConstructor(RuntimeConfigSource.class, String.class); - static final MethodDescriptor RCSP_NEW = MethodDescriptor.ofConstructor(RuntimeConfigSourceProvider.class, String.class); - static final MethodDescriptor RCSF_NEW = MethodDescriptor.ofConstructor(RuntimeConfigSourceFactory.class, String.class); static final MethodDescriptor AL_NEW = MethodDescriptor.ofConstructor(ArrayList.class); static final MethodDescriptor AL_ADD = MethodDescriptor.ofMethod(ArrayList.class, "add", boolean.class, Object.class); @@ -222,6 +206,8 @@ public final class RunTimeConfigurationGenerator { "withConverter", ConfigBuilder.class, Class.class, int.class, Converter.class); static final MethodDescriptor SRCB_WITH_SOURCES = MethodDescriptor.ofMethod(SmallRyeConfigBuilder.class, "withSources", ConfigBuilder.class, ConfigSource[].class); + static final MethodDescriptor SRCB_WITH_CUSTOMIZER = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withCustomizer", void.class, SmallRyeConfigBuilder.class, String.class); static final MethodDescriptor SRCB_BUILD = MethodDescriptor.ofMethod(SmallRyeConfigBuilder.class, "build", SmallRyeConfig.class); @@ -269,16 +255,6 @@ public static final class GenerateOperation implements AutoCloseable { final ResultHandle clinitConfig; final Map> convertersToRegister = new HashMap<>(); final List> additionalTypes; - final Set staticConfigSources; - final Set staticConfigSourceProviders; - final Set staticConfigSourceFactories; - final Set staticConfigBuilders; - final Set runtimeConfigSources; - final Set runtimeConfigSourceProviders; - final Set runtimeConfigSourceFactories; - final Set staticConfigMappings; - final Set runtimeConfigMappings; - final Set 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. @@ -309,16 +285,6 @@ public static final class GenerateOperation implements AutoCloseable { classOutput = Assert.checkNotNullParam("classOutput", builder.getClassOutput()); roots = Assert.checkNotNullParam("builder.roots", builder.getBuildTimeReadResult().getAllRoots()); additionalTypes = Assert.checkNotNullParam("additionalTypes", builder.getAdditionalTypes()); - 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(); generateUnknownFilter(); @@ -356,28 +322,8 @@ public static final class GenerateOperation implements AutoCloseable { final ResultHandle buildTimeBuilder = clinit.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED, clinit.load(true), clinit.load(false), clinit.load(launchMode)); - // add safe static sources - for (String runtimeConfigSource : staticConfigSources) { - clinit.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, buildTimeBuilder, - clinit.newInstance(RCS_NEW, clinit.load(runtimeConfigSource))); - } - // add safe static source providers - for (String runtimeConfigSourceProvider : staticConfigSourceProviders) { - clinit.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, buildTimeBuilder, - clinit.newInstance(RCSP_NEW, clinit.load(runtimeConfigSourceProvider))); - } - // add safe static source factories - for (String discoveredConfigSourceFactory : staticConfigSourceFactories) { - clinit.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, buildTimeBuilder, - clinit.newInstance(RCSF_NEW, clinit.load(discoveredConfigSourceFactory))); - } - - // additional config builders - ResultHandle configBuilders = clinit.newInstance(AL_NEW); - for (String configBuilder : staticConfigBuilders) { - clinit.invokeVirtualMethod(AL_ADD, configBuilders, clinit.load(configBuilder)); - } - clinit.invokeStaticMethod(CU_CONFIG_BUILDER_LIST, buildTimeBuilder, configBuilders); + // static config builder + clinit.invokeStaticMethod(SRCB_WITH_CUSTOMIZER, buildTimeBuilder, clinit.load(CONFIG_BUILDER_STATIC_NAME)); clinitConfig = clinit.checkCast(clinit.invokeVirtualMethod(SRCB_BUILD, buildTimeBuilder), SmallRyeConfig.class); @@ -402,27 +348,8 @@ public void run() { // the build time config, which is for user use only (not used by us other than for loading converters) final ResultHandle buildTimeBuilder = reinit.invokeStaticMethod(CU_CONFIG_BUILDER, reinit.load(true), reinit.load(launchMode)); - // add safe static sources - for (String runtimeConfigSource : staticConfigSources) { - reinit.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, buildTimeBuilder, - reinit.newInstance(RCS_NEW, reinit.load(runtimeConfigSource))); - } - // add safe static source providers - for (String runtimeConfigSourceProvider : staticConfigSourceProviders) { - reinit.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, buildTimeBuilder, - reinit.newInstance(RCSP_NEW, reinit.load(runtimeConfigSourceProvider))); - } - // add safe static source factories - for (String discoveredConfigSourceFactory : staticConfigSourceFactories) { - reinit.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, buildTimeBuilder, - reinit.newInstance(RCSF_NEW, reinit.load(discoveredConfigSourceFactory))); - } - // additional config builders - ResultHandle configBuilders = reinit.newInstance(AL_NEW); - for (String configBuilder : staticConfigBuilders) { - reinit.invokeVirtualMethod(AL_ADD, configBuilders, reinit.load(configBuilder)); - } - reinit.invokeStaticMethod(CU_CONFIG_BUILDER_LIST, buildTimeBuilder, configBuilders); + // static config builder + reinit.invokeStaticMethod(SRCB_WITH_CUSTOMIZER, buildTimeBuilder, reinit.load(CONFIG_BUILDER_STATIC_NAME)); ResultHandle clinitConfig = reinit.checkCast(reinit.invokeVirtualMethod(SRCB_BUILD, buildTimeBuilder), SmallRyeConfig.class); @@ -493,30 +420,8 @@ public void run() { // put sources in the builder readConfig.invokeVirtualMethod(SRCB_WITH_SOURCES, runTimeBuilder, runtimeConfigSourcesArray); - // add discovered sources - for (String runtimeConfigSource : runtimeConfigSources) { - readConfig.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, runTimeBuilder, - readConfig.newInstance(RCS_NEW, readConfig.load(runtimeConfigSource))); - } - - // add discovered source providers - for (String runtimeConfigSourceProvider : runtimeConfigSourceProviders) { - readConfig.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, runTimeBuilder, - readConfig.newInstance(RCSP_NEW, readConfig.load(runtimeConfigSourceProvider))); - } - - // add discovered source factories - for (String discoveredConfigSourceFactory : runtimeConfigSourceFactories) { - readConfig.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, runTimeBuilder, - readConfig.newInstance(RCSF_NEW, readConfig.load(discoveredConfigSourceFactory))); - } - - // additional config builders - ResultHandle configBuilders = readConfig.newInstance(AL_NEW); - for (String configBuilder : runtimeConfigBuilders) { - readConfig.invokeVirtualMethod(AL_ADD, configBuilders, readConfig.load(configBuilder)); - } - readConfig.invokeStaticMethod(CU_CONFIG_BUILDER_LIST, runTimeBuilder, configBuilders); + // runtime config builder + readConfig.invokeStaticMethod(SRCB_WITH_CUSTOMIZER, runTimeBuilder, readConfig.load(CONFIG_BUILDER_RUNTIME_NAME)); final ResultHandle runTimeConfig = readConfig.invokeVirtualMethod(SRCB_BUILD, runTimeBuilder); installConfiguration(runTimeConfig, readConfig); @@ -1502,13 +1407,13 @@ private void reportUnknown(final MethodCreator mc) { private void generateUnknownFilter() { Set mappedProperties = new HashSet<>(); - for (ConfigClassWithPrefix buildTimeMapping : buildTimeConfigResult.buildTimeMappings) { + for (ConfigClassWithPrefix buildTimeMapping : buildTimeConfigResult.getBuildTimeMappings()) { mappedProperties.addAll(ConfigMappings.getProperties(buildTimeMapping).keySet()); } - for (ConfigClassWithPrefix staticConfigMapping : staticConfigMappings) { + for (ConfigClassWithPrefix staticConfigMapping : buildTimeConfigResult.getBuildTimeRunTimeMappings()) { mappedProperties.addAll(ConfigMappings.getProperties(staticConfigMapping).keySet()); } - for (ConfigClassWithPrefix runtimeConfigMapping : runtimeConfigMappings) { + for (ConfigClassWithPrefix runtimeConfigMapping : buildTimeConfigResult.getRunTimeMappings()) { mappedProperties.addAll(ConfigMappings.getProperties(runtimeConfigMapping).keySet()); } @@ -1562,17 +1467,6 @@ public static final class Builder { private ClassOutput classOutput; private BuildTimeConfigurationReader.ReadResult buildTimeReadResult; private List> additionalTypes; - private Set staticConfigSources; - private Set staticConfigSourceProviders; - private Set staticConfigSourceFactories; - private Set staticConfigBuilders; - private Set runtimeConfigSources; - private Set runtimeConfigSourceProviders; - private Set runtimeConfigSourceFactories; - private Set runtimeConfigBuilders; - - private Set staticConfigMappings; - private Set runtimeConfigMappings; Builder() { } @@ -1618,96 +1512,6 @@ public Builder setLaunchMode(LaunchMode launchMode) { return this; } - Set getStaticConfigSources() { - return staticConfigSources; - } - - public Builder setStaticConfigSources(final Set staticConfigSources) { - this.staticConfigSources = staticConfigSources; - return this; - } - - Set getStaticConfigSourceProviders() { - return staticConfigSourceProviders; - } - - public Builder setStaticConfigSourceProviders(final Set staticConfigSourceProviders) { - this.staticConfigSourceProviders = staticConfigSourceProviders; - return this; - } - - Set getStaticConfigSourceFactories() { - return staticConfigSourceFactories; - } - - public Builder setStaticConfigSourceFactories(final Set staticConfigSourceFactories) { - this.staticConfigSourceFactories = staticConfigSourceFactories; - return this; - } - - Set getStaticConfigBuilders() { - return staticConfigBuilders; - } - - public Builder setStaticConfigBuilders(final Set staticConfigBuilders) { - this.staticConfigBuilders = staticConfigBuilders; - return this; - } - - Set getRuntimeConfigSources() { - return runtimeConfigSources; - } - - public Builder setRuntimeConfigSources(final Set runtimeConfigSources) { - this.runtimeConfigSources = runtimeConfigSources; - return this; - } - - Set getRuntimeConfigSourceProviders() { - return runtimeConfigSourceProviders; - } - - public Builder setRuntimeConfigSourceProviders(final Set runtimeConfigSourceProviders) { - this.runtimeConfigSourceProviders = runtimeConfigSourceProviders; - return this; - } - - Set getRuntimeConfigSourceFactories() { - return runtimeConfigSourceFactories; - } - - public Builder setRuntimeConfigSourceFactories(final Set runtimeConfigSourceFactories) { - this.runtimeConfigSourceFactories = runtimeConfigSourceFactories; - return this; - } - - Set getStaticConfigMappings() { - return staticConfigMappings; - } - - public Builder setStaticConfigMappings(final Set staticConfigMappings) { - this.staticConfigMappings = staticConfigMappings; - return this; - } - - Set getRuntimeConfigMappings() { - return runtimeConfigMappings; - } - - public Builder setRuntimeConfigMappings(final Set runtimeConfigMappings) { - this.runtimeConfigMappings = runtimeConfigMappings; - return this; - } - - Set getRuntimeConfigBuilders() { - return runtimeConfigBuilders; - } - - public Builder setRuntimeConfigBuilders(final Set runtimeConfigBuilders) { - this.runtimeConfigBuilders = runtimeConfigBuilders; - return this; - } - public GenerateOperation build() { return new GenerateOperation(this); } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java index 743582f056a7d..5f05ccc8f8fc9 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java @@ -1,11 +1,11 @@ package io.quarkus.deployment.steps; -import static io.quarkus.deployment.configuration.ConfigMappingUtils.CONFIG_MAPPING_NAME; -import static io.quarkus.deployment.configuration.ConfigMappingUtils.processConfigClasses; +import static io.quarkus.deployment.configuration.ConfigMappingUtils.processConfigMapping; import static io.quarkus.deployment.configuration.ConfigMappingUtils.processExtensionConfigMapping; +import static io.quarkus.deployment.configuration.RunTimeConfigurationGenerator.CONFIG_BUILDER_RUNTIME_NAME; +import static io.quarkus.deployment.configuration.RunTimeConfigurationGenerator.CONFIG_BUILDER_STATIC_NAME; import static io.quarkus.deployment.steps.ConfigBuildSteps.SERVICES_PREFIX; import static io.quarkus.deployment.util.ServiceUtil.classNamesNamedIn; -import static io.smallrye.config.ConfigMappings.ConfigClassWithPrefix.configClassWithPrefix; import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_LOCATIONS; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toSet; @@ -34,6 +34,7 @@ import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import org.objectweb.asm.Opcodes; +import io.quarkus.bootstrap.classloading.QuarkusClassLoader; import io.quarkus.deployment.GeneratedClassGizmoAdaptor; import io.quarkus.deployment.IsNormal; import io.quarkus.deployment.annotations.BuildProducer; @@ -71,18 +72,19 @@ import io.quarkus.gizmo.ResultHandle; import io.quarkus.runtime.LaunchMode; import io.quarkus.runtime.annotations.StaticInitSafe; +import io.quarkus.runtime.configuration.AbstractConfigBuilder; import io.quarkus.runtime.configuration.ConfigBuilder; import io.quarkus.runtime.configuration.ConfigDiagnostic; import io.quarkus.runtime.configuration.ConfigRecorder; import io.quarkus.runtime.configuration.DefaultsConfigSource; import io.quarkus.runtime.configuration.DisableableConfigSource; -import io.quarkus.runtime.configuration.MappingsConfigBuilder; import io.quarkus.runtime.configuration.QuarkusConfigValue; import io.quarkus.runtime.configuration.RuntimeOverrideConfigSource; import io.smallrye.config.ConfigMappings.ConfigClassWithPrefix; import io.smallrye.config.ConfigSourceFactory; import io.smallrye.config.SmallRyeConfig; import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; public class ConfigGenerationBuildStep { private static final MethodDescriptor CONFIG_BUILDER = MethodDescriptor.ofMethod( @@ -196,59 +198,68 @@ void runtimeDefaultsConfig( @BuildStep void mappings( + ConfigurationBuildItem configItem, CombinedIndexBuildItem combinedIndex, BuildProducer generatedClasses, BuildProducer reflectiveClasses, BuildProducer configClasses) { - processConfigClasses(combinedIndex, generatedClasses, reflectiveClasses, configClasses, CONFIG_MAPPING_NAME); - } - - @BuildStep - void extensionMappings(ConfigurationBuildItem configItem, - CombinedIndexBuildItem combinedIndex, - BuildProducer generatedClasses, - BuildProducer reflectiveClasses, - BuildProducer configClasses) { + processConfigMapping(combinedIndex, generatedClasses, reflectiveClasses, configClasses); List buildTimeRunTimeMappings = configItem.getReadResult().getBuildTimeRunTimeMappings(); for (ConfigClassWithPrefix buildTimeRunTimeMapping : buildTimeRunTimeMappings) { - processExtensionConfigMapping(buildTimeRunTimeMapping.getKlass(), buildTimeRunTimeMapping.getPrefix(), - combinedIndex, generatedClasses, reflectiveClasses, configClasses); + processExtensionConfigMapping(buildTimeRunTimeMapping, combinedIndex, generatedClasses, reflectiveClasses, + configClasses); } - final List runTimeMappings = configItem.getReadResult().getRunTimeMappings(); + List runTimeMappings = configItem.getReadResult().getRunTimeMappings(); for (ConfigClassWithPrefix runTimeMapping : runTimeMappings) { - processExtensionConfigMapping(runTimeMapping.getKlass(), runTimeMapping.getPrefix(), combinedIndex, - generatedClasses, reflectiveClasses, configClasses); + processExtensionConfigMapping(runTimeMapping, combinedIndex, generatedClasses, reflectiveClasses, configClasses); } } @BuildStep - void builderMappings( + void generateBuilders( ConfigurationBuildItem configItem, List configMappings, + List staticInitConfigBuilders, + List runTimeConfigBuilders, BuildProducer generatedClass, - BuildProducer reflectiveClass, - BuildProducer staticInitConfigBuilder, - BuildProducer runTimeConfigBuilder) { + BuildProducer reflectiveClass) throws Exception { + + Set configSources = discoverService(ConfigSource.class, reflectiveClass); + Set configSourceProviders = discoverService(ConfigSourceProvider.class, reflectiveClass); + Set configSourceFactories = discoverService(ConfigSourceFactory.class, reflectiveClass); + Set configCustomizers = discoverService(SmallRyeConfigBuilderCustomizer.class, reflectiveClass); // For Static Init Config Set staticMappings = new HashSet<>(); staticMappings.addAll(staticSafeConfigMappings(configMappings)); staticMappings.addAll(configItem.getReadResult().getBuildTimeRunTimeMappings()); - String staticInitMappingsConfigBuilder = "io.quarkus.runtime.generated.StaticInitMappingsConfigBuilder"; - generateMappingsConfigBuilder(generatedClass, reflectiveClass, staticInitMappingsConfigBuilder, staticMappings); - staticInitConfigBuilder.produce(new StaticInitConfigBuilderBuildItem(staticInitMappingsConfigBuilder)); + + generateConfigBuilder(generatedClass, reflectiveClass, CONFIG_BUILDER_STATIC_NAME, + staticSafeServices(configSources), + staticSafeServices(configSourceProviders), + staticSafeServices(configSourceFactories), + staticMappings, + staticSafeServices(configCustomizers), + staticInitConfigBuilders.stream().map(StaticInitConfigBuilderBuildItem::getBuilderClassName).collect(toSet())); + reflectiveClass.produce(ReflectiveClassBuildItem.builder(CONFIG_BUILDER_STATIC_NAME).build()); // For RunTime Config Set runTimeMappings = new HashSet<>(); runTimeMappings.addAll(runtimeConfigMappings(configMappings)); runTimeMappings.addAll(configItem.getReadResult().getBuildTimeRunTimeMappings()); runTimeMappings.addAll(configItem.getReadResult().getRunTimeMappings()); - String runTimeMappingsConfigBuilder = "io.quarkus.runtime.generated.RunTimeMappingsConfigBuilder"; - generateMappingsConfigBuilder(generatedClass, reflectiveClass, runTimeMappingsConfigBuilder, runTimeMappings); - runTimeConfigBuilder.produce(new RunTimeConfigBuilderBuildItem(runTimeMappingsConfigBuilder)); + + generateConfigBuilder(generatedClass, reflectiveClass, CONFIG_BUILDER_RUNTIME_NAME, + configSources, + configSourceProviders, + configSourceFactories, + runTimeMappings, + configCustomizers, + runTimeConfigBuilders.stream().map(RunTimeConfigBuilderBuildItem::getBuilderClassName).collect(toSet())); + reflectiveClass.produce(ReflectiveClassBuildItem.builder(CONFIG_BUILDER_RUNTIME_NAME).build()); } /** @@ -260,43 +271,16 @@ void generateConfigClass( List typeItems, LaunchModeBuildItem launchModeBuildItem, BuildProducer generatedClass, - BuildProducer reflectiveClass, - LiveReloadBuildItem liveReloadBuildItem, - List configMappings, - List staticInitConfigBuilders, - List runTimeConfigBuilders) - throws IOException { + LiveReloadBuildItem liveReloadBuildItem) { reportUnknownBuildProperties(launchModeBuildItem.getLaunchMode(), configItem.getReadResult().getUnknownBuildProperties()); + // TODO - Test live reload with ConfigSource if (liveReloadBuildItem.isLiveReload()) { return; } - Set discoveredConfigSources = discoverService(ConfigSource.class, reflectiveClass); - Set discoveredConfigSourceProviders = discoverService(ConfigSourceProvider.class, reflectiveClass); - Set discoveredConfigSourceFactories = discoverService(ConfigSourceFactory.class, reflectiveClass); - - Set staticConfigSourceProviders = staticSafeServices(discoveredConfigSourceProviders); - Set staticConfigSourceFactories = staticSafeServices(discoveredConfigSourceFactories); - - // TODO - duplicated now builderMappings. Still required to filter the unknown properties - Set staticMappings = new HashSet<>(); - staticMappings.addAll(staticSafeConfigMappings(configMappings)); - staticMappings.addAll(configItem.getReadResult().getBuildTimeRunTimeMappings()); - - Set runtimeMappings = new HashSet<>(); - runtimeMappings.addAll(runtimeConfigMappings(configMappings)); - runtimeMappings.addAll(configItem.getReadResult().getBuildTimeRunTimeMappings()); - runtimeMappings.addAll(configItem.getReadResult().getRunTimeMappings()); - - Set runtimeConfigBuilderClassNames = runTimeConfigBuilders.stream() - .map(RunTimeConfigBuilderBuildItem::getBuilderClassName).collect(toSet()); - reflectiveClass - .produce(ReflectiveClassBuildItem.builder(runtimeConfigBuilderClassNames.toArray(new String[0])) - .build()); - RunTimeConfigurationGenerator.GenerateOperation .builder() .setBuildTimeReadResult(configItem.getReadResult()) @@ -305,17 +289,6 @@ void generateConfigClass( .setLiveReloadPossible(launchModeBuildItem.getLaunchMode() == LaunchMode.DEVELOPMENT || launchModeBuildItem.isAuxiliaryApplication()) .setAdditionalTypes(typeItems.stream().map(ConfigurationTypeBuildItem::getValueType).collect(toList())) - .setStaticConfigSources(staticSafeServices(discoveredConfigSources)) - .setStaticConfigSourceProviders(staticConfigSourceProviders) - .setStaticConfigSourceFactories(staticConfigSourceFactories) - .setStaticConfigMappings(staticMappings) - .setStaticConfigBuilders(staticInitConfigBuilders.stream() - .map(StaticInitConfigBuilderBuildItem::getBuilderClassName).collect(toSet())) - .setRuntimeConfigSources(discoveredConfigSources) - .setRuntimeConfigSourceProviders(discoveredConfigSourceProviders) - .setRuntimeConfigSourceFactories(discoveredConfigSourceFactories) - .setRuntimeConfigMappings(runtimeMappings) - .setRuntimeConfigBuilders(runtimeConfigBuilderClassNames) .build() .run(); } @@ -531,31 +504,80 @@ private static void generateDefaultsConfigSource( .produce(ReflectiveClassBuildItem.builder(className).build()); } - private static void generateMappingsConfigBuilder( + private static final MethodDescriptor CUSTOMIZER_BUILDER = MethodDescriptor.ofMethod(SmallRyeConfigBuilderCustomizer.class, + "configBuilder", + void.class, SmallRyeConfigBuilder.class); + private static final MethodDescriptor WITH_SOURCE = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withSource", + void.class, SmallRyeConfigBuilder.class, ConfigSource.class); + private static final MethodDescriptor WITH_SOURCE_PROVIDER = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withSource", + void.class, SmallRyeConfigBuilder.class, ConfigSourceProvider.class); + private static final MethodDescriptor WITH_SOURCE_FACTORY = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withSource", + void.class, SmallRyeConfigBuilder.class, ConfigSourceFactory.class); + private static final MethodDescriptor WITH_MAPPING = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withMapping", + void.class, SmallRyeConfigBuilder.class, String.class, String.class); + private static final MethodDescriptor WITH_CUSTOMIZER = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withCustomizer", + void.class, SmallRyeConfigBuilder.class, SmallRyeConfigBuilderCustomizer.class); + private static final MethodDescriptor WITH_BUILDER = MethodDescriptor.ofMethod(AbstractConfigBuilder.class, + "withBuilder", + void.class, SmallRyeConfigBuilder.class, ConfigBuilder.class); + + private static void generateConfigBuilder( BuildProducer generatedClass, BuildProducer reflectiveClass, String className, - Set mappings) { + Set configSources, + Set configSourceProviders, + Set configSourceFactories, + Set mappings, + Set configCustomizers, + Set configBuilders) { try (ClassCreator classCreator = ClassCreator.builder() .classOutput(new GeneratedClassGizmoAdaptor(generatedClass, true)) .className(className) - .interfaces(ConfigBuilder.class) - .superClass(MappingsConfigBuilder.class) + .interfaces(SmallRyeConfigBuilderCustomizer.class) + .superClass(AbstractConfigBuilder.class) .setFinal(true) .build()) { - MethodCreator method = classCreator.getMethodCreator(CONFIG_BUILDER); + MethodCreator method = classCreator.getMethodCreator(CUSTOMIZER_BUILDER); ResultHandle configBuilder = method.getMethodParam(0); - MethodDescriptor addMapping = MethodDescriptor.ofMethod(MappingsConfigBuilder.class, "addMapping", void.class, - SmallRyeConfigBuilder.class, String.class, String.class); + for (String configSource : configSources) { + method.invokeStaticMethod(WITH_SOURCE, configBuilder, + method.newInstance(MethodDescriptor.ofConstructor(configSource))); + } + + for (String configSourceProvider : configSourceProviders) { + method.invokeStaticMethod(WITH_SOURCE_PROVIDER, configBuilder, + method.newInstance(MethodDescriptor.ofConstructor(configSourceProvider))); + } + + for (String configSourceFactory : configSourceFactories) { + method.invokeStaticMethod(WITH_SOURCE_FACTORY, configBuilder, + method.newInstance(MethodDescriptor.ofConstructor(configSourceFactory))); + } for (ConfigClassWithPrefix mapping : mappings) { - method.invokeStaticMethod(addMapping, configBuilder, method.load(mapping.getKlass().getName()), + method.invokeStaticMethod(WITH_MAPPING, configBuilder, method.load(mapping.getKlass().getName()), method.load(mapping.getPrefix())); } + for (String configCustomizer : configCustomizers) { + method.invokeStaticMethod(WITH_CUSTOMIZER, configBuilder, + method.newInstance(MethodDescriptor.ofConstructor(configCustomizer))); + } + + for (String builder : configBuilders) { + method.invokeStaticMethod(WITH_BUILDER, configBuilder, + method.newInstance(MethodDescriptor.ofConstructor(builder))); + } + method.returnValue(configBuilder); } @@ -568,15 +590,16 @@ private static Set discoverService( ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Set services = new HashSet<>(); for (String service : classNamesNamedIn(classLoader, SERVICES_PREFIX + serviceClass.getName())) { - services.add(service); - reflectiveClass - .produce(ReflectiveClassBuildItem.builder(service).build()); + // The discovery includes deployment modules, so we only include services available at runtime + if (QuarkusClassLoader.isClassPresentAtRuntime(service)) { + services.add(service); + reflectiveClass.produce(ReflectiveClassBuildItem.builder(service).build()); + } } return services; } private static Set staticSafeServices(Set services) { - // TODO - Replace with Jandex? The issue is that the sources may not be in the index... ClassLoader classloader = Thread.currentThread().getContextClassLoader(); Set staticSafe = new HashSet<>(); for (String service : services) { @@ -595,13 +618,13 @@ private static Set staticSafeServices(Set services) { private static Set staticSafeConfigMappings(List configMappings) { return configMappings.stream() .filter(ConfigMappingBuildItem::isStaticInitSafe) - .map(configMapping -> configClassWithPrefix(configMapping.getConfigClass(), configMapping.getPrefix())) + .map(ConfigMappingBuildItem::toConfigClassWithPrefix) .collect(toSet()); } private static Set runtimeConfigMappings(List configMappings) { return configMappings.stream() - .map(configMapping -> configClassWithPrefix(configMapping.getConfigClass(), configMapping.getPrefix())) + .map(ConfigMappingBuildItem::toConfigClassWithPrefix) .collect(toSet()); } } diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/AbstractConfigBuilder.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/AbstractConfigBuilder.java new file mode 100644 index 0000000000000..ac7a5805ca9c7 --- /dev/null +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/AbstractConfigBuilder.java @@ -0,0 +1,60 @@ +package io.quarkus.runtime.configuration; + +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.config.spi.ConfigSourceProvider; + +import io.smallrye.config.ConfigSourceFactory; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +public abstract class AbstractConfigBuilder implements SmallRyeConfigBuilderCustomizer { + protected static void withSource(SmallRyeConfigBuilder builder, ConfigSource configSource) { + builder.withSources(configSource); + } + + protected static void withSource(SmallRyeConfigBuilder builder, ConfigSourceProvider configSourceProvider) { + builder.withSources(configSourceProvider); + } + + protected static void withSource(SmallRyeConfigBuilder builder, ConfigSourceFactory configSourceFactory) { + builder.withSources(configSourceFactory); + } + + protected static void withMapping(SmallRyeConfigBuilder builder, String mappingClass, String prefix) { + try { + // To support mappings that are not public + builder.withMapping(builder.getClassLoader().loadClass(mappingClass), prefix); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + protected static void withBuilder(SmallRyeConfigBuilder builder, ConfigBuilder configBuilder) { + builder.withCustomizers(new SmallRyeConfigBuilderCustomizer() { + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + configBuilder.configBuilder(builder); + } + + @Override + public int priority() { + return configBuilder.priority(); + } + }); + } + + protected static void withCustomizer(SmallRyeConfigBuilder builder, SmallRyeConfigBuilderCustomizer customizer) { + builder.withCustomizers(customizer); + } + + @SuppressWarnings("unchecked") + public static void withCustomizer(SmallRyeConfigBuilder builder, String customizer) { + try { + Class customizerClass = (Class) builder + .getClassLoader().loadClass(customizer); + customizerClass.getDeclaredConstructor().newInstance().configBuilder(builder); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigSourceFactoryProvider.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigSourceFactoryProvider.java deleted file mode 100644 index 15090cf88cba0..0000000000000 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigSourceFactoryProvider.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.quarkus.runtime.configuration; - -import io.smallrye.config.ConfigSourceFactory; - -public interface ConfigSourceFactoryProvider { - ConfigSourceFactory getConfigSourceFactory(final ClassLoader forClassLoader); -} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigUtils.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigUtils.java index adf9a55b15637..b9ef30b6b38ac 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigUtils.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/ConfigUtils.java @@ -4,10 +4,8 @@ import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE; import static io.smallrye.config.SmallRyeConfig.SMALLRYE_CONFIG_PROFILE_PARENT; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; @@ -24,7 +22,6 @@ import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.config.spi.ConfigSource; -import org.eclipse.microprofile.config.spi.ConfigSourceProvider; import io.quarkus.runtime.LaunchMode; import io.smallrye.config.ConfigSourceInterceptor; @@ -79,13 +76,13 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b SmallRyeConfigBuilder builder = emptyConfigBuilder(); if (launchMode.isDevOrTest() && runTime) { - builder.withSources(new RuntimeOverrideConfigSource(Thread.currentThread().getContextClassLoader())); + builder.withSources(new RuntimeOverrideConfigSource(builder.getClassLoader())); } if (runTime) { builder.withDefaultValue(UUID_KEY, UUID.randomUUID().toString()); } if (addDiscovered) { - builder.addDiscoveredSources(); + builder.addDiscoveredCustomizers().addDiscoveredSources(); } return builder; } @@ -177,61 +174,6 @@ public OptionalInt getPriority() { return builder; } - @SuppressWarnings("unchecked") - public static SmallRyeConfigBuilder configBuilder(SmallRyeConfigBuilder builder, List configBuildersNames) { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - try { - List configBuilders = new ArrayList<>(); - for (String configBuilderName : configBuildersNames) { - Class configBuilderClass = (Class) contextClassLoader - .loadClass(configBuilderName); - configBuilders.add(configBuilderClass.getDeclaredConstructor().newInstance()); - } - configBuilders.sort(ConfigBuilderComparator.INSTANCE); - - for (ConfigBuilder configBuilder : configBuilders) { - builder = configBuilder.configBuilder(builder); - if (builder == null) { - throw new ConfigurationException(configBuilder.getClass().getName() + " returned a null builder"); - } - } - - } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | NoSuchMethodException - | IllegalAccessException e) { - throw new ConfigurationException(e); - } - return builder; - } - - /** - * Add a configuration source provider to the builder. - * - * @param builder the builder - * @param provider the provider to add - */ - public static void addSourceProvider(SmallRyeConfigBuilder builder, ConfigSourceProvider provider) { - final Iterable sources = provider.getConfigSources(Thread.currentThread().getContextClassLoader()); - for (ConfigSource source : sources) { - builder.withSources(source); - } - } - - /** - * Add a configuration source providers to the builder. - * - * @param builder the builder - * @param providers the providers to add - */ - public static void addSourceProviders(SmallRyeConfigBuilder builder, Collection providers) { - for (ConfigSourceProvider provider : providers) { - addSourceProvider(builder, provider); - } - } - - public static void addSourceFactoryProvider(SmallRyeConfigBuilder builder, ConfigSourceFactoryProvider provider) { - builder.withSources(provider.getConfigSourceFactory(Thread.currentThread().getContextClassLoader())); - } - public static List getProfiles() { return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getProfiles(); } @@ -298,13 +240,4 @@ public static Optional getFirstOptionalValue(List propertyNames, } return Optional.empty(); } - - private static class ConfigBuilderComparator implements Comparator { - private static final ConfigBuilderComparator INSTANCE = new ConfigBuilderComparator(); - - @Override - public int compare(ConfigBuilder o1, ConfigBuilder o2) { - return Integer.compare(o1.priority(), o2.priority()); - } - } } diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/MappingsConfigBuilder.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/MappingsConfigBuilder.java deleted file mode 100644 index 24d1b607074be..0000000000000 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/MappingsConfigBuilder.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.quarkus.runtime.configuration; - -import io.smallrye.config.SmallRyeConfigBuilder; - -/** - * To support mappings that are not public - */ -public abstract class MappingsConfigBuilder implements ConfigBuilder { - protected static void addMapping(SmallRyeConfigBuilder builder, String mappingClass, String prefix) { - // TODO - Ideally should use the classloader passed to Config, but the method is not public. Requires a change in SmallRye Config. - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - try { - builder.withMapping(contextClassLoader.loadClass(mappingClass), prefix); - } catch (ClassNotFoundException e) { - throw new RuntimeException(e); - } - } -} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/QuarkusConfigFactory.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/QuarkusConfigFactory.java index 9f168ae907e6f..5a2776d493561 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/QuarkusConfigFactory.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/configuration/QuarkusConfigFactory.java @@ -2,6 +2,7 @@ import org.eclipse.microprofile.config.spi.ConfigProviderResolver; +import io.quarkus.runtime.LaunchMode; import io.smallrye.config.SmallRyeConfig; import io.smallrye.config.SmallRyeConfigFactory; import io.smallrye.config.SmallRyeConfigProviderResolver; @@ -23,10 +24,7 @@ public QuarkusConfigFactory() { public SmallRyeConfig getConfigFor(final SmallRyeConfigProviderResolver configProviderResolver, final ClassLoader classLoader) { if (config == null) { - //TODO: this code path is only hit when start fails in dev mode very early in the process - //the recovery code will fail without this as it cannot read any properties such as - //the HTTP port or logging info - return ConfigUtils.emptyConfigBuilder().addDiscoveredSources().build(); + return ConfigUtils.configBuilder(true, true, LaunchMode.NORMAL).build(); } return config; } diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSource.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSource.java deleted file mode 100644 index eeb2f1117618f..0000000000000 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSource.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.quarkus.runtime.configuration; - -import java.lang.reflect.InvocationTargetException; -import java.util.Collections; - -import org.eclipse.microprofile.config.spi.ConfigSource; -import org.eclipse.microprofile.config.spi.ConfigSourceProvider; - -public class RuntimeConfigSource implements ConfigSourceProvider { - private final String configSourceClassName; - - public RuntimeConfigSource(final String configSourceClassName) { - this.configSourceClassName = configSourceClassName; - } - - @Override - public Iterable getConfigSources(final ClassLoader forClassLoader) { - try { - Class configSourceClass = (Class) forClassLoader.loadClass(configSourceClassName); - ConfigSource configSource = configSourceClass.getDeclaredConstructor().newInstance(); - return Collections.singleton(configSource); - } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | NoSuchMethodException - | IllegalAccessException e) { - throw new ConfigurationException(e); - } - } -} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceFactory.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceFactory.java deleted file mode 100644 index 355f22edd5baf..0000000000000 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceFactory.java +++ /dev/null @@ -1,25 +0,0 @@ -package io.quarkus.runtime.configuration; - -import java.lang.reflect.InvocationTargetException; - -import io.smallrye.config.ConfigSourceFactory; - -public class RuntimeConfigSourceFactory implements ConfigSourceFactoryProvider { - private final String configSourceFactoryClassName; - - public RuntimeConfigSourceFactory(final String configSourceFactoryClassName) { - this.configSourceFactoryClassName = configSourceFactoryClassName; - } - - @Override - public ConfigSourceFactory getConfigSourceFactory(final ClassLoader forClassLoader) { - try { - Class configSourceFactoryClass = (Class) forClassLoader - .loadClass(configSourceFactoryClassName); - return configSourceFactoryClass.getDeclaredConstructor().newInstance(); - } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | NoSuchMethodException - | IllegalAccessException e) { - throw new ConfigurationException(e); - } - } -} diff --git a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceProvider.java b/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceProvider.java deleted file mode 100644 index 54e6e33290dee..0000000000000 --- a/core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigSourceProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.quarkus.runtime.configuration; - -import java.lang.reflect.InvocationTargetException; - -import org.eclipse.microprofile.config.spi.ConfigSource; -import org.eclipse.microprofile.config.spi.ConfigSourceProvider; - -public class RuntimeConfigSourceProvider implements ConfigSourceProvider { - private final String configSourceProviderClassName; - - public RuntimeConfigSourceProvider(final String configSourceProviderClassName) { - this.configSourceProviderClassName = configSourceProviderClassName; - } - - @Override - public Iterable getConfigSources(final ClassLoader forClassLoader) { - try { - Class configSourceProviderClass = (Class) forClassLoader - .loadClass(configSourceProviderClassName); - ConfigSourceProvider configSourceProvider = configSourceProviderClass.getDeclaredConstructor().newInstance(); - return configSourceProvider.getConfigSources(forClassLoader); - } catch (ClassNotFoundException | InstantiationException | InvocationTargetException | NoSuchMethodException - | IllegalAccessException e) { - throw new ConfigurationException(e); - } - } -} diff --git a/integration-tests/smallrye-config/src/main/java/io/quarkus/it/smallrye/config/ConfigBuilderCustomizer.java b/integration-tests/smallrye-config/src/main/java/io/quarkus/it/smallrye/config/ConfigBuilderCustomizer.java new file mode 100644 index 0000000000000..243fff08eb312 --- /dev/null +++ b/integration-tests/smallrye-config/src/main/java/io/quarkus/it/smallrye/config/ConfigBuilderCustomizer.java @@ -0,0 +1,13 @@ +package io.quarkus.it.smallrye.config; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +@StaticInitSafe +public class ConfigBuilderCustomizer implements SmallRyeConfigBuilderCustomizer { + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + builder.withDefaultValue("exception.message", "This is an exception!"); + } +} diff --git a/integration-tests/smallrye-config/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer b/integration-tests/smallrye-config/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer new file mode 100644 index 0000000000000..9f865d958b21f --- /dev/null +++ b/integration-tests/smallrye-config/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer @@ -0,0 +1 @@ +io.quarkus.it.smallrye.config.ConfigBuilderCustomizer diff --git a/integration-tests/smallrye-config/src/main/resources/config.properties b/integration-tests/smallrye-config/src/main/resources/config.properties index a057d5062f54c..fc031ecfd4767 100644 --- a/integration-tests/smallrye-config/src/main/resources/config.properties +++ b/integration-tests/smallrye-config/src/main/resources/config.properties @@ -1,3 +1 @@ config.properties=1234 - -exception.message=This is an exception! diff --git a/integration-tests/smallrye-config/src/main/resources/fallback.properties b/integration-tests/smallrye-config/src/main/resources/fallback.properties index 5403f0593c38c..a7903ceca7189 100644 --- a/integration-tests/smallrye-config/src/main/resources/fallback.properties +++ b/integration-tests/smallrye-config/src/main/resources/fallback.properties @@ -1,3 +1 @@ locations.fallback=1234 - -exception.message=This is an exception! diff --git a/integration-tests/smallrye-config/src/main/resources/test-profile.properties b/integration-tests/smallrye-config/src/main/resources/test-profile.properties index bcaec8b63ed77..ad05d5b34bc4c 100644 --- a/integration-tests/smallrye-config/src/main/resources/test-profile.properties +++ b/integration-tests/smallrye-config/src/main/resources/test-profile.properties @@ -1,3 +1 @@ test.profile.main=main - -exception.message=This is an exception! diff --git a/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/BuildTimeCustomConfigBuilder.java b/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/BuildTimeCustomConfigBuilder.java new file mode 100644 index 0000000000000..c52d2f5010593 --- /dev/null +++ b/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/BuildTimeCustomConfigBuilder.java @@ -0,0 +1,15 @@ +package io.quarkus.extest.deployment; + +import java.util.Map; + +import io.smallrye.config.PropertiesConfigSource; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +public class BuildTimeCustomConfigBuilder implements SmallRyeConfigBuilderCustomizer { + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + builder.withSources( + new PropertiesConfigSource(Map.of("prop.recorded.from.btconfigsource", "1234"), "BuildTimeConfigSource", 100)); + } +} diff --git a/integration-tests/test-extension/extension/deployment/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer b/integration-tests/test-extension/extension/deployment/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer new file mode 100644 index 0000000000000..69636f6169932 --- /dev/null +++ b/integration-tests/test-extension/extension/deployment/src/main/resources/META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer @@ -0,0 +1 @@ +io.quarkus.extest.deployment.BuildTimeCustomConfigBuilder diff --git a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/config/ConfigBuilderTest.java b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/config/ConfigBuilderTest.java index 5254275f7dec6..41ebada063e1c 100644 --- a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/config/ConfigBuilderTest.java +++ b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/config/ConfigBuilderTest.java @@ -1,6 +1,7 @@ package io.quarkus.config; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import jakarta.inject.Inject; @@ -30,4 +31,10 @@ void staticConfigBuilder() { void runTimeConfigBuilder() { assertEquals("1234", config.getRawValue("additional.builder.property")); } + + @Test + void buildTimeConfigBuilder() { + assertFalse(config.getConfigSource("BuildTimeConfigSource").isPresent()); + assertEquals("1234", config.getRawValue("prop.recorded.from.btconfigsource")); + } } diff --git a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/RuntimeDefaultsTest.java b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/RuntimeDefaultsTest.java index db522995ff2b9..b5bb2f1b8280f 100644 --- a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/RuntimeDefaultsTest.java +++ b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/RuntimeDefaultsTest.java @@ -31,6 +31,7 @@ public class RuntimeDefaultsTest { void doNotRecordEnvRuntimeDefaults() { Optional defaultValues = config.getConfigSource("RunTime Defaults"); assertTrue(defaultValues.isPresent()); + assertEquals("rtStringOptValue", defaultValues.get().getValue("quarkus.rt.rt-string-opt")); assertEquals("properties", defaultValues.get().getValue("bt.do.not.record")); } diff --git a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/StaticInitSourcesTest.java b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/StaticInitSourcesTest.java index 8c72f140e5579..49c72019e089c 100644 --- a/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/StaticInitSourcesTest.java +++ b/integration-tests/test-extension/extension/deployment/src/test/java/io/quarkus/extest/StaticInitSourcesTest.java @@ -5,7 +5,9 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import io.quarkus.extest.runtime.config.StaticInitNotSafeConfigBuilderCustomizer; import io.quarkus.extest.runtime.config.StaticInitNotSafeConfigSource; +import io.quarkus.extest.runtime.config.StaticInitSafeConfigBuilderCustomizer; import io.quarkus.extest.runtime.config.StaticInitSafeConfigSource; import io.quarkus.test.QuarkusUnitTest; @@ -15,9 +17,9 @@ public class StaticInitSourcesTest { .withApplicationRoot((jar) -> jar .addClasses(ConfiguredBean.class) // Don't change this to types, because of classloader class cast exception. - .addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource", - StaticInitSafeConfigSource.class.getName(), - StaticInitNotSafeConfigSource.class.getName()) + .addAsServiceProvider("io.smallrye.config.SmallRyeConfigBuilderCustomizer", + StaticInitSafeConfigBuilderCustomizer.class.getName(), + StaticInitNotSafeConfigBuilderCustomizer.class.getName()) .addAsResource("application.properties")); @Test diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/EnvBuildTimeConfigSource.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/EnvBuildTimeConfigSource.java index ebfc255b2d0e2..27d297ac883e4 100644 --- a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/EnvBuildTimeConfigSource.java +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/EnvBuildTimeConfigSource.java @@ -6,8 +6,9 @@ public class EnvBuildTimeConfigSource extends EnvConfigSource { public EnvBuildTimeConfigSource() { - super(new HashMap() { + super(new HashMap<>() { { + put("QUARKUS_RT_RT_STRING_OPT", "changed"); put("BT_DO_NOT_RECORD", "env-source"); } }, Integer.MAX_VALUE); diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitNotSafeConfigBuilderCustomizer.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitNotSafeConfigBuilderCustomizer.java new file mode 100644 index 0000000000000..a66bd73cd9e10 --- /dev/null +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitNotSafeConfigBuilderCustomizer.java @@ -0,0 +1,11 @@ +package io.quarkus.extest.runtime.config; + +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +public class StaticInitNotSafeConfigBuilderCustomizer implements SmallRyeConfigBuilderCustomizer { + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + builder.withSources(new StaticInitNotSafeConfigSource()); + } +} diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitSafeConfigBuilderCustomizer.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitSafeConfigBuilderCustomizer.java new file mode 100644 index 0000000000000..b2ba81d75c74e --- /dev/null +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/config/StaticInitSafeConfigBuilderCustomizer.java @@ -0,0 +1,17 @@ +package io.quarkus.extest.runtime.config; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.SmallRyeConfigBuilder; +import io.smallrye.config.SmallRyeConfigBuilderCustomizer; + +@StaticInitSafe +public class StaticInitSafeConfigBuilderCustomizer implements SmallRyeConfigBuilderCustomizer { + public StaticInitSafeConfigBuilderCustomizer() { + System.out.println("StaticInitSafeConfigBuilderCustomizer.StaticInitSafeConfigBuilderCustomizer"); + } + + @Override + public void configBuilder(final SmallRyeConfigBuilder builder) { + builder.withSources(new StaticInitSafeConfigSource()); + } +} diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java b/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java index 72b98a7f9764e..9e088371f3fde 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/TestResourceManager.java @@ -25,13 +25,14 @@ import java.util.function.Function; import java.util.function.Predicate; -import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.jboss.jandex.AnnotationInstance; import org.jboss.jandex.AnnotationValue; import org.jboss.jandex.ClassInfo; import org.jboss.jandex.DotName; import org.jboss.jandex.IndexView; +import io.smallrye.config.SmallRyeConfigProviderResolver; + public class TestResourceManager implements Closeable { public static final String CLOSEABLE_NAME = TestResourceManager.class.getName() + ".closeable"; @@ -179,8 +180,8 @@ public void close() { } } try { - ConfigProviderResolver cpr = ConfigProviderResolver.instance(); - cpr.releaseConfig(cpr.getConfig()); + ((SmallRyeConfigProviderResolver) SmallRyeConfigProviderResolver.instance()) + .releaseConfig(Thread.currentThread().getContextClassLoader()); } catch (Throwable ignored) { } configProperties.clear();