Thursday, November 29, 2007

Using Groovy to work with Files


One of the best applications of Groovy I have come across so far is working with Files. Using Groovy to read data from a file can not get any easier. If you are a Groovy skeptic this is the post for you. Using the maven-groovy-plugin, which allows you to mix Java and Groovy code seamlessly, you no longer have any more excuses (go here for a maven-groovy-plugin how-to).

Recently I was writing unit tests in Groovy for my Java code and I needed to read in the contents of a file for comparison. In Java this would look something like this:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileTest {
public static void main(String[] args) {
try {
String file = "/workspace/sandbox/grvyfile/src/test.txt";
String line = "";
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) !=null) {
sb.append(line);
}
System.out.println("sb.toString() = " + sb.toString());
} catch (FileNotFoundException e) {
System.out.println("e = " + e);
} catch (IOException e) {
System.out.println("e = " + e);
}
}
}

If you are still doing this STOP RIGHT NOW! This boiler plate code took me about 5 minutes to write. Since it's not something I do super frequently I don't commit this syntax to memory, and therefore almost always have to reference the API to remember to use things like java.io.BufferedReader.

Would you like to see the Groovy equivalent? Make sure you don't blink; you might miss it.
def file = "/workspace/sandbox/grvyfile/src/test.txt"
println new File(file).text

That is all folks. In fact if you want to test this real quick, use the groovy command line like the following:
>cd /workspace/sandbox/grvyfile/src
>groovy -e "println new File('test.txt').text"

For those new to Groovy, .text is essentially shorthand for calling the .getText() method on the File object. The method getText() is one of the dozens of new methods added to the JDK (see the GDK for more).

Read my comments in this post (Java: Laugh or Cry, Your Choice...) by Kit Plummer for another good application of Groovy. Here I use the List GDK methods minus() and unique() to decrease the lines of code from ~27 to ~2.

Wednesday, November 28, 2007

Impressive Grails Plugin Architecture

I just finished watching the video of Graeme Rocher explain the Grails Plugin Architecture while at the Grails Exchange in London recently (Graeme Rocher on the Grails Plugin Architecture). I got to say, while long at 53 minutes, it was well worth the time and was pretty impressive.

Some things I didn't realize was all the default core functionality in grails is plugin based; everything in grails is based on plugins. For example: spring, hibernate, internalionalization, domains, controllers, tab libraries, etc are all plugins.

On top of that it is really easy to create your own plugin (watch the video above or go here. Here are some of the things you can do with plugins that make them so powerful:

  • Add new methods, constructors, properties to any class at runtime
  • Perform runtime Spring configuration instead of depending on some static spring XML file that you wish you could change at runtime
  • Modify the web.xml on the fly
  • Add new controllers, domains, tag libraries, etc
Here are just a few plugins that already exist:
  • XFire - exposes a grails service as a SOAP service
  • Searchable - integrates compass and lucene search
  • Remoting - exposes a grails service over RMI, HTTP, or burlap
  • GWT - integrates grails with Google Web Toolkit
  • Acegi/JSecurity - adds security support for grails
  • JMS - exposes grails services as Message Driven Beans
  • Wicket/JSF/Tapestry - bring in different view frameworks
  • Dbmigrate - support sql migration
  • Static resources - helps offload the delivery of static resources to another web server
  • Jasper Reports - create jasper reports in grails
Go here for a complete list: http://grails.codehaus.org/Plugins.

Wednesday, November 21, 2007

Best Groovy quote ever

I think this pretty much sums up Groovy:

"Groovy is executable psuedo-code"
Scott Davis

source http://www.javaworld.com/podcasts/jtech/2007/110107jtech004.html

Friday, November 16, 2007

Javadoc equivalent for Grails (a necessity)

Oh yeah! Graeme Rocher and Marc Palmer have done an excellent job creating the Reference Documentation for the Grails Framework for version 1.0-RC1 (check it out http://grails.org/doc/RC1/). This will be familiar to anyone that has referenced Java's API javadocs like I have for so many years.

This is an absolute necessity for Grails development IMO due to it's dynamicity (I think I just made that up). Outside of checking out the source, yuck, how else would you know which options are available? This is another piece of the puzzle before the official release of Grails 1.0.

Here is what the reference documentation covers:

  • Grails Commands - detailed usage information for the grails commands like generate-all, test-app, etc.
  • Constraints - explains all the current constraints available to Domains like blank, maxSize, etc.
  • Controllers - shows all the information available for controllers
  • Domain Classes - shows all methods available to consumers of domain classes like save, validate, find*, etc.
  • Plugins - information available for plugins
  • Services - explains things like scope and transactional
  • Servlet API - typicaly servlet stuff like request and response
  • Tag Libraries - information available for tag libraries
  • Tags - tags you can use in your GSP and I am sure, but not certain, in your JSPs.

Thursday, November 15, 2007

How to remote into Windows from Linux

You can easily use rdesktop to remote into a Windows machine from Linux. I am using Ubuntu 7.10 and rdesktop works fast and its easy. The default resolution is really small so here is my command to start it up specifying a different resolution.

rdesktop -g 1024x800 server

You can also specify the -f option for full screen mode, but beware it's not easy getting out of rdesktop full mode. Use CRTL+ALT+ENT to exit full screen mode.

Monday, November 5, 2007

New Amazon S3 plugin for Grails

Today, Catina Consulting announced the release of an open source Amazon S3 (Simple Storage Service) plugin for Grails with the following goals:

  • Host and manage file assets on Amazon S3 for storage and performance advantages
  • Provide easy mechanisms to reference S3-hosted assets in Grails applications
  • Make most efficient and cost-effective use of S3 hosting resources
Their first version appears to be rather useful with some excellent capabilities coming in the future. This first version focuses on managing user uploaded static content. Planned features include supporting different media types, custom bucket schemas, security integration, support for both REST and SOAP (currently only uses REST), and reporting on S3 usage.

Installation and configuration seem rather simple so it should be rather easy for Grails users to start immediately using this plugin.

What I think is the most interesting was their ability to reuse the JetS3t Toolkit which is written in Java. This just goes to show how groovy can really be the glue between Java and how developers can reuse all that existing Java code.

Go here to learn more about Amazon S3, which is an online storage web service providing unlimited storage through a simple web service interface (REST or SOAP). S3 aims to provide scalability, high availability, and low latency at cheap costs. It accomplishes this by using the same scalable storage infrastructure that amazon.com uses to run its own global e-commerce network.

For example, Smugmug, a popular photo sharing web site, is using it to store and host pictures. For a simple cost analysis view this post by Jeremey Zawodny who contemplates using S3 to replace his backup server.

And finally, I can't leave the Rails community out of this either so here is an article on using the rails AWS::S3 library (this plugin is more feature complete than the grails plugin).