/** * 处理配置属性文件中的属性源 * * @param propertySource * @param propertySourceName */ private void processPropertySource(EnumerablePropertySource<ConfigItemList> propertySource, String propertySourceName) { // 属性文件内容 if (!propertySource.containsProperty(propertySourceName)) { return; } String content = propertySource.getProperty(propertySourceName).toString(); if (StringUtils.isEmpty(content)) { return; } ConfigItemList configItemList = propertySource.getSource(); if (configItemList == null) { return; } Properties properties = loadProperties(propertySourceName, content); // PropertiesPropertySource pps = new PropertiesPropertySource(propertySourceName, properties); if (properties != null && !properties.isEmpty()) { for (Map.Entry<?, ?> entry : properties.entrySet()) { if (entry.getKey() != null) { configItemList.addItem(new ConfigItem(entry.getKey().toString(), entry.getValue().toString())); } } } }
@Override public Stream<String> getPropertyNames() throws UnsupportedOperationException { List<String> names = new LinkedList<>(); if (ConfigurableEnvironment.class.isAssignableFrom(getEnvironment().getClass())) { MutablePropertySources propertySources = ((ConfigurableEnvironment) getEnvironment()).getPropertySources(); if (propertySources != null) { Iterator<PropertySource<?>> i = propertySources.iterator(); while (i.hasNext()) { PropertySource<?> source = i.next(); if (source instanceof EnumerablePropertySource) { String[] propertyNames = ((EnumerablePropertySource<?>) source).getPropertyNames(); if (propertyNames != null) { names.addAll(Arrays.asList(propertyNames)); } } } } } return names.stream(); }
public static Properties findProps(ConfigurableEnvironment env, String prefix){ Properties props = new Properties(); for (PropertySource<?> source : env.getPropertySources()) { if (source instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source; for (String name : enumerable.getPropertyNames()) { if (name.startsWith(prefix)) { props.putIfAbsent(name, enumerable.getProperty(name)); } } } } return props; }
/** * 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<String, Object> getSubProperties(PropertySources propertySources, String rootPrefix, String keyPrefix) { RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); Map<String, Object> subProperties = new LinkedHashMap<String, Object>(); 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); }
@Override public Map<String, Object> invoke() { Map<String, Object> result = new LinkedHashMap<String, Object>(); result.put("profiles", getEnvironment().getActiveProfiles()); 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))); } properties = postProcessSourceProperties(sourceName, properties); if (properties != null) { result.put(sourceName, properties); } } } return result; }
@Override public Map<String, Object> invoke() { Map<String, Object> result = new LinkedHashMap<String, Object>(); result.put("profiles", getEnvironment().getActiveProfiles()); 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> map = new LinkedHashMap<String, Object>(); for (String name : enumerable.getPropertyNames()) { map.put(name, sanitize(name, enumerable.getProperty(name))); } result.put(sourceName, map); } } return result; }
private Map<String, Object> properties() { Map<String, Object> map = new HashMap(); if (environment instanceof ConfigurableEnvironment) { for (PropertySource<?> propertySource : ((ConfigurableEnvironment) environment).getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) { if (StringUtils.startsWithAny(key, "spring", "eureka", "camunda.bpm")) { map.put(key, propertySource.getProperty(key)); } } } } } return map; }
/** * 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<String, Object> getSubProperties(PropertySources propertySources, String rootPrefix, String keyPrefix) { RelaxedNames keyPrefixes = new RelaxedNames(keyPrefix); Map<String, Object> subProperties = new LinkedHashMap<String, Object>(); 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 Map<String, SpringProperty> initSpringProperties(ConfigurableEnvironment springEnvironment) { Map<String, SpringProperty> springProperties = new HashMap<>(); for (PropertySource<?> eachPropertySource : springEnvironment.getPropertySources()) { if (!(eachPropertySource instanceof EnumerablePropertySource)) { continue; } EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) eachPropertySource; for (String prop : enumerablePropertySource.getPropertyNames()) { String sourceName = enumerablePropertySource.getName(); SpringProperty springProperty = springProperties.get(prop); if (springProperty == null) { springProperty = new SpringProperty(prop); } springProperty.setValue(enumerablePropertySource.getProperty(prop), sourceName); springProperties.put(prop, springProperty); } } return filterSystemAndEnvironmentProperties(springProperties); }
@Override public AutoConfiguration getObject() throws Exception { Assert.notNull(serviceName); String resolvedServiceName = embeddedValueResolver.resolveStringValue(serviceName); List<String> resolvedTags = resolveTags(tags); return MuonConfigBuilder .withServiceIdentifier(resolvedServiceName) .addWriter(autoConfig -> { ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironment) environment; for (PropertySource<?> propertySource : configurableEnvironment.getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { final EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource; for (String name : enumerablePropertySource.getPropertyNames()) { autoConfig.getProperties().put(name, enumerablePropertySource.getProperty(name)); } } } }) .withTags(resolvedTags.toArray(new String[resolvedTags.size()])) .build(); }
@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)); } } } } }
private Map<String, String> getAllKnownProperties(Environment env) { Map<String, String> rtn = new HashMap<>(); if (env instanceof ConfigurableEnvironment) { for (PropertySource<?> propertySource : ((ConfigurableEnvironment) env).getPropertySources()) { if (propertySource instanceof EnumerablePropertySource) { LOGGER.info("processing property source ::: " + propertySource.getName()); for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) { String value = propertySource.getProperty(key).toString(); if (StringUtils.isEmpty(value)) { rtn.put(key, propertySource.getProperty(key).toString()); } } } } } return rtn; }
public ConfigItemChangeEvent(Object source, VersionPropertySource<ConfigItemList> versionPropertySource) { super(source); this.version = versionPropertySource.getVersion(); EnumerablePropertySource<ConfigItemList> enumerablePropertySource = versionPropertySource.getSource(); if (enumerablePropertySource != null) { ConfigItemList configItemList = enumerablePropertySource.getSource(); if (configItemList != null) { this.itemList = configItemList.getItems(); } } }
private MapPropertySource findZuulPropertySource(ConfigurableEnvironment environment) { for (PropertySource<?> propertySource : environment.getPropertySources()) { if (propertySource instanceof MapPropertySource) { for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) { if (key.toLowerCase().startsWith(ZUUL_SERVICE_VERSIONS_ROOT)) { return (MapPropertySource) propertySource; } } } } return null; }
public Map<String, Object> getProperties() { Map<String, Object> m = new HashMap<>(); for (PropertySource<?> source : environment.getPropertySources()) { if (source instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource<?>) source) .getPropertyNames()) { if (name != null && name.startsWith(prefix)) { String subKey = name.substring(prefix.length()); m.put(subKey, source.getProperty(name)); } } } } return m; }
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; }
/** * Returns the property key/value pairs found in {@code sources} and returns them in a {@code Map}. Only instances * of {@code EnumerablePropertySource} are considered. If {@code sources} contains other types of * {@code PropertySource}, they will be ignored, and <em>not</em> included in the returned {@code Map}. * <p> * If {@code prefix} is provided, only properties that start with the supplied prefix will be included in the * returned {@code Map}. If {@code strip} is {@code true} and {@code prefix} is non-null, the {@code prefix} will * be stripped from the property key in the returned {@code Map}. * </p> * * @param sources {@code MutablePropertySources} whose key/value pairs are enumerated and placed into the returned * {@code Map} * @param prefix only include properties whose key starts with the supplied prefix, may be {@code null} * @param strip if {@code true}, strip the {@code prefix} off of the property key in the returned {@code Map} * @return a {@code Map} containing the enumerated property keys and values from {@code sources} */ public static Map<String, Object> asMap(MutablePropertySources sources, String prefix, boolean strip) { Map<String, Object> props = new HashMap<>(); filterEnumerablePropertySources(sources).forEach(source -> { if (!(source instanceof EnumerablePropertySource)) { return; } Stream.of(((EnumerablePropertySource)source).getPropertyNames()) .filter(propName -> prefix == null || propName.startsWith(prefix)) .peek(propName -> LOG.debug( "Found kafka property prefix: [{}], property name: [{}]", prefix, propName)) .collect(Collectors.toMap( propName -> (prefix == null || !strip || !propName.startsWith(prefix)) ? propName : propName.substring(prefix.length()), source::getProperty, (val1, val2) -> { LOG.debug("Merging kafka property value [{}], [{}]: [{}] wins", val1, val2, val2); return val2; }, () -> props )) .forEach((key, value) -> LOG.debug("Kafka property: [{}]=[{}]", key, (isNullValue(value)) ? "null" : value)); }); return props; }
/** * Filters the supplied {@code MutablePropertySources} for instances of {@code EnumerablePropertySource}, and * returns a new {@code MutablePropertySource} containing <em>only</em> {@code EnumerablePropertySource} sources. * * @param sources property sources that may contain instances of {@code EnumerablePropertySource} * @return property sources that contain <em>only</em> {@code EnumerablePropertySource} */ public static MutablePropertySources filterEnumerablePropertySources(MutablePropertySources sources) { MutablePropertySources enumerableSources = new MutablePropertySources(); sources.forEach(source -> { if (source instanceof EnumerablePropertySource) { enumerableSources.addLast(source); } }); return enumerableSources; }
@Test @SuppressWarnings("rawtypes") public void propertyNameOrderingIsPreservedInEnvironment() { final String[] expectedPropertyNames = new String[] { "foo", "baz", "enigma", "x.y.z", "server.url", "key.value.1", "key.value.2", "key.value.3" }; EnumerablePropertySource eps = (EnumerablePropertySource) env.getPropertySources().get( INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); assertArrayEquals(expectedPropertyNames, eps.getPropertyNames()); }
public static Map<String, Object> getAllProperties(String prefix){ init(); if(environment == null)return null; MutablePropertySources propertySources = ((ConfigurableEnvironment)environment).getPropertySources(); Map<String, Object> properties = new LinkedHashMap<String, Object>(); for (PropertySource<?> source : propertySources) { if(source.getName().startsWith("servlet") || source.getName().startsWith("system")){ continue; } if (source instanceof EnumerablePropertySource) { for (String name : ((EnumerablePropertySource<?>) source) .getPropertyNames()) { boolean match = StringUtils.isEmpty(prefix); if(!match){ match = name.startsWith(prefix); } if(match){ Object value = source.getProperty(name); if(value != null){ properties.put(name, value); } } } } } return Collections.unmodifiableMap(properties); }
ConfigurationPropertySources(Collection<PropertySource<?>> sources) { super(APPLICATION_CONFIGURATION_PROPERTY_SOURCE_NAME, sources); this.sources = sources; List<String> names = new ArrayList<String>(); for (PropertySource<?> source : sources) { if (source instanceof EnumerablePropertySource) { names.addAll(Arrays.asList( ((EnumerablePropertySource<?>) source).getPropertyNames())); } } this.names = names.toArray(new String[names.size()]); }
private void processPropertySource(PropertySource<?> source, PropertySourcesPropertyResolver resolver) { if (source instanceof CompositePropertySource) { processCompositePropertySource((CompositePropertySource) source, resolver); } else if (source instanceof EnumerablePropertySource) { processEnumerablePropertySource((EnumerablePropertySource<?>) source, resolver, this.includes); } else { processNonEnumerablePropertySource(source, resolver); } }
private void processEnumerablePropertySource(EnumerablePropertySource<?> source, PropertySourcesPropertyResolver resolver, PropertyNamePatternsMatcher includes) { if (source.getPropertyNames().length > 0) { for (String propertyName : source.getPropertyNames()) { if (includes.matches(propertyName)) { Object value = getEnumerableProperty(source, resolver, propertyName); putIfAbsent(propertyName, value, source); } } } }
private Object getEnumerableProperty(EnumerablePropertySource<?> source, PropertySourcesPropertyResolver resolver, String propertyName) { try { return resolver.getProperty(propertyName, Object.class); } catch (RuntimeException ex) { // Probably could not resolve placeholders, ignore it here return source.getProperty(propertyName); } }
@Test public void orderedItems() throws Exception { StringBuilder yaml = new StringBuilder(); List<String> expected = new ArrayList<String>(); for (char c = 'a'; c <= 'z'; c++) { yaml.append(c + ": value" + c + "\n"); expected.add(String.valueOf(c)); } ByteArrayResource resource = new ByteArrayResource(yaml.toString().getBytes()); EnumerablePropertySource<?> source = (EnumerablePropertySource<?>) this.loader .load("resource", resource, null); assertThat(source).isNotNull(); assertThat(source.getPropertyNames()) .isEqualTo(expected.toArray(new String[] {})); }
private void getNames(PropertySources propertySources, NameCallback callback) { for (PropertySource<?> propertySource : propertySources) { if (propertySource instanceof EnumerablePropertySource) { EnumerablePropertySource<?> source = (EnumerablePropertySource<?>) propertySource; for (String name : source.getPropertyNames()) { callback.addName(name); } } } }
@Test public void propertyParseIsEqualToYaml() throws Exception { HoconPropertySourceLoader hoconLoader = new HoconPropertySourceLoader(); YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader(); EnumerablePropertySource hoconParse = loadProperties(hoconLoader, "/application.conf"); EnumerablePropertySource yamlParse = loadProperties(yamlLoader, "/application.yaml"); verifyPropertyKeysAreSame(hoconParse, yamlParse); verifyPropertyValuesAreSame(hoconParse, yamlParse); }
private void verifyPropertyKeysAreSame(EnumerablePropertySource hoconParse, EnumerablePropertySource yamlParse) { String[] hoconProperties = hoconParse.getPropertyNames(); Arrays.sort(hoconProperties); String[] yamlProperties = yamlParse.getPropertyNames(); Arrays.sort(yamlProperties); assertThat(hoconProperties, is(yamlProperties)); }
public EncryptableEnumerablePropertySourceWrapper(EnumerablePropertySource<T> delegate, EncryptablePropertyResolver resolver) { super(delegate.getName(), delegate.getSource()); Assert.notNull(delegate, "PropertySource delegate cannot be null"); Assert.notNull(resolver, "EncryptablePropertyResolver cannot be null"); this.delegate = delegate; this.resolver = resolver; }
ConfigurationPropertySources(Collection<PropertySource<?>> sources) { super(NAME, sources); this.sources = sources; List<String> names = new ArrayList<String>(); for (PropertySource<?> source : sources) { if (source instanceof EnumerablePropertySource) { names.addAll(Arrays.asList( ((EnumerablePropertySource<?>) source).getPropertyNames())); } } this.names = names.toArray(new String[names.size()]); }