Reality-Loop

Google App Engine: Guestbook with Groovy Groovlets

April 13, 2009 | 2 Minute Read
This post was originally posted on my old blog.

google-app-engine-groovy.png The web is soaring, since Google announced Java support on Google App Engine!

People are getting ahead of themselves to try out their favorite piece of the Java ecosystem on the Google platform.

I figured that I also wanted to experience this pre-alpha-geek feeling :-)

I followed the guidelines to run Groovy appliccations on Google App Engine from glaforge and reimplemented the guestbook example from the Google Appengine SDK with Groovy Groovlets.

The result is here. Leave me a message ;-)


The backend classes (Greeting.java, PMF.java) were not changed (however it seems to be possible to datanucleusenhance classes written in Groovy and compiled with groovyc, see here).

I kicked out the two servlets and the jsp and replaced them with the following two Groovlets.

hello.goovy
import com.google.appengine.api.users.User
import com.google.appengine.api.users.UserService
import com.google.appengine.api.users.UserServiceFactory
import javax.jdo.PersistenceManager
import guestbook.PMF
import guestbook.Greeting

UserService userService = UserServiceFactory.getUserService()
User u = userService.getCurrentUser()

PersistenceManager pm = PMF.get().getPersistenceManager()
String query = "select from " + Greeting.class.getName() + " order by date desc range 0,25"
List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute()

html.html {
 head {
  title "Hello"
  link(type:"text/css", rel:"stylesheet", href:"/stylesheets/main.css")
 }
 body {
  div(class: "main"){
   div ("Today is: ${new Date()}")

   if (u == null) {
    div(class:"login"){
     a (href: userService.createLoginURL(request.getRequestURI()) , "Log in with your Google Account.")
    }
   }
   else {
    div(class:"login"){
     span("Welcome ${u.nickname}. ")
     a (href: userService.createLogoutURL(request.getRequestURI()) , "Log out")
    }
   }

   p ("Leave me a message:")  

   form(method: "POST", action: "/post.groovy"){
    div(){
     textarea(name: "content", rows: "3", cols: "60", "")
    }
    div(){
     input(type: "submit", value: "Post")
    }
   }

   greetings.each{
    def user = "anonymous"
    if(it.author != null) user = it.author
    div(class:"entry-header", "On  ${it.date} ${user} wrote: ")
    div(class:"entry-body", "${it.content}")
   }
  }
 }
}

post.groovy:
import com.google.appengine.api.users.User
import com.google.appengine.api.users.UserService
import com.google.appengine.api.users.UserServiceFactory
import javax.jdo.PersistenceManager
import guestbook.PMF
import guestbook.Greeting

UserService userService = UserServiceFactory.getUserService()
User user = userService.getCurrentUser()
String content = request.getParameter("content")
Date date = new Date()
Greeting greeting = new Greeting(user, content, date)

PersistenceManager pm = PMF.get().getPersistenceManager()
try {
    pm.makePersistent(greeting)
} finally {
    pm.close()
}

response.sendRedirect("/hello.groovy")

This was my first take at Groovlets, so it might well be that things are not state of the art...

http://www.google.com/s2/favicons?domain=twitter.com follow me on twitter, I need some friends :-)