Grails 1.4: The new Resources stuff

I’ve recently migrated one of my applications over to Grails 1.4M1 (mainly because I was busting to try out the new testing stuff that Peter was writing about). In the process, I had a chance to kick the tyres on the new resources infrastructure that Marc has baked into 1.4.

So what’s the verdict? The new Resources framework is jam-packed-full-of-awesome! This stuff has significantly reduced the amount of js/css dependency nightmares that I have been dealing in my view layer. Not only is there far less code to write, I can now migrate between versions of libraries without the drama of updating any of my ref links.

Why use the resources framework?

If, for no other reason, you need to jump on the new resource dependency freight train. The new resources infrastructure radically simplifies dealing with all your JavaScript and CSS files and their dependencies. If you’ve ever nicked any of those slick jQuery components you find on the web (or even if you’ve just tried to spark up jQueryUI), you know the pain that it is to include all those script tags all over your head section (and making sure you get them in the right order).

What do I need to learn?

There’s only a few key concepts to learn:

How do I specify dependencies?

First things first. Let's say you want to include some jQuery component (such as this cute little [growl](http://labs.d-xp.com/growl/) library that I've been playing with for handling Grails messages). You'd normally copy the css into /web-app/css/growl and the js into /web-app/js/growl, then set about adding link tags into the pages that needed them. No more!

You now put all your resource definitions into /grails-app/conf/MyAppResources.groovy (or whatever you’d like to call it for your app, the convention needs to end in “Resources.groovy” though). Here’s a sample of what the file would look like:

modules = {
 growl {
        dependsOn 'jquery'
        resource url:'/css/growl/growl.css'
        resource url:'/js/growl/growl.js'
    }
}
[Read More]

First Experiences with QUnit

I’ve been incorporating more JavaScript into my Webapps lately, and have finally take some time out to have a look at some Unit Testing options for JavaScript. There’s quite a few solid libraries to look at including JSUnit, YUITest and QUnit (and many more BDD ones emerging), so the whole experience can be a little daunting at the start.

For my particularly used case, I wanted something very lightweight and easy to integrate into my existing framework. I ended up going with QUnit, basically because it was (a) simple; (b) popular; (c) consisted of two files; and (d) had plenty of “getting started” articles I could template off. I’ve been very happy with it so far.

If you have JUnit background and are curious what a JavaScript testing library looks like, check out a sample test:

test("You can't handle the truth", function() {
  var someTrueExpression = true;
  ok( someTrueExpression, "True expressions" );
});

test("Equals, and not so much", function() {

  var actual, expected;
  actual = expected = 2;
  equal(expected, actual, "Basic comparison" );

  notEqual(expected, 3, "Asserting not equals");

});

One thing I really like about QUnit is that it makes things very easy to get started. Basically you create a html file with elements for each of the test results, and QUnit looks after the runner and markup of the output. Just add a single QUnit.js file and a supporting QUnit.css and you have a:

QUnit test runner in action

So what does a complete QUnit test page look like? Here’s something you can cut and paste and get started:



   

   

 

 




# Basic QUnit Demo

## 

##
test markup

JavaScript has some special language support around equality (the string example is a classic since 2 == “2” in JavaScript, but 2 !== “2”). So you can do strict and non-strict equality:

test("Equals edge cases", function() {

  equal("2", 2, "Equals are not strict");

  // but this will fail...
  strictEqual("2", 2, "Strict type equals");

});
[Read More]

Making bookmarking of iPhone/iPad apps simpler

I’ve been doing some work for a Government client around making their cyber-safety info accessible to Aussie kids. One of the target devices is iPhone (actually Android is even higher priority since Android handsets are becoming far more common with kids, but that’s another bunch of excitement altogether).

The issue with iPod/iPhone bookmarking is that the Safari Mobile bookmarking icon is so ambiguous that very few people know what it does! We were after a solution for allowing the user to easily add the bookmark to their Home screen, but not get too naggy. Enter a cool little JavaScript lib called Mobile Bookmark Bubble. Here’s the app in action:

Bookmark popup in action

The library does the appropriate content-sensing so that it only activates on iPhone/iPad/iPod touch, and make use of some HTML5 local storage to make sure that if you cancel it away, it won’t come back again (using a nice fadeout after a few seconds of display).

The library ships with some example code to get you up and running quickly, and let’s you use custom metadata elements to point to a custom icon for this app (in my case 128x128 since I’m re-using the standard apple-touch-icon.png mechanism and that size looks slick rescaled on the iPad).

Anyways, it’s a pretty niche lib that does what it does perfectly. Bravo! Just what I was after!

[Read More]

Adding a JavaScript-based diff

I’ve been on the hunt lately for some JavaScript based diffing routines.I’ve had a look at a few of them, and the one I’ve settled on is google-diff-match-patch which they use in the Grails wiki! (Thanks Peter Ledbrook for the pointer!)

