<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Oliver's Place</title>
	
	<link>http://weichhold.com</link>
	<description />
	<lastBuildDate>Thu, 12 Aug 2010 18:13:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/olivers_place" /><feedburner:info uri="olivers_place" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Using Mercurial over SSH with Private Key in TeamCity CI</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/n2k_00hvyew/</link>
		<comments>http://weichhold.com/2010/08/12/using-mercurial-over-ssh-with-private-key-in-teamcity-ci/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 15:52:56 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Continous Integration]]></category>
		<category><![CDATA[Mercurial]]></category>
		<category><![CDATA[TeamCity]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=161</guid>
		<description><![CDATA[Due to the much better Visual Studio and Windows integration of Mercurial I&#8217;ve recently switched from Git to Mercurial for my .Net based projects. After setting up a Windows Server 2008 R2 instance on my VMWare vSphere Box I&#8217;ve switched from Hudson to TeamCity for Continuous Integration and I&#8217;m pretty satisfied with the outcome. I [...]]]></description>
			<content:encoded><![CDATA[<p>Due to the much better Visual Studio and Windows integration of Mercurial I&#8217;ve recently switched from Git to Mercurial for my .Net based projects. After setting up a Windows Server 2008 R2 instance on my <a href="http://www.vmware.com/products/vsphere/">VMWare vSphere Box</a> I&#8217;ve switched from Hudson to TeamCity for Continuous Integration and I&#8217;m pretty satisfied with the outcome. I can finally handle both Java and .Net Projects on a single CI Server.</p>
<p>There was one issue that was causing me a major headache during the setup phase and that was getting Mercurial talk to my private repository server over SSH using private key authentication from within TeamCity&#8217;s Windows Service running under the SYSTEM account (I&#8217;d rather have it run under a non-privileged user but that doesn&#8217;t seem to be supported).<br />
<span id="more-161"></span><br />
The first problem was getting Mercurial to use Putty&#8217;s plink.exe while running under the system account. Normally you would just create a file named mercurial.ini in the user&#8217;s home directory containing something like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code5'); return false;">View Code</a> INI</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1615"><td class="code" id="p161code5"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>ui<span style="">&#93;</span></span>
<span style="color: #000099;">ssh</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;C:\Program Files\putty\plink.exe&quot;</span> -i <span style="color: #933;">&quot;c:\your.ppk&quot;</span></pre></td></tr></table></div>

<p>But where do you put this file in the case of the system account? Some Googling on that matter turned out that it should go to \System32\config\systemprofile. I&#8217;ve tried that to no avail. Mercurial would still try to invoke ssh.exe which is obviously nowhere to be found on a Windows box. I realized that the Mercurial Executable is 32-Bit which meant that the home directory of the SYSTEM account had to be located somewhere under \SysWoW64 and indeed there is a \SysWoW64\config\systemprofile directory. After moving my mercurial.ini to that directory the &#8220;Test Connection&#8221; button in TeamCity would *drumroll* &#8230; HANG!</p>
<p>The reason for this behavior was that plink.exe prompted the user to accept the new ssh host key &#8211; in an invisible shell window. To overcome this hurdle I shutdown the TeamCity Web Service and opened a new shell window running under the SYSTEM Account using the <a href="http://technet.microsoft.com/de-de/sysinternals/bb897553.aspx">psExec Utility</a> by Mark Russinovich:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code6'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1616"><td class="code" id="p161code6"><pre class="shell" style="font-family:monospace;">psexec.exe -s cmd.exe</pre></td></tr></table></div>

<p>Now that I had a shell running under the system account I could connect to my server and accept the host key:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code7'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1617"><td class="code" id="p161code7"><pre class="shell" style="font-family:monospace;">plink 192.168.230.1</pre></td></tr></table></div>

<p>Low and behold clicking the &#8220;Test Connection&#8221; button in TeamCity would now give me a different error:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p161code8'); return false;">View Code</a> SHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1618"><td class="code" id="p161code8"><pre class="shell" style="font-family:monospace;">&quot;C:\Program Files (x86)\Mercurial\hg.exe&quot; identify ssh://git@192.168.230.1/usr/local/hg/testproject' command failed.
stderr: abort: no suitable response from remote hg!
&nbsp;
stdout: remote: abort: There is no Mercurial repository here (.hg not found)!</pre></td></tr></table></div>

<p>Fiddling around with the settings for another hour turned out that there was a problem with my SSH Url in TeamCity: ssh://git@192.168.230.1/usr/local/hg/testproject</p>
<ol>
<li>The ssh URL contained the user name</li>
<li>The path needs to contain a second / at the beginning otherwise the server will see it as &#8220;usr/local/hg/testproject&#8221;</li>
<li>The user name must not be part of the SSH Url but needs be entered in Teamcity&#8217;s &#8220;User name:&#8221; field under &#8220;Authorization Settings&#8221;</li>
</ol>
<p>After changing the SSH Url to: ssh://192.168.230.1//usr/local/hg/testproject (please note the double slash after the host), I could finally connect, pull the sources and build my project.</p>
<p>Please keep in mind that your private key may <b>not</b> be protected by a passphrase!</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=161&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/qC0kMZc8y59bEjUQrpdareyVyJo/0/da"><img src="http://feedads.g.doubleclick.net/~a/qC0kMZc8y59bEjUQrpdareyVyJo/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/qC0kMZc8y59bEjUQrpdareyVyJo/1/da"><img src="http://feedads.g.doubleclick.net/~a/qC0kMZc8y59bEjUQrpdareyVyJo/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/n2k_00hvyew" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/08/12/using-mercurial-over-ssh-with-private-key-in-teamcity-ci/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://weichhold.com/2010/08/12/using-mercurial-over-ssh-with-private-key-in-teamcity-ci/</feedburner:origLink></item>
		<item>
		<title>High performance Grails with memcached</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/f-FKMz62mOA/</link>
		<comments>http://weichhold.com/2010/03/04/high-performance-grails-with-memcached/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:28:24 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[memcached]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=148</guid>
		<description><![CDATA[This article is the second one in my article series about fast loading web pages. The first article dealt with the Django Framework while this one is about Grails which I have recently elected as my preferred rapid web application framework. Ever since I switched from Django to Grails there was one issue that bothered [...]]]></description>
			<content:encoded><![CDATA[<p>This article is the second one in my article series about fast loading web pages. The first article dealt with the <a href="http://www.djangoproject.com/">Django Framework</a> while this one is about <a href="http://grails.org">Grails </a>which I have recently elected as my preferred rapid web application framework. Ever since I switched from Django to Grails there was one issue that bothered me and that was the loss of the ability to serve complete pre-rendered pages from a distributed cache to the enduser while bypassing the application server.</p>
<p>The solution described in this article is in use on the production servers powering <a href="http://mmogle.com">mmogle.com</a> where it ensures page response times below 100ms for cached content.</p>
<p>Let me summarize the concept for readers not familiar with my first article: The goal we want to achieve is to greatly decrease the load on the application server (container) by storing the raw HTML output of rendered pages in a (distributed-) cache to be picked up by a Frontend Web Server without even bothering the application server behind it for the current request. Since we would need stuff like Edge Side Includes (ESI) for dealing with personalized pages, we are going limit ourselves to anonymous users.</p>
<p><span id="more-148"></span></p>
<p>This picture below shows the server setup:</p>

<a href="http://weichhold.com/wp-content/gallery/misc/nginx-memcached.png" title="" class="thickbox" rel="singlepic8" >
	<img class="ngg-singlepic" src="http://weichhold.com/wp-content/plugins/nextgen-gallery/nggshow.php?pid=8&amp;width=420&amp;height=&amp;mode=" alt="nginx-memcached" title="nginx-memcached" />
</a>

<p><a href="http://wiki.nginx.org/Main">Nginx </a>will act as frontend web server. This is where all requests will initially arrive. Requests for static resources will always be handled by nginx. Dynamic requests will be scrutinized and once it is determined that a request could be cached, a cache key is computed based on request URI, cookies etc. Nginx will then proceed with querying memcached if one of the application servers has deposited content under that key. If the answer is positive the response from memcached is directly returned to the client – the application servers are bypassed. Perhaps it is worth noting that the application server (tomcat) is only responsible for storing the content, whereas nginx &#8211; acting as frontend server &#8211; is responsible for retrieving it. This decoupling is what sets this technique apart from the Grails Cache Filter Plugin which both stores and retrieves cached content.</p>
<p>Let’s continue with the actual implementation. When I decided to port the technique I described in the aforementioned article from Python/Django to Groovy/Grails, I thought it was going to be a very easy task. All that’s needed is to grab the rendered page contents within a Grails afterView Filter, put it into memcached and be done with it, right? Wrong! Imagine my surprise when I noticed the rendered output never had a layout applied. After lots of trying I gave up, realising that there is simply no way to get my hands on the <strong>complete </strong>HTML (emphasis on <em>complete</em>) for a rendered Grails page from within the application itself. The reason for this is Grails layouts are based on Sitemesh which is implemented as a ServletFilter. Grails applies the Layout to pages through its GrailsPageFilter ServletFilter which is executed further down in the pipeline after any Grails Filters.</p>
<p>That left me with only one option: a custom ServletFilter that executes before GrailsPageFilter. So I checked out the source of the <a href="http://www.grails.org/plugin/cachefilter">Grails Cache Filter Plugin</a>, stripped it off anything not necessary for our purpose and born was MemcachedFilter. You can download the source <a href="http://weichhold.com/MemcachedFilter.zip">here</a>.</p>
<p>MemcachedFilter is written in Groovy and it needs to be configured to execute before GrailsPageFilter. That means that it’s filter mapping element must appear before the filter mapping for GrailsPageFilter in web.xml. Here’s a sample web.xml snippet:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p148code13'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14813"><td class="code" id="p148code13"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>memcached<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.banshee.servlet.MemcachedFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- memcached filter --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>memcached<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>charEncodingFilter<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>sitemesh<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>MemcachedFilter has a dependency on a bean named “memcachedClient”. Please <a href="http://weichhold.com/2009/09/17/configuring-memcached-in-a-grails-application/">refer to this article</a> for configuration instructions.</p>
<p>Now that the filter is configured and active we actually want to put it to use – which is quite easy. The filter scans all Responses it encounters for the presence of two special headers: &#8220;X-Memcached-Filter-Cache-Key&#8221; and &#8220;X-Memcached-Filter-Cache-Timeout&#8221; (the latter is optional). If the &#8220;X-Memcached-Filter-Cache-Key&#8221; is detected the filter will store the response content in memcached using the header value as key. The expiration timeout will be either a default value or the value of the &#8220;X-Memcached-Filter-Cache-Timeout&#8221; header. It’s that simple. Here’s a usage example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p148code14'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14814"><td class="code" id="p148code14"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FooController
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>SecurityUtils.<span style="color: #006600;">subject</span>.<span style="color: #006600;">principal</span><span style="color: #66cc66;">&#41;</span>      <span style="color: #808080; font-style: italic;">// store response only for anonymous users (using Shiro plugin)</span>
    <span style="color: #66cc66;">&#123;</span>
      response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span>MemcachedFilter.<span style="color: #006600;">MEMCACHED_FILTER_X_CACHE_KEY</span>, ConfigurationHolder.<span style="color: #006600;">config</span>.<span style="color: #006600;">app_prefix</span> <span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">forwardURI</span> <span style="color: #66cc66;">-</span> request.<span style="color: #006600;">contextPath</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      response.<span style="color: #006600;">setHeader</span><span style="color: #66cc66;">&#40;</span>MemcachedFilter.<span style="color: #006600;">MEMCACHED_FILTER_X_CACHE_TIMEOUT</span>, <span style="color: #cc66cc;">120</span><span style="color: #66cc66;">&#41;</span>         <span style="color: #808080; font-style: italic;">// cache for two minutes</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This will store the response content for FooController’s ‘index’ action in memcached for two minutes using the context relative request URI as cache key as long as the requesting user is not logged in. It is of paramount importance that you never ever do this for authenticated users or bad things may happen. You don’t want John Doe to access information intended for the boss, only because the boss happened to be the first user accessing a certain page, do you? To make sure that our Frontend server will try fetch content from the cache for authenticated users we need a way for it to tell if a request is from an anonymous or authenticated user. To do this I wrote a simple Grails Filter that ensures that authenticated users are tagged with a cookie:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p148code15'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14815"><td class="code" id="p148code15"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> CacheFilters
<span style="color: #66cc66;">&#123;</span>
  CacheService questionCacheService
&nbsp;
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #aaaadd; font-weight: bold;">String</span> MEMCACHED_USER_IS_AUTH_INDICATOR_COOKIE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'user_id'</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">def</span> filters <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">/** Sets or deletes a cookie that signals nginx that the current user is authenticated - not used for permission checks */</span>
    setNginxUserIsAuthenticatedIndicator<span style="color: #66cc66;">&#40;</span>controller: <span style="color: #ff0000;">'*'</span>, action: <span style="color: #ff0000;">'*'</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
      afterView <span style="color: #66cc66;">=</span>
      <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> cookie <span style="color: #66cc66;">=</span> request.<span style="color: #006600;">cookies</span>.<span style="color: #663399;">find</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">name</span> <span style="color: #66cc66;">==</span> MEMCACHED_USER_IS_AUTH_INDICATOR_COOKIE <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// is the user authenticated (by login or cookie?)</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>SecurityUtils.<span style="color: #006600;">subject</span>.<span style="color: #006600;">principal</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// yes, set the cookie if needed</span>
          <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>cookie<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
            cookie <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Cookie<span style="color: #66cc66;">&#40;</span>MEMCACHED_USER_IS_AUTH_INDICATOR_COOKIE, User.<span style="color: #663399;">get</span><span style="color: #66cc66;">&#40;</span>SecurityUtils.<span style="color: #006600;">subject</span>.<span style="color: #006600;">principal</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">id</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            cookie.<span style="color: #006600;">setPath</span><span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">getContextPath</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            response.<span style="color: #006600;">addCookie</span><span style="color: #66cc66;">&#40;</span>cookie<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">else</span>
        <span style="color: #66cc66;">&#123;</span>
          <span style="color: #808080; font-style: italic;">// delete the cookie if necessary</span>
          <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>cookie<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#123;</span>
            cookie.<span style="color: #006600;">setMaxAge</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
            cookie.<span style="color: #006600;">setPath</span><span style="color: #66cc66;">&#40;</span>request.<span style="color: #006600;">getContextPath</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            response.<span style="color: #006600;">addCookie</span><span style="color: #66cc66;">&#40;</span>cookie<span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Rest assured that the sole purpose of the cookie is to signal the frontend server that a user is authenticated in order to disable any cache retrieval attempts. No security checks are ever performed against the cookie.</p>
<p>Now that we’ve taken care of storing the content, someone has to retrieve it. As already mentioned we are using Nginx as Frontend server. I’m a big fan of it because of its small footprint, great performance and integrated support for memcached. Below is a Nginx virtual host configuration that implements the remaining bits and pieces:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p148code16'); return false;">View Code</a> APACHE</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14816"><td class="code" id="p148code16"><pre class="apache" style="font-family:monospace;">upstream tomcats
{
  server 127.0.0.1:<span style="color: #ff0000;">8080</span> weight=<span style="color: #ff0000;">1</span>;
}
&nbsp;
server
{
  <span style="color: #00007f;">listen</span> <span style="color: #ff0000;">80</span>;
  server_name example.com;
  access_log /var/log/nginx/example.log;
&nbsp;
  <span style="color: #00007f;">include</span> ua_ban_list.conf;
&nbsp;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
&nbsp;
  default_type text/html;
  charset utf-<span style="color: #ff0000;">8</span>;
&nbsp;
  proxy_redirect http://tomcats/ http://example.com/;
&nbsp;
  location /
  {
    <span style="color: #adadad; font-style: italic;"># only consider GET requests for caching</span>
    if ($request_method != GET)
    {
      proxy_pass http://tomcats;
      break;
    }
&nbsp;
    <span style="color: #adadad; font-style: italic;"># never cache auth requests</span>
    if ($request_uri ~* (^/auth/.*$))
    {
      proxy_pass http://tomcats;
      break;
    }
&nbsp;
    <span style="color: #adadad; font-style: italic;"># detect cookies that indicate an authenticated user</span>
    if ($http_cookie ~* <span style="color: #7f007f;">&quot;(rememberMe|user_id)&quot;</span>)
    {
      <span style="color: #adadad; font-style: italic;"># don't try cache lookup for authenticated user - NEVER</span>
      proxy_pass http://tomcats;
      break;
    }
&nbsp;
    <span style="color: #adadad; font-style: italic;"># compute cache key from app prefix + request_uri</span>
    <span style="color: #adadad; font-style: italic;"># if they computed key does not match the key computed by the application</span>
    <span style="color: #adadad; font-style: italic;"># server when storing content we will get nothing but memcached misses</span>
    <span style="color: #adadad; font-style: italic;"># and NO speedup</span>
    set $memcached_key example_$request_uri;
    memcached_pass memcached;
    error_page <span style="color: #ff0000;">404</span> = @cache_miss;
  }
&nbsp;
  location @cache_miss
  {
    internal;
    proxy_pass http://tomcats;
  }
}</pre></td></tr></table></div>

<p>Please pay special attention to the line where the cache key is computed. A key prefix &#8220;example_&#8221; is used. To get the example to work you would have to add a config variable &#8216;app_prefix&#8217; to Config.groovy which needs to have the value &#8220;example_&#8221;.</p>
<p>Finally I should point out that this approach isn&#8217;t tied to Grails applications. Basically any Java Web application could use it if someone would port the MemcachedFilter from Groovy to Java which should be a piece of cake considering that it was developed from Java Sources in the first place.</p>
<p>That&#8217;s it for now. I hope you enjoyed the ride.</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=148&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/4bnh_9BN06q4dtwgZKaY03kawPg/0/da"><img src="http://feedads.g.doubleclick.net/~a/4bnh_9BN06q4dtwgZKaY03kawPg/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/4bnh_9BN06q4dtwgZKaY03kawPg/1/da"><img src="http://feedads.g.doubleclick.net/~a/4bnh_9BN06q4dtwgZKaY03kawPg/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/f-FKMz62mOA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/03/04/high-performance-grails-with-memcached/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://weichhold.com/2010/03/04/high-performance-grails-with-memcached/</feedburner:origLink></item>
		<item>
		<title>Mapping the result of a native SQL query to a Grails domain class collection</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/ZY2idyBvs-0/</link>
		<comments>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 20:05:27 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Hibernate]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=143</guid>
		<description><![CDATA[Today I could once again not find a proper way to express a query needed for my current Grails project in either Hibernate HQL nor the DSL based variant of Hibernate&#8217;s Criteria API. While it is quite simple to run native SQL Queries in Grails Project using Spring&#8217;s NamedParameterJdbcTemplate I never had to actually Map [...]]]></description>
			<content:encoded><![CDATA[<p>Today I could once again not find a proper way to express a query needed for my current Grails project in either Hibernate HQL nor the DSL based variant of Hibernate&#8217;s Criteria API.</p>
<p>While it is quite simple to run native SQL Queries in Grails Project using Spring&#8217;s <a href="http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/jdbc/core/namedparam/NamedParameterJdbcTemplate.html">NamedParameterJdbcTemplate</a> I never had to actually Map the result of a native SQL back to one of my Grails Domain Classes. After a bit of fiddling around it turned out that everything becomes quite trivial once you get ahold of the Hibernate SessionFactory that is made available by Grails for each request.</p>
<p>Let&#8217;s pretend we have the following Grails domain class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p143code19'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14319"><td class="code" id="p143code19"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.acme.domain</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Foo
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #aaaadd; font-weight: bold;">String</span> name
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>You would execute a native SQL Query that returns a List of Foo instances like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p143code20'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p14320"><td class="code" id="p143code20"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">com.acme.domain.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> sessionFactory
sessionFactory <span style="color: #66cc66;">=</span> ctx.<span style="color: #006600;">sessionFactory</span>  <span style="color: #808080; font-style: italic;">// this only necessary if your are working with the Grails console/shell</span>
<span style="color: #000000; font-weight: bold;">def</span> session <span style="color: #66cc66;">=</span> sessionFactory.<span style="color: #006600;">currentSession</span> 
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> query <span style="color: #66cc66;">=</span> session.<span style="color: #006600;">createSQLQuery</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;select f.* from Foo where f.id = :filter)) order by f.name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">addEntity</span><span style="color: #66cc66;">&#40;</span>com.<span style="color: #006600;">acme</span>.<span style="color: #006600;">domain</span>.<span style="color: #006600;">Foo</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">setInteger</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;filter&quot;</span>, <span style="color: #cc66cc;">88</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
query.<span style="color: #006600;">list</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span>.<span style="color: #006600;">name</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<img src="http://weichhold.com/?ak_action=api_record_view&id=143&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/52mQ4ipylGlYTm6qDFBycrJ-URA/0/da"><img src="http://feedads.g.doubleclick.net/~a/52mQ4ipylGlYTm6qDFBycrJ-URA/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/52mQ4ipylGlYTm6qDFBycrJ-URA/1/da"><img src="http://feedads.g.doubleclick.net/~a/52mQ4ipylGlYTm6qDFBycrJ-URA/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/ZY2idyBvs-0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://weichhold.com/2010/01/18/mapping-result-of-a-native-sql-query-to-grails-domain-class-collection/</feedburner:origLink></item>
		<item>
		<title>A simple GSP toolbar taglib for Grails</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/vQievfMUuZc/</link>
		<comments>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 15:55:26 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=112</guid>
		<description><![CDATA[Did you ever had to implement a menu in Grails GSP where every link element should be seperated from its predecessor by a separator character and where some of the elements can be missing when certain conditions are not met (security etc)? The part with the optional elements can turn the separator handling into a [...]]]></description>
			<content:encoded><![CDATA[<p>Did you ever had to implement a menu in Grails GSP where every link element should be seperated from its predecessor by a separator character and where some of the elements can be missing when certain conditions are not met (security etc)?</p>
<p>The part with the optional elements can turn the separator handling into a real mess. So today I decided to end this nonsense. The result is a tiny taglib which can be integrated into every grails project. The usage is pretty self explanatory:</p>
<p><b><br />
&lt;mx:toolbar separator=&#8217;|'&gt;<br />
&nbsp;&nbsp;&lt;mx:item&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;shiro:hasPermission permission=&#8221;topic:delete&#8221;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:link controller=&#8221;topic&#8221; action=&#8221;delete&#8221; id=&#8221;${topic.id}&#8221;&gt;&lt;g:message code=&#8221;topic.delete&#8221;&gt;&lt;/g:message&gt;&lt;/g:link&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/shiro:hasPermission&gt;<br />
&nbsp;&nbsp;&lt;/mx:item&gt;</p>
<p>&nbsp;&nbsp;&lt;mx:item&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;shiro:hasPermission permission=&#8221;topic:lock&#8221;&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;g:link controller=&#8221;topic&#8221; action=&#8221;lock&#8221; id=&#8221;${topic.id}&#8221;&gt;&lt;g:message code=&#8221;topic.lock&#8221;&gt;&lt;/g:message&gt;&lt;/g:link&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/shiro:hasPermission&gt;<br />
&nbsp;&nbsp;&lt;/mx:item&gt;<br />
&lt;/mx:toolbar&gt;<br />
</b></p>
<p><span id="more-112"></span></p>
<p>And this would be rendered assuming the user has permission for both buttons:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p112code23'); return false;">View Code</a> HTML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11223"><td class="code" id="p112code23"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;/topic/delete/10&quot;&gt;Delete&lt;/a&gt;|&lt;a href=&quot;/topic/lock/10&quot;&gt;Lock&lt;/a&gt;</pre></td></tr></table></div>

<p>Tag Library Source:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p112code24'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p11224"><td class="code" id="p112code24"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MenuTagLib
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">static</span> namespace <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'mx'</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/** The toolbar container */</span>
  <span style="color: #000000; font-weight: bold;">def</span> toolbar <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span> attrs, body <span style="color: #66cc66;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> separator <span style="color: #66cc66;">=</span> attrs.<span style="color: #006600;">separator</span> <span style="color: #66cc66;">?</span>: <span style="color: #ff0000;">'|'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// we'll be using this to keep track of our items</span>
    <span style="color: #000000; font-weight: bold;">def</span> menuItems <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">pageScope</span>.<span style="color: #006600;">menuItems</span> <span style="color: #66cc66;">=</span> menuItems
&nbsp;
    <span style="color: #808080; font-style: italic;">// this will execute the body, so that the items are appended obviously,</span>
    <span style="color: #808080; font-style: italic;">// anything other than the item tags will not render correctly.</span>
    body<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// render every item and add a separator except for the last item </span>
    menuItems.<span style="color: #663399;">eachWithIndex</span>
    <span style="color: #66cc66;">&#123;</span> item, index <span style="color: #66cc66;">-&gt;</span>
      out <span style="color: #66cc66;">&lt;&lt;</span> item
      <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>index <span style="color: #66cc66;">&lt;</span> menuItems.<span style="color: #663399;">size</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        out <span style="color: #66cc66;">&lt;&lt;</span> separator
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;">/** A toolbar item */</span>
  <span style="color: #000000; font-weight: bold;">def</span> item <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span> attrs, body <span style="color: #66cc66;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> itemContent <span style="color: #66cc66;">=</span> body<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>itemContent<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// only add the item if rendering evaluated to non-empty string</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">pageScope</span>.<span style="color: #006600;">menuItems</span> <span style="color: #66cc66;">&lt;&lt;</span> itemContent
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<img src="http://weichhold.com/?ak_action=api_record_view&id=112&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/ZYzPAPSY2pK-EgSLSUUZC3HHVnI/0/da"><img src="http://feedads.g.doubleclick.net/~a/ZYzPAPSY2pK-EgSLSUUZC3HHVnI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/ZYzPAPSY2pK-EgSLSUUZC3HHVnI/1/da"><img src="http://feedads.g.doubleclick.net/~a/ZYzPAPSY2pK-EgSLSUUZC3HHVnI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/vQievfMUuZc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://weichhold.com/2010/01/13/a-simple-gsp-toolbar-taglib-for-grails/</feedburner:origLink></item>
		<item>
		<title>Grails sanitized stracktraces</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/ZNXdZEXPj0c/</link>
		<comments>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/#comments</comments>
		<pubDate>Sat, 10 Oct 2009 21:01:35 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=107</guid>
		<description><![CDATA[The other day I was writing a medium complex HQL query for a Grails App and no matter what the line executing the query would crash like this: ?View Code GROOVYjava.lang.NullPointerException at $Proxy13.createQuery&#40;Unknown Source&#41; at com.acme.PerformanceService.makePerformanceSheet&#40;PerformanceService.groovy:38&#41; ... So how the hell do we figure out what the problem given so few information? Turns out that [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I was writing a medium complex HQL query for a Grails App and no matter what the line executing the query would crash like this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code28'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10728"><td class="code" id="p107code28"><pre class="groovy" style="font-family:monospace;">java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">NullPointerException</span>
	at $Proxy13.<span style="color: #006600;">createQuery</span><span style="color: #66cc66;">&#40;</span>Unknown Source<span style="color: #66cc66;">&#41;</span>
	at com.<span style="color: #006600;">acme</span>.<span style="color: #006600;">PerformanceService</span>.<span style="color: #006600;">makePerformanceSheet</span><span style="color: #66cc66;">&#40;</span>PerformanceService.<span style="color: #006600;">groovy</span>:<span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span>
  ...</pre></td></tr></table></div>

<p>So how the hell do we figure out what the problem given so few information? Turns out that grails sanitizes the exception stacktrace, resulting in better readability by removing a lot of clutter. Sometimes though this optimization will also hide some important details from you. Luckily we can still refer to stacktrace.log which usually resides in the project root folder in development mode. In this particular case opening the file reveiled this:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code29'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10729"><td class="code" id="p107code29"><pre class="groovy" style="font-family:monospace;">java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">NullPointerException</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">dialect</span>.<span style="color: #006600;">Dialect</span>$<span style="color: #cc66cc;">3</span>.<span style="color: #006600;">getReturnType</span><span style="color: #66cc66;">&#40;</span>Dialect.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">125</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">util</span>.<span style="color: #006600;">SessionFactoryHelper</span>.<span style="color: #006600;">findFunctionReturnType</span><span style="color: #66cc66;">&#40;</span>SessionFactoryHelper.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">405</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">AggregateNode</span>.<span style="color: #006600;">getDataType</span><span style="color: #66cc66;">&#40;</span>AggregateNode.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">44</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">SelectClause</span>.<span style="color: #006600;">initializeExplicitSelectClause</span><span style="color: #66cc66;">&#40;</span>SelectClause.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">165</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">HqlSqlWalker</span>.<span style="color: #006600;">useSelectClause</span><span style="color: #66cc66;">&#40;</span>HqlSqlWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">727</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">HqlSqlWalker</span>.<span style="color: #006600;">processQuery</span><span style="color: #66cc66;">&#40;</span>HqlSqlWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">551</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">query</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">645</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">selectStatement</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">281</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">antlr</span>.<span style="color: #006600;">HqlSqlBaseWalker</span>.<span style="color: #006600;">statement</span><span style="color: #66cc66;">&#40;</span>HqlSqlBaseWalker.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">229</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">analyze</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">251</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">doCompile</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">183</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">QueryTranslatorImpl</span>.<span style="color: #006600;">compile</span><span style="color: #66cc66;">&#40;</span>QueryTranslatorImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">134</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">HQLQueryPlan</span>.<span style="color: #66cc66;">&lt;</span>init<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>HQLQueryPlan.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">HQLQueryPlan</span>.<span style="color: #66cc66;">&lt;</span>init<span style="color: #66cc66;">&gt;</span><span style="color: #66cc66;">&#40;</span>HQLQueryPlan.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">80</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">engine</span>.<span style="color: #006600;">query</span>.<span style="color: #006600;">QueryPlanCache</span>.<span style="color: #006600;">getHQLQueryPlan</span><span style="color: #66cc66;">&#40;</span>QueryPlanCache.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">94</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">impl</span>.<span style="color: #006600;">AbstractSessionImpl</span>.<span style="color: #006600;">getHQLQueryPlan</span><span style="color: #66cc66;">&#40;</span>AbstractSessionImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">156</span><span style="color: #66cc66;">&#41;</span>
	at org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">impl</span>.<span style="color: #006600;">AbstractSessionImpl</span>.<span style="color: #006600;">createQuery</span><span style="color: #66cc66;">&#40;</span>AbstractSessionImpl.<span style="color: #006600;">java</span>:<span style="color: #cc66cc;">135</span><span style="color: #66cc66;">&#41;</span>
  ...</pre></td></tr></table></div>

<p>Which almost instantly gave me the hint that something&#8217;s wrong with the aggregate part of my query:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p107code30'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10730"><td class="code" id="p107code30"><pre class="groovy" style="font-family:monospace;">org.<span style="color: #006600;">hibernate</span>.<span style="color: #006600;">hql</span>.<span style="color: #006600;">ast</span>.<span style="color: #006600;">tree</span>.<span style="color: #006600;">AggregateNode</span>.<span style="color: #006600;">getDataType</span></pre></td></tr></table></div>

<p>And indeed the problem was that I had ported an SQL Query to HQL and forgot to rename the sum() aggregate argument from the column name to the domain class property name:</p>
<p>sum(some_property) instead of sum(foo.someProperty)</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=107&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/2oocyQLpVx_jYR_8sYmeMGik5tE/0/da"><img src="http://feedads.g.doubleclick.net/~a/2oocyQLpVx_jYR_8sYmeMGik5tE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/2oocyQLpVx_jYR_8sYmeMGik5tE/1/da"><img src="http://feedads.g.doubleclick.net/~a/2oocyQLpVx_jYR_8sYmeMGik5tE/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/ZNXdZEXPj0c" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/10/10/grails-sanitized-stracktraces/</feedburner:origLink></item>
		<item>
		<title>Configuring memcached in a Grails Application</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/CC8S80VW6fk/</link>
		<comments>http://weichhold.com/2009/09/17/configuring-memcached-in-a-grails-application/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 11:13:31 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=95</guid>
		<description><![CDATA[I&#8217;m a big fan of using memcached for improving the scalability of websites so when I recently fell in love with the Grails Framework I began to look for a way to configure the memcached client which blends in with the framework. Luckily Grails provides a very easy way to configure Spring Beans using a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of using memcached for improving the scalability of websites so when I recently fell in love with the <a href="http://grails.org" target="_blank">Grails Framework </a>I began to look for a way to configure the memcached client which blends in with the framework. Luckily Grails provides a very easy way to configure Spring Beans using a DSL Just create grails-app/conf/spring/resources.groovy and add this:<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p95code33'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9533"><td class="code" id="p95code33"><pre class="groovy" style="font-family:monospace;">beans <span style="color: #66cc66;">=</span>
<span style="color: #66cc66;">&#123;</span>
  memcachedClient<span style="color: #66cc66;">&#40;</span>MemcachedClient, 
    net.<span style="color: #006600;">spy</span>.<span style="color: #006600;">memcached</span>.<span style="color: #006600;">AddrUtil</span>.<span style="color: #006600;">getAddresses</span><span style="color: #66cc66;">&#40;</span>
      ConfigurationHolder.<span style="color: #006600;">config</span>.<span style="color: #006600;">memcached_servers</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#123;</span> bean <span style="color: #66cc66;">-&gt;</span>
    bean.<span style="color: #006600;">scope</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'singleton'</span> 
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Voila, from now on you can get a  memcached client instance anywhere in your application either by standard dependency injection or by calling applicationContext.getBean(&#8216;memcachedClient&#8217;).</p>
<p>It is important to note that this example assumes that you have configured your memcached server(s) in grails-app/conf/Config.groovy like this:<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p95code34'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p9534"><td class="code" id="p95code34"><pre class="groovy" style="font-family:monospace;">environments
<span style="color: #66cc66;">&#123;</span>
  development
  <span style="color: #66cc66;">&#123;</span>
    memcached_servers <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;192.168.230.2:11211&quot;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  production
  <span style="color: #66cc66;">&#123;</span>
    memcached_servers <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;localhost:11211&quot;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<img src="http://weichhold.com/?ak_action=api_record_view&id=95&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/jWd8H1q8Z9Qjiu0KrfWXYO2hldQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/jWd8H1q8Z9Qjiu0KrfWXYO2hldQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/jWd8H1q8Z9Qjiu0KrfWXYO2hldQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/jWd8H1q8Z9Qjiu0KrfWXYO2hldQ/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/CC8S80VW6fk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/09/17/configuring-memcached-in-a-grails-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/09/17/configuring-memcached-in-a-grails-application/</feedburner:origLink></item>
		<item>
		<title>OSX and back</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/CYsrf6EuUTo/</link>
		<comments>http://weichhold.com/2009/07/29/macosx-and-back/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 19:19:25 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=88</guid>
		<description><![CDATA[Recently I have abandoned my plans of switching to MacOSX as my primary Operating System after using the the OS for four months straight without touching Windows. Don’t get me wrong. I enjoyed the ride – a lot! But I also had plenty of reasons for going back to Windows (7). First of all I [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span lang="EN-US">Recently I have abandoned my plans of switching to MacOSX as my primary Operating System after using the the OS for four months straight without touching Windows. <span> </span>Don’t get me wrong. I enjoyed the ride – a lot! But I also had plenty of reasons for going back to Windows (7). First of all I don’t like Apple’s attitude. I think they are turning into some kind of evil empire against which even Microsoft during its darkest days pales in comparison. I also regard Apple’s sloppy handling of serious <a href="http://arstechnica.com/apple/news/2009/05/apple-has-yet-to-patch-critical-java-vulnerabilitya-vulnerability-in-the-java-virtual-machine-which.ars">security vulnerabilities</a> as highly unprofessional.<span> </span>Then there were details that many users wouldn’t even notice. For example I’m obsessed with <a href="http://en.wikipedia.org/wiki/ClearType">ClearType </a>and could never become really comfortable with Apple’s Font Anti Aliasing which is still a lot better than Ubuntu’s though. </span></p>
<p class="MsoNormal"><span lang="EN-US">Anyway I decided to ditch the OS but I’m keeping the hardware – my </span><span lang="EN-US"><a href="http://www.amazon.com/Apple-MB110LL-A-Keyboard-Kit/dp/B000V07N9U/ref=sr_1_1?ie=UTF8&amp;qid=1248895023&amp;sr=8-1">keyboard </a></span><span lang="EN-US">to be precise. After typing on this fantastic keyboard for a couple of months there is no way going back to that </span><span lang="EN-US"><a href="http://www.amazon.com/Logitech-Illuminated-Ultrathin-Keyboard-Backlighting/dp/B001F51G16/ref=sr_1_1?ie=UTF8&amp;qid=1248894958&amp;sr=8-1">cheap plastic crap</a></span><span lang="EN-US"> that you usually deal with on the PC. What’s really odd is that there are peripherals vendors who are charging more for their </span><span lang="EN-US"><a href="http://www.amazon.com/Logitech-Illuminated-Ultrathin-Keyboard-Backlighting/dp/B001F51G16/ref=sr_1_1?ie=UTF8&amp;qid=1248894958&amp;sr=8-1">plastic toys</a></span><span lang="EN-US"> that do not even feature a built-in USB Hub than Apple for their </span><span lang="EN-US">anodized aluminum enclosure that offers a rock solid typing experience and even includes a USB Hub. In conjunction with an utility called <a href="http://www.randyrants.com/sharpkeys/">SharpKeys</a> which I use for swapping <em>Cmd</em> with <em>Alt</em>, this fine piece of craftsmanship can used pretty comfortable under windows.</span></p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=88&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/1KyjmmW_ISnU7wYpWZUWUrAu54k/0/da"><img src="http://feedads.g.doubleclick.net/~a/1KyjmmW_ISnU7wYpWZUWUrAu54k/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/1KyjmmW_ISnU7wYpWZUWUrAu54k/1/da"><img src="http://feedads.g.doubleclick.net/~a/1KyjmmW_ISnU7wYpWZUWUrAu54k/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/CYsrf6EuUTo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/07/29/macosx-and-back/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/07/29/macosx-and-back/</feedburner:origLink></item>
		<item>
		<title>Updating multiple page elements with Grails and Ajax</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/VZmYXWKnIZU/</link>
		<comments>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 09:30:01 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=39</guid>
		<description><![CDATA[Every Web Application Project I’ve been involved in the recent past had one thing in common: the client demanded more and more rich-client features at an ever increasing pace &#8211; without committing itself to real rich-client frameworks such as Flex or Silverlight. This leaves us with Javascript / AJAX for implementing highly dynamic User Interfaces [...]]]></description>
			<content:encoded><![CDATA[<p>Every Web Application Project I’ve been involved in the recent past had one thing in common: the client demanded more and more rich-client features at an ever increasing pace &#8211; without committing itself to real rich-client frameworks such as Flex or Silverlight. This leaves us with Javascript / AJAX for implementing highly dynamic User Interfaces for Web Applications. A very common problem when working with Ajax is the requirement to update multiple parts of a page in response to a single request. This post aims to familiarize the reader with my own approach to this problem.</p>
<p>At its core my solution consists of just two functions &#8211; one running server side and the other executing on the client. The first function is intended to run on the server and is written in Groovy. It was originally written in Python for use with the <a href="http://djangoproject.com">Django</a> framework and I&#8217;ve ported it to Groovy when I switched to <a href="http://grails.org">Grails</a> as my primary RAD Web framework. This function takes a map as argument and returns a JSON encoded map where the key is a valid <a href="http://docs.jquery.com/Selectors">jQuery Selector </a> designating the targets for the update and the value is a map containing information how the matched elements should be modified and how the content for the update shall be produced. Don&#8217;t worry if you find this confusing at first. There&#8217;s a sample further down.</p>
<p><span id="more-39"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code44'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3944"><td class="code" id="p39code44"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> renderElementUpdateResponse<span style="color: #66cc66;">&#40;</span>elements<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> rendered_elements <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
&nbsp;
  elements.<span style="color: #663399;">eachWithIndex</span>
  <span style="color: #66cc66;">&#123;</span> k, v, i <span style="color: #66cc66;">-&gt;</span>
    rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>:<span style="color: #66cc66;">&#93;</span>
    rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'mode'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'mode'</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// if content is a Map, feed it into g.render - using the key as template name and the value as model</span>
    <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">LinkedHashMap</span><span style="color: #66cc66;">&#41;</span>
      rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> g.<span style="color: #006600;">render</span><span style="color: #66cc66;">&#40;</span>template: v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">keySet</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>, model: v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">values</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #b1b100;">else</span>
      rendered_elements<span style="color: #66cc66;">&#91;</span>k<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> v<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'content'</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">def</span> tmp <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'updates'</span>: rendered_elements<span style="color: #66cc66;">&#93;</span>
  render tmp <span style="color: #000000; font-weight: bold;">as</span> JSON
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The second function is written in Javascript and responsible for applying the JSON wrapped HTML fragments to their designated HTML DOM Elements:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code45'); return false;">View Code</a> JAVASCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3945"><td class="code" id="p39code45"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> applyElementUpdates<span style="color: #009900;">&#40;</span>transport<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> updates<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>transport.<span style="color: #660066;">updates</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    updates <span style="color: #339933;">=</span> transport.<span style="color: #660066;">updates</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> scripts <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">// need to queue initializers until all updates are done</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>target <span style="color: #000066; font-weight: bold;">in</span> updates<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'mode'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'execute'</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        code <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#93;</span>
        scripts.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span>
        <span style="color: #000066; font-weight: bold;">continue</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> targetNode <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> newContent <span style="color: #339933;">=</span> updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'content'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> oldContent <span style="color: #339933;">=</span> targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000066; font-weight: bold;">switch</span><span style="color: #009900;">&#40;</span>updates<span style="color: #009900;">&#91;</span>target<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'mode'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'replace'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>newContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'insert'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>newContent <span style="color: #339933;">+</span> oldContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">case</span> <span style="color: #3366CC;">'append'</span><span style="color: #339933;">:</span>
          targetNode.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>oldContent <span style="color: #339933;">+</span> newContent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// now run initializers</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #000066; font-weight: bold;">in</span> scripts<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      code <span style="color: #339933;">=</span> scripts<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>
      <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>code<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So how do you use it? Below you find all necessary code artifacts for implementing the described technique in arbitrary Grails application. Please note that this sample uses <a href="jquery.com">jQuery</a> for carrying out the actual AJAX request but it would be trivial to port this over to any other decent Javascript Framework like <a href="mootools.net">MooTools</a>, Prototype or YUI.<br />
<br/><br />
Let&#8217;s begin with the GSP page in <b>/views/test/index.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code46'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3946"><td class="code" id="p39code46"><pre class="asp" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;%</span><span style="color: #006600; font-weight: bold;">@</span> page <span style="color: #330066;">contentType</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">%&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;</span>html<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>head<span style="color: #006600; font-weight: bold;">&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>g<span style="color: #006600; font-weight: bold;">:</span>javascript src<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;jquery-1.3.2.js&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>g<span style="color: #006600; font-weight: bold;">:</span>javascript src<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;applyElementUpdates.js&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
&nbsp;
    <span style="color: #006600; font-weight: bold;">&lt;</span>script type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;text/javascript&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
    $<span style="color: #006600; font-weight:bold;">&#40;</span>document<span style="color: #006600; font-weight:bold;">&#41;</span>.<span style="color: #9900cc;">ready</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #0000ff; font-weight: bold;">function</span><span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight:bold;">&#123;</span>
      $<span style="color: #006600; font-weight:bold;">&#40;</span><span style="color: #008000;">'#btnClickMe').click(function()</span>
      <span style="color: #006600; font-weight:bold;">&#123;</span>
        $.<span style="color: #9900cc;">ajax</span><span style="color: #006600; font-weight:bold;">&#40;</span>
        <span style="color: #006600; font-weight:bold;">&#123;</span>
          url<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #008000;">'${createLink(controller: 'test', action: 'update')}',</span>
          type<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;POST&quot;</span>,
          dataType<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #cc0000;">&quot;json&quot;</span>,
          cache<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">false</span>,
          async<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">true</span>,
          success<span style="color: #006600; font-weight: bold;">:</span> <span style="color: #0000ff; font-weight: bold;">function</span><span style="color: #006600; font-weight:bold;">&#40;</span>data, <span style="color: #330066;">status</span><span style="color: #006600; font-weight:bold;">&#41;</span>
          <span style="color: #006600; font-weight:bold;">&#123;</span>
            applyElementUpdates<span style="color: #006600; font-weight:bold;">&#40;</span>data<span style="color: #006600; font-weight:bold;">&#41;</span>
          <span style="color: #006600; font-weight:bold;">&#125;</span>
        <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
      <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span>
    <span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight:bold;">&#41;</span><span style="color: #006600; font-weight: bold;">;</span>
    <span style="color: #006600; font-weight: bold;">&lt;/</span>script<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;/</span>head<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>body<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>input type<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;button&quot;</span> id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;btnClickMe&quot;</span> value<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Click me&quot;</span><span style="color: #006600; font-weight: bold;">/&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>div id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;div1&quot;</span><span style="color: #006600; font-weight: bold;">&gt;&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;</span>div id<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;div2&quot;</span><span style="color: #006600; font-weight: bold;">&gt;&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span>
  <span style="color: #006600; font-weight: bold;">&lt;/</span>body<span style="color: #006600; font-weight: bold;">&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>html<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>The controller in <b>/controllers/TestController.groovy</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code47'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3947"><td class="code" id="p39code47"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.converters.JSON</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestController <span style="color: #000000; font-weight: bold;">extends</span> BaseController
<span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">def</span> update <span style="color: #66cc66;">=</span>
  <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> model <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>a: <span style="color: #cc66cc;">1</span>, b: <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// Note: renderElementUpdateResponse is supposed to be implemented in BaseController</span>
    <span style="color: #000000; font-weight: bold;">return</span> renderElementUpdateResponse<span style="color: #66cc66;">&#40;</span>
    <span style="color: #66cc66;">&#91;</span>
      <span style="color: #ff0000;">'#div1'</span>   : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'replace'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment1'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span>,
      <span style="color: #ff0000;">'#div2'</span>   : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'append'</span>, <span style="color: #ff0000;">'content'</span>  : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment2'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span>,
      <span style="color: #ff0000;">'__init__'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'execute'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #ff0000;">'alert(&quot;The page has been updated&quot;)'</span> <span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The call to renderElementUpdateResponse needs some explanation. Let&#8217;s step through the arguments line by line.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code48'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3948"><td class="code" id="p39code48"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'#div1'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'replace'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment1'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p><i>&#8220;[ 'fragment1': model ] ]&#8221;</i> is the only relevant part on the server side. It means &#8220;render the GSP template /views/test/_fragment1.gsp, pass it the variable model as the model and store the resulting string as the content for the update. On the client side it tells the applyElementUpdates() function to replace <i>(mode: replace)</i> the output for this line in all elements with id=&#8221;div1&#8243;.<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code49'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3949"><td class="code" id="p39code49"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'#div2'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'append'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'fragment2'</span>: model <span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>Works basically the same as the previous line only that id=&#8221;div2&#8243; is targetted and rendering /views/test/_fragment2.gsp will produce the content which is going to be appended to the target element rather than replacing it.<br />
<br/></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code50'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3950"><td class="code" id="p39code50"><pre class="groovy" style="font-family:monospace;"> <span style="color: #ff0000;">'__init__'</span>: <span style="color: #66cc66;">&#91;</span> <span style="color: #ff0000;">'mode'</span>: <span style="color: #ff0000;">'execute'</span>, <span style="color: #ff0000;">'content'</span> : <span style="color: #ff0000;">'alert(&quot;The page has been updated&quot;)'</span> <span style="color: #66cc66;">&#93;</span></pre></td></tr></table></div>

<p>In addition to updating DOM elements we can also send code to the client which is run <b>after</b> all elements have been updated. Such fragments are called initializers and are identified on the client by their mode attribute which must be &#8216;execute&#8217;. This sample also demonstrates that fragment content can be specifed directly (<i>alert(&#8220;The page has been updated&#8221;)</i>) instead of being produced by rendering a template. Please note that the selector (&#8216;__init__&#8217;) is unused in this case. The sole reason for chosing that id was to emphasize the purpose of this fragment.<br />
<br/></p>
<p>The final missing bits are the aforementioned Grails templates which are going to producing the content for the updated elements. </p>
<p>The fragment for updating the first div in <b>/views/test/_fragment1.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code51'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3951"><td class="code" id="p39code51"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>span<span style="color: #006600; font-weight: bold;">&gt;</span>Value of A <span style="color: #990099; font-weight: bold;">is</span> $<span style="color: #006600; font-weight:bold;">&#123;</span>a<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight: bold;">&lt;/</span>span<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>The fragment for updating the second div in <b>/views/test/_fragment2.gsp</b>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p39code52'); return false;">View Code</a> ASP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3952"><td class="code" id="p39code52"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>span<span style="color: #006600; font-weight: bold;">&gt;</span>Value of B <span style="color: #990099; font-weight: bold;">is</span> $<span style="color: #006600; font-weight:bold;">&#123;</span>b<span style="color: #006600; font-weight:bold;">&#125;</span><span style="color: #006600; font-weight: bold;">&lt;/</span>span<span style="color: #006600; font-weight: bold;">&gt;&lt;</span>br<span style="color: #006600; font-weight: bold;">/&gt;</span></pre></td></tr></table></div>

<p>Finally, opening the example page on your browser and clicking the button results in the following actions:</p>
<ul>
<li>The div1 element gets updated with the HTML output of rendering /views/test/_fragment1.gsp against the simple model. The contents of div1 are replaced</li>
<li>The div2 element gets updated with the HTML output of rendering /views/test/_fragment2.gsp against the simple model. The HTML is appended to the div rather then replacing the contents.</li>
<li>An alert box is displayed</li>
</ul>
<p>That&#8217;s it folks. I hope you found this post useful. </p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=39&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/nw_ceXLyccw1QQiwi7GN_QrOU5U/0/da"><img src="http://feedads.g.doubleclick.net/~a/nw_ceXLyccw1QQiwi7GN_QrOU5U/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/nw_ceXLyccw1QQiwi7GN_QrOU5U/1/da"><img src="http://feedads.g.doubleclick.net/~a/nw_ceXLyccw1QQiwi7GN_QrOU5U/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/VZmYXWKnIZU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/07/20/updating-multiple-page-fragments-with-grails-and-ajax/</feedburner:origLink></item>
		<item>
		<title>Using GIT with Grails UI-Performance Plugin</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/EZfJxmkiEDs/</link>
		<comments>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 18:16:53 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=36</guid>
		<description><![CDATA[I&#8217;ve been doing a serious amount of Grails Development lately and if there&#8217;s one Grails Plugin I consider mandatory for any serious production deployment then it is the UI-Performance plugin by Burt Beckwith. One of the features of the plugin that reduces server load is versioning and far-future &#8220;Expires&#8221; and &#8220;Cache-Control&#8221; headers for static resources. Each time [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a serious amount of Grails Development lately and if there&#8217;s one Grails Plugin I consider mandatory for any serious production deployment then it is the <a href="http://grails.org/plugin/ui-performance" target="_blank">UI-Performance plugin</a> by Burt Beckwith. One of the features of the plugin that reduces server load is versioning and far-future &#8220;Expires&#8221; and &#8220;Cache-Control&#8221; headers for static resources.</p>
<p>Each time you generate a new WAR for deployment, a new version is applied to all image, .js, and .css files so files can be cached forever. The next time you deploy a new version of your application, all of the resource file names will have changed, so only then will files be requested. The default process for determining the version is to look at the version of the root project folder in the Subversion metadata files. Since I&#8217;ve switched to <a href="http://git-scm.com/">GIT</a> for all my projects a while ago the plugin would fallback to <em>&#8220;System.currentTimeMillis().toString()&#8221; </em>to compute the version.</p>
<p>My solution is to include the following line in my projects Config.groovy:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p36code54'); return false;">View Code</a> GROOVY</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3654"><td class="code" id="p36code54"><pre class="groovy" style="font-family:monospace;">uiperformance.<span style="color: #006600;">determineVersion</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;git rev-list --max-count=1 HEAD&quot;</span>.<span style="color: #993399; font-weight: bold;">execute</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>.<span style="color: #006600;">trim</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>This allows the HEAD commit SHA1 hash of the current branch to be used as the uiperformance version for deployment.</p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=36&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/e-gKGFSwlILiLK4EcTH0qmWJi7U/0/da"><img src="http://feedads.g.doubleclick.net/~a/e-gKGFSwlILiLK4EcTH0qmWJi7U/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/e-gKGFSwlILiLK4EcTH0qmWJi7U/1/da"><img src="http://feedads.g.doubleclick.net/~a/e-gKGFSwlILiLK4EcTH0qmWJi7U/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/EZfJxmkiEDs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/06/18/using-git-with-grails-ui-performance-plugin/</feedburner:origLink></item>
		<item>
		<title>VirtualPC images with Windows XP and IE</title>
		<link>http://feedproxy.google.com/~r/olivers_place/~3/CCCxakzj1jQ/</link>
		<comments>http://weichhold.com/2009/02/18/virtual-xp-images-with-windows-xp-and-ie/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 01:05:47 +0000</pubDate>
		<dc:creator>oliver</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[VirtualPC]]></category>

		<guid isPermaLink="false">http://weichhold.com/?p=29</guid>
		<description><![CDATA[This is one of the very rare occasions where Microsoft actually did something cool. A couple days ago I needed to test one of my web apps with Internet Explorer 8 and just for giggles with IE6 &#8211; although we&#8217;ve dropped support for that dinosaur a while ago. Remembering my previous experience with IE8 and [...]]]></description>
			<content:encoded><![CDATA[<p>This is one of the very rare occasions where Microsoft actually did something cool. A couple days ago I needed to test one of my web apps with Internet Explorer 8 and just for giggles with IE6 &#8211; although we&#8217;ve dropped support for that dinosaur a while ago. Remembering my previous experience with IE8 and my inability to go back to 7 all too well &#8211; yes don&#8217;t laugh at me, I should have known better I know &#8211; I was preparing to fire up VMWare and create two XP Images, one for each browser version. But imagine my surprise when I discovered that Microsoft actually offers pre-configured ready to launch Windows XP VirtualPC Images for every possible IE version for free.</p>
<p>Be advised that these images expire in a couple of months but Microsoft seems to make new images  available shortly before the old ones expire.</p>
<p>Here&#8217;s the link: <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7c2b5317-a40f-4e86-8835-d37170c5923e&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=7c2b5317-a40f-4e86-8835-d37170c5923e&amp;displaylang=en</a></p>
<img src="http://weichhold.com/?ak_action=api_record_view&id=29&type=feed" alt="" />
<p><a href="http://feedads.g.doubleclick.net/~a/dU5_OS4SF_1ESvdAtDc7yQIPDqI/0/da"><img src="http://feedads.g.doubleclick.net/~a/dU5_OS4SF_1ESvdAtDc7yQIPDqI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/dU5_OS4SF_1ESvdAtDc7yQIPDqI/1/da"><img src="http://feedads.g.doubleclick.net/~a/dU5_OS4SF_1ESvdAtDc7yQIPDqI/1/di" border="0" ismap="true"></img></a></p><img src="http://feeds.feedburner.com/~r/olivers_place/~4/CCCxakzj1jQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://weichhold.com/2009/02/18/virtual-xp-images-with-windows-xp-and-ie/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://weichhold.com/2009/02/18/virtual-xp-images-with-windows-xp-and-ie/</feedburner:origLink></item>
	</channel>
</rss>
