Using GORM outside of Grails part 2 – Swing

In an earlier post I wrote about getting GORM to work outside of Grails. It worked, but wasn’t really usable since it could only execute script files, so its usefulness was pretty limited in a real application. Greg Bond replied on the mailing (here and here) with some great enhancements that allowed it to work without script files. So I fleshed that out some more and now have a working implementation and even a Swing application to demonstrate.

The key was that Greg used grails compile to generate his domain class files. I’d just been using the groovyc Ant task, and when I looked at what the Grails compile script the difference turned out to be that Grails uses a Grails-aware subclass of Groovyc, org.codehaus.groovy.grails.compiler.GrailsCompiler. It allows you to specify a resourcePattern attribute to point at the domain class .groovy files for special treatment.

So now instead of one sample project there’s three. One is the gorm standalone project, which creates a jar (gorm_standalone.jar) containing GormHelper which bootstraps GORM. The other two are the sample application, split into GORM domain classes and the Swing application. The domain class application contains the domain class .groovy files plus DataSource.groovy, and most importantly an Ant script that builds a usable jar (domainclasses.jar). The Swing application uses gorm_standalone.jar and domainclasses.jar as libraries and displays a simple UI showing the results of database queries.

Here’s a quick screen shot:

You can download the sample apps here:
GORM standalone app
Sample app domain class app
Sample Swing app

12 Responses to “Using GORM outside of Grails part 2 – Swing”

  1. Burt, I believe Peter Ledbrook is looking at standalone GORM for Grails 1.1, it would be great if you guys get in touch 🙂 we definitely would like to have it for Griffon apps 😉

    great work!

  2. Greg Bond says:

    nice work polishing everything up burt – i particularly like the generation of domainclasses.txt during the gormdomain build – that was just what was needed – i was moving in that direction myself after i discovered that ‘grails war’ generates a similar file ‘WEB-INF/grails.xml’ – now i won’t have to!

    btw you’re missing the build.xml for the gormswing project – just a simple variation on the one you used for the gorm2 project tho

    everything in your example worked for me up to the point where the swing frame is supposed to display its contents – i got a blank frame on os x – no big deal for me since i won’t be working with swing

    thanks again!

  3. Burt says:

    @Greg

    I was running the app in Eclipse so I didn’t think of creating a build.xml – I’ll do that. I’ll also try this on my wife’s Mac – I only tested in Linux and Windows.

  4. Greg Bond says:

    hi burt

    i’ve been using this code in a project now and i wanted to get logging working with it (e.g. to monitor hibernate behavior) – it wasn’t immediately obvious to me but after poking around i realized that grails and hibernate use commons logging to interface to the underlying logger – commons logging requires specifying the underlying logger implementation – to do this you can specify the system property on the java command line e.g. -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger (to use log4j) – to configure log4j one should also add a log4j.properties file on the classpath – grails will spew a lot of stuff to the console unless you explicitly configure the grails loggers in log4j.properties to silence them

  5. Adam Strickland says:

    First, I wanted to say thanks. I was able to make use of this for a quick-and-dirty ETL project… Worked pretty well, I’ve gotta say. The one hiccup I had was where I would get a stack trace with a confusing error message about application not being a property of DomainClassGrailsPlugin in GormHelper… Anyway, it turned out I was using groovy-1.6.0-B2 and when I switched to 1.5.6, everything was OK. I haven’t had a chance to investigate further, but I thought you should know there may be problems when compiling and/or running under Groovy 1.6.0.

    Thanks again

  6. laker2000 says:

    This totally awesome!!!
    Does it work with the latest version of Groovy?

  7. iongion says:

    Can you please update this with grails 1.0.4 and groovy 1.5.7

    This would be awesome to mix with a red5 application

  8. Burt says:

    @iongion – It works fine with Grails 1.0.4 – you just need to copy the appropriate jar files (grails-gorm-1.0.4.jar, etc.). I found that I also needed commons-logging-1.1.jar.

    Note that groovy 1.5.7 has some XML-related bugs that kept it from being used in Grails 1.0.4, so stick with 1.5.6 until Grails 1.1 which will use Groovy 1.6.

  9. iongion says:

    Thank you, made a confusion myself

    I am no java/spring and all those magic words expert, can you add build.xml in the gormswing sample so you can make mortals happy again 🙂

    Also, by looking at the source code in that sample i cannot understand how you can load the domain classes in the swing app without a GroovyShell, i’ve been trying this for 3 days/nights now … so frustrating

    Together with that, the strange configuration cannot find application.properties file, but the shell does create the tables in the db and inserts data

    How can you access the domain classes from the swing app simply by import ? 🙂

    What is the trick ?

    Thanks for your help

  10. […] know that other people might already had this idea and others implement this in a different way and Griffon is on the way, […]

  11. franz says:

    Great work!
    Could you please send me your build.xml file for the swing sample (it not included in the zip)
    (i created one, but it doesn’t run)

    Thanks,
    Franz

  12. Dan Stadler says:

    trying some of this code out, I’m also wishing I could see the build.xml for gormswing, can you send it to me?

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.