mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Tied up loose end for spring profile binding in YAML
The short story: we only support comma-separated spring.profiles in YAML documents. [#51968679]
This commit is contained in:
parent
d5aad97d1f
commit
a7c3edefe8
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Matches a document containing a given key and where the value of that key is an array
|
||||
* containing one of the given values, or where one of the values matches one of the given
|
||||
* values (interpreted as regexes).
|
||||
*/
|
||||
public class ArrayDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private String key;
|
||||
|
||||
private String[] patterns;
|
||||
|
||||
public ArrayDocumentMatcher(final String key, final String... patterns) {
|
||||
this.key = key;
|
||||
this.patterns = patterns;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Properties properties) {
|
||||
if (!properties.containsKey(this.key)) {
|
||||
return MatchStatus.ABSTAIN;
|
||||
}
|
||||
Set<String> values = StringUtils.commaDelimitedListToSet(properties
|
||||
.getProperty(this.key));
|
||||
for (String pattern : this.patterns) {
|
||||
for (String value : values) {
|
||||
if (value.matches(pattern)) {
|
||||
return MatchStatus.FOUND;
|
||||
}
|
||||
}
|
||||
}
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
}
|
@ -17,7 +17,6 @@ package org.springframework.bootstrap.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.bootstrap.config.YamlProcessor.ArrayDocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.DocumentMatcher;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
@ -23,7 +23,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@ -252,40 +251,4 @@ public class YamlProcessor {
|
||||
FOUND, NOT_FOUND, ABSTAIN
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a document containing a given key and where the value of that key is an
|
||||
* array containing one of the given values, or where one of the values matches one of
|
||||
* the given values (interpreted as regexes).
|
||||
*/
|
||||
public static class ArrayDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private String key;
|
||||
|
||||
private String[] patterns;
|
||||
|
||||
public ArrayDocumentMatcher(final String key, final String... patterns) {
|
||||
this.key = key;
|
||||
this.patterns = patterns;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public MatchStatus matches(Properties properties) {
|
||||
if (!properties.containsKey(this.key)) {
|
||||
return MatchStatus.ABSTAIN;
|
||||
}
|
||||
Set<String> values = StringUtils.commaDelimitedListToSet(properties
|
||||
.getProperty(this.key));
|
||||
for (String pattern : this.patterns) {
|
||||
for (String value : values) {
|
||||
if (value.matches(pattern)) {
|
||||
return MatchStatus.FOUND;
|
||||
}
|
||||
}
|
||||
}
|
||||
return MatchStatus.NOT_FOUND;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -25,13 +25,13 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A {@link DocumentMatcher} that sets the active profile if it finds a document with
|
||||
* a key <code>spring.profiles.active</code>.
|
||||
* A {@link DocumentMatcher} that sets the active profile if it finds a document with a
|
||||
* key <code>spring.profiles.active</code>.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public final class ProfileSettingDocumentMatcher implements DocumentMatcher {
|
||||
public class ProfileSettingDocumentMatcher implements DocumentMatcher {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.bootstrap.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.bootstrap.config.YamlProcessor.MatchStatus;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class ArrayDocumentMatcherTests {
|
||||
|
||||
@Test
|
||||
public void testMatchesSingleValue() throws IOException {
|
||||
ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar");
|
||||
assertEquals(MatchStatus.FOUND, matcher.matches(getProperties("foo: bar")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoesNotMatchesIndexedArray() throws IOException {
|
||||
ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar");
|
||||
assertEquals(MatchStatus.ABSTAIN,
|
||||
matcher.matches(getProperties("foo[0]: bar\nfoo[1]: spam")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchesCommaSeparatedArray() throws IOException {
|
||||
ArrayDocumentMatcher matcher = new ArrayDocumentMatcher("foo", "bar");
|
||||
assertEquals(MatchStatus.FOUND, matcher.matches(getProperties("foo: bar,spam")));
|
||||
}
|
||||
|
||||
private Properties getProperties(String values) throws IOException {
|
||||
return PropertiesLoaderUtils.loadProperties(new ByteArrayResource(values
|
||||
.getBytes()));
|
||||
}
|
||||
|
||||
}
|
@ -3,5 +3,5 @@ name: Phil
|
||||
---
|
||||
|
||||
spring:
|
||||
profiles: [goodbye,dev]
|
||||
profiles: goodbye,dev
|
||||
name: Everyone
|
Loading…
Reference in New Issue
Block a user