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
359 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
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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 interface ConfigSourceInterceptorFactory { | ||
/** | ||
* Gets the {@link ConfigSourceInterceptor} from the ConfigSourceInterceptorFactory. Implementations of this | ||
* method must provide the instance of the {@link ConfigSourceInterceptor} to add into the Config Interceptor Chain. | ||
* | ||
* @param context the current {@link ConfigSourceInterceptorContext} with the interceptors already initialized. | ||
* @return the {@link ConfigSourceInterceptor} to add to Config Interceptor Chain and initialize. | ||
*/ | ||
ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context); | ||
|
||
/** | ||
* Gets the Class of the Interceptor. | ||
* | ||
* This is required, because the interceptor priority needs to be sorted before doing initialization. | ||
* | ||
* @return the Class of the {@link ConfigSourceInterceptor} | ||
*/ | ||
Class<? extends ConfigSourceInterceptor> getInterceptorClass(); | ||
|
||
final class Delegate implements 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.