Burk is a long-time programmer and software architect, currently focused on the Java platform with an eye toward mobile platforms. In 2010, he was voted a JavaOne Rock Star for his talk on User Experience Anti-Patterns, and is a co-author of the books "97 Things Every Software Architect Should Know" and "97 Things Every Programmer Should Know". Burk is also a Sun Certified Programmer, Developer, and Enterprise Architect for JEE 5. Burk has posted 25 posts at DZone. View Full User Profile

Groovy Recipes - Greasing the Wheels of Java

11.11.2008
| 7899 views |
  • submit to reddit
Published by: Pragmatic Bookshelf
ISBN: 0978739299

Reviewer Ratings

Relevance:
5

Readability:
5

Overall:
5

Buy it now

One Minute Bottom Line

If you’re interested in Groovy (and if you’re not you should be) then “Groovy Recipes” is a book you’ll find useful over and over again. There’s enough information here to keep you busy learning and experimenting for a long time. But it doesn’t stop there; “Groovy Recipes” is also an introduction to Grails, the Rails-like web application framework written in Groovy.

Review

In Chapter 1, “Introduction”, author Scott Davis provides some pretty good arguments for learning Groovy; starting with this quote, “Groovy is what Java would look like had it been written in the 21st century.” Scott points out that you can use Groovy as a simpler way of writing Java code; optional semi-colons, generated accessors (get and set methods) for class properties, etc. Or, you can use Groovy to go beyond the current limits of Java and take advantage of things like closures and metaprogramming to do some really amazing things.

“Getting Started” is chapter 2, and in it Scott shows us how to install Groovy on Linux, Unix, and the Mac OS as well as on Windows. He then walks us though several ways to execute Groovy code; as a shell script, by compiling and then executing the compiled code and executing the compiled code, running the Groovy Shell, running the Groovy Console, creating and running a Groovy Servlet (called a Groovlet). The chapter finishes with instructions on how to install the Groovy plug-in on Eclipse, IntelliJ IDEA, and TextMate.

Chapter 3, “New to Groovy”, introduces some of the many things that make Groovy easier to use than Java:

  • Optional semi-colons and parentheses.
  • Optional type declaration (aka Duck Typing) and exception handling
  • Operator overloading
  • Safe dereferencing (so you don’t have to explicitly test a variable for null before trying to use it)
  • Groovy autoboxing and Groovy truth (non-null/non-zero values evaluate to true)
  • Improved String, List and Map classes, Ranges, Closures and blocks.

Chapter 4, “Java and Groovy Integration”, shows how easy it can be to integrate Groovy with Java, or Java with Groovy. It starts by dissecting a GroovyBean, sometimes called a POGO (or Plain Old Groovy Object) and explains that Groovy does a lot of the “boiler plate” coding for you, and how to get around that if you want to. It then shows how to call Groovy code from Java and Java code from Groovy, and how to compile your code using the Groovy Joint Compiler (which compiles both Java and Groovy) from the command line or Ant.

“Groovy from the Command Line” is the title of Chapter 5, and in it you’ll learn how to use Groovy as a shell scripting language. You’ll see how to access command-line arguments, system properties and environment variables, how to execute shell commands (like ‘ls’ or ‘dir’), and how to execute other scripts, or just parts of them.

Chapter 6, “File Tricks”, is just sixteen pages, but it covers a lot of ways showing how to access your file system using Groovy. You’ll learn how to use Groovy to:

  • Work through a directory and determine which entries are files and which are other directories
  • Filter a directory listing based on aspects of a file’s name
  • Read a file’s contents into a String or an ArrayList of Strings
  • Write to a new file, or append data to an existing one
  • Merge several text files
  • Copy files (both binary and text) using the methods of reading and writing already covered, or by using the operating system’s copy command, or by using the groovy.util.AntBuilder class; and there’s an added bit showing how to add a file copy method to the File class itself.
  • Move or rename files
  • Delete files or directories
  • Create ZIP or Tar files and how to unzip or untar them

If you work with XML a lot, then Chapters 7&8, “Parsing XML” and “Writing XML” may be worth the price of the book by themselves. Groovy provides two classes (XMLSlurper and XMLParser) that let you read and manipulate the contents of an XML document. It also has two classes (MarkupBuilder and StreamingMarkupBuilder) to easily create valid XML documents from your data. There’s an extra bit showing how to generate HTML documents and how to produce valid XML from a JDBC ResultSet.

Chapter 9 is titled “Web Services”, but it covers far more than just creating and consuming a web service. It also covers things like:

  • Finding local and remote IP addresses and host names
  • Making an HTTP GET, POST, PUT, or DELETE request and processing the results
  • Making RESTful, SOAP, and XML-RPC requests
  • Parsing ATOM and RSS feeds

Chapter 10, “Metaprogramming”, takes you behind the scenes and shows you how some of the magic of Groovy works. You’ll learn how to examine a Class and find out what interfaces it extends, as well as what fields and methods it has. Next is how to create classes at runtime using Expandos, and how to add new methods – even to compiled final classes – at runtime using Categories or the ExpandoMetaClass.

The last two chapters are designed to get you up and running with Grails (Groovy’s web framework) in short order.

Chapter 11, “Working with Grails, walks you through the process ofinstalling Grails (on Unix, Linux, Mac OS, and Windows), creating and running your first Grails application, showing you how to modify things like the database you’re using and the overall look and feel of the application. There’s also a quick lesson on the Groovy Object Relationship Model and how to specify the various relation types (one to one, one to many, and many to many) in Groovy so the database will be populated properly.

Chapter 12, “Grails and Web Services”, covers creating RESTful and JSON based web services and ATOM and Podcast feeds.

Published at DZone with permission of its author, Burk Hufnagel.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)