org.grails.config.PropertySourcesConfig Maven / Gradle / Ivy
/*
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.config;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import groovy.util.ConfigObject;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySources;
import org.springframework.core.env.PropertySourcesPropertyResolver;
import grails.util.GrailsStringUtils;
/**
* @author Graeme Rocher
* @since 3.0
*/
public class PropertySourcesConfig extends NavigableMapConfig {
protected PropertySources propertySources;
protected PropertySourcesPropertyResolver propertySourcesPropertyResolver;
public PropertySourcesConfig(PropertySources propertySources) {
this.propertySources = propertySources;
this.propertySourcesPropertyResolver = new PropertySourcesPropertyResolver(propertySources);
initializeFromPropertySources(propertySources);
}
public PropertySourcesConfig() {
this.propertySources = new MutablePropertySources();
this.propertySourcesPropertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
}
public PropertySourcesConfig(Map mapPropertySource) {
MutablePropertySources mutablePropertySources = new MutablePropertySources();
NavigableMap map = new NavigableMap();
map.merge(mapPropertySource, true);
mutablePropertySources.addFirst(new MapPropertySource("config", map));
this.propertySources = mutablePropertySources;
this.propertySourcesPropertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
initializeFromPropertySources(this.propertySources);
}
public PropertySourcesConfig(PropertySource propertySource) {
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(propertySource);
this.propertySources = mutablePropertySources;
this.propertySourcesPropertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
initializeFromPropertySources(this.propertySources);
}
public PropertySources getPropertySources() {
return this.propertySources;
}
public void refresh() {
initializeFromPropertySources(this.propertySources);
}
protected void initializeFromPropertySources(PropertySources propertySources) {
EnvironmentAwarePropertySource environmentAwarePropertySource = new EnvironmentAwarePropertySource(propertySources);
if (propertySources instanceof MutablePropertySources) {
String applicationConfig = "applicationConfigurationProperties";
if (propertySources.contains(applicationConfig)) {
((MutablePropertySources) propertySources).addBefore(applicationConfig, environmentAwarePropertySource);
}
else {
((MutablePropertySources) propertySources).addLast(environmentAwarePropertySource);
}
}
List> propertySourceList = DefaultGroovyMethods.toList(propertySources);
Collections.reverse(propertySourceList);
for (PropertySource propertySource : propertySourceList) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
mergeEnumerablePropertySource(enumerablePropertySource);
}
}
}
private void mergeEnumerablePropertySource(EnumerablePropertySource enumerablePropertySource) {
if (enumerablePropertySource instanceof NavigableMapPropertySource) {
this.configMap.merge(((NavigableMapPropertySource) enumerablePropertySource).getSource(), false);
}
else {
Map map = new LinkedHashMap<>();
String[] propertyNames = enumerablePropertySource.getPropertyNames();
for (String propertyName : propertyNames) {
Object value = enumerablePropertySource.getProperty(propertyName);
if (value instanceof ConfigObject) {
if (((ConfigObject) value).isEmpty()) {
continue;
}
}
else {
value = processAndEvaluate(value);
}
map.put(propertyName, value);
}
this.configMap.merge(map, true);
}
}
private Object processAndEvaluate(Object value) {
if (value instanceof CharSequence) {
value = resolvePlaceholders(value.toString());
}
else if (value instanceof List) {
List