« Return to Thread: Extending the _Event system in Grails (Re: was something else I can't remember now)

Re: Extending the _Event system in Grails (Re: was something else I can't remember now)

by Richard Vowles :: Rate this Message:

Reply to Author | View in Thread

On Mon, Jun 22, 2009 at 7:28 PM, Graeme Rocher <graeme.rocher@...> wrote:
Hi Richard,

_Events.groovy is a script and hence like all other scripts it plays by the same rules in that you don't have access to sources from inside the script unless you explicitly compile and load them. You need to depend on compile() and classpath() and then use the classloader to explicitly load your classes:

myClass = classLoader.loadClass("MyClass")

The reason is this is because some events run before compilation happens. You can't have it both ways.

Hi Graeme - that was why I was suggesting to not have it both ways.  I agree, the problem I get is because I depend on classes that haven't been compiled yet, and the _Events.groovy loads at the start of the script running process. Hence, errors - the classes aren't compiled yet - the problem is with the way _Events.groovy is used.

My suggestion is to introduce a naming convention to allow the *delay* of loading some event based scripts until a specific event fires. The GrailsBuildEventListener will find the files (all starting with _Event) but not load them until the named event fires (e.g. compile ends, the event CompileEnd fires, the GrailsBuildEventLIstener will look for any previously found files called _Event_CompileEnd.groovy and then load them with the current class loader, by which time the classes should exist). The effect as far as I can ascertain (but without actually doing it) is that it would have no side-effects and would be a clean solution to this particular issue. You may know that it can't be done and I'll run into all sorts of difficulty :-)

I realize that I could code my events using loadClass("full pathname to class") which would in this release (and previous releases) get around this problem, but it makes the events messy and hard to maintain. I will however be doing this so I get around the double compile problem, especially if the easyb stuff is to be useful to people now. This suggested fix is not necessary, but it would be nice. I will probably code it up in any case as I am already patching some of my scripts (the GrailsTest JIRA for example which allows all unit/integration/functional tests to be run in one go without having to run three different command line requests), and just attach it as a JIRA.

Richard

--
---
Richard Vowles,
Talk to me about development in Grails, Groovy, Java and for the Blackberry
ph: +64275467747, linkedin, skype:rvowles
get 2Gb shared disk space in the cloud - Dropbox, its incredibly useful! - http://tinyurl.com/cmcceh
podcast: http://www.illegalargument.com

 « Return to Thread: Extending the _Event system in Grails (Re: was something else I can't remember now)