Skip to content

Commit

Permalink
Merge pull request #30964 from radcortez/fix-29630
Browse files Browse the repository at this point in the history
Add ConfigMappings from a builder class to support full hot reload
  • Loading branch information
gsmet authored Feb 8, 2023
2 parents af74ebb + 4cf5e3b commit e92c482
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,6 @@ public static final class GenerateOperation implements AutoCloseable {
clinit.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, buildTimeBuilder,
clinit.newInstance(RCSF_NEW, clinit.load(discoveredConfigSourceFactory)));
}
// add mappings
for (ConfigClassWithPrefix configMapping : staticConfigMappings) {
clinit.invokeStaticMethod(CU_WITH_MAPPING, buildTimeBuilder,
clinit.load(configMapping.getKlass().getName()), clinit.load(configMapping.getPrefix()));
}

// additional config builders
ResultHandle configBuilders = clinit.newInstance(AL_NEW);
Expand Down Expand Up @@ -468,11 +463,12 @@ public void run() {
reinit.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, buildTimeBuilder,
reinit.newInstance(RCSF_NEW, reinit.load(discoveredConfigSourceFactory)));
}
// add mappings
for (ConfigClassWithPrefix configMapping : staticConfigMappings) {
reinit.invokeStaticMethod(CU_WITH_MAPPING, buildTimeBuilder,
reinit.load(configMapping.getKlass().getName()), reinit.load(configMapping.getPrefix()));
// 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);

ResultHandle clinitConfig = reinit.checkCast(reinit.invokeVirtualMethod(SRCB_BUILD, buildTimeBuilder),
SmallRyeConfig.class);
Expand Down Expand Up @@ -555,12 +551,6 @@ public void run() {
readBootstrapConfig.invokeStaticMethod(CU_ADD_SOURCE_FACTORY_PROVIDER, bootstrapBuilder,
readBootstrapConfig.newInstance(RCSF_NEW, readBootstrapConfig.load(discoveredConfigSourceFactory)));
}
// add bootstrap mappings
for (ConfigClassWithPrefix configMapping : staticConfigMappings) {
readBootstrapConfig.invokeStaticMethod(CU_WITH_MAPPING, bootstrapBuilder,
readBootstrapConfig.load(configMapping.getKlass().getName()),
readBootstrapConfig.load(configMapping.getPrefix()));
}

// add bootstrap config builders
ResultHandle bootstrapConfigBuilders = readBootstrapConfig.newInstance(AL_NEW);
Expand Down Expand Up @@ -649,12 +639,6 @@ public void run() {
readConfig.newInstance(RCSF_NEW, readConfig.load(discoveredConfigSourceFactory)));
}

// add mappings
for (ConfigClassWithPrefix configMapping : runtimeConfigMappings) {
readConfig.invokeStaticMethod(CU_WITH_MAPPING, runTimeBuilder,
readConfig.load(configMapping.getKlass().getName()), readConfig.load(configMapping.getPrefix()));
}

// additional config builders
ResultHandle configBuilders = readConfig.newInstance(AL_NEW);
for (String configBuilder : runtimeConfigBuilders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public class ConfigGenerationBuildStep {
SmallRyeConfigBuilder.class, "withSources",
SmallRyeConfigBuilder.class, ConfigSource[].class);

private static final MethodDescriptor WITH_MAPPING = MethodDescriptor.ofMethod(
SmallRyeConfigBuilder.class, "withMapping",
SmallRyeConfigBuilder.class, Class.class, String.class);

@BuildStep
void staticInitSources(
BuildProducer<StaticInitConfigSourceProviderBuildItem> staticInitConfigSourceProviderBuildItem,
Expand Down Expand Up @@ -217,6 +221,33 @@ void extensionMappings(ConfigurationBuildItem configItem,
}
}

@BuildStep
void builderMappings(
ConfigurationBuildItem configItem,
List<ConfigMappingBuildItem> configMappings,
BuildProducer<GeneratedClassBuildItem> generatedClass,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
BuildProducer<StaticInitConfigBuilderBuildItem> staticInitConfigBuilder,
BuildProducer<RunTimeConfigBuilderBuildItem> runTimeConfigBuilder) {

// For Static Init Config
Set<ConfigClassWithPrefix> 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));

// For RunTime Config
Set<ConfigClassWithPrefix> 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));
}

/**
* Generate the Config class that instantiates MP Config and holds all the config objects
*/
Expand Down Expand Up @@ -256,6 +287,7 @@ void generateConfigClass(
staticConfigSourceFactories.addAll(staticInitConfigSourceFactories.stream()
.map(StaticInitConfigSourceFactoryBuildItem::getFactoryClassName).collect(Collectors.toSet()));

// TODO - duplicated now builderMappings. Still required to filter the unknown properties
Set<ConfigClassWithPrefix> staticMappings = new HashSet<>();
staticMappings.addAll(staticSafeConfigMappings(configMappings));
staticMappings.addAll(configItem.getReadResult().getBuildTimeRunTimeMappings());
Expand Down Expand Up @@ -484,6 +516,34 @@ private static void generateDefaultsConfigSource(
reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, false, className));
}

private static void generateMappingsConfigBuilder(
BuildProducer<GeneratedClassBuildItem> generatedClass,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
String className,
Set<ConfigClassWithPrefix> mappings) {

try (ClassCreator classCreator = ClassCreator.builder()
.classOutput(new GeneratedClassGizmoAdaptor(generatedClass, true))
.className(className)
.interfaces(ConfigBuilder.class)
.setFinal(true)
.build()) {

MethodCreator method = classCreator.getMethodCreator(CONFIG_BUILDER);
ResultHandle configBuilder = method.getMethodParam(0);

for (ConfigClassWithPrefix mapping : mappings) {
method.invokeVirtualMethod(WITH_MAPPING, configBuilder,
method.loadClass(mapping.getKlass()),
method.load(mapping.getPrefix()));
}

method.returnValue(configBuilder);
}

reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, false, className));
}

private static Set<String> discoverService(
Class<?> serviceClass,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void secrets() {
}

@ConfigMapping(prefix = "secrets.my")
interface MappingSecret {
public interface MappingSecret {
String secret();
}
}

0 comments on commit e92c482

Please sign in to comment.