From ea41efcbe1a85f76f1b93003eb0f42bb807dbbd5 Mon Sep 17 00:00:00 2001 From: "christian.oestreich" Date: Tue, 8 Dec 2015 17:12:55 -0600 Subject: [PATCH] Support plugin.yml and plugin-[environment].yml in grails plugins --- .../GrailsApplicationPostProcessor.groovy | 16 ++--- .../groovy/grails/plugins/GrailsPlugin.java | 2 +- .../grails/plugins/AbstractGrailsPlugin.java | 59 ++++++++++++++----- 3 files changed, 54 insertions(+), 23 deletions(-) mode change 100644 => 100755 grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy mode change 100644 => 100755 grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java mode change 100644 => 100755 grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java diff --git a/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy b/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy old mode 100644 new mode 100755 index 154efad7515..2b392e46853 --- a/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy +++ b/grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy @@ -132,13 +132,15 @@ class GrailsApplicationPostProcessor implements BeanDefinitionRegistryPostProces def plugins = pluginManager.allPlugins if(plugins) { for(GrailsPlugin plugin in plugins.reverse()) { - def pluginPropertySource = plugin.propertySource - if(pluginPropertySource) { - if(pluginPropertySource instanceof EnumerablePropertySource) { - propertySources.addLast( new PrefixedMapPropertySource( "grails.plugins.$plugin.name", (EnumerablePropertySource)pluginPropertySource ) ) - } - propertySources.addLast pluginPropertySource - } + def pluginPropertySources = plugin.propertySources + pluginPropertySources.each { pluginPropertySource -> + if (pluginPropertySource) { + if (pluginPropertySource instanceof EnumerablePropertySource) { + propertySources.addLast(new PrefixedMapPropertySource("grails.plugins.$plugin.name", (EnumerablePropertySource) pluginPropertySource)) + } + propertySources.addLast pluginPropertySource + } + } } } ((DefaultGrailsApplication)grailsApplication).config = new PropertySourcesConfig(propertySources) diff --git a/grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java b/grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java old mode 100644 new mode 100755 index e5bd0c0c8b7..d544c844210 --- a/grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java +++ b/grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java @@ -275,7 +275,7 @@ public interface GrailsPlugin extends ApplicationContextAware, Comparable, Grail String getDependentVersion(String name); - PropertySource getPropertySource(); + List> getPropertySources(); /** * Refreshes this Grails plugin reloading any watched resources as necessary diff --git a/grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java b/grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java old mode 100644 new mode 100755 index 356b35452c1..5bb527f0c3b --- a/grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java +++ b/grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java @@ -20,6 +20,7 @@ import grails.io.IOUtils; import grails.plugins.GrailsPlugin; import grails.plugins.GrailsPluginManager; +import grails.util.Environment; import grails.util.GrailsNameUtils; import groovy.lang.GroovyObjectSupport; import org.grails.config.yaml.YamlPropertySourceLoader; @@ -37,7 +38,13 @@ import java.io.IOException; import java.net.URL; -import java.util.*; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Abstract implementation that provides some default behaviours @@ -47,11 +54,11 @@ public abstract class AbstractGrailsPlugin extends GroovyObjectSupport implements GrailsPlugin { private static final Logger LOG = LoggerFactory.getLogger(AbstractGrailsPlugin.class); - public static final String PLUGIN_YML = "plugin.yml"; + public static final String PLUGIN_YML = "plugin{0}.yml"; public static final String PLUGIN_YML_PATH = "/" + PLUGIN_YML; private static final List DEFAULT_CONFIG_IGNORE_LIST = Arrays.asList("dataSource", "hibernate"); private static Resource basePluginResource = null; - protected PropertySource propertySource; + protected List> propertySources = new ArrayList>(); protected org.codehaus.groovy.grails.commons.GrailsApplication application; protected GrailsApplication grailsApplication; protected boolean isBase = false; @@ -83,20 +90,24 @@ public AbstractGrailsPlugin(Class pluginClass, GrailsApplication application) this.application = new LegacyGrailsApplication(application); this.grailsApplication = application; this.pluginClass = pluginClass; - Resource resource = readPluginConfiguration(pluginClass); - if(resource != null && resource.exists()) { + + for (String environmentSuffix : getEnvironmentSuffixes()) { + Resource resource = readPluginConfiguration(pluginClass, environmentSuffix); + YamlPropertySourceLoader propertySourceLoader = new YamlPropertySourceLoader(); - try { - this.propertySource = propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-plugin.yml", resource, null, false, DEFAULT_CONFIG_IGNORE_LIST); - } catch (IOException e) { - LOG.warn("Error loading plugin.yml for plugin: " + pluginClass.getName() +": " + e.getMessage(), e); + if (resource != null && resource.exists()) { + try { + this.propertySources.add(propertySourceLoader.load(GrailsNameUtils.getLogicalPropertyName(pluginClass.getSimpleName(), "GrailsPlugin") + "-" + MessageFormat.format(PLUGIN_YML, environmentSuffix), resource, null, false, DEFAULT_CONFIG_IGNORE_LIST)); + } catch (IOException e) { + LOG.warn("Error loading plugin.yml for plugin: " + pluginClass.getName() + ": " + e.getMessage(), e); + } } } } @Override - public PropertySource getPropertySource() { - return propertySource; + public List> getPropertySources() { + return propertySources; } /* (non-Javadoc) @@ -112,16 +123,34 @@ public boolean isEnabled(String[] profiles) { return true; } - protected Resource readPluginConfiguration(Class pluginClass) { - final URL urlToPluginYml = IOUtils.findResourceRelativeToClass(pluginClass, PLUGIN_YML_PATH); - + protected Resource readPluginConfiguration(Class pluginClass, String environmentSuffix) { + final URL urlToPluginYml = IOUtils.findResourceRelativeToClass(pluginClass, MessageFormat.format(PLUGIN_YML_PATH, environmentSuffix)); Resource urlResource = urlToPluginYml != null ? new UrlResource(urlToPluginYml) : null; - if(urlResource != null && urlResource.exists()) { + if (urlResource != null && urlResource.exists()) { return urlResource; } + return null; } + /** + * Get the non-environment and environment suffix list to load for the plugin + * + * @return list of environments to load plugin + */ + private List getEnvironmentSuffixes() { + List environmentSuffixes = new ArrayList(); + try { + //Suffix for simple plugin-[environment].yml will be addedLast first and take precedent over plugin.yml + environmentSuffixes.add("-" + Environment.getCurrent().getName().toLowerCase()); + } catch (Exception e) { + LOG.warn("Error getting current environment for plugin: " + pluginClass.getName() + ": " + e.getMessage(), e); + } + //Suffix for simple plugin.yml will load last due to addLast later where the last added is lowest precedent + environmentSuffixes.add(""); + return environmentSuffixes; + } + public String getFileSystemName() { return getFileSystemShortName() + '-' + getVersion(); }