From 4c211a23d9d304c1e3c14c4bf2697df10394ef7b Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Wed, 16 Mar 2022 17:04:20 +0000 Subject: [PATCH] Add Runtime Default values in the static Config --- .../RunTimeConfigurationGenerator.java | 46 ++++++++++--------- .../config/RuntimeInitConfigSource.java | 11 ----- .../RuntimeInitConfigSourceFactory.java | 25 ++++++++++ .../StaticInitConfigMappingInvalidTest.java | 6 +-- .../config/StaticInitConfigMappingTest.java | 11 +++-- .../config/StaticInitConfigSource.java | 13 ------ .../config/StaticInitConfigSourceFactory.java | 27 +++++++++++ 7 files changed, 88 insertions(+), 51 deletions(-) delete mode 100644 core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSource.java create mode 100644 core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSourceFactory.java delete mode 100644 core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSource.java create mode 100644 core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSourceFactory.java 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 4c3dbbb39bdf9..796137674b6d7 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 @@ -423,14 +423,36 @@ public static final class GenerateOperation implements AutoCloseable { .setModifiers(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL); clinit.writeStaticField(C_RUN_TIME_DEFAULTS_CONFIG_SOURCE, clinit.newInstance(RTDVCS_NEW)); + // create the map for run time specified values config source + final ResultHandle specifiedRunTimeValues = clinit.newInstance(HM_NEW); + if (!liveReloadPossible) { + //we don't need these in devmode + //including it would just cache the first values + //but these can already just be read directly, as we are in the same JVM + for (Map.Entry entry : specifiedRunTimeDefaultValues.entrySet()) { + clinit.invokeVirtualMethod(HM_PUT, specifiedRunTimeValues, clinit.load(entry.getKey()), + clinit.load(entry.getValue())); + } + } + + final ResultHandle specifiedRunTimeSource = clinit.newInstance(PCS_NEW, specifiedRunTimeValues, + clinit.load("Specified default values"), clinit.load(Integer.MIN_VALUE + 100)); + + cc.getFieldCreator(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE) + .setModifiers(Opcodes.ACC_STATIC | (liveReloadPossible ? Opcodes.ACC_VOLATILE : Opcodes.ACC_FINAL)); + clinit.writeStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE, specifiedRunTimeSource); + // the build time config, which is for user use only (not used by us other than for loading converters) final ResultHandle buildTimeBuilder = clinit.invokeStaticMethod(CU_CONFIG_BUILDER_WITH_ADD_DISCOVERED, clinit.load(true), clinit.load(false), clinit.load(launchMode)); - final ResultHandle array = clinit.newArray(ConfigSource[].class, 2); - // build time values + final ResultHandle array = clinit.newArray(ConfigSource[].class, 3); + // build time values (recorded visible for runtime) clinit.writeArrayValue(array, 0, buildTimeConfigSource); - // build time defaults + // build time runtime defaults for Config Roots clinit.writeArrayValue(array, 1, buildTimeRunTimeDefaultValuesConfigSource); + // runtime default values (recorded during build time) + clinit.writeArrayValue(array, 2, clinit.readStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE)); + clinit.invokeVirtualMethod(SRCB_WITH_SOURCES, buildTimeBuilder, array); // add safe static sources for (String runtimeConfigSource : staticConfigSources) { @@ -595,24 +617,6 @@ public void run() { readConfig.invokeStaticMethod(CU_ADD_SOURCE_PROVIDER, runTimeBuilder, readConfig.newInstance( MethodDescriptor.ofConstructor("io.quarkus.runtime.generated.ConfigSourceProviderImpl"))); - // create the map for run time specified values config source - final ResultHandle specifiedRunTimeValues = clinit.newInstance(HM_NEW); - if (!liveReloadPossible) { - //we don't need these in devmode - //including it would just cache the first values - //but these can already just be read directly, as we are in the same JVM - for (Map.Entry entry : specifiedRunTimeDefaultValues.entrySet()) { - clinit.invokeVirtualMethod(HM_PUT, specifiedRunTimeValues, clinit.load(entry.getKey()), - clinit.load(entry.getValue())); - } - } - final ResultHandle specifiedRunTimeSource = clinit.newInstance(PCS_NEW, specifiedRunTimeValues, - clinit.load("Specified default values"), clinit.load(Integer.MIN_VALUE + 100)); - - cc.getFieldCreator(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE) - .setModifiers(Opcodes.ACC_STATIC | (liveReloadPossible ? Opcodes.ACC_VOLATILE : Opcodes.ACC_FINAL)); - clinit.writeStaticField(C_SPECIFIED_RUN_TIME_CONFIG_SOURCE, specifiedRunTimeSource); - // add in the custom sources that bootstrap config needs ResultHandle bootstrapConfigSourcesArray = null; if (bootstrapConfigSetupNeeded()) { diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSource.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSource.java deleted file mode 100644 index 9e8ef943f4141..0000000000000 --- a/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSource.java +++ /dev/null @@ -1,11 +0,0 @@ -package io.quarkus.config; - -import java.util.Map; - -import io.smallrye.config.common.MapBackedConfigSource; - -public class RuntimeInitConfigSource extends MapBackedConfigSource { - public RuntimeInitConfigSource() { - super("", Map.of("config.static.init.my-prop", "1234")); - } -} diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSourceFactory.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSourceFactory.java new file mode 100644 index 0000000000000..9b08c0bca2139 --- /dev/null +++ b/core/test-extension/deployment/src/test/java/io/quarkus/config/RuntimeInitConfigSourceFactory.java @@ -0,0 +1,25 @@ +package io.quarkus.config; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +import io.smallrye.config.ConfigSourceContext; +import io.smallrye.config.ConfigSourceFactory; +import io.smallrye.config.ConfigValue; +import io.smallrye.config.common.MapBackedConfigSource; + +public class RuntimeInitConfigSourceFactory implements ConfigSourceFactory { + @Override + public Iterable getConfigSources(final ConfigSourceContext context) { + ConfigValue value = context.getValue("skip.build.sources"); + if (value.getValue() != null && value.getValue().equals("true")) { + return List.of(new MapBackedConfigSource("RuntimeInitConfigSource", Map.of("config.static.init.my-prop", "1234")) { + }); + } else { + return Collections.emptyList(); + } + } +} diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingInvalidTest.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingInvalidTest.java index 8d38daab3c3c0..cb3bc0e8766af 100644 --- a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingInvalidTest.java +++ b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingInvalidTest.java @@ -14,9 +14,9 @@ public class StaticInitConfigMappingInvalidTest { @RegisterExtension static final QuarkusUnitTest TEST = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClass(RuntimeInitConfigSource.class) - .addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource", - RuntimeInitConfigSource.class.getName())) + .addClass(RuntimeInitConfigSourceFactory.class) + .addAsServiceProvider("io.smallrye.config.ConfigSourceFactory", + StaticInitConfigSourceFactory.class.getName())) .assertException(throwable -> { }); diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingTest.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingTest.java index 422201a2a4db1..267a922199d0f 100644 --- a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingTest.java +++ b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigMappingTest.java @@ -1,6 +1,7 @@ package io.quarkus.config; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import javax.inject.Inject; @@ -9,23 +10,27 @@ import io.quarkus.test.QuarkusUnitTest; import io.smallrye.config.ConfigMapping; +import io.smallrye.config.SmallRyeConfig; public class StaticInitConfigMappingTest { @RegisterExtension static final QuarkusUnitTest TEST = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClass(StaticInitConfigSource.class) - .addAsServiceProvider("org.eclipse.microprofile.config.spi.ConfigSource", - StaticInitConfigSource.class.getName())); + .addClass(StaticInitConfigSourceFactory.class) + .addAsServiceProvider("io.smallrye.config.ConfigSourceFactory", + StaticInitConfigSourceFactory.class.getName())); // This does not come from the static init Config, but it is registered in both static and runtime. // If it doesn't fail, it means that the static mapping was done correctly. @Inject StaticInitConfigMapping mapping; + @Inject + SmallRyeConfig config; @Test void staticInitMapping() { assertEquals("1234", mapping.myProp()); + assertTrue(config.getConfigSource("StaticInitConfigSource").isPresent()); } @ConfigMapping(prefix = "config.static.init") diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSource.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSource.java deleted file mode 100644 index 88071fa326e10..0000000000000 --- a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSource.java +++ /dev/null @@ -1,13 +0,0 @@ -package io.quarkus.config; - -import java.util.Map; - -import io.quarkus.runtime.annotations.StaticInitSafe; -import io.smallrye.config.common.MapBackedConfigSource; - -@StaticInitSafe -public class StaticInitConfigSource extends MapBackedConfigSource { - public StaticInitConfigSource() { - super("", Map.of("config.static.init.my-prop", "1234")); - } -} diff --git a/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSourceFactory.java b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSourceFactory.java new file mode 100644 index 0000000000000..7c621361ca005 --- /dev/null +++ b/core/test-extension/deployment/src/test/java/io/quarkus/config/StaticInitConfigSourceFactory.java @@ -0,0 +1,27 @@ +package io.quarkus.config; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +import io.quarkus.runtime.annotations.StaticInitSafe; +import io.smallrye.config.ConfigSourceContext; +import io.smallrye.config.ConfigSourceFactory; +import io.smallrye.config.ConfigValue; +import io.smallrye.config.common.MapBackedConfigSource; + +@StaticInitSafe +public class StaticInitConfigSourceFactory implements ConfigSourceFactory { + @Override + public Iterable getConfigSources(final ConfigSourceContext context) { + ConfigValue value = context.getValue("skip.build.sources"); + if (value.getValue() != null && value.getValue().equals("true")) { + return List.of(new MapBackedConfigSource("StaticInitConfigSource", Map.of("config.static.init.my-prop", "1234")) { + }); + } else { + return Collections.emptyList(); + } + } +}