com.nepxion.matrix.selector.PropertySourceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matrix-aop Show documentation
Show all versions of matrix-aop Show documentation
Nepxion Matrix is an AOP framework integrated with Spring AutoProxy, Spring Registrar and Spring Import Selector
package com.nepxion.matrix.selector;
/**
* Title: Nepxion Matrix
* Description: Nepxion Matrix AOP
* Copyright: Copyright (c) 2017-2050
* Company: Nepxion
* @author Haojun Ren
* @version 1.0
*/
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.
*
* @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 keyPrefix (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.containsKey(key)) {
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;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy