Plugin Configurations in Grails

Perhaps, you have a plugin and you would like to configure it instead of hard coding the settings. Lets say, it is a url for REST service that your plugin consumes. In this post, we cover how to incorporate the plugin configurations into part of the default Grails configurations(conf/Config.groovy). In the second part, we cover how to separate the plugins configurations into a separate configurations file while still part of the application

Configurations From App conf/Config.groovy

First, add the configurations setting in the Grails app configuration file(conf/Config.groovy):

// This default value...
yourConfigurationName.url='http://service.com/restapi/'
...
environments {
  development {
    // configurations when in development environment
    yourConfigurationName.url='http://service.com/restapi/'
  }
}

The configuration name may be as you like. From your plugin, this configurations setting can be accessed as follows:

class NodeDriverProxyService {
    def grailsApplication
    def http

    public someMethod(){
        http = new HTTPBuilder(grailsApplication.config.yourConfigurationName.url)
...

As you see, first we inject grailsApplication and then access your setting via grailsApplicaiton.config

This way it is also possible to externalize plugin settings among the other grails configurations as described in post Externalizing Configurations per Grails Application

Configuration from plugins config file

Perhaps, you like to put plugin configurations into separate configuration file instead Grails default Config.groovy
First, create your configuration file and add the configurations. For example grails-app/conf/myPluginName.groovy:

// This default value...
yourConfigurationName.url=http://service.com/restapi/
...
environments {
  development {
    // configurations when in development environment
    yourConfigurationName.url=http://service.com/restapi/
  }
}

This is exact same as part of the Application configurations. Next, here is how to access from plugin:

GroovyClassLoader classLoader = new GroovyClassLoader(getClass().getClassLoader())
ConfigObject config
try {
   config = new ConfigSlurper().parse(classLoader.loadClass('myPluginName'))
}
catch (Exception e) {/*
    if exception, set default settings
*/}

//to access
config.yourConfigurationName.url

Here, we load the file and then use Groovy ConfigSlurper utility to parse and access plugin configurations

To summarize, we covered two ways for plugins configurations being incorporated in the application. One as part of the Grails configuration and another as a separate plugin configurations within the application.

Issues

1. grailsApplication is null
Make sure grailsApplication.config is used within a method. i.e:

class someClass{
def grailsApplication
def url = grailsApplication.config.pluginName.url//this is going to be null

public someMethod(){
   url = grailsApplication.config.pluginName.url//this works!!!
  }
}

Useful Links

  • http://groovy.codehaus.org/ConfigSlurper
  • http://stackoverflow.com/questions/843216/configuration-of-grails-plugin

Leave a Reply

Your email address will not be published. Required fields are marked *