My particular need was around diffing a fairly complex “tree” structure. These tree structures are actually imported from files that 3rd party releases from time to time and client import them directly into our product (it’s not always easy to see what’s changed between the versions they release - hence the need for a quick “visual diff” solution.

Here’s an example of the one of these control libraries (the Australian ISM) being diffed (notice the token changes I’ve hacked into the version on the right):

GMARC Diff of ISM Versions

What I really like about google-diff-match-patch is that the API is very simple, and comes in a single JS file with no dependencies. In fact, the download has the algo ported to Java, JavaScript, C++, C#, Objective C, Lua and Python. How awesome! So if you need to scale up to some custom backend magic in Java, the same API is there for you.

In my case, I wanted to diff the whole tree in html (since I can generated the tree itself from a nice re-usable Grails taglib). So in the end I took the approach:

  • Generate the “left” tree contents in a hidden textarea
  • Generate the “right” tree contents in a hidden textarea (these two textareas are needed so I have somewhere to hold the unchanged text of the two trees)
  • Display the unchanged left tree on the left (960gs FTW)
  • Display the results of the JavaScript diff function on the right
    The library comes with a sample JavaScript “prettyHtml” diff which you can cut and paste and customise. Check out the demo that comes with the lib for a sample.

Loving the new diff feature! If you’re in the market for a diff capability for your app, this one is definitely worth having a look at (and it’s Apache2 so you can happily use it in your commercial app).

Thanks to Neil Fraser for an awesome lib!

[Read More]

Where have you been?!? - Glens Amazing Adventures in GRC Startup Land with GMARC...

Running Sign

I’m very tempted to reference the failed Aussie Tourism campaign here.. I’ve been dark in the public space for ages, but there’s been a lot going on under the radar! Half way through last year I hooked up with a few other business partners and started working on the Grails-based product for the GRC sector. You probably won’t know this space, but this stuff is super big in the Australian Government areas I’ve previously worked where compliance with the ISM is a big, costly and time-intensive deal.

Enter GMARC… The little Aussie GRC Grails app that could… (after launch we’ll have a website at http://www.gmarc.com.au, but the graphic designer is still busy working on it ATM)

GMARC Assets

Rather than your typical “Build the next Twitter” type startup, we’ve gone the other way. Instead of aiming big, we’ve aimed very small. Our key approach with GMARC has been:

  • Work in a small underserviced sector that you know really well, and where you have lots of relationships to spread the word
  • Develop a product that generates real $ savings to the client (some clients have estimated 2-3 Full Time Employees worth of savings annually)
  • Make a very complex process simpler. Much simpler. But don’t loose any of the nuance. (it has been invaluable having two highly respected GRC consultants as co-owners - this space needs hardcore realworld experience)
  • Road test it all in a real environment business away from the dev lab!
  • Don’t go broke on the journey

We've been slogging heaps of hours into this project over the last 9 months, hence my lack of contributions here (plus I've been flatout helping out with church stuff, but that's a whole other nightmare). What's most exciting is that we're now gearing up for our official product launch at [AusCERT](http://conference.auscert.org.au/conf2011/) - a big security conference out here in Australia that runs in May on the Gold Coast.

There’s still tons of work to do, and lots of Graphic Designer input still to come, but we’re now getting close to a 1.0 and it’s super exciting. We have a bunch of clients chasing us for pricing (which has been very validating), and we’re scampering to get all the basic features done and dusted before going Hollywood launch.

GMARC dashboard with charts of TRA coverage

So what sorts of Grailsy things does it make use of?

[Read More]

Some Personal Tech Goals for 2011...

Inspired by a mate’s list of tech goals, I thought I’d do some thinking about things I really want to get into in 2011. So here’s my current wish list of things to pursue this year (in no particular order).

Goals 2011

  • JavaScript - I’ve spent plenty of time with YUI in 2010 and just working with that library took my JavaScript skills to a whole new hardcore level. Matthew Taylor really excited me about JS when we had him on the Grails Podcast. This year, though, I want to make JavaScript my “learn a language” project and learn it well enough to be able to write a reusable library in it. I have some JavaScript consulting coming up early in the year so I’ve got plenty of incentive to step up. I’ve recently been reading JavaScript Patterns and found it a fantastic stretcher. Also picked up John Resig’s book on MEAP which has been a pretty hardcore read.
  • Android - I have a HTC Desire, and the company that I co-lead has a strong Android consulting business, and adoption is exploding in Australia, so there’s really no reason for me to not step up this year and put together a few simple apps. I’m full of ideas, so there’s plenty of motivation here.* HTML5/CSS3 - Outside of Android, I’m not sure I’ll be putting this to too much use given my common client platforms, but it will become an inevitable, so I’m keen to at least learn the new semantic markup tags and start working with them.* Grails Testing - I’ve made pretty solid use of the standard Grails unit testing support (which is fantastic), but I’m keen to explore the Geb and Spock stuff this year, and it’s been on my list for ages. Time to make it happen. Ditto for Gradle, since that’s coming to Grails in a big way soon…There’s also talk of a Second Edition of that Grails book for late 2011. Who knows what might happen there…
  • Graphic Design - This one is an evergreen. Though not just “web design”, but actually more general (and basic) design skills that I can use outside of a Web setting (including print and email). I’ve been spending a lot of time lately with the Non-Designers Design Book which has been an amazing primer on basic design principles. Just need tons of practice, and I have lots of opportunity in 2011.

But enough talk, need to go and make some stuff happen! Will keep you posted…

[Read More]

Creating Word docx documents dynamically from Grails (or Java :-)

I’m presently working on a little side project that needs to generate a bunch of customisable client reports. One format of interest to our target market is the ubiquitous Word document. Now I know that Word switched to an zipped XML format a while back, so it’s given me a chance to do some researching into what Java libraries might exist for creating and editing the Word xml format.

I’ve played with Apache POI before, but it’s Word support for the latest gear is a little scratchy, so I figured I would hunt around for an nice libraries that people have built for handling the new XML format natively. I didn’t have to hunt far to discover the conveniently named docx4j.

This library gives you everything you need to create/load/edit/write Word docx documents from Java, and comes with a Maven repo, online Javadoc, and nice set of Sample code. What more could you ask for? :-)

But I’m sure you’re just keen to see how to use it in your Grails app, right? Enough talk… First you’ll need to add a couple of entries to your /grails-app/conf/BuildConfig.groovy to make sure you pull in the dependencies. In the extract below, I’ve added a couple of new repos, and the dependency to pull in the docx4j jars:

    repositories {
        grailsPlugins()
        grailsHome()

        // uncomment the below to enable remote dependency resolution
        // from public Maven repositories
        mavenLocal()
        mavenCentral()

        // Details from docx4j Getting Started Guide at http://dev.plutext.org/svn/docx4j/trunk/docx4j/docs/Docx4j_GettingStarted.html
        mavenRepo "http://dev.plutext.org/svn/docx4j/trunk/docx4j/m2"
        mavenRepo "https://webdavclient4j.svn.sourceforge.net/svnroot/webdavclient4j/trunk/m2"
    }
    dependencies {
        compile 'org.docx4j:docx4j:2.5.0'
    }

With all our dependencies in place, we’ll just need to create some controller action that generates our Word document and sends it back to the client. I’m going to show one that queries my Asset class to pull back a set of assets and dump them in a Word doc:


  import org.docx4j.openpackaging.packages.WordprocessingMLPackage

  def exportWord = {
    WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage()
    def mainPart = wordMLPackage.getMainDocumentPart()

    // create some styled heading...
    mainPart.addStyledParagraphOfText("Title", "Asset Library")
    mainPart.addStyledParagraphOfText("Subtitle", "Generated at " + Calendar.getInstance().getTime().toString())

    // Add our list of assets to the document
    Asset.list().each { asset ->
      mainPart.addParagraphOfText(asset.name)
    }

    // write out our word doc to disk
    File file = File.createTempFile("wordexport-", ".docx")
    wordMLPackage.save file

    // and send it all back to the browser
    response.setHeader("Content-disposition", "attachment; filename=assets.docx");
    response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
    response.outputStream << file.readBytes()
    file.delete()

  }

With our document sent, the user can fire it up from their browser window. Here’s a sample of the output from above:

There are tons of things you can do with customising the documents in memory. I’ll be doing some work importing Word templates, customising their fields, then exporting the finished product to the browser. Fun times ahead!

[Read More]

Grails with Redis: Painless NoSQL

In the leadup to JavaOne, I whipped up a little competition app in Grails using the new Redis support. Graeme has written up a great GORM for Redis intro post that gives you all the info you need to get underway, so I’ll just add my experience to those interested. Here’s my little JavaOne competition app in action:

Redis Giveaway App

If you haven’t played with Redis before, think of it as a supercharged memcached. Except, of course, It’s persistent, and the values can be sets and lists.

First up, you’ll need a redis binary to run on your system. I’m developing on Windows these days, and you can get a Windows Redis binary. It’s just a few files that you can unzip where you please. Fire up redis-server.exe and you’re off and running

As for the Grails side, you’ll want to “grails uninstall-plugin hibernate”, since I’m using just redis without any other SQL stores. Check out Graeme’s post to learn how you can mix and match hibernate and Redis in the same app.

So what other changes did I need to make to store my data in Redis? None! Graeme’s done all the hard work of implement all the basic GORM stuff (including dynamic finders) right into the Redis plugin! Rock on!

One neat feature I did take advantage off is Redis unique support for random() selection of data elements. For example, I selected my competition winner by implementing the following code:

def winner = {

        log.debug "Generating random winner"
        Submission winningSub = Submission.random()
        render "${winningSub.contestant.name} - ${winningSub.dTerm.name} ${winningSub.sTerm.name} ${winningSub.lTerm.name}"

    }

So the takeaway from all this is… if you’ve been keen to explore some NoSQL options, there’s never been a better time to have a look at Grails Redis support and dip your toes in the NoSQL waters. Having GORM support makes it insanely easy to experiment with your NoSQL store.

If you want to see how Grails is interacting with Redis, fire up redis-client, then issue the “monitor” command. You’ll see how GORM is doing its reads and writes against your Redis store. Interesting sets flying around the place!

[Read More]

When Elephants Dance - Why Java EE6 is not your Grandmas Enterprise Stack ... in Canberra next week

I’m speaking at the ACT Oracle User Group conference next week on Java EE6. Normally I’d do a talk on Grails to this set (and pump the book of course!), but Peter McNeil had already put a Grails talk together and I was already brimming with other ideas…

Anyways, I’ve been doing some client work on JEE6 lately, and I was so inspired by Adam Bien’s live coding talk at JavaOne 2010, I’m going to try and reproduce it locally! It’s no Grails, but EE6 gets a lot of things right! I’ve been very impressed with it. The abstract for the talk is:

Elephant Dancing

Learn some of the new enterprise features in Java EE6 which can dramatically improve developer productivity by eliminating boilerplate code, fully embracing annotations and adopting the “convention over configuration” paradigm. In this live-coding workshop, we’ll develop a scalable, data-driven, multi-tier, REST-exposed, JSON-friendly Web2.0 application from scratch in under an hour!

Should be a really fun talk! I’m going to follow Adam’s pattern start from a clean slate and just code for an hour talking about EE6 features as I add them:

It’s going to be a great time. So if you’re free and in Canberra on Wednesday the 14th October, drop on by the Dev Stream at the Oracle JUG at 10am for the talk.

The code will be flowing freely… I hope…

[Read More]

Groovy/Grails BOF at JavaOne promises a full on double rainbow experience

Public Domain Double Rainbow

The plans for the Groovy and Grails BOF at JavaOne are coming together and it’s gonna be a “full on double rainbow all the way across the sky“ experience. “But what does it mean?” Glad you asked.

This year we have our most comprehensive panel ever! We’ll have Peter Ledbrook (Grails Evangelist and Grails in Action co-author), Guillaume Laforge (Groovy lead), Andres Almiray (Griffon lead and Griffon in Action author), Dierk Koenig (Groovy in Action lead), and hopefully a few more surprises.

This is a fantastic opportunity to catch up on the latest product roadmaps for Groovy, Grails, and Griffon, and ask questions from the floor directly to the product leads themselves! It’s going to be one fantastic get-together for the Groovy community.

Of course we’ll be recording it all for a special edition of the Grails Podcast and we’ll be kicking on to a local haunt to test out our awesome Pool skills while shooting the breeze on metaclasses, mixins and other Groovy love.

Make sure you put it in your schedule (S313988) for 7:30 PM on Monday night! It’s gonna be fantastic! See you there!

[Read More]

Introducing thriftebook.com: 400 LOC on Google App Engine

I’ve just launched thriftebook - a tiny ebook deal aggregator that can make the task of not spending money on ebooks… well.. harder. The whole app is written in Gaelyk - a groovy-based web framework that makes writing Google App Engine apps a complete snack. The site is basically a clone of the excellent Ruby techbooksdotd but with a few essential enhancements that I was craving.

Thriftebook screenshot

For one, you can now subscribe to a single RSS feed to get all the deals directly into your aggregator (neither of Manning or Apress provide feeds right now), or start following @thriftebook to get it straight into your twitter feed. The Website also shows how long the deal has been active, so you can get a feel for when it’s likely to expire. It’s been a fun little journey, greatly sped up by Rob’s excellent Regexp work for each of the publishers documented here. I wrote the majority of the app in a single day last Tuesday, and then patched in a few extra features as I thought of them. So the Gaelyk learning curve is pretty low.

In about 400 LOC, built with the excellent Gaelyk maven plugin, I’ve managed to incorporate:

thriftebook logo

So how has my first major Gaelyk development experience been? Pretty great once I got underway. It felt a lot like hacking on a php app: really quick to get going, but I’m not sure I could scale it out to an app of any size (I know you can write tidy php apps that maintain like a dream, I’m just saying that I never had the discipline for that). The Gaelyk samples encourage you to following a MVC approach, and but it doesn’t presently provide any direct unit testing support, so the temptation to just “hack something up” is pretty strong, meaning I’d probably switch to a Grails app if I was going to write something any bigger than a few actions. (shameless plug: If you’re looking for a fun Grails book, go check mine out).

I have to say that the docs were absolutely first rate, and the maven plugin made local and remote deployment super simple (mvn gae:run and mvn gae:deploy). If you’re keen to have a look at the source, it’s all on BitBucket for your cloning pleasure. You don’t need anything installed locally except Maven. Fire off a “mvn gae:run” and it’ll pull down the internet for you.

Try not to buy too many ebooks… At about $10 a go, my current library greatly exceeds my reading pace, but it’s so hard to turn down a good bargain…

[Read More]

Funky Grails Tables with YUI DataTable

Need to present some tablular data sources from a backend datasource and looking for a slick and painless cross-browser solution? You should definitely take a look at the YUI Datatable.

I’ve been using it on one of our internal projects where we need to present a large amount of tabular data, but let the client sort it as they need to. Our first draft has given us something like this:

YUI Datatable in action

One of the things I love about YUI is that the documentation and examples are first class, and with the new Dependency Configurator, getting your CSS and JS organised is much more straightforward.

You can use YUI controls with backend JSON, XML or even in-page HTML elements, but for my purposes, I needed to dynamically change the table based an incoming specification id. My backend controller does a little magic on how it slices the data, but you might consider a simpler version as something like:

def listData = {

        def lib= ControlLibrary.get(params.id)
        def controlsJSON = [ controls : lib.controls]
    render controlsJSON as JSON
}

So we’re just rending some JSON which consists of a key of “controls” followed by a list of individual controls (which is the data you see in the cell). Now, on to the client side. First go and sort out your dependencies, then we’ll get cracking with some Javascript…

You’ll need a div for the control to land in. So let’s spec that out first:

Ok. We have our div in place, it’s time to setup the Datatable code itself. YUI components typically rely on a DataSource component to do the actual fetching of the remote data. You tell the DataSource what kind of data to expect (JSON, XML, etc), give it a way to map the incoming data into something useful to you, and then feed it to a UI component for rendering. Let’s take that one step at a time… First let’s setup the DataSource… we’ll wrap it in a window “load” event to make sure it doesn’t fire until the page is loaded and all our JS and CSS is pulled down…

[Read More]

Book Review: Beginning Java EE6 Platform with Glassfish 3

I really have to take a rest from this book review business, but I’ve been buying heaps of books through Tech Books Deal of the Day, and this title has been very useful for one of my current bits of client work.

First of all, Beginning Java EE6 with Glassfish 3 came at a time when one of our clients requested we implement some JPA2 infrastructure (around EJB3) for them to store some JAXB objects coming in via JAX WS. I’d worked with earlier versions of all of these technologies, but wasn’t across the latests spec changes in EE6 (which was their preferred stack). So this book was a $10 special on exactly the right day for me!

First of all, this a a sensational title if you have a background in Java EE. It was basically the single volume I needed to get up to speed with all the latest specs in a hurry. The examples are all Maven-based, the sample code is simple but genuinely representative of the kinds of issues I was facing, and the coverage is across the whole EE stack.

JEE 6 Specs

What I like most about this book, however, is the the author has done a lot of thinking about the ordering of concepts. The book progresses nicely from JPA2, through EJB3, Transactions, the Web Tier (JSF) and finally on to JMS, Web Services , REST, and JAXB. It’s all covered in the right order! (We spent a lot of time writing and rewriting in an attempt to get this right in Grails in Action so I appreciate the effort). The author is on the EE, JPA2 and EJB3.1 Expert Groups, and it shows in this material. Lots of useful tip and tricks covered along the way and heaps of things I didn’t know.

Be warned though, you need to know that this is a book on “Beginning Java EE6” even though the byline is “From Novice to Professional”. Even in 450 pages, you won’t find a deep-dive on all the EE concepts, or detailed coverage of every JPA option. The only point this really bothered me was in JPA2 Criteria Queries. This is a pretty important new part of the spec, but doesn’t get a mention here. Even things like TypedQuery didn’t get a shout-out. (BTW, if you’re looking for a JPA2 Deepdive, Java Persistence with JPA is a great, albeit dry, alternative -which is also a $10 ebook!).

If you have some background in Java EE, but you haven’t made the switch to Java EE6, this is definitely a title that you’ll want in your library. A fantastic survey of all the APIs, with genuinely useful background tips, source code and discussion.

Interestingly, it seems that Apress have dumped their DRM protection on PDFs (used to have a password-based protection), which is great news for everyone using PDFs on their e-readers etc.

If you’re looking to step up to JEE6, this one is definitely worth having… and if you can snag it for $10 it’s a fantastic bargain!

[Read More]

Book Review: Groovy for Domain Specific Languages

The guys over at Packt Press sent us a free copy of Groovy for Domain Specific Languages to review for the Grails Podcast. We’ll talk about it in an upcoming show, but I promised the publisher I’d post some thoughts on my blog, so here we are.

First, I’m super excited that there is now a title dedicated purely to Groovy DSLs. Every time I hear Guillaume or Paul talk about DSLs I get swamped with great ideas that I’ve never really had time to explore. This book gave me a reason.

First up, this book is intentionally targeted to straight Java devs, so you get a lot of space devoted to things that even casual Groovy guys will already know. There’s the inevitable chapters on Groovy essentials, using the standard builders, Java integration, and even a chapter devoted to teaching you Grails! (Some people spend entire books on just that.. but I digress. :-) To be honest, this would not serve you well as your first book on Groovy, so I’m not sure some of that content was well served, but I can appreciate the publishing pressure of reaching the widest possible market, and the background material is well done.

What I was really hoping for was some coverage of DSLs from the ground up, and I was really happy. The author spends the whole of Chapter 3 just on closures. This is well worth it. There are tons of edge cases here that I wasn’t across and I really appreciated the dedicated coverage. Straight after you do the closure deepdive, you spend chapter 4 retrofitting Twitter4j with your own basic DSL. The examples were exactly at the right level here, and I was loving the gentle pace.

Groovy Book Review logo

Chapter 5 moves on the standard Groovy builders to give you a feel for some of the standard DSL strategies, but this is space better covered in the Groovy in Action though usefully introduced here. Seasoned groovy programmers will find this fairly skippable, but Java guys will be served with a good grab-bag of examples to clarify their thinking.

Chapter 6 is another “examples” chapter, this time with Grails, Gant and a range of BDD tools (including Spock). Given the spread, the chapter is long but terse and I would liked to have seen chapters 5 and 6 collapsed into a tighter chapters of “DSLs to shape your thinking”.

Chapters 7 and 8 deep dive on Builders and writing your own DSLs - and these represent the heart of the book. Tons of good information in here that you’ll only find scattered across tons of Wiki pages. The book is worth it for just these chapters. You’ll get a feel for builder strategies, along with a “from scratch” DSL creation. Great info in here. I’ll reference these chapters a lot when I work up the keen-ness to tackle my own DSL.

Chapter 9 rounds out the book with a Java/Groovy integration chapter. Didn’t quite follow how this linked to the DSL concept, but if Packt were only going to release one Groovy book, I can see why this material would be good background.

Overall? I’m really glad there is now a dedicate DSL book! This a really solid book on Groovy DSLs that doesn’t shy away from the hardcore material, but still gives you the stepping stones to get there. To be honest, this book could be a lot better if the editing team took a knife to a lot of the non-DSL stuff and sharpened up the book’s vision. The material is well-written though, and I’m sure I’m going to be referencing this material.

[Read More]

From WSDL to JAXB to JPA with a single schema: Adventures in Hyperjaxb3

The client brief was pretty straightforward: “We need to persist this class to a Database”. No problem. Hibernate FTW. “Actually, we’d like to use JPA2 to do the heavy lifting”. No problem, we’ll annotate up the domain classes with a few @Entity statements and we’re off. “Oh, and the class we wish to persist is a graph of objects that are dynamically generated via JAXB”. Things are getting a little more complex now.

Converting your WSDL into a set of JAXB classes is super simple: Use your IDE or the very cool Maven Plugin to get you up and running. But annotating those generated classes with JPA annotations on the way through? That’s a little trickier. Fortunately wsimport just invokes the JAXB compiler (xjc) under the cover, and xjc support plugins! You know where we’re going here..

Enter hyperjaxb3. This very slick little library provides an xjc plugin to markup your JAXB classes with JPA annotations. It will even generate your persistence.xml file on the way through. Most importantly of all, it knows how to work around the known incompatibilities between JPA and JAXB (for example, that your xsd:date entries will get marshalled to javax.xml.datatype.XMLGregorianCalendar which is not a supported type for JPA).

Hyperjaxb3 works around things like this by providing @Transient getters and setters for the XMLGregorianCalendar methods, whilst generating native JPA getters and setter for standard java.util.Date classes. Very tricky.

Hyperjaxb will also handle collections cleverly, so List gets marshalled off into its own 1:m table. It means that complex graphs will generate lots of tables, but everything worked for my prototype which was a very complex schema. I’m super impressed.

It took several days to get all the dependencies right, and it did involve copying jaxb and jaxws jars into my jdk1.6.0/jre/lib/endorsed folder - so I thought I’d put up a sample POM below to save you some time. I’m not sure that the approach is even going to work under scale, given the complexity of the graph. I suspect we’d be better off writing a simple JPA wrapper class that we marshall via Dozer but it does give the client their preferred option to explore.

To save you some time, I’ll provide a Sample Maven POM that is wired up for everything you need to get yourself up and running. Just add your favorite IDE and you’re in business. Oh, and don’t forget that /jre/lib/endorsed thing.

Happy transformations!
</p

[Read More]

My NoSQL for Java Devs slides are now online

Had a fantastic time last night with the guys at my local Canberra JUG giving a presentation titled “NoSQL - Life Beyond the Outer Join”. I’ll embed the slides further down.

In the talk we looked at four different types of NoSQL options for Java guys: Simple Key/Value in-memory stores (Memcached), sophisticated distributed key/value stores (Voldemort), Document Databases (CouchDB), and Graph Databases (Neo4j).

We also had a play with the common java clients that are used to access them: Spy for Memcached, Ektorp for CouchDb, and the bundled libs for Neo4j and Voldemort.

I’ve put all the source up on a Bitbucket Project which you can clone. It’s all Mavenized, so you can knock yourself out with whatever IDE works for you. If you’re on Windows, I’ve also included links to the download pages of the various servers on the BitBucket Wiki Page for the project.

**[NoSQL - Life Beyond the Outer Join](http://www.slideshare.net/glen_a_smith/nosql-life-beyond-the-outer-join "NoSQL - Life Beyond the Outer Join")**
View more [presentations](http://www.slideshare.net/) from [glen_a_smith](http://www.slideshare.net/glen_a_smith).

Thanks to the bunch of folk that turned up on the night. It was great crowd full of interesting questions and discussions!

Happy NoSQLing!

[Read More]

Adding a Check For Updates button to your Air application

Adobe Air ships with a sensational “Application Update” framework that makes deploying new versions of your application a snack. There’s even a fantastic blog post on how to get started using it. Typically it just involves throwing a few lines of ActionScript into your app’s startup:

var appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
appUpdater.configurationFile = new File("app:/updateConfig.xml");
appUpdater.initialize();

Then throwing together a few lines of updateConfig.xml to set the period of the check and which error dialogs to suppress or show.

But what if you don’t just want the “auto update” feature? Perhaps you want to let your client manually check for updates (for example because your autoupdate frequency was deliberately set to a long time period between checks, or perhaps you put out a patched version to immediately fix a problem for a particular client). For these case, you want some kind of “Check For Updates” button on your About dialog (or wherever). Perhaps something like:

Check For Updates Button

For this kind of scenario, you’ll want to step out of the ApplicationUpdaterUI class, and into its lower-level cousing ApplicationUpdater (sans UI).

The ApplicationUpdater class gives you the low level plumbing to hook into each stage of the update check (is there a new version available? Was the download successful? etc) and override each stage as you see fit. This gives you the ability to display, for example, a custom confirmation dialog to see whether the user want to upgrade to the new version. You can also access the release notes section of your server side updateDescriptor.xml file. For example, I have something like this:

A custom Update Available dialog

Nice. So how do you go about implementing such a beast? Here’s the sample code from the dialog above to give you a leg up on implementing your own custom update logic:

protected function checkForUpdate_clickHandler(event:MouseEvent):void
{
    checkForUpdate.enabled = false;   // disable "check for update" button till we're done
    appUpdater = new ApplicationUpdater();
    appUpdater.configurationFile = new File("app:/updateConfig.xml");
    appUpdater.addEventListener(StatusUpdateEvent.UPDATE_STATUS, afterUpdateCheck);
    // Not sure which update "fail" event might fire, so we catch them all
    appUpdater.addEventListener(StatusUpdateErrorEvent.UPDATE_ERROR, afterUpdateError);
    appUpdater.addEventListener(ErrorEvent.ERROR, afterUpdateError);
    appUpdater.addEventListener(DownloadErrorEvent.DOWNLOAD_ERROR, afterUpdateError);
    appUpdater.initialize();
    appUpdater.checkNow();
}

protected function afterUpdateCheck(event:StatusUpdateEvent) :void {
    if (event.available) {
        event.preventDefault(); // suppress the auto-download functionality
        // prompt user for update, event.details[0][1] holds the release notes from the XML
        Alert.show("An updated version is available: v" + event.version + "." +
            event.details[0][1] + "nWould you like to update?", "Update Available",
            ( Alert.YES | Alert.NO),
            this,
            function(event:CloseEvent):void {
                if (event.detail == Alert.YES) {
                    // The default update mechanism will replace the item
                    appUpdater.downloadUpdate();
                } else {
                    // No update required, exit here and re-enable check button
                    checkForUpdate.enabled = true;
                }
            },
            null, Alert.YES, null);
    } else {
        checkForUpdate.enabled = true;
        Alert.show("No updates are available at this time",
            "No Updates Available", Alert.OK, this, null, null, Alert.OK, null);
    }

}

protected function afterUpdateError(event:ErrorEvent) : void {

    Alert.show("Error updating application. The error is: " + event.text +
     " (error " + event.errorID + ")", "Error During Update", Alert.OK, this,
        null, null, Alert.OK, null);
    checkForUpdate.enabled = true;

}
[Read More]

Netbeans and solving the dreaded unrecognized project; missing plug-in?

I has recently handed a truckload of client code all developed in Netbeans. In order to get a feel for the interdependencies, I thought I just fire up a clean copy of Netbeans and open it up. Unfortunately, when I tried to open the project I was greeted with the dreaded “unrecognized project; missing plug-in” dialog:

Unrecognised project; missing plugin

Hmnmm… That’s ok, so I thought I’d just open it up as a “free-form” Netbeans project, but that’s no good either, since Netbeans complains that it’s already a Netbeans project! After a bunch of mucking around, I found out the problem, I was missing a plugin! But which one?

If you open up the nbproject/project.xml file will have something like this:


    org.netbeans.modules.j2ee.earproject
     ...

So that type element gives you the skinny. In this case I was missing the EAR project plugin. Once that was installed, the project opened up just fine. Thought I’d blog it up to save you some google time down the track.

Happy recognised projects!

[Read More]

Canberra JUG Talk: NoSQL - Life after the Outer Join

Next month I’ll be hanging out at my local JUG giving a bit of a survey of the main players in the NoSQL movement along with the Java APIs that they offer. If you’re in or around Canberra on July 14, make sure drop by for a great night of Pizza and Java Geekery with a somewhat schemaless design ;-)

Will slideshare up the slides, and bitbucket up the source, once I’ve done the presento. Till then, here’s the blurb for the night:

Abstract

The NoSQL (Not Only SQL) movement has been gaining a lot of press over the last year as a means of scaling massive data storage, complex relationships and lightening fast retrieval for the Web’s biggest sites. This month we’re taking a trip to the big end of town and looking at some of the backend technologies that are powering sites like Twitter, Facebook, LinkedIn, Reddit, Digg and Google.

We’ll be looking at popular Java clients and servers that play in the NoSQL space and have a brief survey of the following popular NoSQL platforms: Document Databases (MongoDB/CouchDB), Sophisticated Key/Value Stores (Cassandra), Graph Databases (Neo4j), and simple Key/Value stores (Memcached). It’l be a lightening tour of what each technology offers, some source code on how it works, and lots of headshifts about how to store data such that you don’t ever need another Left Outer Join!

We’ll have a lot of fun, you’ll pick up a bunch of new buzzwords to impress your peers, and you can take the source away and play at home. Put it in your diary now!

About Glen

Glen is a hardcore Java trainer and speaker, co-author of Grails in Action from Manning, co-host of the Grails Podcast and a former winner of Australian Masterchef. He blogs at http://blogs.bytecode.com.au/glen and Twitters at http://www.twitter.com/glen_a_smith.

[Read More]

Hacking your first Air application

I’ve recently been developing some concepts for a client that needed to be a cross-platform rich client app. By nature I’m a Java guy, but because of the target environment, getting a JRE on the target platform was going to be problematic. So we were looking into other cross-platform alternatives, and the install/update experience with Air seemed to fit the bill (I would love to be doing it in Griffon, but the JRE install experience is just too clumsy for our target - hopefully Oracle will fix that in v7!).

The platform is really easy to get productive on. You can write Air apps in Flash or Flex or just plain old HTML/CSS/Javascript/jQuery. If you choose the html route, there’s very few new skills to learn and you’ll have bundled up your first app within a few minutes of getting started.

You have a known Webkit engine bundled with Air, so there’s no special html/css/js hackarounds required - and you can just use good old jQuery for your interaction magic. Even the install badge generation and automated update experience is super easy to get going. The developer experience is fantastic. They’ve really done some good thinking here.

So how do you get started with Air? Here’s a few jump points that I’ve collected to get you started:

I’ve found Aptana studio to be a fantastic way to which up and run prototypes from directly within the IDE. The plugin also gives you a new /Export/AIR Application option from Eclipse which makes the bundling of your .air file very straightforward

As for supporting books? I bought the ebook for Adobe AIR 1.5 Cookbook and it has been a fantastic resource. It gives almost all the cookbook examples in both Flex and Javascript so you can quickly take advantage of lots of the common operations (tray integration, start on logon, custom window shapes, etc). I’ve really enjoyed it and found it an awesome reference.

You can also download an older title which is completely free called Adobe Air for Javascript Developers Pocket Guide. A much smaller title which gives you a good taste for the platform none-the-less.

Anyways, I’m having a fantastic time with Air and have found the learning curve (using the HTML/Javascript model) to be extremely straightforward - productive from day one. I’ll report back on the Air front once our app actually goes live for the client. It’s a Government initiative, so there’ll be lots of press when the launch happens!

Can’t wait to play with some options for a Grails backend that fronts an Air client. It’s definitely on my list!

[Read More]

Switching to ebooks for a year... the journey so far

I’m never been much of a fan of ebooks - “But I prefer paper…. I can read a real book in bed… blah blah”, but I’ve never given the whole ebook thing a real solid shake. So this year, I’ve made a decision to only purchase ebooks for a year. No exceptions. So how’s the experiment going so far? Great! Things I’ve learned so far…

How do you handle the “screen only” reading thing?

Not perfect, but totally good so far. A few months back I bought a Netbook (eeepc 1001ha) as part of my transition to be completely “post-mac”. It’s been the most amazing device ever. Small enough to carry around, but enough heart that I’ve done Java, Grails and Dotnet development on it without a hitch. Not as a main machine, but as a “sync to revision control” beast that I can use on the road, it’s perfect.

No so interested in a dedicated ebook reader (kindle,nook,ipad). Having a real PC with me means that I can copy and paste code samples from ebooks and try them out. But what about reading in bed? Well. That’s part of the joy. I read tech books on my PC when I’m in work mode, not when I’m supposed to be hanging out with my family. It’s better sleep hygiene too.

Seriously, do you end up reading the entire book onscreen?

So far, probably not. But I didn’t read the real thing cover-to-cover either. Now days I tend to read the intro chapters then the stuff I want to get done. Tend to do a lot more scanning than I did with a real book, but so far I’ve been trying out a lot more than I previously did - which I’m sure is a better learning experience.

Some books definitely lend themselves more to ebook format. Something like jQuery Cookbook is ideal in ebook format since you’re typically trying to get something specific done and it’s easy to search/reference it directly. I’m not sure something like The Pragmatic Programmer would work as an ebook. That book is a “cover-to-cover” deal, with reflection rather than hacking the key idea. Will have to wait and see whether I digest anything like that in e-format.

Are you spending more time searching your ebook library for stuff?

Interestingly, I still find I’m spending more time jumping from the table-of-contents rather than searching - seems quicker when I just “remember” the part of the book I read things in.

What about the cost?

[Read More]

The Canberra Java Roadshow

I’ve long been a big fan of our local Canberra Java User Group, so when Peter McNeil said he was keen to put together a little travelling Java roadshow, I jumped at the chance. He’s even got a basic flyer in place.

So here’s the deal: a few of the guys from our local JUG will be running free Java info sessions during lunchtime in Belconnen, Civic, Woden and Tuggeranong over the next few months. If you’d like to get some guys from your workplace along, make sure you touch base with us ASAP.

Whilst the first one is just on quality in the development cycle, we’ve got plans for a few different themed sessions (which I’m sure that Grails will make an appearance in :-). If you’re keen to find out more, make sure you check out the CJUG Website.

I’ve been very quiet on the blogging front, since I’m working hard on a new startup project (shrink wrap, not Web 2.0 ;-) – more details on that soon. I’ve also decided to follow Dean’s lead in blogging once a week in 2010. Let’s see how far I get…

[Read More]

Hacking Custom Authentication Providers with Grails Spring Security

The Grails Spring Security plugin totally rocks. Even though Peter wrote a fantastic chapter on it for Grails in Action, I’ve always been a bit scared of it (based on some early bad experiences with the raw acegi codebase which was pretty insanely complex to get going).

Anyways, I’ve had cause to revisit Spring Security for a new client project, and had some tricky corner cases to solve. In particular, the app exposes a REST API that needs a special custom security provider. The client is issued an API key when they purchase the COTS product, and (basically) a hash of this key is remoted by a rich client during the authentication process so they can access backend services. However they can also access parts of the app using a normal browser interface with a user password and standard “remember me” features.

So the trick was supporting both a custom auth mechanism for certain URLs (eg /api/**) whilst maintaining usernames and passwords for the rest of the app. Turns out that it’s all totally doable. First a disclaimer: I know next to nothing about Spring Security, so there is probably way better ways to accomplish this, so feel free to give feedback about how I could simplify all this so that future googlers can benefit. That aside, here’s my crack.

Ok. To get all this custom stuff happening you’ll need to implement a few things:

Let’s start with the easy stuff and define our custom Authentication object to hold our credentials. You really only need a custom one so that your AuthenticationProvider class can answer true to supportsClass(auth), and there’s probably good adaptors I could have subclassed. Given I’m doing it all in Groovy, it’s very concise to implement the entire interface anyways:

import org.springframework.security.*

class CustomAppTokenAuthentication implements Authentication {

    String name
    GrantedAuthority[] authorities
     Object credentials
     Object details
     Object principal
     boolean authenticated

}

Ok. We have our “holder” for the credentials that the user is going to present. I’m going to populate the credentials and principal details from the incoming http request. It’s the role of the SpringSecurityFilter to do that scraping, then fire off the AuthenticationManager‘s pipeline of AuthenticationProviders. Here’s rough crack at a custom Filter:

import org.springframework.security.ui.*
import org.springframework.security.context.*
import org.springframework.beans.factory.*
import org.springframework.context.*
import javax.servlet.*
import javax.servlet.http.*

class CustomAppTokenFilter extends SpringSecurityFilter implements InitializingBean{

    def authenticationManager
    def customProvider

    void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {

        if (SecurityContextHolder.getContext().getAuthentication() == null) {

            def userId = request.getParameter("userId")
            def apiKey = request.getParameter("apiKey")
            if ( userId && apiKey ) {

                def myAuth = new CustomAppTokenAuthentication(
                        name: userId,
                        credentials: apiKey,
                        principal: userId,
                        authenticated: true
                )

                myAuth = authenticationManager.authenticate(myAuth);
                if (myAuth) {
                    println "Successfully Authenticated ${userId} in object ${myAuth}"

                    // Store to SecurityContextHolder
                    SecurityContextHolder.getContext().setAuthentication(myAuth);
                 }
            }
        }
        chain.doFilter(request, response)
    }

    int getOrder() {
        return FilterChainOrder.REMEMBER_ME_FILTER
    }

    void afterPropertiesSet() {
        def providers = authenticationManager.providers
        providers.add(customProvider)
        authenticationManager.providers = providers
    }
}

There’s a little magic going on here in afterPropertiesSet() where I add my custom AuthenticationProvider (which is going to actually validate the token) to the existing pipeline of AuthenticationManager providers. I was hoping to do that via configuration, but couldn’t find out how. Ideas?

With all that in place, the actual logic of firing the request is pretty straightforward. If there is a userId and apiKey coming in, and the user hasn’t already been authenticated, you create your custom Authentication holder object for the credentials (contrary to what you might think, the authenticated: true attribute means “I should be inspected by an AutheticationProvider classes” NOT “the user has been authenticated with this credential”). Traps for young players.

[Read More]

Getting Sitemesh running on Google App Engine

I’ve been having a ball playing with Gaelyk for developing Google App Engine applications in Groovy. One thing that Gaelyk lacks (for now! It’s only 0.3.x) is any kind of layout engine like Sitemesh. No probs, just add then the Sitemesh filter to your web.xml and you’re off, right?

    <filter>
        <filter-name>sitemesh</filter-name>
        <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>sitemesh</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Well that would be wonderful! However Sitemesh 2.4.1 has some integration points with JNDI that will bring you a world of hurt (Error 500) along with a description in your app engine logs. It will work locally, but fail once deployed. For later googlers, here’s the strings from the appengine logs:

Error for /
java.lang.NoClassDefFoundError: javax.naming.InitialContext is a restricted class. Please see the Google App Engine developer's guide for more details.
    at com.google.apphosting.runtime.security.shared.stub.javax.naming.InitialContext.(InitialContext.java)
    at com.opensymphony.module.sitemesh.Factory.getEnvEntry(Factory.java:91)

Fortuntely some kind soul has documented the fix for that Factory class already. Followed those instructions and I was in business.

Now, onto some more DataStore experiments with Gaelyk…

[Read More]