Wednesday, July 7, 2010

creating custom grails artefacts

I wanted to create my own kind of artefacts, in order to make easy extension of my application easier. I already had an interface called "Module" that has been implemented by classes that add new content to my sites. There were two problems that artefacts could solve for me:
  1. Dynamic reloading on code change won't work in non-artefact classes :-(
  2. Whenever I introduce a new module, I have to manually register it with a service handler.
As an artefact a module is automagically added and corresponding classes are hot-swapped if changed. I followed the great instructions on this page to get started.

The only thing I have done differently was in the last step, when I access the modules like this:

def grailsApplication

def modules = grailsApplication.getModuleClasses().collect { 
 it.referenceInstance
}

The thing is, getModuleClasses() returns 'GrailsClasses' and not java.lang.classes. At first I took the wrong road from there, trying to use it.class, which is then the DefaultModuleClass that you had to define with no abilities. I wondered how to obtain the underlying 'real' module class. Looking into the properties of the GrailsClass I found out that you can get the class of the module by accessing 'class'. But you can do even better: A instance is already attached and can be accessed by it.referenceInstance.

Happy artefacting!

No comments:

Post a Comment