tags:

views:

34

answers:

1

Hello, How do i find all the controllers running in an application ?

I am trying to create a menu using YUI where only registered controllers will have a menu shown. A controller class will create a static list with various properties detailing name, action, etc. (much like grails-nav plugin).

I want to create a taglib that can find all controllers, identify which ones have this static list then look into each list and build up a menu.

I think i can use ControllerGrailsClass.metaClass.hasProperty to identify whether a given controller has the static property - but how do I find all the Controller classes to interrogate ?

Thanks in advance

A: 

You can get a list from the GrailsApplication object. Example:

class TestController {

    def grailsApplication // gets injected automatically

    def test = {
        grailsApplication.controllerClasses.each { controllerArtefact ->
            def controllerClass = controllerArtefact.getClazz()
            println "$controllerArtefact, $controllerClass"
        }
    }
}

If you're not in a controller, you can get a hold of the grails application object like so:

import org.codehaus.groovy.grails.commons.ApplicationHolder

def grailsApplication = ApplicationHolder.application
ataylor
Thanks ataylor for your prompt reply. I just saw this in the definitive guide book (via google search), it injects the grailsApplication class and adds a selection of dynamic getters get*Classes etc. The other question worth asking is how do you find this info? For example i searched the grails API and the class had no mention of these dynamic methods. I did various google searches and again did not find anything. The grails reference does not make any references to these - well i certainly havent found.
Primus
thanks again ataylor, some pointers on a good source for this info would also be appreciated. I have the definitive guide to grails but cant ever remember seeing this kind of info.
Primus
The getControllerClasses, getDomainClasses, etc. methods are dynamically resolved in DefaultGrailsApplication.invokeMethod() and getProperty() to support plugin-provided artifact classes. For example the Quartz plugin adds a Job artifact, so getJobClasses will return all Job artifacts if the Quartz plugin is installed.
Burt Beckwith
Harri, I implemented something similar a while back; I can't remember exactly how I discovered that dynamic method, but I typically make liberal use of google and the grails source code to track down things like this.
ataylor