This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Groovy and the fly likes Griffon Lifecycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Griffon Lifecycle" Watch "Griffon Lifecycle" New topic

Griffon Lifecycle

Tom Patterson

Joined: May 03, 2004
Posts: 3
In the Ready.groovy script I would like to load some data into my view. My controller already has a closure to do this. However, I don't know how to reference my controller from the Ready.groovy script! I have been looking for examples of this and have come up empty. Any help would be appreciated!
Tom Patterson

Joined: May 03, 2004
Posts: 3
So no replies to my question. I was motivated to press in and figure it out on my own (With the help of Griffon In Action, the online Griffon Guide, and the Griffon API Documentation). Here it is:

The first thing I needed to know about was "app".

This is described in the following section of Griffon In Action:
5.1.1 Quick tour of injected properties and methods.
"The app property points to the current running application."

So now I know that the app is injected into the Ready.groovy script.

In the same section it says: "... by inspecting app.controllers and checking
for their type."

This is a good clue as to how I can get to my controller.

In section 6.3.1 I found the following: "The object that is used to access the portions is the app property. This property is unique from the other injectable properties in that it’s always available in MVC group members because Griffon injects its value at the metaclass level. It also implements the griffon.core.GriffonApplication interface."

So now I know that app implements griffon.core.GriffonApplication.

Online there is the very good Griffon Guide at:

In Chapter 11 (Resource Management) it talks about Resolving Configured Resources. It also has a link to griffon.core.GriffonApplication.

That link takes you to the Griffon API Documentation:

Of interest to me was the method summary:
Map getControllers()
Returns all currently available controller instances, keyed by group name.

Eureka! I can now get to my controller.

So I printed out the map that was returned by getControllers:

The first line is the key to my one and only controller.
The second line is the value of the controller instance (its reference)

So I still had to do a few more things:

I added the following import statement to Ready.groovy:
import hypertax.HyperTaxController

I then declared my controller:
HyperTaxController controller

I then assigned the value from the map to the controller variable.

Finally I called the controller method that accesses the database and populates the view:

Here's the complete Ready.groovy script:

import hypertax.HyperTaxController
* This script is executed inside the UI thread, so be sure to call
* long running code in another thread.
* You have the following options
* - execOutsideUI { // your code }
* - execFuture { // your code }
* - Thread.start { // your code }
* You have the following options to run code again inside the UI thread
* - execInsideUIAsync { // your code }
* - execInsideUISync { // your code }

Map controllerMap
HyperTaxController controller

controllerMap = app.getControllers()
controllerMap.each {entry ->
println entry.key
println entry.value
controller = entry.value


I hope this helps someone else out there that is wrestling with Griffon!

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

We unfortunately do not have too many experts in this area which sometimes leads to lack of an answer. However, thank you for posting that detailed answer, which has now earned you a cow.

[My Blog] [JavaRanch Journal]
Tom Patterson

Joined: May 03, 2004
Posts: 3
Jaikiran, thank you for the cow!
I agree. Here's the link:
subject: Griffon Lifecycle
It's not a secret anymore!