Polish profile expression support
Issue: SPR-12458
This commit is contained in:
@@ -73,7 +73,7 @@ public interface Environment extends PropertyResolver {
|
||||
/**
|
||||
* Return the set of profiles explicitly made active for this environment. Profiles
|
||||
* are used for creating logical groupings of bean definitions to be registered
|
||||
* conditionally, for example based on deployment environment. Profiles can be
|
||||
* conditionally, for example based on deployment environment. Profiles can be
|
||||
* activated by setting {@linkplain AbstractEnvironment#ACTIVE_PROFILES_PROPERTY_NAME
|
||||
* "spring.profiles.active"} as a system property or by calling
|
||||
* {@link ConfigurableEnvironment#setActiveProfiles(String...)}.
|
||||
@@ -98,11 +98,11 @@ public interface Environment extends PropertyResolver {
|
||||
* Return whether one or more of the given profiles is active or, in the case of no
|
||||
* explicit active profiles, whether one or more of the given profiles is included in
|
||||
* the set of default profiles. If a profile begins with '!' the logic is inverted,
|
||||
* i.e. the method will return true if the given profile is <em>not</em> active.
|
||||
* For example, <pre class="code">env.acceptsProfiles("p1", "!p2")</pre> will
|
||||
* return {@code true} if profile 'p1' is active or 'p2' is not active.
|
||||
* i.e. the method will return {@code true} if the given profile is <em>not</em> active.
|
||||
* For example, {@code env.acceptsProfiles("p1", "!p2")} will return {@code true} if
|
||||
* profile 'p1' is active or 'p2' is not active.
|
||||
* @throws IllegalArgumentException if called with zero arguments
|
||||
* or if any profile is {@code null}, empty or whitespace-only
|
||||
* or if any profile is {@code null}, empty, or whitespace only
|
||||
* @see #getActiveProfiles
|
||||
* @see #getDefaultProfiles
|
||||
* @see #acceptsProfiles(Profiles)
|
||||
@@ -112,7 +112,8 @@ public interface Environment extends PropertyResolver {
|
||||
boolean acceptsProfiles(String... profiles);
|
||||
|
||||
/**
|
||||
* Return whether the active profiles match the given {@link Profiles} predicate.
|
||||
* Return whether the {@linkplain #getActiveProfiles() active profiles}
|
||||
* match the given {@link Profiles} predicate.
|
||||
*/
|
||||
boolean acceptsProfiles(Profiles profiles);
|
||||
|
||||
|
||||
@@ -19,11 +19,11 @@ package org.springframework.core.env;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Profile predicate that may be {@link Environment#acceptsProfiles(Profiles) accepted} by
|
||||
* an {@link Environment}.
|
||||
* <p>
|
||||
* May be implemented directly or, more usually, created using the {@link #of(String...)
|
||||
* of(...)} factory method.
|
||||
* Profile predicate that may be {@linkplain Environment#acceptsProfiles(Profiles)
|
||||
* accepted} by an {@link Environment}.
|
||||
*
|
||||
* <p>May be implemented directly or, more usually, created using the
|
||||
* {@link #of(String...) of(...)} factory method.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 5.1
|
||||
@@ -32,31 +32,36 @@ import java.util.function.Predicate;
|
||||
public interface Profiles {
|
||||
|
||||
/**
|
||||
* Test if this profile predicate matches against given active profiles.
|
||||
* @param activeProfiles test whether a given profile is currently active
|
||||
* Test if this profile predicate matches against the given active profiles
|
||||
* predicate.
|
||||
* @param activeProfiles predicate that tests whether a given profile is
|
||||
* currently active
|
||||
*/
|
||||
boolean matches(Predicate<String> activeProfiles);
|
||||
|
||||
/**
|
||||
* Return a new {@link Profiles} instance that checks for matches against the given
|
||||
* profile strings. The returned instance will
|
||||
* {@link Profiles#matches(Predicate)} match} if any one of the given profile strings
|
||||
* match.
|
||||
* <p>
|
||||
* A profile string may contains a simple profile name (for example
|
||||
* {@code "production"}) or a profile expression. A profile expression allows for more
|
||||
* complicated profile logic to be expressed, for example
|
||||
* profile strings.
|
||||
*
|
||||
* <p>The returned instance will {@linkplain Profiles#matches(Predicate) match}
|
||||
* if any one of the given profile strings matches.
|
||||
*
|
||||
* <p>A profile string may contain a simple profile name (for example
|
||||
* {@code "production"}) or a profile expression. A profile expression allows
|
||||
* for more complicated profile logic to be expressed, for example
|
||||
* {@code "production & cloud"}.
|
||||
* <p>
|
||||
* The following operators are supported in profile expressions:
|
||||
*
|
||||
* <p>The following operators are supported in profile expressions:
|
||||
* <ul>
|
||||
* <li>{@code !} - A logical <em>not</em> of the profile</li>
|
||||
* <li>{@code &} - A logical <em>and</em> of the profiles</li>
|
||||
* <li>{@code |} - A logical <em>or</em> of the profiles</li></li>
|
||||
* <p>
|
||||
* Please note that the {@code &} and {@code |} operators may not be mixed without
|
||||
* using parentheses. For example {@code "a & b | c"} is not a valid expression, it
|
||||
* must be expressed as {@code "(a & b) | c"}.
|
||||
* <li>{@code |} - A logical <em>or</em> of the profiles</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Please note that the {@code &} and {@code |} operators may not be mixed
|
||||
* without using parentheses. For example {@code "a & b | c"} is not a valid
|
||||
* expression; it must be expressed as {@code "(a & b) | c"} or
|
||||
* {@code "a & (b | c)"}.
|
||||
*
|
||||
* @param profiles the profiles to include
|
||||
* @return a new {@link Profiles} instance
|
||||
|
||||
@@ -19,7 +19,6 @@ package org.springframework.core.env;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -30,6 +29,7 @@ import org.springframework.util.StringUtils;
|
||||
* Internal parser used by {@link Profiles#of}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 5.1
|
||||
*/
|
||||
class ProfilesParser {
|
||||
|
||||
@@ -43,7 +43,7 @@ class ProfilesParser {
|
||||
}
|
||||
|
||||
private static Profiles parseExpression(String expression) {
|
||||
Assert.hasText(expression,
|
||||
Assert.hasText(expression, () ->
|
||||
"Invalid profile expression [" + expression + "]: must contain text");
|
||||
StringTokenizer tokens = new StringTokenizer(expression, "()&|!", true);
|
||||
return parseTokens(expression, tokens);
|
||||
@@ -97,7 +97,7 @@ class ProfilesParser {
|
||||
|
||||
private static void assertWellFormed(String expression, boolean wellFormed) {
|
||||
Assert.isTrue(wellFormed,
|
||||
() -> "Malformed profile expression '" + expression + "'");
|
||||
() -> "Malformed profile expression [" + expression + "]");
|
||||
}
|
||||
|
||||
private static Profiles or(Profiles... profiles) {
|
||||
@@ -122,7 +122,7 @@ class ProfilesParser {
|
||||
return (profiles) -> profiles.matches(activeProfile);
|
||||
}
|
||||
|
||||
enum Operator {
|
||||
private enum Operator {
|
||||
AND,
|
||||
OR
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user