Archive for January, 2011

This Week in Grails (2011-04)

Sunday, January 30th, 2011

Maybe it’s due to the timing of when I started these posts (around the holidays), but it does seem like each week there are more interesting items to discuss than the previous one. Not that I’m complaining of course 🙂

I wanted to draw some attention to some important items in particular. One is that there’s a new Nabble forum for Plugins, with sub-categories for common themes like security, search, etc. You can post by email or via the web. It’s embedded at grails.org here but you can also get to it directly here. Hopefully this will help focus things so plugin discussions tend to happen at this forum, and more general discussions will continue on the mailing list.

Another is a great post at Tomas Lin’s blog, 7 Business Requirements Often Forgotten By Grails Developers. These are some very important topics to consider (for any app really, not just Grails) and it generated quite a Tweet storm.


Translations of this post:


Miscellaneous Items

Plugins

There was one new plugin released:

  • extended-validation version 0.1. Provides partial validation of (groups) of fields, cascaded validation and instance validators for non-domain objects.

There were also several updated plugins:

  • blueprint version 1.0.2. Provides Blueprint CSS framework resource files and tags
  • codenarc version 0.9. Static code analysis for Groovy
  • freemarker-tags version 0.5.8. Use Grails Dynamic Tag Libraries as Freemarker directives
  • grails-melody version 1.0. Integrates the JavaMelody system monitoring tool
  • ic-alendar version 0.3. Export event data in the iCalendar format – not sure why it’s 1.3.6+ though
  • infinispan version 0.3.9. Adds support for the JBoss Infinispan distributed cache

Interesting Tweets

Jobs



User groups and Conferences



This Week in Grails (2011-03)

Sunday, January 23rd, 2011

This week was another good one for Groovy and Grails-related items, especially the job listings which do seem to be picking up. I’ve added a subsection with keyword search links for a few job sites and also a link to the @SpringSourceJob Twitter feed (Grails jobs are quite rare but we are hiring for various interesting positions).


Translations of this post:


Miscellaneous Items

Plugins

There were 2 new plugins released:

  • feature-flipper version 0.1. Provides a mechanism to turn features on and off. It is inspired by Flikr’s ‘flipping out’ work in this space to do the same, and the implementation is quite similar.
  • jalarms version 0.1. Send application notifications using a wide range of protocols via JAlarms

There were also several updated plugins:

  • aws version 0.2. Provide easy access to simpler functions of Amazon Web Services (AWS)
  • activemq version 0.2.1. Embeds ActiveMQ for messaging
  • ajaxflow version 0.1.19. Enables Ajaxified Webflows
  • barcode4j version 0.2.1. Generates barcodes using http://barcode4j.sourceforge.net/
  • cassandra version 0.7.0.20110120.1. Provides access to Cassandra (not a GORM layer)
  • dbunit-operator version 1.5. Create initial data with the help of DbUnit
  • export version 0.8. Export domain objects to a variety of formats (CSV, Excel, ODS, PDF, RTF and XML)
  • external-config version 2.0. Provides easy and consistent facility to have externalized configuration files
  • fixtures version 1.0.3. Load test data via a convenient DSL
  • gdsflex version 0.8.5. Provides integration between Grails and Adobe Flex using Granite Data Services
  • grom version 0.2.2. Sends notifications on Windows, Linux, and Mac
  • infinispan version 0.3.8. Adds support for the JBoss Infinispan distributed cache
  • spreedly version 0.1.5. Provides access to the Spreedly online payment service API
  • spring-security-core version 1.1. The official Grails security plugin; integrates with Spring Security
  • weceem-spring-security version 1.0-RC3. Provides the glue needed to make Weceem plugin use Spring Security for authorisation and authentication

Interesting Tweets

Jobs



User groups and Conferences

This Week in Grails (2011-02)

Sunday, January 16th, 2011

Another interesting week for Groovy- and Grails-related news.


Translations of this post:


Miscellaneous Items

Plugins

There were 4 new plugins released:

  • aws version 0.1. Provide easy access to simpler functions of Amazon Web Services (AWS)
  • database-migration version 0.1. Official Grails plugin for database migrations
  • google-url-shortener version 0.1. Use Google’s URL shortener service
  • jquery-json version 2.2. Supplies jQuery JSON resources, and depends on the jQuery plugin to include the core jquery libraries

