Friday, December 11, 2009

Groovy on Google App Engine Part II

In the previous post, I linked to the demo of the Atlanta Groovy on Grails User Group presentation from August given by Pratik Patel. In this, post we will review the steps required to create a Groovy app in the demo, and in addtion create some nice little Groovy features to the application.


Using Eclipse 3.5 Galileo the following plugins were installed for this demo..
- Groovy Eclipse Plugin
- Google AppEngine Eclipse Plugin
- Aptana Studio Plugin

Before starting you will need a Google AppEngine Account. Google AppEngine now supports Python and Java. We will be running Groovy over Java using the Groovy Gaelyk library that allows Groovy libraries to run on the Google App Engine. The data service is also on Google's servers and is using DataStore.

Next, create a Google AppEngine project in Eclipse. The Google AppEngine plugin should add this feature into your Eclipse IDE. We should treat the project as if we were creating a Google AppEngine java project, and then we will include the Gaelyk libraries to allow Groovlets to run on the server.

Update the appengine-web.xml

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="">

<!-- Configure java.util.logging -->
<property name="java.util.logging.config.file"
<exclude path="/WEB-INF/**.groovy" />
<exclude path="**.gtpl" />

Update web.xml to handle Gaelyk servlets

<web-app xmlns="" version="2.5">
<!-- The Gaelyk Groovlet servlet -->

<!-- The Gaelyk template servlet -->

<!-- Specify a mapping between *.groovy URLs and Groovlets -->

<!-- Specify a mapping between *.gtpl URLs and templates -->

<!-- Define index.gtpl as a welcome file -->

Copy Gaelyk and Groovy jar file into your webapp/lib directory.

Next, we need an index.gtpl that will be the landing page for this application. The index.gtpl used in this demo can be found here. In this index page we provide links to all the source code for this project. The application starts at the edititems.gtpl page.

The edititems.gtpl will load the data from Google DataStore with the following lines...

<% def query = new Query("Item")
def entityList = datastoreService.prepare(query).asList(FetchOptions.Builder.withLimit(100))

The Items.groovy entity seen above is used to handle the different types of requests as well.

All of the code can be found here. There is a good bit of code for each function of the Items.groovy file, so the exercise of reviewing those functions can be completed outside of this article.

Also, one unique item is a screen scraper which is executed when selecting the "import" button in the edititems.gtpl file. This function uses the XMLSlurper and TagSoup to screen scrape an html file in a wikipedia table. The function then generates a list of buildings in Jacksonville, FL sorted by height and imports the list into the Google DataStore database. The list will be displayed next time the edititems.gtpl is viewed.

public class Building {

def findTallestInJacksonville = {
def xmlReader = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
def url = new URL("")
url.withReader { reader ->
def html = xmlReader.parse(reader)
def listTable = html.body.div.div[0].div.div[1].table[0].tr
return listTable.list()

The code can be deployed from Eclipse to Google by selecting the project and right click > Google > Deploy to App Engine. The entire code base and functoning application that was deployed can be found here...

Also, I recommend Google AppEngine, since it also has some nice management features and is free up to certain limits. This is a great way to start up an application for free until it is successful. Good luck.

No comments: