-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for #196. Support configurable Config Sources.
- Loading branch information
Showing
10 changed files
with
191 additions
and
68 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
implementation/src/main/java/io/smallrye/config/ConfigSourceContext.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,8 @@ | ||
package io.smallrye.config; | ||
|
||
import io.smallrye.common.annotation.Experimental; | ||
|
||
@Experimental("") | ||
public interface ConfigSourceContext { | ||
ConfigValue getValue(String name); | ||
} |
16 changes: 16 additions & 0 deletions
16
implementation/src/main/java/io/smallrye/config/ConfigSourceFactory.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; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.smallrye.common.annotation.Experimental; | ||
|
||
@Experimental("") | ||
public interface ConfigSourceFactory { | ||
ConfigSource getSource(ConfigSourceContext context); | ||
|
||
default OptionalInt getPriority() { | ||
return OptionalInt.empty(); | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
implementation/src/test/java/io/smallrye/config/ConfigConfigSourceTest.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,94 @@ | ||
package io.smallrye.config; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.OptionalInt; | ||
import java.util.stream.StreamSupport; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.smallrye.config.common.AbstractConfigSource; | ||
|
||
public class ConfigConfigSourceTest { | ||
@Test | ||
public void configure() { | ||
final SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDefaultInterceptors() | ||
.withSources(KeyValuesConfigSource.config("my.prop", "1234")) | ||
.withSources(new ConfigSourceFactory() { | ||
@Override | ||
public ConfigSource getSource(final ConfigSourceContext context) { | ||
return new AbstractConfigSource("test", 1000) { | ||
final String value = context.getValue("my.prop").getValue(); | ||
|
||
@Override | ||
public Map<String, String> getProperties() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getValue(final String propertyName) { | ||
return value; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public OptionalInt getPriority() { | ||
return OptionalInt.of(1000); | ||
} | ||
}) | ||
.build(); | ||
|
||
final List<ConfigSource> configSources = StreamSupport.stream(config.getConfigSources().spliterator(), false) | ||
.collect(toList()); | ||
assertEquals(1, configSources.stream().filter(source -> source.getName().equals("test")).count()); | ||
|
||
assertEquals("1234", config.getRawValue("my.prop")); | ||
assertEquals("test", config.getConfigValue("my.prop").getConfigSourceName()); | ||
assertEquals("1234", config.getRawValue("any")); | ||
assertEquals("test", config.getConfigValue("any").getConfigSourceName()); | ||
} | ||
|
||
@Test | ||
public void lowerPriority() { | ||
final SmallRyeConfig config = new SmallRyeConfigBuilder() | ||
.addDefaultSources() | ||
.addDefaultInterceptors() | ||
.withSources(KeyValuesConfigSource.config("my.prop", "1234")) | ||
.withSources(new ConfigSourceFactory() { | ||
@Override | ||
public ConfigSource getSource(final ConfigSourceContext context) { | ||
return new AbstractConfigSource("test", 0) { | ||
final ConfigValue value = context.getValue("my.prop"); | ||
|
||
@Override | ||
public Map<String, String> getProperties() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getValue(final String propertyName) { | ||
return value != null ? value.getValue() : null; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public OptionalInt getPriority() { | ||
return OptionalInt.of(0); | ||
} | ||
}) | ||
.build(); | ||
|
||
assertEquals("1234", config.getRawValue("my.prop")); | ||
assertEquals("KeyValuesConfigSource", config.getConfigValue("my.prop").getConfigSourceName()); | ||
assertNull(config.getRawValue("any")); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...zookeeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigException.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
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
21 changes: 21 additions & 0 deletions
21
...eeper/src/main/java/io/smallrye/config/source/zookeeper/ZooKeeperConfigSourceFactory.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,21 @@ | ||
package io.smallrye.config.source.zookeeper; | ||
|
||
import java.util.OptionalInt; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.smallrye.config.ConfigSourceContext; | ||
import io.smallrye.config.ConfigSourceFactory; | ||
|
||
public class ZooKeeperConfigSourceFactory implements ConfigSourceFactory { | ||
@Override | ||
public ConfigSource getSource(final ConfigSourceContext context) { | ||
return new ZooKeeperConfigSource(context.getValue(ZooKeeperConfigSource.ZOOKEEPER_URL_KEY).getValue(), | ||
context.getValue(ZooKeeperConfigSource.APPLICATION_ID_KEY).getValue()); | ||
} | ||
|
||
@Override | ||
public OptionalInt getPriority() { | ||
return OptionalInt.of(150); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ces/zookeeper/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceFactory
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 @@ | ||
io.smallrye.config.source.zookeeper.ZooKeeperConfigSourceFactory |
1 change: 0 additions & 1 deletion
1
...per/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource
This file was deleted.
Oops, something went wrong.