Making Java Groovy at JavaOne 2013

Monday morning I gave my “Making Java Groovy” presentation at JavaOne in San Francisco. This is my first trip to JavaOne, and the sheer size of it is rather overwhelming. Of course, it’s also obvious at almost every turn that JavaOne is the weak sister of Oracle Open World, but hey, it could have been IBM, right?

I’ve been thinking about this presentation for literally years. I give a related talk on the No Fluff, Just Stuff tour all the time, and I’ve done similar talks at DevNexus, Gr8conf.us, and more. But this is JavaOne — the heart of the Java universe — and doing well here is supposed to mean something. Besides, if the evals are good enough, you can become a JavaOne Rock Star, which is likely the closest I’ll ever get to being any kind of rock star, so there’s that.

The biggest thing I worried about, though, was condensing my talk into an hour. On the NFJS tour I’m accustomed to 90 minute blocks, and frankly I can talk about Groovy all day long (and frequently do in training classes). I kept cutting parts I liked, though, until I thought I had something reasonably condensed.

Perhaps it’s not surprising, therefore, that I was much more nervous for this talk than normal. Nate Schutta (fellow NFJS speaker and one of the best presenters I’ve ever met) likes to remind me that we speakers are an odd breed. It’s pretty strange to find somebody who both likes to code and likes to stand up in front of an audience and talk about it. Actually, it’s weird enough to do the latter. Most people find public speaking terrifying. Somehow we manage to like it and even look forward to it.

Since this was my only talk at the conference (probably a mistake on my part; I should have submitted more proposals), I knew I could use everything I had. At NFJS conferences, I worry that after two or three talks, everybody’s heard all my jokes, which is a serious disadvantage. Of course, that assumes they listened to them in the first place. :)

I still had time after setting up, so I did a couple of things to entertain the audience before the mics were turned on. One was that I went to http://speedtest.net and checked the speed of the ethernet connection, which came in at around 85 Mbps. Sweet. I did this while the projector was on, partly just to see the reaction and partly to make the “wow, I could have downloaded that whole Breaking Bad episode in five minutes” joke.

That is a joke, of course. I would never do anything our corporate masters at the MPAA (and the RIAA) wouldn’t want, and if I did, I certainly wouldn’t admit it in a public forum like this.

Then I opened up a tab in my browser to http://emergencykitten.com . I liked refreshing the page anyway, but I claimed I did it in case anything in the presentation went wrong, which might even have been true.

I noticed, btw, that there is a GitHub link on emergencykitten.com, which says that the site apparently runs using nodejs on Heroku. If you look in the data folder there, you’ll find kittens.yml (!), which contains the links to the Flikr pages for all the images.

I’ll come back to that in a moment.

JavaOne wanted me to use PowerPoint, which was a singularly uncomfortable proposition. I used it anyway, but felt free to make fun of it during the talk, especially because it became balky every time I switched from it to a demo and back. The slides are mostly for summaries anyway, since my talk is almost completely code based, but I did add some fun meme-based pictures.

For example, check out this one:

grinds_my_gears

The slides themselves are now on slideshare.net, so you can see the whole presentation here.

I start with a demonstration that you can execute compiled Groovy code with the Java command, if you just add the “groovy-all” jar file to your classpath. That’s a cool demo, but it also shows that you don’t need to modify your production Java environment at all in order to add Groovy to your system.

Then I talk about operator overloading, and how even though I don’t do it a lot, it shows up in all sorts of places in the Groovy JDK.

I jumped from there to POGOs, and wrote a POGO that had a couple of attributes and AST transformations for @ToString, @EqualsAndHashCode, and @TupleConstructor, before replacing all three with @Canonical.

I then did my favorite demo of accessing and using the Groovy v3 geocoder. The code for that is in my book’s GitHub repository in a couple of different places, along with Spock tests for both online and offline (!) usage.

This nice image followed:

success_kid_mjg

I went from there to my default JSON example, which is still based on ICNDB: the Internet Chuck Norris Database. When I use it now I generally use the firstName and lastName properties so I can replace “Chuck Norris” with Guillaume Laforge, or Paul King, or even Brian Goetz. As luck would have it, the site was up and running, so I was able to do that, too.

That leads to my favorite image:

most_interesting_mjg

I said I would come back to the Flikr issue, and since I want to add something technical to this post, I’ll do that here. Flikr has a REST API, but it’s rather more involved than I like to use in a presentation. Here’s my basic search:

String key = '...my key...'
String endPoint = 'http://api.flickr.com/services/rest?'
def params = [method: 'flickr.photos.search', api_key: key,
    format: 'json', tags: 'cat', nojsoncallback: 1,
    media: 'photos']

I’m searching for cat pictures, which as everyone knows is the reason the Internet was invented. Most of the parameters are required, but I used nojsoncallback=1 to keep from wrapping the JSON response in a method. The only tag I submit is ‘cat‘, but I might want to add some more because that doesn’t always give me what I want. You have to be careful with Flikr, though, because you can get back almost anything.

