-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to customize the SmallRyeConfigBuilder via ServiceLoader (#975
- Loading branch information
Showing
6 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
implementation/src/main/java/io/smallrye/config/SmallRyeConfigBuilderCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.smallrye.config; | ||
|
||
/** | ||
* This {@code SmallRyeConfigBuilderCustomizer} allows to customize a {@link SmallRyeConfigBuilder}, used to create | ||
* a {@link SmallRyeConfig} instance. | ||
* <p> | ||
* Instances of this interface will be discovered via the {@link java.util.ServiceLoader} mechanism and can be | ||
* registered by providing a {@code META-INF/services/io.smallrye.config.SmallRyeConfigBuilderCustomizer} which | ||
* contains the fully qualified class name of the custom {@link SmallRyeConfigBuilderCustomizer} implementation. | ||
*/ | ||
public interface SmallRyeConfigBuilderCustomizer { | ||
/** | ||
* Customize the current {@link SmallRyeConfigBuilder}. | ||
* | ||
* @param builder the current {@link SmallRyeConfigBuilder}. | ||
*/ | ||
void configBuilder(SmallRyeConfigBuilder builder); | ||
|
||
/** | ||
* Returns the customizer priority. Customizers are sorted by ascending priority and executed in that order. | ||
* | ||
* @return the priority value. | ||
*/ | ||
default int priority() { | ||
return 0; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
testsuite/extra/src/test/java/io/smallrye/config/test/builder/CustomConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.smallrye.config.test.builder; | ||
|
||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.SmallRyeConfigBuilderCustomizer; | ||
|
||
public class CustomConfigBuilder implements SmallRyeConfigBuilderCustomizer { | ||
@Override | ||
public void configBuilder(final SmallRyeConfigBuilder builder) { | ||
builder.withDefaultValue("from.custom.builder", "1234"); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
testsuite/extra/src/test/java/io/smallrye/config/test/builder/CustomOneConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.smallrye.config.test.builder; | ||
|
||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.SmallRyeConfigBuilderCustomizer; | ||
|
||
public class CustomOneConfigBuilder implements SmallRyeConfigBuilderCustomizer { | ||
@Override | ||
public void configBuilder(final SmallRyeConfigBuilder builder) { | ||
builder.withDefaultValue("one", "one").addDefaultSources(); | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 1; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
testsuite/extra/src/test/java/io/smallrye/config/test/builder/CustomTwoConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.smallrye.config.test.builder; | ||
|
||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.SmallRyeConfigBuilderCustomizer; | ||
|
||
public class CustomTwoConfigBuilder implements SmallRyeConfigBuilderCustomizer { | ||
@Override | ||
public void configBuilder(final SmallRyeConfigBuilder builder) { | ||
builder.withDefaultValue("one", "two"); | ||
if (builder.isAddDefaultSources()) { | ||
builder.withDefaultValue("addDefaultSources", "true"); | ||
} | ||
} | ||
|
||
@Override | ||
public int priority() { | ||
return 2; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...ra/src/test/java/io/smallrye/config/test/builder/SmallRyeConfigBuilderCustomizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.smallrye.config.test.builder; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.jboss.shrinkwrap.api.exporter.ZipExporter; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
|
||
public class SmallRyeConfigBuilderCustomizerTest { | ||
@Test | ||
void builder(@TempDir Path tempDir) throws Exception { | ||
JavaArchive serviceJar = ShrinkWrap | ||
.create(JavaArchive.class, "service.jar") | ||
.addAsManifestResource(new StringAsset("io.smallrye.config.test.builder.CustomConfigBuilder"), | ||
"services/io.smallrye.config.SmallRyeConfigBuilderCustomizer"); | ||
|
||
Path servidePath = tempDir.resolve("resources-one.jar"); | ||
serviceJar.as(ZipExporter.class).exportTo(servidePath.toFile()); | ||
|
||
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
|
||
try (URLClassLoader urlClassLoader = urlClassLoader(contextClassLoader, "jar:" + servidePath.toUri() + "!/")) { | ||
Thread.currentThread().setContextClassLoader(urlClassLoader); | ||
|
||
SmallRyeConfig config = new SmallRyeConfigBuilder().build(); | ||
|
||
assertEquals("1234", config.getRawValue("from.custom.builder")); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(contextClassLoader); | ||
} | ||
} | ||
|
||
@Test | ||
void priority(@TempDir Path tempDir) throws Exception { | ||
JavaArchive serviceJar = ShrinkWrap | ||
.create(JavaArchive.class, "service.jar") | ||
.addAsManifestResource(new StringAsset( | ||
"io.smallrye.config.test.builder.CustomOneConfigBuilder\n" + | ||
"io.smallrye.config.test.builder.CustomTwoConfigBuilder\n"), | ||
"services/io.smallrye.config.SmallRyeConfigBuilderCustomizer"); | ||
|
||
Path servidePath = tempDir.resolve("resources-one.jar"); | ||
serviceJar.as(ZipExporter.class).exportTo(servidePath.toFile()); | ||
|
||
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); | ||
|
||
try (URLClassLoader urlClassLoader = urlClassLoader(contextClassLoader, "jar:" + servidePath.toUri() + "!/")) { | ||
Thread.currentThread().setContextClassLoader(urlClassLoader); | ||
|
||
SmallRyeConfig config = new SmallRyeConfigBuilder().build(); | ||
|
||
assertEquals("two", config.getRawValue("one")); | ||
assertEquals("true", config.getRawValue("addDefaultSources")); | ||
} finally { | ||
Thread.currentThread().setContextClassLoader(contextClassLoader); | ||
} | ||
} | ||
|
||
private static URLClassLoader urlClassLoader(ClassLoader parent, String... urls) { | ||
return new URLClassLoader(Stream.of(urls).map(spec -> { | ||
try { | ||
return new URL(spec); | ||
} catch (MalformedURLException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
}).toArray(URL[]::new), parent); | ||
} | ||
} |