Skip to content

Commit

Permalink
Add API to register customizers directly with the builder
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Aug 10, 2023
1 parent f318ddb commit 682e0e9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public SmallRyeConfig getConfigFor(

return configProviderResolver.getBuilder()
.forClassLoader(classLoader)
.addDiscoveredCustomizers()
.addDefaultSources()
.addDefaultInterceptors()
.addDiscoveredSources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SmallRyeConfigBuilderCustomizer> customizers = new ArrayList<>();
// sources are not sorted by their ordinals
private final List<ConfigSource> sources = new ArrayList<>();
private final List<ConfigSourceProvider> sourceProviders = new ArrayList<>();
Expand All @@ -69,6 +70,7 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
private final KeyMap<String> 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;
Expand All @@ -80,6 +82,11 @@ public class SmallRyeConfigBuilder implements ConfigBuilder {
public SmallRyeConfigBuilder() {
}

public SmallRyeConfigBuilder addDiscoveredCustomizers() {
addDiscoveredCustomizers = true;
return this;
}

@Override
public SmallRyeConfigBuilder addDiscoveredSources() {
addDiscoveredSources = true;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -577,6 +589,10 @@ ClassLoader getClassLoader() {
return classLoader;
}

public boolean isAddDiscoveredCustomizers() {
return addDiscoveredCustomizers;
}

public boolean isAddDefaultSources() {
return addDefaultSources;
}
Expand Down Expand Up @@ -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<SmallRyeConfigBuilderCustomizer> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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 {
Expand All @@ -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"));
Expand Down

0 comments on commit 682e0e9

Please sign in to comment.