Polish "Add profile expression support"
Issue: SPR-12458
This commit is contained in:
@@ -322,6 +322,23 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public boolean acceptsProfiles(String... profiles) {
|
||||
Assert.notEmpty(profiles, "Must specify at least one profile");
|
||||
for (String profile : profiles) {
|
||||
if (StringUtils.hasLength(profile) && profile.charAt(0) == '!') {
|
||||
if (!isProfileActive(profile.substring(1))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (isProfileActive(profile)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean acceptsProfiles(Profiles profiles) {
|
||||
Assert.notNull(profiles, "Profiles must not be null");
|
||||
|
||||
@@ -97,18 +97,22 @@ 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. Profiles can simple indicators ('{@code p1}',
|
||||
* {@code !p1}) or more complex boolean expressions. See {@link Profiles#of(String...)}
|
||||
* for syntax details.
|
||||
* 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.
|
||||
* @throws IllegalArgumentException if called with zero arguments
|
||||
* or if any profile is {@code null}, empty or whitespace-only
|
||||
* @see #getActiveProfiles
|
||||
* @see #getDefaultProfiles
|
||||
* @see #acceptsProfiles(Profiles)
|
||||
* @deprecated as of 5.1 in favor of {@link #acceptsProfiles(Profiles)}
|
||||
*/
|
||||
default boolean acceptsProfiles(String... profiles) {
|
||||
return acceptsProfiles(Profiles.of(profiles));
|
||||
}
|
||||
@Deprecated
|
||||
boolean acceptsProfiles(String... profiles);
|
||||
|
||||
/**
|
||||
* Returns whether the active profiles match the given {@link Profiles} set.
|
||||
* Return whether the active profiles match the given {@link Profiles} predicate.
|
||||
*/
|
||||
boolean acceptsProfiles(Profiles profiles);
|
||||
|
||||
|
||||
@@ -16,30 +16,32 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* A set of profiles that may be {@link Environment#acceptsProfiles(Profiles) accepted} by
|
||||
* 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.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 5.0
|
||||
* @see #of(String...)
|
||||
* @since 5.1
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Profiles {
|
||||
|
||||
/**
|
||||
* Test if this profile set matches against given active profiles.
|
||||
* Test if this profile predicate matches against given active profiles.
|
||||
* @param activeProfiles test whether a given profile is currently active
|
||||
*/
|
||||
boolean matches(ActiveProfiles activeProfiles);
|
||||
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(ActiveProfiles) matches} if any one of the given profile
|
||||
* strings match.
|
||||
* {@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
|
||||
@@ -59,18 +61,8 @@ public interface Profiles {
|
||||
* @param profiles the profiles to include
|
||||
* @return a new {@link Profiles} instance
|
||||
*/
|
||||
public static Profiles of(String... profiles) {
|
||||
static Profiles of(String... profiles) {
|
||||
return ProfilesParser.parse(profiles);
|
||||
}
|
||||
|
||||
/**
|
||||
* The current set of active profiles.
|
||||
*/
|
||||
interface ActiveProfiles {
|
||||
|
||||
/**
|
||||
* Tests if given profile is currently active.
|
||||
*/
|
||||
boolean contains(String profile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,10 +19,10 @@ 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;
|
||||
|
||||
import org.springframework.core.env.Profiles.ActiveProfiles;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -30,7 +30,6 @@ import org.springframework.util.StringUtils;
|
||||
* Internal parser used by {@link Profiles#of}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 5.0
|
||||
*/
|
||||
class ProfilesParser {
|
||||
|
||||
@@ -55,7 +54,7 @@ class ProfilesParser {
|
||||
Operator operator = null;
|
||||
while (tokens.hasMoreTokens()) {
|
||||
String token = tokens.nextToken().trim();
|
||||
if(token.isEmpty()) {
|
||||
if (token.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
switch (token) {
|
||||
@@ -86,7 +85,8 @@ class ProfilesParser {
|
||||
return merge(expression, elements, operator);
|
||||
}
|
||||
|
||||
private static Profiles merge(String expression, List<Profiles> elements, Operator operator) {
|
||||
private static Profiles merge(String expression, List<Profiles> elements,
|
||||
Operator operator) {
|
||||
assertWellFormed(expression, !elements.isEmpty());
|
||||
if (elements.size() == 1) {
|
||||
return elements.get(0);
|
||||
@@ -115,16 +115,17 @@ class ProfilesParser {
|
||||
}
|
||||
|
||||
private static Profiles equals(String profile) {
|
||||
return (activeProfile) -> activeProfile.contains(profile);
|
||||
return (activeProfile) -> activeProfile.test(profile);
|
||||
}
|
||||
|
||||
private static Predicate<Profiles> isMatch(ActiveProfiles activeProfile) {
|
||||
private static Predicate<Profiles> isMatch(Predicate<String> activeProfile) {
|
||||
return (profiles) -> profiles.matches(activeProfile);
|
||||
}
|
||||
|
||||
enum Operator {
|
||||
AND, OR
|
||||
};
|
||||
AND,
|
||||
OR
|
||||
}
|
||||
|
||||
private static class ParsedProfiles implements Profiles {
|
||||
|
||||
@@ -138,7 +139,7 @@ class ProfilesParser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(ActiveProfiles activeProfiles) {
|
||||
public boolean matches(Predicate<String> activeProfiles) {
|
||||
for (Profiles candidate : this.parsed) {
|
||||
if (candidate.matches(activeProfiles)) {
|
||||
return true;
|
||||
@@ -149,7 +150,9 @@ class ProfilesParser {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return StringUtils.arrayToCommaDelimitedString(this.expressions);
|
||||
return StringUtils.arrayToDelimitedString(this.expressions, " or ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user