Skip to content

Commit

Permalink
Config - No need to flatten the config anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Nov 24, 2015
1 parent d58960d commit 50cf244
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Metadata extends CodeGenConfig {
public static final String APPLICATION_GRAILS_VERSION = "info.app.grailsVersion";
public static final String SERVLET_VERSION = "info.app.servletVersion";
public static final String WAR_DEPLOYED = "info.app.warDeployed";
public static final String DEFAULT_SERVLET_VERSION = "2.5";
public static final String DEFAULT_SERVLET_VERSION = "3.0";

private static Holder<Reference<Metadata>> holder = new Holder<Reference<Metadata>>("Metadata");
public static final String BUILD_INFO_FILE = "META-INF/grails.build.info"
Expand Down
1 change: 1 addition & 0 deletions grails-core/src/main/groovy/grails/config/Config.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface Config extends PropertyResolver, ConfigMap {
/**
* @return The flat version of the config
*/
@Deprecated
Map<String, Object> flatten()

/**
Expand Down
42 changes: 35 additions & 7 deletions grails-core/src/main/groovy/grails/config/ConfigProperties.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package grails.config

import grails.core.support.GrailsConfigurationAware
import groovy.transform.CompileStatic


Expand All @@ -26,16 +25,45 @@ import groovy.transform.CompileStatic
* @since 3.0
*/
@CompileStatic
class ConfigProperties implements GrailsConfigurationAware {
class ConfigProperties extends Properties {

private Properties properties = new Properties()
private final Config config

Properties resolveProperties() {
properties
ConfigProperties(Config config) {
this.config = config
}

@Override
void setConfiguration(Config co) {
properties = co.toProperties()
Set<String> stringPropertyNames() {
return config.keySet()
}

@Override
Enumeration<?> propertyNames() {
def i = config.keySet().iterator()
return [
hasMoreElement: {-> i.hasNext() },
nextElement: {-> i.next() }
] as Enumeration
}

@Override
String getProperty(String key) {
return config.getProperty(key)
}

@Override
String getProperty(String key, String defaultValue) {
return config.getProperty(key, defaultValue)
}

@Override
Enumeration<Object> keys() {
return (Enumeration<Object>)propertyNames()
}

@Override
Set<Object> keySet() {
return (Set<Object>)stringPropertyNames()
}
}
14 changes: 14 additions & 0 deletions grails-core/src/main/groovy/grails/config/Settings.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,18 @@ interface Settings {
* Whether serving static HTML pages from src/main/resources/public is enabled
*/
String RESOURCES_ENABLED = 'grails.resources.enabled'

/**
* Whether to log request parameters in the console
*/
String SETTING_LOG_REQUEST_PARAMETERS = "grails.exceptionresolver.logRequestParameters";
/**
* The parameters to exclude from logging
*/
String SETTING_EXCEPTION_RESOLVER_PARAM_EXCLUDES = "grails.exceptionresolver.params.exclude";
/**
* The class to use for stacktrace filtering. Should be an instanceof {@link org.grails.exceptions.reporting.StackTraceFilterer}
*/
String SETTING_LOGGING_STACKTRACE_FILTER_CLASS = "grails.logging.stackTraceFiltererClass";

}
2 changes: 1 addition & 1 deletion grails-core/src/main/groovy/grails/util/Holders.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static void setConfig(Config config) {
configs.set(config);

// reset flat config
flatConfigs.set(config == null ? null : config.flatten());
flatConfigs.set(config == null ? null : config);
}

public static Config getConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ private static List<TraitInjector> getTraitInjectors() {

traitInjectors = TraitInjectionSupport.resolveTraitInjectors(traitInjectors);
}
return Collections.unmodifiableList(traitInjectors);
if(traitInjectors != null) {
return Collections.unmodifiableList(traitInjectors);
}
else {
return Collections.emptyList();
}
}

public static void processTraitsForNode(final SourceUnit sourceUnit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void addLast(Config config) {
}

@Override
@Deprecated
public Map<String, Object> flatten() {
Map<String, Object> flattened = new LinkedHashMap<String, Object>();
for(Config c : configs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import grails.config.Config;
import grails.util.GrailsStringUtils;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.convert.ConversionException;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
Expand All @@ -31,7 +33,9 @@
* @author Graeme Rocher
* @since 3.0
*/

public abstract class NavigableMapConfig implements Config {
protected static final Logger LOG = LoggerFactory.getLogger(NavigableMapConfig.class);
protected ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
protected ConfigurableConversionService conversionService = new DefaultConversionService();
protected NavigableMap configMap = new NavigableMap() {
Expand Down Expand Up @@ -133,8 +137,12 @@ public Set<Entry<String, Object>> entrySet() {
}

@Override
@Deprecated
public Map<String, Object> flatten() {
return configMap.toFlatConfig();
if(LOG.isWarnEnabled()) {
LOG.warn("A plugin or your application called the flatten() method which can degrade startup performance");
}
return configMap;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public PropertySourcesConfig() {

public PropertySourcesConfig(Map<String, Object> mapPropertySource) {
MutablePropertySources mutablePropertySources = new MutablePropertySources();
mutablePropertySources.addFirst(new MapPropertySource("config", mapPropertySource));
NavigableMap map = new NavigableMap();
map.merge(mapPropertySource, true);

mutablePropertySources.addFirst(new MapPropertySource("config", map));
this.propertySources = mutablePropertySources;
this.propertySourcesPropertyResolver = new PropertySourcesPropertyResolver(propertySources);
initializeFromPropertySources(propertySources);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class YamlPropertySourceLoader extends YamlProcessor implements PropertySourceLo

@Override
PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
return load(name, resource, profile, true)
}

PropertySource<?> load(String name, Resource resource, String profile, boolean parseFlatKeys ) throws IOException {
if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
if (profile == null) {
matchDefault = true
Expand All @@ -55,7 +59,7 @@ class YamlPropertySourceLoader extends YamlProcessor implements PropertySourceLo
resources = [resource] as Resource[]
def propertySource = new NavigableMap()
process { Properties properties, Map<String, Object> map ->
propertySource.merge(map, true)
propertySource.merge(map, parseFlatKeys)
}
if (!propertySource.isEmpty()) {
return new NavigableMapPropertySource(name, propertySource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public void updateFlatConfig() {
if (config == null) {
flatConfig = new LinkedHashMap();
} else {
flatConfig = config.flatten();
flatConfig = config;
}
}

@SuppressWarnings("unchecked")
public Map<String, Object> getFlatConfig() {
return getConfig().flatten();
return getConfig();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public AbstractGrailsPlugin(Class<?> pluginClass, GrailsApplication application)
if(resource != null && resource.exists()) {
YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader();
try {
this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-plugin.yml", resource, null);
this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-plugin.yml", resource, null, false);
} catch (IOException e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CoreGrailsPlugin extends Plugin {
propertySourcesPlaceholderConfigurer(GrailsPlaceholderConfigurer) {
placeholderPrefix = placeHolderPrefix
}
grailsConfigProperties(ConfigProperties)
grailsConfigProperties(ConfigProperties, config)
legacyGrailsApplication(LegacyGrailsApplication, application)

// replace AutoProxy advisor with Groovy aware one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,12 +811,9 @@ public void setBeanClassLoader(ClassLoader beanClassLoader) {
}

public String getGspEncoding() {
Map<?, ?> config = Holders.getFlatConfig();
Config config = Holders.getConfig();
if (config != null) {
Object gspEnc = config.get(GroovyPageParser.CONFIG_PROPERTY_GSP_ENCODING);
if ((gspEnc != null) && (gspEnc.toString().trim().length() > 0)) {
return gspEnc.toString();
}
return config.getProperty(GroovyPageParser.CONFIG_PROPERTY_GSP_ENCODING, System.getProperty("file.encoding", "us-ascii"));
}
return System.getProperty("file.encoding", "us-ascii");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,9 @@ public GroovyPageParser(String name, String uri, String filename, String gspSour
}

public GroovyPageParser(String name, String uri, String filename, String gspSource, String expressionCodecName) throws IOException {
Map<?, ?> config = Holders.getFlatConfig();
Config config = Holders.getConfig();
if (config != null) {
Object sitemeshPreprocessEnabled = config.get(GroovyPageParser.CONFIG_PROPERTY_GSP_SITEMESH_PREPROCESS);
if (sitemeshPreprocessEnabled != null) {
final boolean enableSitemeshPreprocessing = GrailsStringUtils.toBoolean(String.valueOf(sitemeshPreprocessEnabled).trim());
setEnableSitemeshPreprocessing(enableSitemeshPreprocessing);
}
setEnableSitemeshPreprocessing(config.getProperty(GroovyPageParser.CONFIG_PROPERTY_GSP_SITEMESH_PREPROCESS, Boolean.class, enableSitemeshPreprocessing));
}

GrailsPluginInfo pluginInfo = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class GrailsReactorConfigurationReader implements ConfigurationReader {
Config configuration
Properties configurationProperties = new Properties()

GrailsReactorConfigurationReader(Config configuration, ConfigProperties configurationProperties) {
this(configuration, configurationProperties.resolveProperties())
}

GrailsReactorConfigurationReader(Config configuration, Properties configurationProperties) {
this.configuration = configuration
this.configurationProperties = configurationProperties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ class GrailsCli {
}

private initializeProfile() {
BuildSettings.TARGET_DIR.mkdirs()
BuildSettings.TARGET_DIR?.mkdirs()

populateContextLoader()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.grails.taglib.encoder

import grails.config.Config
import grails.util.GrailsNameUtils
import groovy.transform.CompileStatic

Expand Down Expand Up @@ -51,23 +52,30 @@ class OutputEncodingSettings {
(TAGLIB_CODEC_NAME): 'none',
(TAGLIB_DEFAULT_CODEC_NAME): 'none']

Map flatConfig
Config flatConfig

OutputEncodingSettings(Map flatConfig) {
OutputEncodingSettings(Config flatConfig) {
this.flatConfig = flatConfig
}

String getCodecSettings(GrailsPluginInfo pluginInfo, String codecWriterName) {
if (!codecWriterName) return null

Map codecSettings = getConfigForPrefix(CONFIG_PROPERTY_GSP_CODECS + ".")
Map codecSettings = flatConfig?.getProperty(CONFIG_PROPERTY_GSP_CODECS, Map.class)
if(pluginInfo != null) {
codecSettings = mergePluginCodecSettings(pluginInfo, codecSettings)
def pluginKey = "${pluginInfo.getName()}.${CONFIG_PROPERTY_GSP_CODECS}"

def firstTry = flatConfig?.getProperty(pluginKey, Map.class, null)
if(firstTry == null) {
pluginKey = "${GrailsNameUtils.getPropertyNameForLowerCaseHyphenSeparatedName(pluginInfo.getName())}.${CONFIG_PROPERTY_GSP_CODECS}"
firstTry = flatConfig?.getProperty(pluginKey, Map.class, codecSettings)
}
codecSettings = firstTry
}
String codecInfo = null
if (!codecSettings) {
if (codecWriterName==EXPRESSION_CODEC_NAME) {
codecInfo = flatConfig.get(CONFIG_PROPERTY_DEFAULT_CODEC)?.toString()
codecInfo = flatConfig?.getProperty(CONFIG_PROPERTY_DEFAULT_CODEC)?.toString()
}
} else {
codecInfo = codecSettings.get(codecWriterName)?.toString()
Expand All @@ -86,35 +94,4 @@ class OutputEncodingSettings {
codecInfo
}

private Map mergePluginCodecSettings(GrailsPluginInfo pluginInfo, Map codecSettings) {
if(!codecSettings) {
codecSettings = [:]
}
def pluginName = pluginInfo.getName()
// handle lower case hyphen separated name format, for example 'ui-platform'
doMergePluginCodecSettings(pluginName, codecSettings)
// handle property name format for plugin name, for example 'uiPlatform'
doMergePluginCodecSettings(GrailsNameUtils.getPropertyNameForLowerCaseHyphenSeparatedName(pluginName), codecSettings)
return codecSettings
}

private void doMergePluginCodecSettings(String pluginName, Map codecSettings) {
Map codecSettingsForPlugin = getConfigForPrefix("${pluginName}.${CONFIG_PROPERTY_GSP_CODECS}.".toString())
if(codecSettingsForPlugin) {
codecSettings.putAll(codecSettingsForPlugin)
}
}

private Map getConfigForPrefix(String gspCodecsPrefix) {
Map codecSettings = (Map)flatConfig.inject([:]){ Map map, Map.Entry entry ->
if(entry.getKey() instanceof CharSequence) {
String mapKey = entry.getKey().toString()
if(mapKey.startsWith(gspCodecsPrefix)) {
map.put(mapKey.substring(gspCodecsPrefix.length()), entry.getValue())
}
}
map
}
return codecSettings
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.grails.taglib.encoder

import grails.plugins.GrailsPluginInfo
import org.grails.config.PropertySourcesConfig
import spock.lang.Issue
import spock.lang.Specification
import spock.lang.Unroll;
Expand All @@ -12,7 +13,7 @@ class OutputEncodingSettingsSpec extends Specification {
def "per plugin settings should be supported - #pluginNameInConfig"() {
given:
def flatConfig = [("${pluginNameInConfig}.grails.views.gsp.codecs.expression".toString()): 'none']
def outputEncodingSettings = new OutputEncodingSettings(flatConfig)
def outputEncodingSettings = new OutputEncodingSettings(new PropertySourcesConfig(flatConfig))
def grailsPluginInfo = Mock(GrailsPluginInfo)
when:
def codecSettings=outputEncodingSettings.getCodecSettings(grailsPluginInfo, "expression")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static GrailsApplication currentApplication() {
*/
public static Map currentConfiguration() {
GrailsApplication application = currentApplication();
return application == null ? new ConfigObject() : application.getConfig();
return application == null ? Collections.emptyMap() : application.getConfig();
}

/**
Expand All @@ -101,7 +101,7 @@ public static Map currentConfiguration() {
@Deprecated
public static Map currentFlatConfiguration() {
GrailsApplication application = currentApplication();
return application == null ? Collections.emptyMap() : application.getFlatConfig();
return application == null ? Collections.emptyMap() : application.getConfig();
}

/**
Expand Down
Loading

0 comments on commit 50cf244

Please sign in to comment.