I assemble the needed URL and submit it in the usual way:

def qs = params.collect { it }.join('&')
String jsonTxt = "$endPoint$qs".toURL().text
def json = new JsonSlurper().parseText(jsonTxt)

Turn the map entries into list elements of the form “key=value” then join with an &; convert the string to a URL and download the text, then parse it with a JsonSlurper. This stuff becomes automatic after a while.

The complication with Flikr is that the response doesn’t actually include the image URL. Instead it breaks it into pieces, which I have to reassemble:

def urls = []
json.photos.photo.each { p ->
    urls << "http://farm${p.farm}.staticflickr.com/${p.server}/${p.id}_${p.secret}.jpg"
}

Drilling down into each photo element, I can extract the farm, server, id, and secret elements and use them to create the URL for the actual image. This I then insert into a Java ImageIcon:

new SwingBuilder().edt {
    frame(title:'Cat pictures', visible: true, pack: true,
        defaultCloseOperation: WC.EXIT_ON_CLOSE, 
        layout:new GridLayout(0, 2)) {
        urls[0..5].each { String url ->
            label(icon:new ImageIcon(url.toURL()))
        }
    }
}

The only odd part of this is the WC constant, which is an alias for javax.swing.WindowConstants. I build a GUI with six cat pictures in it, and I’m done.

Putting it all together (so you can copy and paste if you want — change the key to your own to make it run):

import groovy.json.*
import groovy.swing.SwingBuilder

import java.awt.BorderLayout as BL
import java.awt.GridLayout
import javax.swing.ImageIcon
import javax.swing.WindowConstants as WC

String key = '...insert your key here...'
String endPoint = 'http://api.flickr.com/services/rest?'
def params = [method: 'flickr.photos.search', api_key: key,
    format: 'json', tags: 'cat', nojsoncallback: 1,
    media: 'photos']
    
def qs = params.collect { it }.join('&')
String jsonTxt = "$endPoint$qs".toURL().text
def json = new JsonSlurper().parseText(jsonTxt)

def urls = []
json.photos.photo.each { p ->
    urls << "http://farm${p.farm}.staticflickr.com/${p.server}/${p.id}_${p.secret}.jpg"
}

new SwingBuilder().edt {
    frame(title:'Cat pictures', visible: true, pack: true,
        defaultCloseOperation: WC.EXIT_ON_CLOSE, 
        layout:new GridLayout(0, 2)) {
        urls[0..5].each { String url ->
            label(icon:new ImageIcon(url.toURL()))
        }
    }
}

I ran the script and got two cat pictures and four pictures of tractors. I was confused until somebody pointed out that they were probably Caterpillar tractors, which shows what kind of trouble you can get into on Flikr. Believe me, it could have been worse.

Bringing it back to the GitHub repo for emergencykitten.com, maybe in the future I’ll grab the Flikr URLs in their repo and do the same thing. Still, this is a reasonably cool demo, and why not live a bit dangerously anyway?

The talk went well and a good time was had by all. At least I hope so, though I won’t know for sure until I see the session surveys and find out of I’m a rock star after all.

For the record, I’d like to say that as Groovy Rock Stars go, if Paul King is Led Zeppelin, Guillaume Laforge is the Rolling Stones, and Graeme Rocher is the Beatles, then I want to be E.L.O. I always liked E.L.O. and owned several of their albums when I was a kid.

Actually, that’s not quite right. I didn’t own several of their albums. I owned several of their 8-track tapes. Whoa.

I have to say, though, that the most interesting experience of all was seeing my book on the shelves of the bookstore at the conference. I went in late in the day and was about to frown at the number of copies on the shelf when somebody walked up, took one, browsed through it, grinned wryly to himself, and actually bought the bloody thing. I was extremely tempted to introduce myself and offer to sign it or take a picture of him holding it, but in the end I decided that would be too creepy.

My wife, who came along to do sightseeing in San Francisco, agreed with that. Still, I’ll be at the conference until Thursday, so if anyone wants me to add my chicken scratch to their book, just ask around. I promise not to hover too close to the store.

About Ken Kousen
I teach software development training courses. I specialize in all areas of Java and XML, from EJB3 to web services to open source projects like Spring, Hibernate, Groovy, and Grails. Find me at Google+ on Google+ I am the author of "Making Java Groovy", a Java / Groovy integration book published by Manning in the Fall of 2013.

2 Responses to Making Java Groovy at JavaOne 2013

  1. Pretty instructive….looking onward to coming back.

  2. TimeSearcher says:

    Sorry I didn’t attend JavaOne. I have your eBook (which I bought after seeing one of your NFJS talks on DZone). Thanks for your book — it’s great!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,375 other followers

%d bloggers like this: