forked from smallrye/smallrye-config
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes smallrye#274. Added interceptor to activate a Configuration Pro…
…file.
- Loading branch information
Showing
11 changed files
with
345 additions
and
60 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
33 changes: 33 additions & 0 deletions
33
implementation/src/main/java/io/smallrye/config/ConfigSourceInterceptorFactory.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,33 @@ | ||
package io.smallrye.config; | ||
|
||
/** | ||
* This ConfigSourceInterceptorFactory allows to initialize a {@link ConfigSourceInterceptor}, with access to the | ||
* current {@link ConfigSourceInterceptorContext}. | ||
* | ||
* Interceptors in the chain are initialized in priority order and the current | ||
* {@link ConfigSourceInterceptorContext} contains the current interceptor, plus all other interceptors already | ||
* initialized. | ||
*/ | ||
public abstract class ConfigSourceInterceptorFactory { | ||
public abstract ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context); | ||
|
||
public abstract Class<? extends ConfigSourceInterceptor> getInterceptorClass(); | ||
|
||
static final class Delegate extends ConfigSourceInterceptorFactory { | ||
private final ConfigSourceInterceptor interceptor; | ||
|
||
Delegate(final ConfigSourceInterceptor interceptor) { | ||
this.interceptor = interceptor; | ||
} | ||
|
||
@Override | ||
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) { | ||
return interceptor; | ||
} | ||
|
||
@Override | ||
public Class<? extends ConfigSourceInterceptor> getInterceptorClass() { | ||
return interceptor.getClass(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
implementation/src/main/java/io/smallrye/config/ProfileConfigSourceInterceptor.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,40 @@ | ||
package io.smallrye.config; | ||
|
||
import java.util.Optional; | ||
|
||
import javax.annotation.Priority; | ||
|
||
@Priority(600) | ||
public class ProfileConfigSourceInterceptor implements ConfigSourceInterceptor { | ||
public static final String SMALLRYE_PROFILE = "smallrye.config.profile"; | ||
|
||
private final String profile; | ||
|
||
public ProfileConfigSourceInterceptor(final String profile) { | ||
this.profile = profile; | ||
} | ||
|
||
public ProfileConfigSourceInterceptor(final ConfigSourceInterceptorContext context) { | ||
this(context, SMALLRYE_PROFILE); | ||
} | ||
|
||
public ProfileConfigSourceInterceptor( | ||
final ConfigSourceInterceptorContext context, | ||
final String profileConfigName) { | ||
this.profile = Optional.ofNullable(context.proceed(profileConfigName)).map(ConfigValue::getValue).orElse(null); | ||
} | ||
|
||
@Override | ||
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) { | ||
if (profile != null) { | ||
final ConfigValue profileValue = context.proceed("%" + profile + "." + name); | ||
if (profileValue != null) { | ||
final ConfigValue originalValue = context.proceed(name); | ||
return originalValue.getConfigSourceOrdinal() > profileValue.getConfigSourceOrdinal() ? originalValue | ||
: profileValue; | ||
} | ||
} | ||
|
||
return context.proceed(name); | ||
} | ||
} |
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
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
36 changes: 0 additions & 36 deletions
36
implementation/src/test/java/io/smallrye/config/ConfigSourceProfileInterceptorTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.