A while back, groovyblogs.org pointed me to a site suggesting that I should Pimp my Grails Bootstrap.
“Nice,” I thought, “but it should be possible to clean things up a bit…make it a bit more ‘groovy.’”
So here is my attempt:
import grails.util.Environment
class BootStrap {
def init = {servletContext ->
if (this.respondsTo(Environment.current.name))
this.invokeMethod(Environment.current.name, servletContext)
}
def destroy = {
}
def production(servletContext) {
}
def test(servletContext) {
}
def development(servletContext) {
}
}
A couple of things to note:
- Note the use of the Grails 1.1+ Environment class, rather than the older (now deprecated GrailsUtil stuff)
- The methods must be true methods, not closure definitions (otherwise respondsTo() doesn’t know about them, it seems)
- It’s all optional: if you only need development(say), you only have to define development()
- the method names should correspond to what would be returned by Environment.current.name: (for the predefined environments, these are given by: Environment.DEVELOPMENT.name, Environment.TEST.name, etc.)
- In this example, respondsTo()/invokeMethod() need to be invoked via this. Don’t know why; shouldn’t be needed as far as I can see, so YMMV
There’s also http://jira.codehaus … rg/browse/GRAILS-289, which hints at better things to come.
