Quick and Easy Groovy for the Web

22 June 2011

Groovy can be used pretty easily to spin up some simple web pages in almost the same way one would hack out some PHP or JSP without going to the trouble to do an all-out Grails project.

The Groovy Servlet allows you to pack up the groovy-all-*.jar, a simple web.xml, and whatever *.groovy scripts you want and deploy it right into Tomcat as a plain WAR file. The Groovy Servlet page

Here's a bit of a script I put together to jump start a simple Groovlet project by packaging a WAR file from a directory of scripts. This isn't Groovy Servlet code itself, but just a command-line tool. (The Groovy Servlet page linked previously has examples for writing your own servlets.) This script will copy in the Groovy JAR and generate the basic web.xml to wire up the GroovyServlet to dynamically execute your scripts. I also have a downloadable copy of package_groovlet.groovy.

#!/usr/bin/env groovy

if (args.size() < 1) {
    print """\
        |Usage: package_groovlet.groovy <war-name>
        |Package the current directory into a Groovy Servlet war.

def war = args[0]
def embed = "${System.getenv()['GROOVY_HOME']}/embeddable"

def ant = new AntBuilder()

ant.sequential {
    delete(dir: 'build')
    mkdir(dir: 'build/WEB-INF/lib')
    copy(toDir: 'build/WEB-INF/lib') {
        fileset(dir: embed) {
            include(name: 'groovy-all-*.jar')
    copy(toDir: 'build') {
        fileset(dir: '.') {
            exclude(name: 'build/**')

new FileOutputStream('build/WEB-INF/web.xml').withWriter { webxml ->
    webxml.print """\
        <!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" >

ant.jar(destfile: "build/${war}", basedir: 'build')
println "Created build/${war}"

Top Posting in Mutt

27 May 2010

Outlook, Entourage, and Evolution have trained me in work environments to top-post my email replies against my curmudgeonly better judgment.

Now that I'm mostly job searching and sending lots of email from my home account, I've finally buckled to the peer pressure, and configured mutt to top-post my signature in the .muttrc:

set sig_on_top = yes

I don't even frequent those old mailing lists where someone would yell at me for top-posting, anyway. Next I'll likely just dump Mutt and go to a desktop mail client, like Thunderbird.

Ubuntu Linux on the MacBook Pro

12 January 2010

OS X on my MacBook Pro was getting sort of screwy, and I was preparing to reinstall, so I thought I'd see how far I could get with Ubuntu 9.04 on the MacBook Pro.

Booting EFI

First things first, I read a bunch about lilo and grub and the Mac's EFI instead of a normal BIOS. I was expecting some real trouble here, but the Ubuntu install CD booted up fine, and after the install finished, it was able to just boot from the hard drive with no extra work.

Wireless Networking

I was happy to see the wireless work and even our Juniper VPN software seemed to work pretty easily -- it uses Linux' built-in tunnel interfaces. I had initially found the wireless to be a bit flakey, and it would freeze up shortly after connecting. It was sort of similar to the trouble I was having on the EeePC, so I installed the current linux-modules-backports package, and it got much better.

Software for Work

One of my first concerns was going to be accessing the corporate Exchange servers for my email, address book, and calendar, but Evolution with the Exchange Connector (straight from the Ubuntu repository) can handle this. It's not all that stable, but it works well enough with a couple restarts of the client through the day.

Evolution sometimes gets a bit slow or otherwise wonky, and that in turn causes the clock/calendar applet on the Gnome Panel to freeze up. Sometimes the whole panel freezes. A quick kill of gnome-panel gets it all moving again, but having a stopped clock can get really inconvenient. Evolution's notifier daemon is also unreliable, so I've gotten accustomed to being aware of my schedule and the time, and not relying on a pop-up reminder.

I installed Eclipse myself, so I can keep it more up-to-date, so I had a 1.6 version of Subclipse. This immediately caused me some trouble when Ubuntu 9.04 was only shipping Subversion 1.5. I ended up installing Subversion 1.6 from source, and then later going back to packaged versions when I upgraded to Ubuntu 9.10.

Function Keys

Using Eclipse, I really need the top function keys to work as function keys, and not volume, brightness, etc as is default for the Mac keyboard.

To get the keyboard to default to F-keys, and require the Fn key combo for the other controls, I needed to add a module parameter, so add create the /etc/modprobe.d/hid_apple.conf file with the single line:

options hid_apple fnmode=2

Upgrade to Ubuntu 9.10

The upgrade became available, so I jumped on it right away -- that's what I do. It went rather fabulously -- I had almost no trouble. Only Eclipse gave me trouble with the GTK+ update. I twiddled the environment in my Eclipse start-up script, per some advice, and the components in Eclipse almost always work now. I hear this'll be more completely fixed when newer versions of Eclipse are released.

The upgrade to Ubuntu 9.10 also took away Java 5, since Sun officially dropped support for it about the time Ubuntu released. Most of our projects just built fine with Java 6, but a few needed to be updated a bit to get them compliant with Java 6. I chose the OpenJDK installs, bounced back to Sun a couple times, but have now pretty reliably settled into OpenJDK with no problems.

Power Management

One of my favorite perks of running Linux is getting back control of the machine. I configured CPU frequency scaling to use the "conservative" CPU governor to save battery, I can sleep or suspend the machine (which works!) with a push of the power button, and it stays awake when I close the lid to walk to a meeting.

Running Linux is like coming home again, and it's serving my daily needs well at work. The MacBook hardware is holding up nicely too, which has not been my experience with older plastic-cased PCs.

A Useful Example in Java, Ruby, and Groovy

04 June 2008

We've been using Java and Ruby for work, and we've recently been asked to look more toward Groovy where we had been using Ruby, so I rewrote a useful little iconv script which we use to re-encode large volumes of data (which the GNU iconv doesn't seem to want to handle).

For illustration, here are the implementations I wrote:

Java (40 lines):

import java.io.*;

public class Iconv {
    public static void main(final String[] args) throws Exception {
        if (args.length < 2) {
                    "Usage: Iconv <in>:<encoding> <out>:<encoding>");

        final String[] ins = args[0].split(":");
        final String[] outs = args[1].split(":");

        final String inFile = ins[0];
        final String inEnc = ins[1];
        final String outFile = outs[0];
        final String outEnc = outs[1];

        Writer writer = null;
        try {
            writer = new OutputStreamWriter(
                    new FileOutputStream(outFile), outEnc);
            Reader reader = null;
            try {
                reader = new InputStreamReader(
                        new FileInputStream(inFile), inEnc);
                Integer character = null;
                while ((character = reader.read()) >= 0) {
            } finally {
                if (reader != null)
        } finally {
            if (writer != null)

Ruby (20 lines):

#!/usr/bin/env jruby

require 'iconv'
require 'fileutils'

if ARGV.size < 2 then
    puts "Usage: iconv.rb <from>:<enc> <to>:<enc>"

in_file, in_enc = ARGV[0].split(":")
out_file, out_enc = ARGV[1].split(":")

Iconv.open(out_enc, in_enc) do |conv|
    File.open(out_file, 'w') do |out|
        File.open(in_file, 'r').each_line do |line|

Groovy (19 lines):

#!/usr/bin/env groovy

if (args.size() < 2) {
    println "Usage: iconv.groovy <from>:<enc> <to>:<enc>"

final ins = args[0].split(":")
final outs = args[1].split(":")
final from = ins[0]
final fromEnc = ins[1]
final to = outs[0]
final toEnc = outs[1]

new FileOutputStream(to).withWriter(toEnc) { writer ->
    new FileInputStream(from).withReader(fromEnc) { reader ->
        writer << reader

Using the Groovy 1.6 beta, I could have used the new left-side array assignment syntax to shorten the groovy version to 15 lines:

[from, fromEnc] = args[0].split(":")

I had initially set out to skip over Groovy and just learn Ruby, as I had viewed Groovy as a sort of compromise -- Ruby would be the real twist of the mind, but alas, my neural pathways seem a bit more rigid than I had hoped. I can think a little quicker in Groovy than Ruby and get a few things done. Groovy is also a bit more compatible with the work directives handed down from on high -- we want to be a Java shop.

Update (2008-06-04): I updated the code above to reflect some comments. To be complete, the Java code should null check the streams it's trying to close in the finally blocks.

All the Posts

June 2011

May 2010

January 2010

June 2008

March 2008

December 2007

October 2007

August 2007

July 2007

June 2007

May 2007

March 2007

February 2007

January 2007

November 2006

September 2006

August 2006

July 2006

March 2006

January 2006

December 2005

October 2005

August 2005

May 2005

April 2005

March 2005

January 2005

December 2004

November 2004

October 2004

September 2004

May 2004

April 2004

March 2004

January 2004

September 2003

August 2003

July 2003