Remove use of regular expressions in Spring profile-based doc matching
Closes gh-1309
This commit is contained in:
parent
fff280470a
commit
3a887151e6
|
|
@ -30,7 +30,9 @@ import org.springframework.util.StringUtils;
|
|||
* values (interpreted as regexes).
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @deprecated as of 1.4.1 in favor of exact String-based matching
|
||||
*/
|
||||
@Deprecated
|
||||
public class ArrayDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private final String key;
|
||||
|
|
|
|||
|
|
@ -18,8 +18,10 @@ package org.springframework.boot.yaml;
|
|||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.beans.factory.config.YamlProcessor.MatchStatus;
|
||||
|
|
@ -39,8 +41,6 @@ import org.springframework.util.StringUtils;
|
|||
*/
|
||||
public class SpringProfileDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private static final String[] DEFAULT_PROFILES = new String[] { "^\\s*$" };
|
||||
|
||||
private static final String SPRING_PROFILES = "spring.profiles";
|
||||
|
||||
private String[] activeProfiles = new String[0];
|
||||
|
|
@ -80,11 +80,9 @@ public class SpringProfileDocumentMatcher implements DocumentMatcher {
|
|||
}
|
||||
|
||||
private DocumentMatcher getActiveProfilesDocumentMatcher() {
|
||||
String[] profiles = this.activeProfiles;
|
||||
if (profiles.length == 0) {
|
||||
profiles = DEFAULT_PROFILES;
|
||||
}
|
||||
return new ArrayDocumentMatcher(SPRING_PROFILES, profiles);
|
||||
return this.activeProfiles.length == 0 ? new EmptyProfileDocumentMatcher()
|
||||
: new ActiveProfilesDocumentMatcher(
|
||||
new HashSet<String>(Arrays.asList(this.activeProfiles)));
|
||||
}
|
||||
|
||||
private String extractProfiles(String profiles, ProfileType type) {
|
||||
|
|
@ -112,4 +110,76 @@ public class SpringProfileDocumentMatcher implements DocumentMatcher {
|
|||
POSITIVE, NEGATIVE
|
||||
}
|
||||
|
||||
/**
|
||||
* Base class for profile-based {@link DocumentMatcher DocumentMatchers}.
|
||||
*/
|
||||
private static abstract class AbstractProfileDocumentMatcher
|
||||
implements DocumentMatcher {
|
||||
|
||||
@Override
|
||||
public final MatchStatus matches(Properties properties) {
|
||||
if (!properties.containsKey(SPRING_PROFILES)) {
|
||||
return MatchStatus.ABSTAIN;
|
||||
}
|
||||
Set<String> profiles = StringUtils
|
||||
.commaDelimitedListToSet(properties.getProperty(SPRING_PROFILES));
|
||||
return matches(profiles);
|
||||
}
|
||||
|
||||
protected abstract MatchStatus matches(Set<String> profiles);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AbstractProfileDocumentMatcher} that matches a document when a value in
|
||||
* {@code spring.profiles} is also in {@code spring.profiles.active}.
|
||||
*/
|
||||
private static class ActiveProfilesDocumentMatcher
|
||||
extends AbstractProfileDocumentMatcher {
|
||||
|
||||
private final Set<String> activeProfiles;
|
||||
|
||||
ActiveProfilesDocumentMatcher(Set<String> activeProfiles) {
|
||||
this.activeProfiles = activeProfiles;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MatchStatus matches(Set<String> profiles) {
|
||||
if (profiles.isEmpty()) {
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
for (String activeProfile : this.activeProfiles) {
|
||||
if (profiles.contains(activeProfile)) {
|
||||
return MatchStatus.FOUND;
|
||||
}
|
||||
}
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link AbstractProfileDocumentMatcher} that matches a document when {@code
|
||||
* spring.profiles} is empty or contains a value with no text.
|
||||
*
|
||||
* @see StringUtils#hasText(String)
|
||||
*/
|
||||
private static class EmptyProfileDocumentMatcher
|
||||
extends AbstractProfileDocumentMatcher {
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Set<String> profiles) {
|
||||
if (profiles.isEmpty()) {
|
||||
return MatchStatus.FOUND;
|
||||
}
|
||||
for (String profile : profiles) {
|
||||
if (!StringUtils.hasText(profile)) {
|
||||
return MatchStatus.FOUND;
|
||||
}
|
||||
}
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Deprecated
|
||||
public class ArrayDocumentMatcherTests {
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Reference in New Issue