-
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.
Fixes #274. Added interceptor to activate a Configuration Profile.
- Loading branch information
Showing
11 changed files
with
419 additions
and
59 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
37 changes: 37 additions & 0 deletions
37
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,37 @@ | ||
package io.smallrye.config; | ||
|
||
import java.util.OptionalInt; | ||
|
||
/** | ||
* 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 { | ||
/** | ||
* The default priority value, {@code 100}. | ||
*/ | ||
int DEFAULT_PRIORITY = 100; | ||
|
||
/** | ||
* 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); | ||
|
||
/** | ||
* Returns the interceptor priority. This is required, because the interceptor priority needs to be sorted | ||
* before doing initialization. | ||
* | ||
* @return the priority value. | ||
*/ | ||
default OptionalInt getPriority() { | ||
return OptionalInt.empty(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
package io.smallrye.config; | ||
|
||
import java.util.Comparator; | ||
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 static final Comparator<ConfigValue> CONFIG_SOURCE_COMPARATOR = (o1, o2) -> { | ||
int res = Integer.compare(o2.getConfigSourceOrdinal(), o1.getConfigSourceOrdinal()); | ||
if (res != 0) { | ||
return res; | ||
} | ||
|
||
if (o1.getConfigSourceName() != null && o2.getConfigSourceName() != null) { | ||
return o2.getConfigSourceName().compareTo(o1.getConfigSourceName()); | ||
} else { | ||
return res; | ||
} | ||
}; | ||
|
||
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); | ||
if (originalValue != null && CONFIG_SOURCE_COMPARATOR.compare(profileValue, originalValue) > 0) { | ||
return originalValue; | ||
} else { | ||
return 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
Oops, something went wrong.