private PropertySources loadPropertySources(String[] locations, boolean mergeDefaultSources) { try { PropertySourcesLoader loader = new PropertySourcesLoader(); for (String location : locations) { Resource resource = this.resourceLoader .getResource(this.environment.resolvePlaceholders(location)); String[] profiles = this.environment.getActiveProfiles(); for (int i = profiles.length; i-- > 0;) { String profile = profiles[i]; loader.load(resource, profile); } loader.load(resource); } MutablePropertySources loaded = loader.getPropertySources(); if (mergeDefaultSources) { for (PropertySource<?> propertySource : this.propertySources) { loaded.addLast(propertySource); } } return loaded; } catch (IOException ex) { throw new IllegalStateException(ex); } }
/** * 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); }
private PropertySources deducePropertySources() { PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(); if (configurer != null) { // Flatten the sources into a single list so they can be iterated return new FlatPropertySources(configurer.getAppliedPropertySources()); } if (this.environment instanceof ConfigurableEnvironment) { MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment) .getPropertySources(); return new FlatPropertySources(propertySources); } // empty, so not very useful, but fulfils the contract return new MutablePropertySources(); }
/** * 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); }
/** * Renders a {@code PropertySources} as a {@code Stream<PropertySource>} * * @param sources PropertySources * @return a {@code Stream} of {@code PropertySource} */ public static Stream<PropertySource<?>> asStream(PropertySources sources) { if (sources == null) { return Stream.empty(); } Iterable<PropertySource<?>> iterable = sources::iterator; return StreamSupport.stream(iterable.spliterator(), false); }
/** * Create a new PropertyValues from the given PropertySources. * @param propertySources a PropertySources instance * @param nonEnumerableFallbackNames the property names to try in lieu of an * {@link EnumerablePropertySource}. * @param includes the property name patterns to include */ PropertySourcesPropertyValues(PropertySources propertySources, Collection<String> nonEnumerableFallbackNames, PropertyNamePatternsMatcher includes) { Assert.notNull(propertySources, "PropertySources must not be null"); Assert.notNull(includes, "Includes must not be null"); this.propertySources = propertySources; this.nonEnumerableFallbackNames = nonEnumerableFallbackNames; this.includes = includes; PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver( propertySources); for (PropertySource<?> source : propertySources) { processPropertySource(source, resolver); } }
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); } } } }
public static PropertySources create(ElasticConfig... elasticConfigs) { Preconditions.checkNotNull(elasticConfigs); final MutablePropertySources sources = new MutablePropertySources(); for (ElasticConfig elasticConfig : elasticConfigs) { sources.addLast(new RegistryPropertySource(elasticConfig)); } return sources; }
public static PropertySources create(ConfigGroup... configGroups) { Preconditions.checkNotNull(configGroups); final MutablePropertySources sources = new MutablePropertySources(); for (ConfigGroup configGroup : configGroups) { sources.addLast(new ConfigGroupResource(configGroup)); } return sources; }
void setSources(PropertySources sources) { sources.forEach(source -> this.sources.addLast(source)); }
FlatPropertySources(PropertySources propertySources) { this.propertySources = propertySources; }
public void setPropertySources(PropertySources propertySources) { this.propertySources = propertySources; }
public PropertySources getPropertySources() { return this.propertySources; }
private static PropertySources createPropertySources( PropertySource<?> propertySource) { MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(propertySource); return propertySources; }
public void setPropertySources(PropertySources propertySources) { this.propertySources = new MutablePropertySources(propertySources); }
public PropertySources getAppliedPropertySources() throws IllegalStateException { Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied"); return this.appliedPropertySources; }
private PropertySourcesPropertyResolver getPropertyResolverBeforeSpring4(final PropertySourcesPlaceholderConfigurer placeholderConfigurer) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { Field field = placeholderConfigurer.getClass().getDeclaredField("propertySources"); field.setAccessible(true); return new PropertySourcesPropertyResolver((PropertySources) field.get(placeholderConfigurer)); }
private PropertySourcesPropertyResolver getPropertyResolverBeforeSpring4(final PropertySourcesPlaceholderConfigurer placeholderConfigurer) throws ReflectiveOperationException { return new PropertySourcesPropertyResolver((PropertySources) PropertySourcesPlaceholderConfigurer.class.getField("propertySources").get(placeholderConfigurer)); }
private PropertySourcesPropertyResolver getPropertyResolverBeforeSpring4( final PropertySourcesPlaceholderConfigurer placeholderConfigurer) throws ReflectiveOperationException { return new PropertySourcesPropertyResolver((PropertySources) PropertySourcesPlaceholderConfigurer.class .getField(PropertySourcesEnum.PROPERTYSOURCES.getName()).get(placeholderConfigurer)); }
private PropertySourcesPropertyResolverExtension(PropertySources propertySources) { super(propertySources); }
public PropertySources load() throws IOException { MutablePropertySources propSources = new MutablePropertySources(); propSources.addFirst(new PropertiesPropertySource(UUID.randomUUID().toString(), this.mergeProperties())); return propSources; }
/** * Returns the property sources that were actually applied during * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}. * @return the property sources that were applied * @throws IllegalStateException if the property sources have not yet been applied * @since 4.0 */ public PropertySources getAppliedPropertySources() throws IllegalStateException { Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied"); return this.appliedPropertySources; }
/** * Create a new PropertyValues from the given PropertySources. * @param propertySources a PropertySources instance * @param includePatterns property name patterns to include from system properties and * environment variables * @param nonEnumerableFallbackNames the property names to try in lieu of an * {@link EnumerablePropertySource}. */ public PropertySourcesPropertyValues(PropertySources propertySources, Collection<String> includePatterns, Collection<String> nonEnumerableFallbackNames) { this(propertySources, nonEnumerableFallbackNames, new PatternPropertyNamePatternsMatcher(includePatterns)); }
/** * Customize the set of {@link PropertySources} to be used by this configurer. * Setting this property indicates that environment property sources and local * properties should be ignored. * @see #postProcessBeanFactory */ public void setPropertySources(PropertySources propertySources) { this.propertySources = new MutablePropertySources(propertySources); }