From d7948112b402747d440dd0cb076f782dc3a834bb Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 26 Mar 2014 11:28:18 +0000 Subject: [PATCH] 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. --- .../boot/bind/PropertySourceUtils.java | 92 +++++++++++++++++++ .../boot/bind/RelaxedPropertyResolver.java | 64 +------------ 2 files changed, 95 insertions(+), 61 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/bind/PropertySourceUtils.java diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourceUtils.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourceUtils.java new file mode 100644 index 00000000000..1766fd6f6f2 --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourceUtils.java @@ -0,0 +1,92 @@ +/* + * 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.boot.bind; + +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.PropertySources; + +/** + * Convenience class for manipulating PropertySources. + * + * @author Dave Syer + * + * @see PropertySource + * @see PropertySources + */ +public abstract class PropertySourceUtils { + + /** + * Return a Map of all values from the specified {@link PropertySources} that start + * with a particular key. + * @param propertySources the property sources to scan + * @param keyPrefix the key prefixes to test + * @return a map of all sub properties starting with the specified key prefixes. + * @see PropertySourceUtils#getSubProperties(PropertySources, String, String) + */ + public static Map getSubProperties(PropertySources propertySources, + String keyPrefix) { + return PropertySourceUtils.getSubProperties(propertySources, null, keyPrefix); + } + + /** + * Return a Map of all values from the specified {@link PropertySources} that start + * with a particular key. + * @param propertySources the property sources to scan + * @param rootPrefix a root prefix to be prepended to the keyPrefex (can be + * {@code null}) + * @param keyPrefix the key prefixes to test + * @return a map of all sub properties starting with the specified key prefixes. + * @see #getSubProperties(PropertySources, String, String) + */ + public static Map getSubProperties(PropertySources propertySources, + String rootPrefix, String keyPrefix) { + RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); + Map subProperties = new LinkedHashMap(); + for (PropertySource source : propertySources) { + if (source instanceof EnumerablePropertySource) { + for (String name : ((EnumerablePropertySource) source) + .getPropertyNames()) { + String key = PropertySourceUtils.getSubKey(name, rootPrefix, + keyPrefixes); + if (key != null) { + subProperties.put(key, source.getProperty(name)); + } + } + } + } + return Collections.unmodifiableMap(subProperties); + } + + private static String getSubKey(String name, String rootPrefixes, + RelaxedNames keyPrefix) { + rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes); + for (String rootPrefix : new RelaxedNames(rootPrefixes)) { + for (String candidateKeyPrefix : keyPrefix) { + if (name.startsWith(rootPrefix + candidateKeyPrefix)) { + return name.substring((rootPrefix + candidateKeyPrefix).length()); + } + } + } + return null; + } + +} diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java index 6e672281e6a..5603e40847f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedPropertyResolver.java @@ -16,14 +16,10 @@ package org.springframework.boot.bind; -import java.util.Collections; -import java.util.LinkedHashMap; import java.util.Map; import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.PropertyResolver; -import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySources; import org.springframework.util.Assert; @@ -141,68 +137,14 @@ public class RelaxedPropertyResolver implements PropertyResolver { * {@link ConfigurableEnvironment}. * @param keyPrefix the key prefix used to filter results * @return a map of all sub properties starting with the specified key prefix. - * @see #getSubProperties(PropertySources, String) - * @see #getSubProperties(PropertySources, String, String) + * @see PropertySourceUtils#getSubProperties(PropertySources, String) + * @see PropertySourceUtils#getSubProperties(PropertySources, String, String) */ public Map getSubProperties(String keyPrefix) { Assert.isInstanceOf(ConfigurableEnvironment.class, this.resolver, "SubProperties not available."); ConfigurableEnvironment env = (ConfigurableEnvironment) this.resolver; - return getSubProperties(env.getPropertySources(), this.prefix, keyPrefix); - } - - /** - * Return a Map of all values from the specified {@link PropertySources} that start - * with a particular key. - * @param propertySources the property sources to scan - * @param keyPrefix the key prefixes to test - * @return a map of all sub properties starting with the specified key prefixes. - * @see #getSubProperties(PropertySources, String, String) - */ - public static Map getSubProperties(PropertySources propertySources, - String keyPrefix) { - return getSubProperties(propertySources, null, keyPrefix); - } - - /** - * Return a Map of all values from the specified {@link PropertySources} that start - * with a particular key. - * @param propertySources the property sources to scan - * @param rootPrefix a root prefix to be prepended to the keyPrefex (can be - * {@code null}) - * @param keyPrefix the key prefixes to test - * @return a map of all sub properties starting with the specified key prefixes. - * @see #getSubProperties(PropertySources, String, String) - */ - public static Map getSubProperties(PropertySources propertySources, - String rootPrefix, String keyPrefix) { - RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); - Map subProperties = new LinkedHashMap(); - for (PropertySource source : propertySources) { - if (source instanceof EnumerablePropertySource) { - for (String name : ((EnumerablePropertySource) source) - .getPropertyNames()) { - String key = getSubKey(name, rootPrefix, keyPrefixes); - if (key != null) { - subProperties.put(key, source.getProperty(name)); - } - } - } - } - return Collections.unmodifiableMap(subProperties); - } - - private static String getSubKey(String name, String rootPrefixes, - RelaxedNames keyPrefix) { - rootPrefixes = (rootPrefixes == null ? "" : rootPrefixes); - for (String rootPrefix : new RelaxedNames(rootPrefixes)) { - for (String candidateKeyPrefix : keyPrefix) { - if (name.startsWith(rootPrefix + candidateKeyPrefix)) { - return name.substring((rootPrefix + candidateKeyPrefix).length()); - } - } - } - return null; + return PropertySourceUtils.getSubProperties(env.getPropertySources(), this.prefix, keyPrefix); } }