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.

Recap

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="http://appengine.google.com/ns/1.0">
<application>pac-gcloud</application>
<version>1</version>

<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file"
value="WEB-INF/logging.properties"/>
</system-properties>
<static-files>
<exclude path="/WEB-INF/**.groovy" />
<exclude path="**.gtpl" />
</static-files>
</appengine-web-app>


Update web.xml to handle Gaelyk servlets

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<!-- The Gaelyk Groovlet servlet -->
<servlet>
<servlet-name>GroovletServlet</servlet-name>
<servlet-class>groovyx.gaelyk.GaelykServlet</servlet-class>
</servlet>

<!-- The Gaelyk template servlet -->
<servlet>
<servlet-name>TemplateServlet</servlet-name>
<servlet-class>groovyx.gaelyk.GaelykTemplateServlet</servlet-class>
</servlet>

<!-- Specify a mapping between *.groovy URLs and Groovlets -->
<servlet-mapping>
<servlet-name>GroovletServlet</servlet-name>
<url-pattern>*.groovy</url-pattern>
</servlet-mapping>

<!-- Specify a mapping between *.gtpl URLs and templates -->
<servlet-mapping>
<servlet-name>TemplateServlet</servlet-name>
<url-pattern>*.gtpl</url-pattern>
</servlet-mapping>

<!-- Define index.gtpl as a welcome file -->
<welcome-file-list>
<welcome-file>index.gtpl</welcome-file>
</welcome-file-list>
</web-app>


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("http://en.wikipedia.org/wiki/List_of_tallest_buildings_in_Jacksonville")
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...

http://pac-gcloud.appspot.com/

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: