Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support plugin.yml and plugin-[environment].yml in grails plugins #9471

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions grails-core/src/main/groovy/grails/boot/config/GrailsApplicationPostProcessor.groovy
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion grails-core/src/main/groovy/grails/plugins/GrailsPlugin.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public interface GrailsPlugin extends ApplicationContextAware, Comparable, Grail
String getDependentVersion(String name);


PropertySource<?> getPropertySource();
List<PropertySource<?>> getPropertySources();

/**
* Refreshes this Grails plugin reloading any watched resources as necessary
Expand Down
59 changes: 44 additions & 15 deletions grails-core/src/main/groovy/org/grails/plugins/AbstractGrailsPlugin.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String> DEFAULT_CONFIG_IGNORE_LIST = Arrays.asList("dataSource", "hibernate");
private static Resource basePluginResource = null;
protected PropertySource<?> propertySource;
protected List<PropertySource<?>> propertySources = new ArrayList<PropertySource<?>>();
protected org.codehaus.groovy.grails.commons.GrailsApplication application;
protected GrailsApplication grailsApplication;
protected boolean isBase = false;
Expand Down Expand Up @@ -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<PropertySource<?>> getPropertySources() {
return propertySources;
}

/* (non-Javadoc)
Expand All @@ -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<String> getEnvironmentSuffixes() {
List<String> environmentSuffixes = new ArrayList<String>();
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();
}
Expand Down