There were also several updated plugins:

  • ajaxflow version 0.1.16. Enables Ajaxified Webflows
  • attachmentable version 0.2.1. Provides a generic way to add and manage attachments
  • dbunit-operator version 1.4. Create initial data with the help of DbUnit
  • elasticsearch version 0.13.0-b. Adds support for the http://www.elasticsearch.com/ RESTful search engine
  • freemarker-tags version 0.5.5. Use Grails Dynamic Tag Libraries as Freemarker directives
  • gmetrics version 0.3.1. Provides static code analysis for Groovy code
  • gsp-arse version 1.3. My favorite plugin name. Adds parsing of resource files (e.g. javascript, css) from the base path directory as GSP files so you can use tags and data binding
  • jasper version 1.2. Enables use of JasperReports
  • jquery-plugin-authoring version 1.0.2. Template to make creating jQuery plugins easier
  • liquibase version 1.9.3.6. Liquibase database migration plugin (users should switch to the new database-migration plugin)
  • maven-publisher version 0.7.5. Publish Grails projects and plugins to local and remote Maven repositories
  • multi-tenant-core version 1.0.3. Supports multiple ‘customers’ (or tenants) from one installation of a Grails application
  • ofchart version 0.6.3. Open Flash Chart Plugin
  • screencasts version 0.5. Upload, manage and display screencasts
  • twitter-checker version 0.2. Monitors Twitter for events including new followers and unfollows in your account, new RTs of you made by other users, and new mentions to you made by other users
  • weceem-spring-security version 1.0-RC2. Provides the glue needed to make Weceem plugin use Spring Security for authorisation and authentication

Interesting Tweets

Jobs

User groups and Conferences

Using the Log4j MDC in Grails

Friday, January 14th, 2011

I read this interesting blog post about using the Log4j MDC to add information to logged messages that was referenced on the Grails User mailing list and wondered what it would take to get it working in Grails since the example is traditional Java. Turns out it’s pretty simple.


But first – what is the MDC? It’s Log4j’s MDC class, which implements a Mapped Diagnostic Context. There’s a similar class, NDC, which implements a Nested Diagnostic Context. In this case there’s no need for nesting, we just want to store temporary information in thread-local storage under a key, so MDC makes more sense. Click the links to read the Javadoc for the two classes, and see this wiki page for a comparison of the two.

Also note that if there’s an SLF4j wrapper for this class that you can use if you want the logging implementation transparency that SLF4j provides.


Ok, so how would we use this in Grails? There are just two steps:

  • create a filter to store data in the MDC
  • edit the logging pattern to include the data

We have two options for the filter – either a Grails filter, or a servlet filter. Grails filters only apply to controller requests and are written in Groovy, whereas servlet filters apply to whatever request pattern you specify in web.xml and are written in Java or Groovy. So the decision about which to use comes down to what you want to filter, and if you have Groovy/Java performance concerns.

Grails Filter

To use a Grails filter, run the create-filters script, e.g.

grails create-filters com.burtbeckwith.mdctest.logging

and add this code to grails-app/conf/com/burtbeckwith/mdctest/LoggingFilters.groovy:

package com.burtbeckwith.mdctest

import org.apache.log4j.MDC

class LoggingFilters {

   def filters = {
      all(controller:'*', action:'*') {
         before = {
            MDC.put 'username', request.userPrincipal?.name ?: 'N/A'
         }
         afterView = { e ->
            MDC.remove 'username'
         }
      }
   }
}

We're storing the logged-in user's name to the MDC under the username key, or 'N/A' if there's no authenticated user. You can also store other request-specific information, but for this example we'll keep things simple. Also note that after the request is finished processing we remove the data from the MDC to avoid memory leak issues.

Servlet Filter

If you want to use a servlet filter, create a class in src/java, e.g. src/java/com/burtbeckwith/mdctest/MdcFilter.java, and add this code:

package com.burtbeckwith.mdctest;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.MDC;

public class MdcFilter implements Filter {

   public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
         throws IOException, ServletException {
      try {
         Principal principal =1;
         chain.doFilter(req, res);
      }
      finally {
         MDC.remove("username");
      }
   }

   public void init(FilterConfig fc) {/*nothing to do*/}
   public void destroy() {/*nothing to do*/}
}

You'll need to add this to web.xml, so run

grails install-templates

if you haven't already and add a filter and a filter-mapping element:

<filter>
   <filter-name>com.burtbeckwith.mdctest.MdcFilter</filter-name>
   <filter-class>com.burtbeckwith.mdctest.MdcFilter</filter-class>
</filter>

<filter-mapping>
   <filter-name>com.burtbeckwith.mdctest.MdcFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

Edit the logging pattern

We need to update the logging pattern to include the MDC data, so add an appender override for the 'console' appender (update your other appenders similarly) in grails-app/conf/Config.groovy:

log4j = {
   appenders {
      console name: 'stdout',
              layout: pattern(conversionPattern: '%c{2} %m - %X{username}%n')
   }
   ...
}

The significant change is the addition of %X{username} to include the username value.

Testing

To test this, I installed the Spring Security plugin and added a test user. I created two simple controllers, one that requires authentication:

import grails.plugins.springsecurity.Secured

@Secured(['ROLE_ADMIN'])
class AdminController {
   def index = {
      log.debug 'accessing admin controller'
      render 'Hello admin'
   }
}

