Groovy 1.7 / Grails 1.2 New Features

Friday, January 15th, 2010

The topic of this month’s Chicago Groovy User Group (CGUG) meeting was a discussion of the new features and enhancements in Groovy 1.7 and Grails 1.2. Both Groovy 1.7 and Grails 1.2 are hot off the press, with their releases happening on December 22 and December 23 respectively. The founders of CGUG, Bill Gloff, Bryan Williams, and myself led the meeting by cherry-picking a few topics that we thought were particularly interesting. We discussed each topic in greater detail and demonstrated each by running live examples.

The Groovy topics that we covered included:

  • Grape
  • AST Viewer / AST Builder
  • Groovy Truth Customization

The Grails topics that we covered included:

  • Dependency resolution DSL
  • Improved dynamic finders for boolean properties
  • Named url mappings
  • Testing infrastructure improvements
  • Support for hasOne mapping
  • Named query support

One of the topics that I covered is the new grails dependency resolution DSL. Built on Apache Ivy, the dependency resolution DSL allows you to define your dependencies without having to use Maven or Ivy directly. This means that you no longer have to fiddle with Maven POM files or Ant configuration files to manage your external dependencies. Instead, you can simply express your dependencies using a DSL in the grails-app/conf/BuildConfig.groovy file. For example, to install version 5.1.10 of the mysql-connector-java jar file from Maven’s central repository, the following syntax would be used:

repositories {
     mavenCentral()
}
dependencies {
     runtime 'mysql:mysql-connector-java:5.1.10'
}

This approach is simple to use and understandable, even to developers without any prior knowledge of Maven or Ivy.

Another Grails feature that I covered is the new testing infrastructure. In previous versions of Grails, there was no way to specify the type of test (e.g. EasyB, Spock) that you wanted to execute. You could specify the phase of a test (e.g. unit or integration) that you wanted to execute, but there was no way to tell Grails that you only want to run Spock tests for this particular test run, for example.

The new syntax looks like the following:

grails test-app phase:type

The following examples demonstrate how you would use the new syntax:

grails test-app unit:spock // execute all Spock unit tests
grails test-app :spock // execute all Spock tests in all phases

The old syntax of targeting a specific phase still applies. To execute all integration tests, the following command would still be valid:

grails test-app --integration

Switching gears to the Groovy side of things, one enhancement that I really like is the ability to customize the Groovy Truth. By adding an asBoolean() method to one your classes, you are able to define what it means for instances of that class to be considered true. For example, suppose you have an address class with all the typical address-like fields, such as address, city, state, and zip. Now suppose you only want instances of that class to evaluate to true if the city field has a value. To do that, you would add the asBoolean() method to the class like so:

class Address {
     String city
     String state
     String zip
     boolean asBoolean() { city }
}

The asBoolean() method will only evaluate to true if city has a length > 0, as demonstrated by the following:

assert new Address(city: 'Chicago', state: 'IL', zip: '60606')
assert !new Address(city: '', state: 'IL', zip: '60606')

Hopefully this has whet your appetite to want to dive in and learn more about the other great Groovy and Grails features and enhancements that have been added since their previous releases. Since this blog has barely scraped the surface, visit Groovy 1.7 release notes and Grails 1.2 release notes to learn more.

Bookmark and Share

posted by Jeff Palmer

Why You Should Consider JBoss Seam for Your Next Project

Tuesday, January 5th, 2010

I recently had the opportunity to convert a Microsoft Access-based application to a web-based application using JBoss Seam. Having used many Java web frameworks the past few years, including Grails, Wicket, and Struts 2, I wanted to highlight some of the key features in Seam that help differentiate it from some of the other framework contenders. The intention of this blog isn’t to start any flamewars by comparing Seam on a feature by feature basis with other Java frameworks, but simply to focus on the areas of Seam that I feel help make it a solid choice when starting out on a new project.

Seam-gen
Seam ships with an application generator tool called seam-gen. The purpose of seam-gen is to allow for the automatic creation of various application artifacts, including common configuration files, a build script, and directories for source and view code. It can also automatically generate a complete CRUD application based on an existing database schema. For the project that I worked on, this came in especially handy since I had an existing database to work with. Seam-gen did a tremendous job in creating the CRUD source and view code, complete with pagination, searching, and sorting of data. The code that was created was so complete that very few tweaks were required to make it production ready.

State Management
With Seam, gone are the days of having to manage application state programatically. All Seam components have a default scope as well as a scope that can be set using annotations. Once a component is annotated, Seam takes over the rest so the programmer doesn’t have to worry about managing the component’s creation, state, and removal. Seam also introduces a new scope not seen in any of the other application frameworks: the conversation scope, which is similar to the session scope used in most web frameworks, although more powerful. As opposed to the HttpSession, a conversation allows multiple state containers for a single user. This allows multiple application tabs or windows to be opened and worked on without worry that the work being performed in one tab or window will overwrite that being performed in another tab or window.

ORM Best Practices
It’s no surprise that Seam makes use of ORM best practices since it was created by Gavin King, the inventor of Hibernate. Seam manages the persistence context from the time a request comes in to the time a response is rendered. This relieves developers from having to implement data access hacks or having to worry about various ORM errors, such as the dreaded LazyInitializationException. Another major benefit of having the persistence context managed by Seam is that performance can be greatly improved since the extended persistence context acts as a cache, storing database queries in-memory and only committing them to the database at the end of a conversation.

Third Party Library Integration
Seam provides several features to allow for the seamless integration with third party libraries. By simply bundling a few JAR files in your application, you can easily perform tasks such as generating PDF reports, sending email, displaying rich text, and creating charts. Seam allows this integration to be performed by providing simple tags that can be dropped onto a web page and then used just like ordinary JSF tags. For example, by wrapping content in a <p:document> tag, Seam generates a PDF document by leveraging the open source iText library. Similarly, Seam includes tags to integrate with the JavaMail API for sending mail, the ANTLR API for displaying rich text, and JFreeChart for creating charts.

AJAX Support
Seam has leading-edge support for integrating AJAX in an application. Without writing a single line of JavaScript code, Seam applications can become AJAX enabled. This is a great feature for those who aren’t JavaScript experts. By adding just a few simple lines of code to the project that I worked on, the application was able to provide instant validation to form fields as well as performing data searches without doing a full page refresh, giving it a true desktop-like feel.

Summary
Choosing a Java web framework is not easy. New frameworks appear everyday and get added to the long list of options. While no framework is a silver bullet that will solve all the needs of a typical web application, I highly recommend having a look at the Seam framework to simplify your web programming needs.

Bookmark and Share

posted by Jeff Palmer

Ubiquity Firefox Extension

Wednesday, October 1st, 2008

Stumbled across the Ubiquity Firefox Extension and was so impressed with it that I figured I ought to write a quick blog entry about it. What it does is allow the user to quicky and easily interact with the web by issuing commands to perform a myriad of useful tasks, such as performing on-the-fly Wikipedia searches, adding Google maps to an email, and translating words or phrases simply by highlighting them. These are just a few of the many feature Ubiquity has to offer. There is also a great feature where a developer can write his own commands, so the limitations of this extension are boundless. I highly recommend checking this extension out as I am sure you won’t be disappointed.

Bookmark and Share

posted by Jeff Palmer

WindyCity Rails Conference

Monday, September 22nd, 2008

Attended the WindyCity Rails Conference this past weekend (September 20th). Overall a decent meeting. One highlight of the meeting was the QA session with David Heinemeir Hansson, the creator of Ruby on Rails. It was an open forum where the participants of the conference were allowed to ask David whatever questions they wanted. Some interesting questions were asked and it was great to hear David’s responses to all of them. One question that I found particularly interesting was in regards to performance in Rails. According to David, developers should focus on optimizing HTTP before all else (even code). He recommended using the YSlow Firefox plugin tool to do this.

Although all of the presenters did a really good job at presenting their material, the overall setup of the conference was a bit disorganized. Most of the presentations were only 30 minutes long, so no sooner did the presenters start getting into the crux of their material than it was time to end the presentation and move on to the next one. Also, since there were only 2 rooms for 3 concurrent presentations, if you were unlucky enough to go to a presentation in the room housing 2 presentations, it was a bit hard to hear the presentation you were interested in since there was another one going on about 100 feet away.

Overall though, it was really impressive to see just how strong the Rails community is in Chicago. The energy and enthusiasm of the participants was amazing, making me realize just how prevalent the Rails framework has become.

Now only if we can organize a WindyCity Grails conference…

Bookmark and Share

posted by Jeff Palmer

Grails / Rails Development

Friday, September 12th, 2008

I had the opportunity to write a Grails application for the City of Chicago recently (it’s actually going live tonight) and boy, to quote the Grails catchphrase “Develop with pleasure”, what a pleasure it was. From creating custom taglibs to utilizing some of the great plugins available to Grails, it really made working on the project, dare I say, fun! And fast. Although it’s impossible to compare how long the app would have taken had I written it in another framework, I’d hazard to guess that I saved about 25% by going the Grails route. It almost felt like the framework was working with me, as opposed to recent development I did using Struts 2, where it often felt like the framework was not always on my side.

Having enjoyed working with Grails so much, I wanted to delve deeper into the other main dynamic web framework. You know the one I’m talking about, right? Of course you do (especially since it’s on the title of this blog). Rails of course. I recently started creating a toy app using Rails and have found learning Rails really easy for someone with a background in Grails. Like Grails, Rails follows the “convention over configuration” approach, making life a lot easier fore the developer. Not only does this help keep files nicely organized, but getting away from all of that Xml configuration is really awesome.  I realize there are a lot of zealots out there who feel a certain loyalty to one framework or the other, but honestly, to me, both frameworks seem to do a great job at what they’re designed to do, namely, make life easier for the developer. Granted I haven’t spent a lot of time doing Rails development yet, so perhaps this statement is a bit premature (my apologies if I just offended someone with a firm stake in the ground in one of the 2 camps), but so far, things seems about equal between the two. They both make starting a new app a cinch by creating a nice, logical project directory. They both provide simple means to create the core MVC components. They both have pretty good IDE support, which seems like it’s only getting better with time. And the best part of all is they both utilize dynamic languages at their core, which really makes coding in Java feel so archaic.

I’ll be attending the WindyCityRails conference next Saturday (September 20th), so it will be interesting to hear what the Rails community has on tap for the future. I’ll be sure to comment on it in one of my upcoming blogs.

Stay tuned for my next blog where I’ll be talking about my experience integrating FLex 3 with Grails. Until then, happy programming!

Bookmark and Share

posted by Jeff Palmer

CityTech Home