下面列出了org.springframework.core.env.EnumerablePropertySource#getPropertyNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Map<String, Map<String, Object>> getAllEnv() {
Map<String, Map<String, Object>> result = new LinkedHashMap<String, Map<String, Object>>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
String sourceName = entry.getKey();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
Map<String, Object> properties = new LinkedHashMap<String, Object>();
for (String name : enumerable.getPropertyNames()) {
properties.put(name, sanitize(name, enumerable.getProperty(name)));
}
result.put(sourceName, properties);
}
}
return result;
}
public Map<String, Map<String, Object>> getAllEnv() {
Map<String, Map<String, Object>> result = new LinkedHashMap<String, Map<String, Object>>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
String sourceName = entry.getKey();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
Map<String, Object> properties = new LinkedHashMap<String, Object>();
for (String name : enumerable.getPropertyNames()) {
properties.put(name, sanitize(name, enumerable.getProperty(name)));
}
result.put(sourceName, properties);
}
}
return result;
}
/**
* Get property names from {@link EnumerablePropertySource}, and get property value from {@link ConfigurableEnvironment#getProperty(String)}
*/
private void getProperties(ConfigurableEnvironment environment, PropertySource<?> propertySource,
Map<String, Object> configFromSpringBoot) {
if (propertySource instanceof CompositePropertySource) {
// recursively get EnumerablePropertySource
CompositePropertySource compositePropertySource = (CompositePropertySource) propertySource;
compositePropertySource.getPropertySources().forEach(ps -> getProperties(environment, ps, configFromSpringBoot));
return;
}
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
try {
configFromSpringBoot.put(propertyName, environment.getProperty(propertyName, Object.class));
} catch (Exception e) {
throw new RuntimeException(
"set up spring property source failed.If you still want to start up the application and ignore errors, you can set servicecomb.config.ignoreResolveFailure to true.",
e);
}
}
return;
}
LOGGER.debug("a none EnumerablePropertySource is ignored, propertySourceName = [{}]", propertySource.getName());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void updateFlatConfig() {
super.updateFlatConfig();
if(this.environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment configurableEnv = ((ConfigurableEnvironment)environment);
for(PropertySource<?> propertySource : configurableEnv.getPropertySources()) {
if(propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource)propertySource;
for(String propertyName : enumerablePropertySource.getPropertyNames()) {
flatConfig.put(propertyName, enumerablePropertySource.getProperty(propertyName));
}
}
}
}
}
public Map<String, Object> getEnv() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String name : enumerable.getPropertyNames()) {
if(!result.containsKey(name)){
result.put(name, sanitize(name, enumerable.getProperty(name)));
}
}
}
}
return result;
}
public Map<String, Object> getEnv(String prefix) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String name : enumerable.getPropertyNames()) {
if(!result.containsKey(name)&&name.startsWith(prefix)&&!prefix.equals(name)){
result.put(name, sanitize(name, enumerable.getProperty(name)));
}
}
}
}
return result;
}
public Map<String, Object> getEnv() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String name : enumerable.getPropertyNames()) {
if(!result.containsKey(name)){
result.put(name, sanitize(name, enumerable.getProperty(name)));
}
}
}
}
return result;
}
public Map<String, Object> getEnv(String prefix) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (Entry<String, PropertySource<?>> entry : getPropertySources().entrySet()) {
PropertySource<?> source = entry.getValue();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String name : enumerable.getPropertyNames()) {
if(!result.containsKey(name)&&name.startsWith(prefix)&&!prefix.equals(name)){
result.put(name, sanitize(name, enumerable.getProperty(name)));
}
}
}
}
return result;
}
static Properties extractZipkinProperties(ConfigurableEnvironment env) {
Properties properties = new Properties();
Iterator<PropertySource<?>> it = env.getPropertySources().iterator();
while (it.hasNext()) {
PropertySource<?> next = it.next();
if (!(next instanceof EnumerablePropertySource)) continue;
EnumerablePropertySource source = (EnumerablePropertySource) next;
for (String name : source.getPropertyNames()) {
if (name.startsWith("zipkin")) properties.put(name, source.getProperty(name));
}
}
return properties;
}
@Override
public Set<String> getPropertyKeys() {
Set<String> keys = new HashSet<>();
final MutablePropertySources mutablePropertySources = configurableEnvironment.getPropertySources();
for (final org.springframework.core.env.PropertySource<?> propertySource : mutablePropertySources) {
if (propertySource instanceof EnumerablePropertySource) {
final EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
for (final String propertyName : enumerablePropertySource.getPropertyNames()) {
keys.add(propertyName);
}
}
}
return keys;
}
private Map<String, String> extractProperties(String path) {
PropertySource<?> source = extractPropertySource(path);
Map<String, String> map = new LinkedHashMap<String, String>();
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String name : enumerable.getPropertyNames()) {
map.put(name, source.getProperty(name) == null ? null
: source.getProperty(name).toString());
}
}
return map;
}
public static CacheKey get(EnumerablePropertySource<?> source) {
if (source instanceof MapPropertySource) {
return new CacheKey(((MapPropertySource) source).getSource().keySet());
}
return new CacheKey(source.getPropertyNames());
}
public static Map<String, Object> getPropertyMap(ConfigurableEnvironment environment) {
final Map<String, Object> propertyMap = new HashMap<>();
final List<String> seedNodes = new ArrayList<>();
final List<String> extensions = new ArrayList<>();
final List<String> loggers = new ArrayList<>();
for (final PropertySource source : environment.getPropertySources()) {
if (isEligiblePropertySource(source)) {
final EnumerablePropertySource enumerable = (EnumerablePropertySource) source;
LOG.debug("Adding properties from property source " + source.getName());
for (final String name : enumerable.getPropertyNames()) {
if (name.startsWith(PROPERTY_SOURCE_NAME) && !propertyMap.containsKey(name)) {
String key = name.substring(PROPERTY_SOURCE_NAME.length());
String value = environment.getProperty(name);
if (key.startsWith(AKKA_CLUSTER_SEED_NODES_KEY)) {
seedNodes.add(value);
} else if (key.startsWith(AKKA_ESTENSIONS_KEY)) {
extensions.add(value);
} else if (key.startsWith(AKKA_LOGGERS_KEY)) {
loggers.add(value);
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("Adding property {}={}" + key, value);
}
propertyMap.put(key, value);
if(name.startsWith(REDIS_NAME) && !propertyMap.containsKey(name)){
String readJournalKey = ("akka-persistence-redis.read-journal.redis.").concat(name.substring(REDIS_NAME.length()));
String journalKey = ("akka-persistence-redis.journal.redis.").concat(name.substring(REDIS_NAME.length()));
String snapshotKey = ("akka-persistence-redis.snapshot.redis.").concat(name.substring(REDIS_NAME.length()));
propertyMap.put( readJournalKey, value);
propertyMap.put( journalKey, value);
propertyMap.put( snapshotKey, value);
}
}
}
}
}
propertyMap.put(AKKA_CLUSTER_SEED_NODES_KEY, seedNodes);
propertyMap.put(AKKA_ESTENSIONS_KEY, extensions);
propertyMap.put(AKKA_LOGGERS_KEY, loggers);
}
return Collections.unmodifiableMap(propertyMap);
}
private void merge(PropertySource<?> source, Map<String, Object> properties) {
if (source instanceof CompositePropertySource) {
List<PropertySource<?>> sources = new ArrayList<>(
((CompositePropertySource) source).getPropertySources());
Collections.reverse(sources);
for (PropertySource<?> nested : sources) {
merge(nested, properties);
}
}
else if (source instanceof EnumerablePropertySource) {
Map<String, Object> otherCollectionProperties = new LinkedHashMap<>();
boolean sourceHasDecryptedCollection = false;
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
for (String key : enumerable.getPropertyNames()) {
Object property = source.getProperty(key);
if (property != null) {
String value = property.toString();
if (value.startsWith(ENCRYPTED_PROPERTY_PREFIX)) {
properties.put(key, value);
if (COLLECTION_PROPERTY.matcher(key).matches()) {
sourceHasDecryptedCollection = true;
}
}
else if (COLLECTION_PROPERTY.matcher(key).matches()) {
// put non-encrypted properties so merging of index properties
// happens correctly
otherCollectionProperties.put(key, value);
}
else {
// override previously encrypted with non-encrypted property
properties.remove(key);
}
}
}
// copy all indexed properties even if not encrypted
if (sourceHasDecryptedCollection && !otherCollectionProperties.isEmpty()) {
properties.putAll(otherCollectionProperties);
}
}
}
private boolean startsWith(ConfigurableEnvironment environment, String prefix) {
final String actualPrefix = prefix.endsWith(".") ? prefix : prefix + ".";
boolean started = false;
MutablePropertySources mutablePropertySources = environment.getPropertySources();
for (PropertySource propertySource : mutablePropertySources) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource source = EnumerablePropertySource.class.cast(propertySource);
String[] propertyNames = source.getPropertyNames();
for (String propertyName : propertyNames) {
if (propertyName.startsWith(actualPrefix)) {
started = true;
break;
}
}
}
}
return started;
}
private static Map<String, Object> doExtraProperties(ConfigurableEnvironment environment) {
Map<String, Object> properties = new LinkedHashMap<>(); // orderly
Map<String, PropertySource<?>> map = doGetPropertySources(environment);
for (PropertySource<?> source : map.values()) {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource propertySource = (EnumerablePropertySource) source;
String[] propertyNames = propertySource.getPropertyNames();
if (ObjectUtils.isEmpty(propertyNames)) {
continue;
}
for (String propertyName : propertyNames) {
if (!properties.containsKey(propertyName)) { // put If absent
properties.put(propertyName, propertySource.getProperty(propertyName));
}
}
}
}
return properties;
}
List<ConnectionEndpoint> getConfiguredConnectionEndpoints(Environment environment) {
List<ConnectionEndpoint> connectionEndpoints = new ArrayList<>();
if (environment instanceof ConfigurableEnvironment) {
ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment;
MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
if (propertySources != null) {
Pattern pattern = Pattern.compile(MATCHING_PROPERTY_PATTERN);
for (PropertySource<?> propertySource : propertySources) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource =
(EnumerablePropertySource<?>) propertySource;
String[] propertyNames = enumerablePropertySource.getPropertyNames();
Arrays.stream(ArrayUtils.nullSafeArray(propertyNames, String.class))
.filter(propertyName-> pattern.matcher(propertyName).find())
.forEach(propertyName -> {
String propertyValue = environment.getProperty(propertyName);
if (StringUtils.hasText(propertyValue)) {
int defaultPort = propertyName.contains("servers")
? DEFAULT_CACHE_SERVER_PORT
: DEFAULT_LOCATOR_PORT;
String[] propertyValueArray = trim(propertyValue.split(","));
ConnectionEndpointList list =
ConnectionEndpointList.parse(defaultPort, propertyValueArray);
connectionEndpoints.addAll(list);
}
});
}
}
}
}
return connectionEndpoints;
}