From 682e0e9ee46b7317858cebef3ef113f706506349 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Thu, 10 Aug 2023 15:51:40 +0100 Subject: [PATCH] Add API to register customizers directly with the builder --- .../mapping/MappingSmallRyeConfigFactory.java | 1 + .../config/SmallRyeConfigBuilder.java | 30 +++++++++++++++---- .../config/SmallRyeConfigFactory.java | 1 + .../SmallRyeConfigBuilderCustomizerTest.java | 15 ++++++++-- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/examples/mapping/src/main/java/io/smallrye/config/examples/mapping/MappingSmallRyeConfigFactory.java b/examples/mapping/src/main/java/io/smallrye/config/examples/mapping/MappingSmallRyeConfigFactory.java index 66ff872e4..f4d8e4880 100644 --- a/examples/mapping/src/main/java/io/smallrye/config/examples/mapping/MappingSmallRyeConfigFactory.java +++ b/examples/mapping/src/main/java/io/smallrye/config/examples/mapping/MappingSmallRyeConfigFactory.java @@ -11,6 +11,7 @@ public SmallRyeConfig getConfigFor( return configProviderResolver.getBuilder() .forClassLoader(classLoader) + .addDiscoveredCustomizers() .addDefaultSources() .addDefaultInterceptors() .addDiscoveredSources() diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java index 99a3a01cc..a48e15981 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilder.java @@ -57,6 +57,7 @@ public class SmallRyeConfigBuilder implements ConfigBuilder { public static final String META_INF_MICROPROFILE_CONFIG_PROPERTIES = "META-INF/microprofile-config.properties"; + private final List customizers = new ArrayList<>(); // sources are not sorted by their ordinals private final List sources = new ArrayList<>(); private final List sourceProviders = new ArrayList<>(); @@ -69,6 +70,7 @@ public class SmallRyeConfigBuilder implements ConfigBuilder { private final KeyMap defaultValues = new KeyMap<>(); private final ConfigMappingProvider.Builder mappingsBuilder = ConfigMappingProvider.builder(); private ClassLoader classLoader = SecuritySupport.getContextClassLoader(); + private boolean addDiscoveredCustomizers = false; private boolean addDefaultSources = false; private boolean addDefaultInterceptors = false; private boolean addDiscoveredSources = false; @@ -80,6 +82,11 @@ public class SmallRyeConfigBuilder implements ConfigBuilder { public SmallRyeConfigBuilder() { } + public SmallRyeConfigBuilder addDiscoveredCustomizers() { + addDiscoveredCustomizers = true; + return this; + } + @Override public SmallRyeConfigBuilder addDiscoveredSources() { addDiscoveredSources = true; @@ -410,6 +417,11 @@ public SmallRyeConfigBuilder forClassLoader(ClassLoader classLoader) { return this; } + public SmallRyeConfigBuilder withCustomizers(SmallRyeConfigBuilderCustomizer... customizers) { + Collections.addAll(this.customizers, customizers); + return this; + } + @Override public SmallRyeConfigBuilder withSources(ConfigSource... configSources) { Collections.addAll(sources, configSources); @@ -577,6 +589,10 @@ ClassLoader getClassLoader() { return classLoader; } + public boolean isAddDiscoveredCustomizers() { + return addDiscoveredCustomizers; + } + public boolean isAddDefaultSources() { return addDefaultSources; } @@ -642,16 +658,20 @@ public SmallRyeConfigBuilder setAddDiscoveredValidator(final boolean addDiscover @Override public SmallRyeConfig build() { - ConfigMappingProvider mappingProvider = mappingsBuilder.build(); - defaultValues.putAll(mappingProvider.getDefaultValues()); + if (addDiscoveredCustomizers) { + for (SmallRyeConfigBuilderCustomizer customizer : ServiceLoader.load(SmallRyeConfigBuilderCustomizer.class, + classLoader)) { + customizers.add(customizer); + } + } - ServiceLoader customizers = ServiceLoader.load(SmallRyeConfigBuilderCustomizer.class, - classLoader); customizers.stream() - .map(ServiceLoader.Provider::get) .sorted(Comparator.comparingInt(SmallRyeConfigBuilderCustomizer::priority)) .forEach(customizer -> customizer.configBuilder(SmallRyeConfigBuilder.this)); + ConfigMappingProvider mappingProvider = mappingsBuilder.build(); + defaultValues.putAll(mappingProvider.getDefaultValues()); + SmallRyeConfig config = new SmallRyeConfig(this); ConfigMappings.mapConfiguration(config, mappingProvider); return config; diff --git a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigFactory.java b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigFactory.java index f45ac2ad3..b7bd6be68 100644 --- a/implementation/src/main/java/io/smallrye/config/SmallRyeConfigFactory.java +++ b/implementation/src/main/java/io/smallrye/config/SmallRyeConfigFactory.java @@ -51,6 +51,7 @@ static final class Default extends SmallRyeConfigFactory { public SmallRyeConfig getConfigFor(SmallRyeConfigProviderResolver configProviderResolver, ClassLoader classLoader) { return configProviderResolver.getBuilder().forClassLoader(classLoader) + .addDiscoveredCustomizers() .addDefaultSources() .addDefaultInterceptors() .addDiscoveredSources() diff --git a/testsuite/extra/src/test/java/io/smallrye/config/test/builder/SmallRyeConfigBuilderCustomizerTest.java b/testsuite/extra/src/test/java/io/smallrye/config/test/builder/SmallRyeConfigBuilderCustomizerTest.java index f4f5cfe41..acdffa383 100644 --- a/testsuite/extra/src/test/java/io/smallrye/config/test/builder/SmallRyeConfigBuilderCustomizerTest.java +++ b/testsuite/extra/src/test/java/io/smallrye/config/test/builder/SmallRyeConfigBuilderCustomizerTest.java @@ -20,7 +20,16 @@ public class SmallRyeConfigBuilderCustomizerTest { @Test - void builder(@TempDir Path tempDir) throws Exception { + void builder() { + SmallRyeConfig config = new SmallRyeConfigBuilder() + .withCustomizers(new CustomConfigBuilder()) + .build(); + + assertEquals("1234", config.getRawValue("from.custom.builder")); + } + + @Test + void discoveredBuilder(@TempDir Path tempDir) throws Exception { JavaArchive serviceJar = ShrinkWrap .create(JavaArchive.class, "service.jar") .addAsManifestResource(new StringAsset("io.smallrye.config.test.builder.CustomConfigBuilder"), @@ -34,7 +43,7 @@ void builder(@TempDir Path tempDir) throws Exception { try (URLClassLoader urlClassLoader = urlClassLoader(contextClassLoader, "jar:" + servidePath.toUri() + "!/")) { Thread.currentThread().setContextClassLoader(urlClassLoader); - SmallRyeConfig config = new SmallRyeConfigBuilder().build(); + SmallRyeConfig config = new SmallRyeConfigBuilder().addDiscoveredCustomizers().build(); assertEquals("1234", config.getRawValue("from.custom.builder")); } finally { @@ -59,7 +68,7 @@ void priority(@TempDir Path tempDir) throws Exception { try (URLClassLoader urlClassLoader = urlClassLoader(contextClassLoader, "jar:" + servidePath.toUri() + "!/")) { Thread.currentThread().setContextClassLoader(urlClassLoader); - SmallRyeConfig config = new SmallRyeConfigBuilder().build(); + SmallRyeConfig config = new SmallRyeConfigBuilder().addDiscoveredCustomizers().build(); assertEquals("two", config.getRawValue("one")); assertEquals("true", config.getRawValue("addDefaultSources"));