Being on a Flex training course these last few days – the course I’m following is dispensed by Baao and it really rocks ! However, even if Flex looks great, I think they are some more enhancements needed for me poor and lazy Java/Groovy developer. And that’s the reason of this new post !
Flex provides a very convenient and performant way to realize client/server communication through the mechanism called
RemoteObject. It is indeed a technique based onto a binary format that Adobe has now opened since December 2007 : the Active Message Format (see Wikipedia for more details), that is used to serialize objects and to convert them from Java to ActionScript back-and-forth between the server and the client.
The problem here is that ActionScript classes should be explicitely written in order to be bound at runtime onto the corresponding Java server classes (or Groovy through the Grails Flex plugin). This implies you need to your model classes written in Java or Groovy into ActionScript classes – which is a very unpleasant and ungrateful task …
There are some attempts at automating this task and some of them seem to be good solutions (see Farata DTO2FX or Enunciate). But these solutions only target Java beans and not Groovy ones ! Moreover, Groovy solutions seem to need much more engineering before being integrated into existings bundles (see this mail). So I decided trying to write such a little tool for not having to duplicate a Groovy bean into an ActionScript DTO.
Note : it’s not ‘the’ solution. Just mine … and I just spend 2 hours on it. My idea here is just to use some
MetaClass facilities of Groovy (see previous post). I start created a
AS3BeanGenerator Groovy class that will generate the AS3 code from a Groovy Object definition.
The main method of this class looks like this :
Because, it’s a simple bean, there’s only 3 parts : first generate the header of code (package and class declarations), then generate some stuffs for each Groovy property and finnaly append the footer (ie: the closing curly braces). Note here that I have added 2 extra lines corresponding to id and version properties that are dynamic properties added by Grails runtime.
Now the property generation method :
EXCLUDED_PROPERTIES being a list of properties added by Groovy or Grails runtime and
JAVA_TO_AS3_TYPES being a correspondance map between Java and AS3 types:
Finally, here’s the code for the header generation :
Note that I make the AS3 bean class
Bindable and that I bind it onto the
RemoteClass those FQN is done using the Groovy object class name.
So OK, I now have a base generator but how to integrate it with development tooling ? Answers to this questions may be different depending on your favourite tool… You may want to integrate it with Ant or Maven or … (whatever else). I simply choosed for the moment to integrate it within Eclipse using a simple launcher that I placed as one of my favourite and that directly processed the currently selected resource.
Here’s the glue code I write for this launcher (also in Groovy !) :
It’s a nasty piece of code (OK I agree but remember : only 2 hours) that assumes the following :
argsis the full path of the selected Groovy class. It contains ‘/’ characters and is usually located into
src/groovydirectory (remember : Grails or Groovy)
argsis the path to the source directory for generation. Something like a
src/main/flexinto a Flex project
argsis the name of the target generation package (maybe different that the package of the domain class)
A convenient way to configure the launcher is to use the prompting facilities of Eclipse like this :
Maybe I’ll try the other integration solution later on. Hum, already thinking of a Maven plugin to do this job … Let me we know if someone has some interest in it.