Skip to content

Commit d794811

Browse files
author
Dave Syer
committed
Extract sub-properties methods to PropertySourceUtils
The static convenience methods in RelaxedPropertyResolver were not really relaxed, so since they are public (and useful) it makes more sense to extract them to a utility class.
1 parent 006d7b6 commit d794811

File tree

2 files changed

+95
-61
lines changed

2 files changed

+95
-61
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2012-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.bind;
18+
19+
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.core.env.EnumerablePropertySource;
24+
import org.springframework.core.env.PropertySource;
25+
import org.springframework.core.env.PropertySources;
26+
27+
/**
28+
* Convenience class for manipulating PropertySources.
29+
*
30+
* @author Dave Syer
31+
*
32+
* @see PropertySource
33+
* @see PropertySources
34+
*/
35+
public abstract class PropertySourceUtils {
36+
37+
/**
38+
* Return a Map of all values from the specified {@link PropertySources} that start
39+
* with a particular key.
40+
* @param propertySources the property sources to scan
41+
* @param keyPrefix the key prefixes to test
42+
* @return a map of all sub properties starting with the specified key prefixes.
43+
* @see PropertySourceUtils#getSubProperties(PropertySources, String, String)
44+
*/
45+
public static Map<String, Object> getSubProperties(PropertySources propertySources,
46+
String keyPrefix) {
47+
return PropertySourceUtils.getSubProperties(propertySources, null, keyPrefix);
48+
}
49+
50+
/**
51+
* Return a Map of all values from the specified {@link PropertySources} that start
52+
* with a particular key.
53+
* @param propertySources the property sources to scan
54+
* @param rootPrefix a root prefix to be prepended to the keyPrefex (can be
55+
* {@code null})
56+
* @param keyPrefix the key prefixes to test
57+
* @return a map of all sub properties starting with the specified key prefixes.
58+
* @see #getSubProperties(PropertySources, String, String)
59+
*/
60+
public static Map<String, Object> getSubProperties(PropertySources propertySources,
61+
String rootPrefix, String keyPrefix) {
62+
RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix);
63+
Map<String, Object> subProperties = new LinkedHashMap<String, Object>();
64+
for (PropertySource<?> source : propertySources) {
65+
if (source instanceof EnumerablePropertySource) {
66+
for (String name : ((EnumerablePropertySource<?>) source)
67+
.getPropertyNames()) {
68+
String key = PropertySourceUtils.getSubKey(name, rootPrefix,
69+
keyPrefixes);
70+
if (key != null) {
71+
subProperties.put(key, source.getProperty(name));
72+
}
73+
}
74+
}
75+
}
76+
return Collections.unmodifiableMap(subProperties);
77+
}
78+
79+
private static String getSubKey(String name, String rootPrefixes,
80+
RelaxedNames keyPrefix) {
81+
rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes);
82+
for (String rootPrefix : new RelaxedNames(rootPrefixes)) {
83+
for (String candidateKeyPrefix : keyPrefix) {
84+
if (name.startsWith(rootPrefix + candidateKeyPrefix)) {
85+
return name.substring((rootPrefix + candidateKeyPrefix).length());
86+
}
87+
}
88+
}
89+
return null;
90+
}
91+
92+
}

spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,10 @@
1616

1717
package org.springframework.boot.bind;
1818

19-
import java.util.Collections;
20-
import java.util.LinkedHashMap;
2119
import java.util.Map;
2220

2321
import org.springframework.core.env.ConfigurableEnvironment;
24-
import org.springframework.core.env.EnumerablePropertySource;
2522
import org.springframework.core.env.PropertyResolver;
26-
import org.springframework.core.env.PropertySource;
2723
import org.springframework.core.env.PropertySources;
2824
import org.springframework.util.Assert;
2925

@@ -141,68 +137,14 @@ public String resolveRequiredPlaceholders(String text)
141137
* {@link ConfigurableEnvironment}.
142138
* @param keyPrefix the key prefix used to filter results
143139
* @return a map of all sub properties starting with the specified key prefix.
144-
* @see #getSubProperties(PropertySources, String)
145-
* @see #getSubProperties(PropertySources, String, String)
140+
* @see PropertySourceUtils#getSubProperties(PropertySources, String)
141+
* @see PropertySourceUtils#getSubProperties(PropertySources, String, String)
146142
*/
147143
public Map<String, Object> getSubProperties(String keyPrefix) {
148144
Assert.isInstanceOf(ConfigurableEnvironment.class, this.resolver,
149145
"SubProperties not available.");
150146
ConfigurableEnvironment env = (ConfigurableEnvironment) this.resolver;
151-
return getSubProperties(env.getPropertySources(), this.prefix, keyPrefix);
152-
}
153-
154-
/**
155-
* Return a Map of all values from the specified {@link PropertySources} that start
156-
* with a particular key.
157-
* @param propertySources the property sources to scan
158-
* @param keyPrefix the key prefixes to test
159-
* @return a map of all sub properties starting with the specified key prefixes.
160-
* @see #getSubProperties(PropertySources, String, String)
161-
*/
162-
public static Map<String, Object> getSubProperties(PropertySources propertySources,
163-
String keyPrefix) {
164-
return getSubProperties(propertySources, null, keyPrefix);
165-
}
166-
167-
/**
168-
* Return a Map of all values from the specified {@link PropertySources} that start
169-
* with a particular key.
170-
* @param propertySources the property sources to scan
171-
* @param rootPrefix a root prefix to be prepended to the keyPrefex (can be
172-
* {@code null})
173-
* @param keyPrefix the key prefixes to test
174-
* @return a map of all sub properties starting with the specified key prefixes.
175-
* @see #getSubProperties(PropertySources, String, String)
176-
*/
177-
public static Map<String, Object> getSubProperties(PropertySources propertySources,
178-
String rootPrefix, String keyPrefix) {
179-
RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix);
180-
Map<String, Object> subProperties = new LinkedHashMap<String, Object>();
181-
for (PropertySource<?> source : propertySources) {
182-
if (source instanceof EnumerablePropertySource) {
183-
for (String name : ((EnumerablePropertySource<?>) source)
184-
.getPropertyNames()) {
185-
String key = getSubKey(name, rootPrefix, keyPrefixes);
186-
if (key != null) {
187-
subProperties.put(key, source.getProperty(name));
188-
}
189-
}
190-
}
191-
}
192-
return Collections.unmodifiableMap(subProperties);
193-
}
194-
195-
private static String getSubKey(String name, String rootPrefixes,
196-
RelaxedNames keyPrefix) {
197-
rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes);
198-
for (String rootPrefix : new RelaxedNames(rootPrefixes)) {
199-
for (String candidateKeyPrefix : keyPrefix) {
200-
if (name.startsWith(rootPrefix + candidateKeyPrefix)) {
201-
return name.substring((rootPrefix + candidateKeyPrefix).length());
202-
}
203-
}
204-
}
205-
return null;
147+
return PropertySourceUtils.getSubProperties(env.getPropertySources(), this.prefix, keyPrefix);
206148
}
207149

208150
}

0 commit comments

Comments
 (0)