Simple SQL shell for Tomcat resources

#!/usr/bin/env groovy

/**
 * Read resources' data from Apache Tomcat's XML configuration and provide
 * a simple SQL shell prompt via STDIN/STDOUT.
 *
 * Copyright (c) 2009 Jochen Kupperschmidt <http://homework.nwsnet.de/>
 * Version: 15-Aug-2009
 * Released under the terms of the MIT License.
 */

import groovy.sql.Sql


/**
 * Get resource attributes by resource name from the named file.
 */
Map readResource(String filename, String name) {
    def map = [:]
    new XmlSlurper().parse(new File(filename)).GlobalNamingResources.Resource.find {
        it.@name.text().equals(name)
    }.each {
        map.name = it.@name.text()
        map.driverClassName = it.@driverClassName.text()
        map.username = it.@username.text()
        map.password = it.@password.text()
        map.url = it.@url.text()
    }
    return map
}

// Load named resource from file.
def rsrc = readResource('/path/to/tomcat/conf/server.xml', 'jdbc/someName')

// Load the required database adapter JAR explicitly.
this.class.classLoader.rootLoader.addURL(new URL(
    'file:///path/to/tomcat/common/lib/mysql-connector-java-5.0.x-bin.jar'))

// Instanciate database connection.
def sql = Sql.newInstance(rsrc.url, rsrc.username, rsrc.password, rsrc.driverClassName)

// Provide a basic shell. Exit on an empty input line.
System.in.eachLine {
    if (it.equals('')) {
        System.exit(0)
    }
    sql.eachRow(it) { println it }
    println()
}