10. Using Groovy in Actions

PROBLEM

The use of dynamic languages are becoming very popular in Java, and Groovy is becoming the predominant choice. Besides supporting the dynamic invocation of java code, Groovy also adds additional functionality to makes things easier to do with Java. While Skyway supports the Blending Actions with non-generated Java Code and Blending Actions with non-generated Spring Beans, the ability to leverage Groovy provides additional options to application developers.

SOLUTION

Skyway Builder offers support for Groovy through the Invoke Groovy Step. Using Spring’s prescribed approach of dynamic language integration, the Groovy script is maintained externally from the Java code and wired-in (injected) using Spring’s <lang:groovy> element.

HOW IT WORKS

The Invoke Groovy Step can be added to an Action to perform Groovy-based processing. Typical uses of the Invoke Groovy Step are to

Here's an example of some Groovy code that's used in GetFlickrContent operation of the the FlickrViewer sample project. In addition to using standard java code, the Invoke Groovy step also supports the additional functionality provide by Groovy's. The follow snippet shows the use of Groovy's special XML processing support (XMLParser).

Example 3.3. Using Groovy Code using Invoke Groovy Step

System.out.println("inFeed: "+inFeed);
def xmlFeed = new XmlParser().parse(inFeed);
def idx = 0;
xmlFeed.channel.item.each { item ->
idx++;
def title = item.title.text()
currentEntry = new flickrviewer.domain.Entry();
currentEntry.id = idx;
currentEntry.flickrlink = item.link.text()
currentEntry.imagelink = item.media.'@url'.text()
System.out.println("DEBUG: "+item.'media:content'.text());
currentEntry.title = item.title.text()
currentEntry.content = item.description.text()
System.out.println("Title: "+currentEntry.title);
System.out.println("Link: "+currentEntry.imagelink);
entries.add(currentEntry);
}


RELATED RECIPES

  1. Using Groovy in Actions

  2. Accessing Data using Groovy