Archive for September, 2009

Using AppEngine Logging with the Grails AppEngine Plugin

Grails is all about convention-over-configuration, and in most cases that idea works just fine. Sometime that idea isn’t so great.

The Grails App engine plugin provides an example of this. When packaging and deploying a new version of your app, the grails appengine task generates the appengine-web.xml file for you automatically. This file is used by Google App Engine to configure your app. The problem I faced recently is that I needed to make a change to this file, and there was no way to do it without modifying the GANT task that generates the file. Which is what I did.

In my case I wanted to enable the handy logging mechanism provided by Google AppEngine. GAE uses the java.util.logging framework to accomplish this as documented here.

I copied the logging.properties file to: MY_GRAILS_APP/web-app/WEB-INF

Then to point to my logging.properties file, I had to modify the GANT task that generates the appengine-web.xml file.

USER_HOME/.grails/1.1.1/projects/MY_GRAILS_APP/plugins/app-engine-0.8.5/scripts/_Events.groovy

I modified the section that stamps out the appengine-web.xml file to look like this:

println "Generating appengine-web.xml file for application [$appName]"	
	new File("$stagingDir/WEB-INF/appengine-web.xml").write """<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    <application>${appName}</application>
    <version>${appVersion}</version>
    <sessions-enabled>${enableSessions}</sessions-enabled>
    <ssl-enabled>${enableSsl}</ssl-enabled>
	<system-properties>
	    <!-- this property should be necessary. it avoids the report of an abnormal and unexpected situation!
	        <property name="appengine.orm.disable.duplicate.emf.exception" value="true" />
	     -->
	     <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
    </system-properties>
 
</appengine-web-app>

After running a grails app-engine deploy, the new version of the app was deployed and the log messages came through in my Google App Engine Dashboard.

Perhaps in future releases of the grails app engine plugin, a better way will be provided to make changes to the appengine-web.xml file.

Using the Google App-Engine Plugin for Grails

Like Rails, Grails can be configured with plugins. There is an entire repository at grails.org. Many of the plugins are very helpful, such as the Grails Google App Engine Plugin.

I used the plugin to create and deploy a web site for my company, Clearbox Consulting. The plugin made the entire process that much easier.

Google App Engine is simply awesome. It provides persistence, logging, version control, statistics and many other cool features. Check it out.

I really don’t need to be running a grails-enabled web site, hosted via google appengine just yet. For now I am only serving up static content. But I figured it wouldn’t hurt to get Grails setup, so that ini the future I can add more enhanced functionality.

Executing Shell Commands in Groovy

In Java, to execute a shell command, I would use something like this:

import java.io.*;
public class ExecuteCommand
{
   public static void main(String[] args)
   {
      try
      {
         Process proc = Runtime.getRuntime().exec("set" );
         BufferedReader result = new BufferedReader(
         new InputStreamReader(proc.getInputStream()));
         String line;
         while((line = result.readLine()) != null)
         {
            System.out.println(line);
         }
      }
      catch(IOException ex)
      {
         ex.printStackTrace();
      }
   }
}

The same thing can be done in Groovy with the following one-liner:

println "set".execute().text

GPG Encryption — Using Groovy

Here is a great example of how Groovy is so much less verbose than Java.

The script below is a simple utility I wrote that encrypts a directory of files using GPG. The command.execute() syntax in groovy is so much less verbose than the Java counterpart.

def fileEncrypter(srcFolder, destFolder)
{
	new File(destFolder).eachFile{file -&gt;
		println "Deleting File: " + file.absolutePath
		file.delete()
	}
 
	new File(srcFolder).eachFile{file -&gt;
		String command = "gpg -e -o "
		command += destFolder + "/" + file.name
		command += ".pgp -r "My Key" --yes -q "
		command += srcFolder + "/" + file.name
		println "Encrypting file: " + file.absolutePath
		command.execute().text
	}
}

Tags: ,

The day I discovered Groovy

Groovy has changed my life. At least my life as a software engineer. After many years of Java programming, I was disenchanted with the language’s restrictiveness. I delved into Ruby to find solace in dynamic languages and I found that I wasn’t exactly happy switching completely out of the Java paradigm. Especially when it came to constructing Enterprise-level applications. Well… along came Groovy… and my world has changed. Closures, Dynamic Typing and many other features that allow you to get things done quicker… and it all runs within the JVM. Brilliant…

Here are some cool examples of looping mechanisms in Groovy:

0.upto(4) { print "$it " }

outputs

0 1 2 3 4

http://groovy.codehaus.org/Looping

Tags:

Welcome to katiemacpherson.com

It’s about time I started to collect my thoughts into one place. Besides, I’ve benefited greatly during the course of my career as a software engineer from the random musings, hints, fixes, and tips posted by other software engineers out there. So I think it is about time I gave something back.