Profiling web requests in your Grails application

| Comments

Here’s a simple filter I’ve been using to help me detect points of improvement in my application:

class UtilFilters {

    def filters = {

        profiler(controller: '*', action: '*') {
            before = {
                request._timeBeforeRequest = System.currentTimeMillis()
            }

            after = {
                request._timeAfterRequest = System.currentTimeMillis()
            }

            afterView = {
                if (params.showTime) {
                    session._showTime = params.showTime == "on"
                }
                if (session._showTime) {
                    def actionDuration = request._timeAfterRequest - request._timeBeforeRequest
                    def viewDuration = System.currentTimeMillis() - request._timeAfterRequest
                    log.debug("Request duration for (${controllerName}/${actionName}): ${actionDuration}ms/${viewDuration}ms")
                }
            }
        }
    }

}

To use it, put this class in the grails-app/conf folder of your project. To activate the profile, call any URL of your application with the showTime=on parameter, like this:

Using Cache-Headers plugin in a non-english server

| Comments

UPDATE: This problem has been fixed in version 1.1.3 of the plugin. Thanks Marc and Luke Daley!

This week I tried the Marc Palmer’s excelent plugin Cache-Headers, and it really rocks! Using it I can make all my server-side generated images be cached on the client, reducing significantly the bandwidth and cpu-power necessary by my application.

But there’s a little gotcha: The plugin (as of version 1.1.2) uses a SimpleDateFormat to generate and check the Last-Modified header, and the implementation creates this SimpleDateFormat with the system’s default Locale, in my case Portuguese. This causes errors like this:

Apache Shiro tags for Facelets - Securing your JSF pages

| Comments

UPDATE There is now a JSF 2.0 version of this library available in the project repo

First, a small introduction. You can skip it and go straight to the source code, if you want.

I started working with Apache Shiro when it was still called JSecurity, and I have to say that it really rocks! I tried to use Spring Security (Acegi) in some projects, but the easiness and lean approach of Shiro is unbeatable. For a quick introduction, here’s a quote from the project’s site:

Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management.
With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.

Using Spring DI in your Gaelyk projects

| Comments

Since I started using Gaelyk, one of the features I missed most (coming from a Grails background) is Spring’s dependency injection. Until recently I didn’t even know if it was possible to use Spring in Google App Engine, so I decided to do a little investigation on the subject, and found out that it’s very easy indeed.

Here’s a little tutorial on how to configure Spring in your Gaelyk project. I’m assuming you have basic knowledge of Spring, Gaelyk and Maven.

First, let’s create a Gaelyk project. The easiest way is using the excellent maven-gaelyk archetype:

1
mvn archetype:generate -DarchetypeGroupId=org.codeconsole -DarchetypeArtifactId=gaelyk-archetype -DarchetypeVersion=0.5.5 -DarchetypeRepository=http://maven-gaelyk.googlecode.com/svn/repository/ -DartifactId=gaelyk-spring -DgroupId=com.deluan.gaelyk -DgaeApplicationName=gaelyk-spring

Calling the correct Grails version automatically from command line

| Comments

UPDATE: For latest version and instructions, see http://github.com/deluan/grails.sh

Now that I decided to organize and publish some of my code/hacks here, I thought it would be a good thing to republish here my Grails caller script.

I work on and maintain various Grails projects at the same time, and some of them uses versions of Grails as old as 1.0.3! So the question is: How to call the right version of grails command for a given project, the version that the project was created with?

How to use an external log4j.properties in you Grails project

| Comments

In a recent Grails project, I had to follow some corporate guidelines regarding application deployment, and one of those were that the log4j configuration for an application must be externalized in a properties file.

I searched for a Grails plugin that could help me with this, with no luck. Then I remembered that a Grails application is just a Spring application in disguise, so I looked for a Spring way to do this.

There are at least two ways to do this using Spring: Log4jConfigListener and Log4jConfigurer. I chose the later because the former assumes an expanded WAR file, which was not my case.

Here’s the recipe I came up with:

Creating permalinks with Grails

| Comments

I’ve been working with Grails for some time now and if I had to choose just one good thing to say about it is how it’s community is really great!

I think it’s time to start giving back some contribution, and here’s the first one: A Permalink Codec to generate permalinks based on strings. It strips out all non word chars and convert the resulting string to lowercase: