Resources 1.1(.1) plugin released

Posted by: on Oct 21, 2011 | 5 Comments

After a frantic 12 hours or so of coding and debugging, I managed to release version 1.1.1 of the Resources plugin for Grails today.

It was meant to be 1.1 but there was a bug that needed fixing immediately after releasing so 1.1.1 is where we’re at. This version is tested with Grails 1.3.7 and Grails 2.0 RC1 which is set to drop this weekend.

What’s new in this release of Resources?

Well aside from some useful bug fixes there’s a few interesting changes:

  1. If you forget to include r:layoutResources tags, you will get an error in the console. The error may or may not display in the browser currently depending on whether you used site mesh layouts or not. We may be able to improve this in future. This means that people who wondered where their JS code was disappearing to after installing Resources into a 2.0 app will now get an error telling them that they need to add r:layoutResources.
  2. There is now support for laying out arbitrary dispositions by passing a “disposition” to the r:layoutResources tag. This means you can position sets of resources wherever you like in the output, using multiple dispositions. Currently this is only relevant for JavaScript but in future we should be able to support images so that you can e.g. have a bunch of “preloaded” images that are hidden in the page somewhere.
  3. The integration with Grails 2.0 has been “finalized” now. The ResourceService has changed to a “grailsResourceProcessor” bean, and all g:javascript invocations result in called to r:require or r:script as necessary. The default layouts in 2.0 now use layoutResources and a default “application” module exists in new projects.

There are still some outstanding important bugs that I will endeavour to fix soon, as well as improving the error reporting on all fronts.

Enjoy!

Optimising your Application with Grails Resources Plugin

Optimising your Application with Grails Resources Plugin

Posted by: on Sep 12, 2011 | 16 Comments

It’s been nearly a year since we first announced the Resources framework for Grails. It’s about time we looked at some of the more advanced features that it provides so that you can make the most of this very powerful plugin.

How we use Weceem CMS at NoticeLocal

Posted by: on Feb 15, 2011 | 8 Comments

We have an online notice boards startup called NoticeLocal that we’ve written with Grails.

In my experience over the years it became obvious that one of the major pain-points of using Java to deploy web apps is the hassle of updating a bit of content here or there, replacing an image – those kinds of things that should be simple but aren’t. Also a lot of the time this stuff is not subject to the same strict versioning processes that we are using for our code.

As you may know I am paid to work on the Weceem CMS plugin and application, that is also Grails based.

For NoticeLocal I decided that it would suit us well to use an instance of Weceem CMS running on one Tomcat, to supply the marketing/PR site for the service, as well as the multi-language content for UI elements in the main application.

It turned out this was really easy to do, even though Weceem currently has no API to speak of.

To do this we:

  1. Have a vanilla WeceemApp WAR running in one Tomcat instance
  2. Have our NoticeLocal application WAR running in another Tomcat instance
  3. Wrote a trivial taglib in the NoticeLocal application to retrieve content nodes from the CMS instance using vanilla HTTP requests to URLs with a predefined prefix, and stores the text them in a local ehcache.

So, in Weceem we have a bunch of HTML content nodes that represent different elements of the main NoticeLocal app’s UI – we have several different Weceem templates that these different nodes use – e.g. one for side panels, one for full page content (but laid out inside the main NoticeLocal app sitemesh layout, and one for inline UI messages such as those shown on demand when the user performs some action.

So in our NoticeLocal application we use our taglib to render a “remote CMS message” in our pages using the not-so-well-known Grails/SiteMesh g:applyLayout tag that, just like Grails layouts, will parse out the body of the content that we’ve cached, and render it in situ.

Oh, and for bonus points we use a simple regex replace to give us GSP-like variable expansion when rendering the content from the local cache, so we can perform variable substitutions like ${userName} – so you can write that in a HTML node in Weceem, and by the time the NoticeLocal app renders it, it has your name in it.

At any point we can log into the CMS, update some UI message, and when the cache expires the users see the new updated stuff. No app redeploys.

Oh, and because its in a CMS we solve the hard problem – i18n translations of rich content with styling, images etc.

In a nutshell, it’s great!

Bean-Fields plugin 0.6 released

Posted by: on Mar 13, 2010 | 4 Comments

I have released Bean-Fields plugin 0.6. This extremely handy plugin for Grails applications makes your data form GSPs DRY by centralizing the rendering and styling of your fields, handling <label> rendering, rendering appropriate HTML field based on property type, application of HTML max length constraints, rendering “required field” indicators, and rendering per-field errors. Rendering a whole bean’s worth of fields can be as simple as:

<bean:form beanName="book" properties="title, primaryAuthor.name, isbn"/>

Version 0.6 fixes a bunch of bugs related to rendering fields for nested property paths e.g. propertyName=”book.author.firstName” and introduces support for list / array properties eg “book.authors[3].firstName” (This was really quite painful to implement!). Radio groups are working properly now, and test coverage much improved – thanks to contribs from Antony Stubbs.

It also adds a user-definable threshold for whether a radio group or select list should be used for a field with an inList constraint.

Full list of resolved issues is here.

Weceem 0.8 Released – Highlights and background

Posted by: on Jan 12, 2010 | 4 Comments

I’ve just managed to push out version 0.8 of the Weceem CMS for Grails.

This is a pretty cool if slightly unglamourous release because it has focussed on some performance and security stuff – oh and compatibility with the latest and greatest Grails 1.2.0 release.

Let me first apologise for the ropey typography on weceem.org – we haven’t yet had the time/resources to fix up the CSS styles so things are a little ugly in places. We will fix it as soon as we get time!

Anyway, we added a security policy. This is a groovy script (currently only loaded once at startup) that lets you define what different users can do. This uses a DSL that lets you declare roles and then say what they can do in different spaces, including whether they can admin the space, edit/view/delete/create content, and even do this by URI requested. (see more info and example here)

Because we believe strongly that Weceem should not force you to use a particular authentication library, we had to decouple the policy mechanism from authentication. As a result these roles are completely uninterpreted by Weceem. To integrate and authentication system all you have to be able to do is provide the name, email, login and list of roles for the currently logged-in user. (an example here)

This has enabled some cool stuff in WeceemSecurityService which relies on the security policy. The service has utility methods for implementing our security logic, e.g. isUserAllowedToViewContent(Content c). For example in previous versions of Weceem you could not preview content if it was not in a publicContent status (eg not Published).

Thankfully from version 0.8, anybody who has the EDIT permission can view non-published content. the default security policy ensures that the default administrator account has EDIT permission, so you can preview away as much as you like. We plan future updates to this to allow the security policy to control who can manipulate different types of content, which will be really powerful for people using custom content types.

On the performance front, it became obvious that something needed to be done because the default “index” page installed by Weceem into new spaces was resulting in huge amounts of SQL queries for a single page.

This is because the page is made up like this:

  • It pulls in a Template node for the styling
  • It pulls in three Widgets for reusable HEAD section tags and header and footer
  • The header widget iterates over all root content nodes and their children to render the menu with the wcm:menu tag
  • The page itself links to various StyleSheet and JavaScript nodes to pull in styling and scripts – these are processed on separate requests but still add to the overall burden of the page

This can result in a lot of SQL chatter because we have (rightly so) made no effort to optimize this until now.

There are a couple of areas here that would make a big difference to the SQL traffic.

It is very important to realise that turning on the 2nd level cache in your Grails app’s GORM configuration does not magically give you major performance improvements. My understanding of this rather complex area (which frankly I found very disappointing) is:

  • The 2nd level cache is only used for retrieval by object id. This is very important
  • The query caches are used to cache the ids of objects returned for a given query
  • Query caches are invalidated frequently by Hibernate if your model is not primarily read-only (and can cause some threading contention)

Luckily a CMS is pretty read-only in terms of number of requests that actually read vs update content, so the 2nd level cache is a good candidate for us here.

One of the major SQL hits for Weceem is resolving a URI to the ultimate content node to render. Due to the model we need to query for each part of the URI, so a request for /a/b/c results in three selects. So that’s an easy one – we added URI path -> content id caching (and some other smarts) into the ContentRepositoryService. So once content has been hit, it will always be retrieved by id in future, via the 2nd level cache.

Another issue is iterating over child nodes. This is less trivial. We are using some query caching but I have noticed that some of the criteria were not hitting the caches despite this – it needs further investigation. I think that due to the polymorphic nature of the content model and query cache invalidation issues, we may stop using these in future (think blog comments being submitted and invalidating ALL your caches).

Next up: Template and Widget nodes are GSP pages that we compile and evaluate. It turned out that due to issues in Grails GSP handling (that persist in 1.2 to my knowledge), there is no internal caching of compiled GSP classes built from non-Resource content e.g. strings. This results in a leak of PermGen space which ultimately results in VM collapse. So we now have a simple cache of compiled Template and Widget GSP pages, which is automatically invalidated as necessary when templates and widgets are edited, so it is transparent to the end user that there is a cache.

Finally with regard to performance, we introduced a nice simple wcm:cache tag. This lets you cache fragments of a Template/Widget and hence get major performance improvements. The cache is currently fixed at 1 hour, but its great for anything that pulls in remote content or for any expensive node iteration tags you might be using. More enhancements will come in future.

A couple of nice little things we squeezed in:

  1. The Cancel button is back on the content editor screen, in the “right” place for Windows users (meh) and browsers (who made return always select the first button argh!)
  2. The wcm:link tag now passes through any unused attributes eg class=”whatever”
  3. We added a JS syntax highlighting script to the default space that you can use to render code snippets in your content easily

Anyway, please enjoy this release. Soon time to get started on 0.9 which should see the Blog functionality completed and other refinements.