Java Code Examples for org.springframework.core.env.EnumerablePropertySource#getPropertyNames()

The following examples show how to use org.springframework.core.env.EnumerablePropertySource#getPropertyNames() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MqEnvProp.java    From pmq with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: GspAutoConfiguration.java    From grails-boot with Apache License 2.0 6 votes vote down vote up
@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));
                }
            }
        }
    }
}
 
Example 3
Source File: EnvProp.java    From radar with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: ConfigurationSpringInitializer.java    From servicecomb-java-chassis with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example 5
Source File: MqEnvProp.java    From pmq with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: Deployer.java    From spring-cloud-cli with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: SpringPropertySource.java    From hub-detect with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: ZipkinStorageConsumerAutoConfiguration.java    From zipkin-sparkstreaming with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 9
Source File: EnvProp.java    From radar with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: EnvProp.java    From radar with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: MqEnvProp.java    From pmq with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 12
Source File: SpringIterableConfigurationPropertySource.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
public static CacheKey get(EnumerablePropertySource<?> source) {
    if (source instanceof MapPropertySource) {
        return new CacheKey(((MapPropertySource) source).getSource().keySet());
    }
    return new CacheKey(source.getPropertyNames());
}
 
Example 13
Source File: AkkaConfigPropertyAdapter.java    From servicecomb-pack with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 14
Source File: EnvironmentDecryptApplicationInitializer.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
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);
		}

	}
}
 
Example 15
Source File: EnvironmentUtils.java    From dubbo-spring-boot-project with Apache License 2.0 3 votes vote down vote up
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;

    }
 
Example 16
Source File: OnPropertyPrefixCondition.java    From spring-boot-web-support with GNU General Public License v3.0 3 votes vote down vote up
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;

    }
 
Example 17
Source File: ClusterAwareConfiguration.java    From spring-boot-data-geode with Apache License 2.0 2 votes vote down vote up
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;
		}