and one that doesn't:

class TestController {
   def index = {
      log.debug 'accessing test controller'
      render 'Hello world'
   }
}

Then I configured controllers to log at the debug level:

log4j = {
   ...
   debug 'grails.app.controller'
   ...
}

and ran the application. Accessing /test shows that I'm not logged in:

mdctest.TestController accessing test controller - N/A

and accessing /admin after logging in displays my username:

mdctest.AdminController accessing admin controller - admin

Download the sample application here.

There's a user 'admin' configured in BootStrap.groovy with password 'password' that you can use to test login.

Note that the sample application has both filters implemented, so you should remove the one you don't want to use.

  1. HttpServletRequest)req).getUserPrincipal(); MDC.put("username", principal == null ? "N/A" : principal.getName([back]

This Week in Grails (2011-01)

Sunday, January 09th, 2011

This week had a lot of activity – over 20 plugins were updated and there were a lot of job leads.

I’ve added a new section for Meetups, user group meetings, etc. I’ll post whatever I find, but if you run a user group please contact me in advance of the meetup and I’ll be sure to get it included in that week’s update.


Translations of this post:


Miscellaneous Items

Plugins

There were 5 new plugins released:

  • blanq-mail version 0.3. Send asynchronous email
  • content-buffer version 1.0-RC1. Use this plugin to append page fragments to pre-defined sections in your layout template
  • sendfile version 0.1. Allow the use of sendfile or X-sendfile functionality of web-servers.
  • jquery-plugin-authoring version 1.0.1. Template to make creating jQuery plugins easier
  • weceem-spring-security version 1.0-RC1. Provides the glue needed to make Weceem plugin use Spring Security for authorisation and authentication.

There were also several updated plugins:

  • activiti version 5.1. Integrates the Activiti BPM Suite and workflow system
  • ajaxflow version 0.1.15. Enables Ajaxified Webflows
  • attachmentable version 0.2.0. Provides a generic way to add and manage attachments
  • batch-launcher version 0.4.2. Load and run batch (non-webapp) Grails applications
  • extended-data-binding version 0.5. DataBinder configuration
  • fixtures version 1.0.2. Load test data via a convenient DSL
  • full-calendar version 1.4.10.1. Adds support for the FullCalendar jQuery plugin
  • gsp-arse version 1.2. Parse scripts, css, special files as GSP
  • jasypt-encryption version 0.1.2. Integration the Jasypt encryption library
  • jquery version 1.4.4.1. Integrates jQuery
  • jquery-validation version 1.7.3. Supplies jQuery Validation resources, and depends on the jQuery plugin to include the core jquery libraries.
  • jquery-validation-ui version 1.1.1. Client Side Validation without writing JavaScript
  • liquibase version 1.9.3.6. LiquiBase Database Refactoring
  • multi-tenant-core version 1.0.2. Lets you run multiple ‘customers’ (or tenants) from one installation of a Grails application
  • nerderg-form-tags version 1.1. Form Taglib
  • rendering version 0.4.2. Render GSPs as PDFs, JPEGs, GIFs and PNGs
  • remote-control version 1.1. Execute code inside a remote Grails application
  • rest version 0.6. Enables the usage of HTTPBuilder
  • smartgwt version 0.2. Integrates GWT and SmartGWT
  • spreedly version 0.1.4. Provides access to the Spreedly online payment service API
  • webdav version 0.3.1. Provides a webdav interface
  • webtest version 3.0.1. Integrates webtest functional testing

Interesting Tweets

Jobs

Meetups

This Week in Grails (2010-52)

Saturday, January 01st, 2011

I expected things to be slower this week due to the holidays, but there were plenty of Grails-related items.

Starting this week these “This Week in Grails” posts will be translated into various languages. Thanks to Aitor Alzola for the Spanish translation, Hu Jian for the Chinese translation, Paulo Pereira for the Portuguese translation, and Nabil Adouani for the French translation. Contact me if you’re interested in doing a translation into another language.

Translations of this post:

Miscellaneous Items

Plugins

There were 2 new plugins released:

There were also several updated plugins:

  • codenarc version 0.8.1. Static code analysis for Groovy
  • dynamic-domain-class version 0.2.1. Create domain classes dynamically at runtime
  • grails-melody version 0.9. Integrates the JavaMelody system monitoring tool
  • jquery-validation-ui version 1.1. Client Side Validation without writing JavaScript
  • multi-tenant-core version 1.0.1. Lets you run multiple ‘customers’ (or tenants) from one installation of a Grails application
  • multi-tenant-spring-security version 0.2.1. Integrates the multi-tenant plugin with the spring-security-core plugin
  • spreadshirt version 0.4. Wrapper for the Spreadshirt/ API
  • xfire version 0.8.3. Expose Web services using Grails service classes

Interesting Tweets

Jobs

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.