<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>Ted Naleid &#187; grails</title>
	<atom:link href="http://naleid.com/blog/category/grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<lastBuildDate>Wed, 16 May 2012 00:54:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Upgrading to Grails 2 Unit Testing</title>
		<link>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/</link>
		<comments>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/#comments</comments>
		<pubDate>Wed, 02 May 2012 04:15:28 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=580</guid>
		<description><![CDATA[Grails 2 has a lot of great new unit testing features that make many test scenarios easier. The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating [...]]]></description>
			<content:encoded><![CDATA[<p>Grails 2 has a lot of great <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">new unit testing features</a> that make many test scenarios easier.</p>
<p>The grails documentation does an OK job of describing some of the new features, but there really wasn&#8217;t anywhere that I could find that had a comprehensive list of changes you should make to your code when migrating from grails 1.3.X to 2.0.X.</p>
<p>This blog post is the list of changes that I wished I had when I started to migrate our code.<span id="more-580"></span></p>
<p>I found these pages helpful in figuring things out:</p>
<ul>
<li>The <a href="http://grails.org/doc/latest/guide/testing.html#9.3 Functional Testing" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_9.3_Functional_Testing?referer=');">Grails 2 Testing Documentation</a></li>
<li>Peter Ledbrook&#8217;s <a href="http://pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html" onclick="pageTracker._trackPageview('/outgoing/pledbrook.github.com/grails-howtos/en/upgradeToGrails2.html?referer=');">&#8220;Upgrade to Grails 2&#8243;</a> blog post</li>
<li>The Grails 2 <a href="http://www.grails.org/doc/2.0.x/guide/gettingStarted.html#upgradingFromPreviousVersionsOfGrails" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/doc/2.0.x/guide/gettingStarted.html_upgradingFromPreviousVersionsOfGrails?referer=');">&#8220;Upgrading From Previous Versions of Grails&#8221;</a> section of the user guide.</li>
<li>Rob Fletcher&#8217;s <a href="http://blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/" onclick="pageTracker._trackPageview('/outgoing/blog.freeside.co/blog/2011/11/29/grails-2-upgrade-part-1/?referer=');">Upgrading to Grails 2: Part 1</a></li>
<li>This google plus thread on <a href="https://plus.google.com/117411438136918964913/posts/axyCZk1P5L5" onclick="pageTracker._trackPageview('/outgoing/plus.google.com/117411438136918964913/posts/axyCZk1P5L5?referer=');">people&#8217;s experiences upgrading to grails 2</a>
<li><a href="http://jira.grails.org/browse/GRAILS-7617" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-7617?referer=');">GRAILS-7617</a> &#8211; TestFor(ControllerClass) annotation doesn&#8217;t provide renderArgs</li>
</ul>
<p>The company I&#8217;m consulting at made a large investment in unit testing in the grails 1.2/1.3 days with 500-600 unit tests that heavily leverage custom extended base classes, `Mixin`s, and the grails 1.3 way of doing unit testing.   This investment helped us keep most of our testing quick in unit tests, but has made this the most painful grails upgrade cycle I&#8217;ve been through (more painful than the grails 1.0->1.1 upgrade, which was the previous worst).</p>
<p>There have been surprisingly few changes to the actual production code to get things going, the vast majority of changes have been in how unit testing works.</p>
<h3>General Test Upgrade Tips</h3>
<ul>
<li>If you use an IDE with a debugger, it&#8217;s <strong>very</strong> useful to have a separate copy of your code that works against grails 1.3.7 so that you can have 2 instances of IntelliJ and can step through line by line in each codebase to see where things diverge.</li>
<li>Methods with <code>@Test</code>, <code>@Before</code>, and <code>@After</code> MUST be declared <code>public void</code> or else they won&#8217;t be picked up</li>
<li>I had real problems with using the new grails annotations (like <code>@Mock</code>) while extending a custom super-class, I haven&#8217;t dug to the bottom of this yet, but the grails annotations seem to have trouble with class hierarchies, avoid them if possible</li>
<li>To that end, if you use the new grails annotations (like <code>@TestFor</code>), you MUST remove the old <code>extends GrailsUnitTestCase</code></li>
<li>Integration tests should also never extend <code>GrailsUnitTestCase</code>, they should instead extend <code>GroovyTestCase</code> and just use <code>assert</code></li>
<li>I&#8217;ve hit a number of test pollution issues when refactoring my code, where one test hasn&#8217;t properly cleaned up after itself affecting other tests downstream.  If your test passes in isolation, you&#8217;ll need to run the tests in the same order as <code>grails test-app</code> does and slowly take out tests till your tests passes.  Then you know which test is causing the pollution.  The grails 2 test runner doesn&#8217;t show you the test order, but you can get it by setting a breakpoint in the <code>JUnit4GrailsTestType.doPrepare</code> method (line 54 as of grails 2.0.3) and executing this in your IDE: <code>testClasses.name.each { println it }</code>.  Then you can take that list, join it together into one line and prepend <code>grails test-app</code> on it to run them in order.</li>
</ul>
<p>Another way to get the in-order list of test classes from the test reports after a failed test using a little grep and sed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">grep</span> testsuite target<span style="color: #000000; font-weight: bold;">/</span>test-reports<span style="color: #000000; font-weight: bold;">/</span>TESTS-TestSuites.xml <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-v</span> testsuites <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cut</span> -d\  <span style="color: #660033;">-f8-9</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/name=&quot;(.*)&quot; package=&quot;(.*)&quot;/\2.\1/'</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">grep</span> .</pre></div></div>

<h3>Mocking Class Methods by mucking with the metaClass</h3>
<p>Groovy is now more stringent on closure arguments matching the type signature of a method it&#8217;s meant to mock.  If the method you&#8217;re mocking types it&#8217;s variables, you need to match that with your closure.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> theQuery, theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">MyDomain.<span style="color: #006600;">metaClass</span>.<span style="color: #ff0000;">'static'</span>.<span style="color: #993399; font-weight: bold;">executeUpdate</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">String</span> theQuery, <span style="color: #aaaadd; font-weight: bold;">Collection</span> theParams <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Otherwise it won&#8217;t find your closure and will still call the regular method you&#8217;re trying to mock.  Put a breakpoint/<code>println</code> in your mocked methods to ensure that they&#8217;re being called by the code under test.</p>
<h2>Unit Testing Domain Classes in Grails 2</h2>
<h3>mockDomain</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span> 
    ...
    <span style="color: #006600;">mockDomain</span><span style="color: #66cc66;">&#40;</span>Person<span style="color: #66cc66;">&#41;</span>
    mockDomain<span style="color: #66cc66;">&#40;</span>Vehicle<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person, Vehicle<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
   ...</pre></div></div>

<p>If you&#8217;ve given <code>mockDomain</code> instances to save, you&#8217;ll need to save them manually now.</p>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockDomain<span style="color: #66cc66;">&#40;</span>Person, <span style="color: #66cc66;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If you keep the old style of domain mocking through <code>mockDomain</code> you need to ensure that you aren&#8217;t calling it twice on the same class (say in setUp and then again in a test method, or in a superclass and a subclass).  This causes really hard to find test pollution errors downstream with the grails 2.0.3 code.  It appears that it&#8217;s messing with the metaClass again and re-mocking it, but then when the test is done not fully cleaning up after both metaClass modifications.  I consider this a bug as <code>mockDomain</code> should be idempotent; if you&#8217;ve already mocked a class calling <code>mockDomain</code> again shouldn&#8217;t remock the class, but this is what we have now.</p>
<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Mock<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError: <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
    ...</pre></div></div>

<p>One caveat is that validation is now enforced on these objects, you can either pass in <code>validate:false</code> on the object, or use the <a href="https://bitbucket.org/tednaleid/grails-test-data/wiki/Home" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/grails-test-data/wiki/Home?referer=');">grails BuildTestData plugin</a> to create your valid objects:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">@TestMixin<span style="color: #66cc66;">&#40;</span>GrailsUnitTestMixin<span style="color: #66cc66;">&#41;</span>
@Build<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>Person<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">class</span> MyTests <span style="color: #66cc66;">&#123;</span>
    ...
    @Test <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> testPerson<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        Person.<span style="color: #006600;">build</span><span style="color: #66cc66;">&#40;</span>name: <span style="color: #ff0000;">&quot;Bob&quot;</span><span style="color: #66cc66;">&#41;</span>
        ...</pre></div></div>

<p>The Build Test Data plugin has recently been enhanced to work with grails unit tests, and I recommend using it&#8217;s <code>@Build</code> annotation in place of <code>@Mock</code> in pretty much every case.  It just makes things so much easier by building fully valid objects automatically for you.</p>
<h2>Controller Unit Tests in Grails 2</h2>
<h3>mockConfig</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockConfig <span style="color: #ff0000;">&quot;&quot;&quot;
    grails.foo.bar='baz'
&quot;&quot;&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grailsApplication.<span style="color: #006600;">config</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">foo</span>.<span style="color: #006600;">bar</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'baz'</span></pre></div></div>

<h3>mockParams</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockParams.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">params.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span></pre></div></div>

<p>So you can replace all <code>mockParams</code> with just <code>params</code>.</p>
<h3>mockFlash</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">&quot;Expected Message&quot;</span>, mockFlash.<span style="color: #006600;">error</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;Expected Message&quot;</span> <span style="color: #66cc66;">==</span> flash.<span style="color: #006600;">error</span></pre></div></div>

<p>So you can replace all <code>mockFlash</code> with just <code>flash</code>.</p>
<h3>renderArgs and redirectArgs</h3>
<p>The very useful <code>renderArgs</code> and <code>redirectArgs</code> mock objects are gone in grails 2.0 and I find the new way of testing without them to be harder than it was.</p>
<h3>renderArgs.view</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">'edit'</span> <span style="color: #66cc66;">==</span> renderArgs.<span style="color: #006600;">view</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> view.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/edit'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>redirectArgs</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #ff0000;">'person'</span>, redirectArgs.<span style="color: #006600;">controller</span>
assertEquals <span style="color: #ff0000;">'show'</span>, redirectArgs.<span style="color: #006600;">action</span>
assertEquals personInstance.<span style="color: #006600;">id</span>, redirectArgs.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #006600;">endsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/person/show/${personInstance.id}&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>(or just use <code>contains</code> or <code>.split('/')</code> to find parts of the url)</p>
<h3>renderArgs.model</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> renderArgs.<span style="color: #006600;">model</span>.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> model.<span style="color: #006600;">person</span>.<span style="color: #006600;">id</span> <span style="color: #66cc66;">==</span> person.<span style="color: #006600;">id</span></pre></div></div>

<p>This seems to work unless you&#8217;re rendering a template in your controller rather than a view. In that case, the model isn&#8217;t populated and cannot be accessed as far as I can tell.  This is documented in <a href="http://jira.grails.org/browse/GRAILS-8659" onclick="pageTracker._trackPageview('/outgoing/jira.grails.org/browse/GRAILS-8659?referer=');">GRAILS-8659</a> which is marked as &#8220;Not a Bug&#8221;, something I disagree with as the workaround is much more painful.</p>
<p>The workaround, per Graeme Rocher on that ticket is to mock the template rendering:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">views<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'/test/_bar.gsp'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Hello ${name}'</span>
controller.<span style="color: #006600;">renderTemplate</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> response.<span style="color: #006600;">contentAsString</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">&quot;Hello John&quot;</span></pre></div></div>

<p>Where controller:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> renderTemplate<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    render template:<span style="color: #ff0000;">&quot;bar&quot;</span>, model:<span style="color: #66cc66;">&#91;</span>name:<span style="color: #ff0000;">&quot;John&quot;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The naming convention of what you set in the map should be in the format &#8220;/person/_editAddress.gsp&#8221;, where you&#8217;re in the <code>PersonController</code> and the template name is <code>editAddress</code> but there were a few situations where I had trouble getting this right.</p>
<p>If you want to be sure what value to set the path to in the <code>views</code> map, set a breakpoint in <code>RenderDynamicMethod.java</code> around like 319 where it does this check:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">String</span> templateUri <span style="color: #66cc66;">=</span> webRequest.<span style="color: #006600;">getAttributes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getTemplateURI</span><span style="color: #66cc66;">&#40;</span>controller, templateName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span></pre></div></div>

<p>The value of that <code>templateUri</code> is what you want to set in your <code>views</code> map.</p>
<h3>redirectArgs.params</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> redirectArgs.<span style="color: #006600;">params</span>.<span style="color: #006600;">foo</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// strip off everything up till the ? that marks the start of the query string</span>
<span style="color: #000000; font-weight: bold;">def</span> params <span style="color: #66cc66;">=</span> WebUtils.<span style="color: #006600;">fromQueryString</span><span style="color: #66cc66;">&#40;</span>response.<span style="color: #006600;">redirectUrl</span>.<span style="color: #663399;">find</span><span style="color: #66cc66;">&#40;</span>/<span style="color: #66cc66;">&#91;</span>^<span style="color: #66cc66;">?</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">*</span>$/<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> params.<span style="color: #006600;">foo</span></pre></div></div>

<p>gross.  This really should be better and usability has regressed on this.</p>
<h3>mockResponse</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">assertEquals <span style="color: #cc66cc;">404</span>, mockResponse.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, mockResponse.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>Grails 2.0.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #cc66cc;">404</span> <span style="color: #66cc66;">==</span> response.<span style="color: #006600;">status</span>
assertEquals <span style="color: #ff0000;">&quot;foo bar baz&quot;</span>, response.<span style="color: #006600;">contentAsString</span></pre></div></div>

<p>So you should be able to find/replace <code>mockResponse</code> with just <code>response</code></p>
<h3>mockLogging</h3>
<p>grails 1.3.x:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">mockLogging<span style="color: #66cc66;">&#40;</span>Foo<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>grails 2.0.x:</p>
<p>In many cases, this can simply be deleted.  If it&#8217;s either the class under test (the class you are doing a <code>@TestFor</code> on) or something you&#8217;ve already got in a <code>@Mock</code> or a <code>@Build</code> class level annotation, it already has logging.</p>
<p>For those classes that need logging, but are not already mocked, you can use the <code>grails.test.MockUtils.mockLogging</code> static method.  Just add this static import to your class to get the same behavior:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> grails.<span style="color: #006600;">test</span>.<span style="color: #006600;">MockUtils</span>.<span style="color: #66cc66;">*</span>
    ...
    <span style="color: #006600;">mockLogging</span><span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If it&#8217;s a collaborator that isn&#8217;t already mocked out, you can use the <code>mockFor</code> method as described in the <a href="http://grails.org/doc/latest/guide/testing.html#mockingCollaborators" onclick="pageTracker._trackPageview('/outgoing/grails.org/doc/latest/guide/testing.html_mockingCollaborators?referer=');">mocking collaborators</a> section of the grails user docs.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> foo <span style="color: #66cc66;">=</span> mockFor<span style="color: #66cc66;">&#40;</span>Foo, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// true here is &quot;loose&quot; mocking, otherwise you need to `demand` method calls for them to not fail.</span></pre></div></div>

<p>Overall, I&#8217;m much happier with grails 2.0.  It has a number of great features and speed improvements that make it a worthwhile upgrade.  I complain above about a few places where I think testing is not as easy as it was, but most of these could be remedied with a few patches to grails-core.  </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2012/05/01/upgrading-to-grails-2-unit-testing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Speed up your Grails / Spring Security Development with an Auto Login Bookmarklet</title>
		<link>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/</link>
		<comments>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 05:35:21 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=479</guid>
		<description><![CDATA[When you&#8217;re doing dev on your website, how often do you log in with the same username and password? I bet it&#8217;s 20+ of times a day when you&#8217;re actively developing. Having to log in manually impedes development speed. If you watch what your browser is doing when it&#8217;s interacting with a Spring Security application, [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re doing dev on your website, how often do you log in with the same username and password?  I bet it&#8217;s 20+ of times a day when you&#8217;re actively developing.</p>
<p>Having to log in manually impedes development speed.  </p>
<p>If you watch what your browser is doing when it&#8217;s interacting with a Spring Security application, you&#8217;ll see that (by default) it&#8217;s POSTing 2 parameters (<code>j_username</code> and <code>j_password</code>) to <code>http://localhost:8080/YOURAPP/j_spring_security_check</code>.</p>
<p>It&#8217;s easy to automate the login process with a little bit of vanilla javascript. Edit this javascript url to replace <code>YOURAPP</code>, <code>YOURUSERNAME</code>, and <code>YOURPASSWORD</code>, then make a bookmark out of it in your browser:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20path<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://localhost:8080/YOURAPP/j_spring_security_check'</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20params<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'j_username'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'YOURUSERNAME'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'j_password'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'YOURPASSWORD'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20form<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">,</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>for<span style="color: #009900;">&#40;</span>var<span style="color: #339933;">%</span>20key<span style="color: #339933;">%</span>20in<span style="color: #339933;">%</span>20params<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20hiddenField<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;hidden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">,</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>hiddenField<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Any time you want to log in, just click that bookmark.  You&#8217;re now fully authenticated and in the app without having to interact with the login page. </p>
<p>Alternatively, if you&#8217;re using Google Chrome (or Firefox), you can create a <a href="http://www.google.com/support/chrome/bin/answer.py?answer=95653" onclick="pageTracker._trackPageview('/outgoing/www.google.com/support/chrome/bin/answer.py?answer=95653&amp;referer=');">&#8220;search engine&#8221;</a> associated with a user-defined keyword.  Type the keyword in the address bar to launch it. </p>
<p>You can even parameterize it to log in as a variety of users.</p>
<p>Say that you&#8217;ve got a number of different test users in your app: &#8220;admin&#8221;, &#8220;joeuser&#8221;, &#8220;sales&#8221;, &#8220;finance&#8221;, etc.  All of the test users have the same password, but different usernames with different roles.  If you make the username in the javascript url a &#8220;%s&#8221;, Chrome will replace that &#8220;%s&#8221; with your &#8220;search term&#8221;.</p>
<p>So if your app is &#8220;superapp&#8221; and all passwords are &#8220;password&#8221;, you can use this to create a Chrome search engine that lets you login with whatever test user you want</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">javascript<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20path<span style="color: #339933;">=</span><span style="color: #3366CC;">'http://localhost:8080/superapp/j_spring_security_check'</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20params<span style="color: #339933;">=</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'j_username'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'%s'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'j_password'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'password'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>var<span style="color: #339933;">%</span>20form<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;method&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;POST&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;action&quot;</span><span style="color: #339933;">,</span>path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>for<span style="color: #009900;">&#40;</span>var<span style="color: #339933;">%</span>20key<span style="color: #339933;">%</span>20in<span style="color: #339933;">%</span>20params<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>var<span style="color: #339933;">%</span>20hiddenField<span style="color: #339933;">=</span>document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;type&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;hidden&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">,</span>key<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>hiddenField.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;value&quot;</span><span style="color: #339933;">,</span>params<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>hiddenField<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span>document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To set it up, go into your preferences (cmd-,) and press the &#8220;Manage Search Engines&#8221; button.</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_manage_search_engines.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_manage_search_engines.png" alt="" title="chrome_manage_search_engines" width="875" height="132" class="aligncenter size-full wp-image-480" /></a></p>
<p>Then under &#8220;Other Search Engines&#8221; click in the box to &#8220;Add a new search engine&#8221;</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_search_engine.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_search_engine.png" alt="" title="chrome_add_new_search_engine"  class="aligncenter wp-image-481" /></a></p>
<p>Name it with your app&#8217;s name (&#8220;superapp login&#8221;), set the keyword to an abbreviation of your app&#8217;s name (&#8220;sa&#8221;), and set the url to the edited javascript command to log in with your app&#8217;s url/username/password (potentially with the username as &#8220;%s&#8221; to parameterize it).</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_after.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_add_new_after.png" alt="" title="chrome_add_new_after"  class="aligncenter wp-image-482" /></a></p>
<p>Once you save it, you can then go to your browser&#8217;s address bar (cmd-L) and type your abbreviation (&#8220;sa&#8221;) to get a new &#8220;search engine&#8221;.  Then enter the username you want to log in as.</p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_search_engine_in_action.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/11/chrome_search_engine_in_action.png" alt="" title="chrome_search_engine_in_action" class="aligncenter  wp-image-483" /></a></p>
<p>Hit enter and you&#8217;ll automatically be logged in to your app, without having to interact with your normal login page.</p>
<p>Automating this can help to keep you in the zone, especially if you&#8217;re using a security framework that allows deep linking.  </p>
<p>If deep linking is enabled, the quickest way to get back to the page you&#8217;re iterating on after your session has expired (or you&#8217;ve bounced the app) is to reload the page.  As it&#8217;s redirecting you to the login page, go to your address bar (cmd-L), type your keyword (ex: &#8220;sa&#8221;) and any associated username (ex: &#8220;admin&#8221;) and hit enter.  You&#8217;ll be logged in before the login page displays and Spring Security will redirect you back to the page you originally requested.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/11/29/speed-up-your-grails-spring-security-development-with-autologin-bookmarklet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Better Grails Batch Import Performance with Redis and Jesque</title>
		<link>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/</link>
		<comments>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 04:25:25 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[jesque]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=451</guid>
		<description><![CDATA[A couple of years ago, I put up a well-received blog post on tuning Batch Import Performance with Grails an MySQL. I&#8217;ve recently needed to revisit some batch importing procedures and have acquired a few extra tools in my Grails utility belt since writing that post: Grails Redis and Grails Jesque. Redis is a very [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of years ago, I put up a well-received blog post on tuning <a href="http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/">Batch Import Performance with Grails an MySQL</a>.</p>
<p>I&#8217;ve recently needed to revisit some batch importing procedures and have acquired a few extra tools in my Grails utility belt since writing that post: <a href="http://grails.org/plugin/redis" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/redis?referer=');">Grails Redis</a> and <a href="http://grails.org/plugin/jesque" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/jesque?referer=');">Grails Jesque</a>.</p>
<p><a href="http://redis.io" onclick="pageTracker._trackPageview('/outgoing/redis.io?referer=');">Redis</a> is a very fast key/value store, where the values are not just strings, but are data structures like lists, sets, and hash maps.  I&#8217;m the main author of the grails redis plugin, and it&#8217;s my favorite pragmatic technology of the past few years.  If you&#8217;re new to Redis, check out the <a href="http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/">presentation slides I gave at this year&#8217;s gr8conf</a>.</p>
<p><a href="https://github.com/gresrun/jesque" onclick="pageTracker._trackPageview('/outgoing/github.com/gresrun/jesque?referer=');">Jesque</a> is a Java implementation of <a href="https://github.com/defunkt/resque" onclick="pageTracker._trackPageview('/outgoing/github.com/defunkt/resque?referer=');">Resque</a>.  A Redis-backed message queueing system for creating background jobs.  The Jesque plugin is fully integrated with Grails and allows you to create worker jobs that are spring injected and have an active hibernate session.  Resque was written in Ruby by the folks at GitHub.</p>
<p>This combination makes parallelizing work very easy, as most of the pain of trying to spin off threads in grails is handled for you by Jesque.  Yes, there&#8217;s GPars, but the threads that it creates aren&#8217;t spring injected and don&#8217;t have hibernate sessions.</p>
<p>Using Jesque is as simple as:</p>
<ol>
<li>create a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy?referer=');"><code>Job</code></a> class that implements a <code>perform</code> method.</li>
<li>tell Jesque to start up 1..n worker threads that monitor a queue and use your <code>Job</code> to process work</li>
<li><code>enqueue</code> work on the queue so workers can pick it up</li>
</ol>
<p>I&#8217;ve created a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src?referer=');">bitbucket repository with all of the source code</a> from the original Batch Import post, as well as with the enhancements below.</p>
<p>The example problem is that there is a <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/src/groovy/com/naleid/example/Library.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/src/groovy/com/naleid/example/Library.groovy?referer=');">Library</a> class that produces metadata for 100,000 books that we want to persist in the database as <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/domain/com/naleid/example/Book.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/domain/com/naleid/example/Book.groovy?referer=');">Book</a> domain objects.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.naleid.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #aaaadd; font-weight: bold;">Book</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> title
    <span style="color: #aaaadd; font-weight: bold;">String</span> isbn
    <span style="color: #aaaadd; font-weight: bold;">Integer</span> edition
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> constraints <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;">static</span> mapping <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        isbn column:<span style="color: #ff0000;">'isbn'</span>, index:<span style="color: #ff0000;">'book_isbn_idx'</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The naive way of doing this takes Grails ~3 hours to do the inserts.  The <a href="http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/">original batch performance post</a> showed how to improve this time from 3 hours to 3 minutes with a few Grails and MySQL tweaks.  </p>
<p>Using Redis + Jesque to parallelize the task, I&#8217;m able to cut that time in half again to a little over 90 seconds on my MacBook Air.</p>
<p>On real-world imports, where there is quite a bit more data and potentially other linked domain objects that can be memoized with the redis-plugin, I&#8217;ve seen a >100x speed improvement over the original serial import, even with the tuning tips from my original post.</p>
<p>Install <a href="http://redis.io/download" onclick="pageTracker._trackPageview('/outgoing/redis.io/download?referer=');">redis</a> and <a href="https://bitbucket.org/tednaleid/example-jesque-batch/overview" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/overview?referer=');">clone the test project from bitbucket</a> to try it yourself.  Just <code>grails-run app</code>, go to <a href="http://localhost:8080/example-jesque-batch/" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/?referer=');">the running app on localhost</a> and click on the link to the <a href="http://localhost:8080/example-jesque-batch/serialBook/index" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/serialBook/index?referer=');">SerialBookController</a> to see the original version, or the <a href="http://localhost:8080/example-jesque-batch/parallelBook/index" onclick="pageTracker._trackPageview('/outgoing/localhost_8080/example-jesque-batch/parallelBook/index?referer=');">ParallelBookController</a> to see the faster Redis+Jesque version.  Each will display the length of time they took to do the insert after they&#8217;re done.</p>
<p>The ParallelBookController calls <code><a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy?referer=');">bookService</a>.parallelImportBooksInLibrary()</code>.  That method spins up a number of worker threads, iterates through the books in the <code>Library</code> and enqueues each one on a Jesque queue.  When it&#8217;s done iterating through the <code>Library</code>, it tells all the threads to end when they&#8217;re done processing all the work:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">def</span> parallelImportBooksInLibrary<span style="color: #66cc66;">&#40;</span>library<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">Integer</span> workerCount <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">10</span>
        <span style="color: #aaaadd; font-weight: bold;">String</span> queueName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;import:book&quot;</span>
        withWorkers<span style="color: #66cc66;">&#40;</span>queueName, BookConsumerJob, workerCount<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            library.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #aaaadd; font-weight: bold;">Map</span> bookValueMap <span style="color: #66cc66;">-&gt;</span>
                <span style="color: #aaaadd; font-weight: bold;">String</span> bookValueMapJson <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>bookValueMap <span style="color: #000000; font-weight: bold;">as</span> JSON<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
                jesqueService.<span style="color: #006600;">enqueue</span><span style="color: #66cc66;">&#40;</span>queueName, BookConsumerJob.<span style="color: #006600;">simpleName</span>, bookValueMapJson<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> withWorkers<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> queueName, <span style="color: #000000; font-weight: bold;">Class</span> jobClass, <span style="color: #aaaadd; font-weight: bold;">Integer</span> workerCount <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>, Closure closure<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">def</span> workers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #000000; font-weight: bold;">def</span> fullQueueName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;resque:queue:$queueName&quot;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
            workers <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>..<span style="color: #006600;">workerCount</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> jesqueService.<span style="color: #006600;">startWorker</span><span style="color: #66cc66;">&#40;</span>queueName, jobClass.<span style="color: #006600;">simpleName</span>, jobClass<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
            closure<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #808080; font-style: italic;">// wait for all the work we've generated to be pulled off the queue</span>
            <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>redisService.<span style="color: #006600;">exists</span><span style="color: #66cc66;">&#40;</span>fullQueueName<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> sleep<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">500</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// all work is off the queue, tell each worker to kill themselves when they're finished</span>
            workers<span style="color: #66cc66;">*</span>.<span style="color: #006600;">end</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The work queue that persist the <code>Book</code> domain objects in the database are very simple Jesque Job artefacts that are spring injected and have an active hibernate session.  They can be of any class type.  The only requirement is that they have a method named <code>perform</code> that is called and passed an item of work from the queue.</p>
<p>Here&#8217;s the example <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/jobs/com/naleid/example/BookConsumerJob.groovy?referer=');"><code>BookConsumerJob</code></a> class that persists a <code>Book</code> to the database:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.naleid.example</span>
&nbsp;
<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> BookConsumerJob <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> bookService
&nbsp;
    <span style="color: #993333;">void</span> perform<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> bookJson<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        bookService.<span style="color: #006600;">updateOrInsertBook</span><span style="color: #66cc66;">&#40;</span>JSON.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>bookJson<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You can see how simple the <code>BookConsumerJob</code> class is.  It also calls out to the same <a href="https://bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/example-jesque-batch/src/tip/grails-app/services/com/naleid/example/BookService.groovy?referer=');">bookService</a> method that the serial batch import calls to import a <code>Book</code>.</p>
<p>One other neat thing about using Jesque is that it adheres to the Resque conventions for what gets stored in Redis.  This means that you can <code>gem install resque-web</code> and then launch <code>resque-web</code> to get a nice monitoring platform for your Jobs and to see errors, or how much work is left in the queue.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/10/13/better-grails-batch-import-performance-with-redis-and-jesque/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Smart Bash/Zsh Aliases to Run Appropriate Grails Version</title>
		<link>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/</link>
		<comments>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 03:46:39 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=423</guid>
		<description><![CDATA[I&#8217;m currently on a project that has a couple of different apps that are using different versions of grails that need to be run concurrently. Switching a symlink no longer fit the way I needed to work so I came up with a couple bash/zsh aliases that are smart about the version of grails for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently on a project that has a couple of different apps that are using different versions of grails that need to be run concurrently.  <a href="http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/">Switching a symlink</a> no longer fit the way I needed to work so I came up with a couple bash/zsh aliases that are smart about the version of grails for the current directory.</p>
<p>These aliases work for both the <code>grails</code> as well as the <code>grails-debug</code> commands (for attaching a remote debugger).</p>
<p>If there is an <code>application.properties</code> file in the current directory, we can find the current version of grails for the app.</p>
<p>If there isn&#8217;t an <code>application.properties</code> file in the current directory, the script just defaults to whatever version of grails you&#8217;ve already set up as your default through the standard <code>$GRAILS_HOME</code> environment variable.  You can use the <a href="http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/">grails symlink switching aliases</a> that I created previously to easily move this between versions.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">grails</span>=<span style="color: #ff0000;">&quot;execute_grails_version grails&quot;</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> grails-debug=<span style="color: #ff0000;">&quot;execute_grails_version grails-debug&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> execute_grails_version<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">GRAILS_CMD</span>=<span style="color: #007800;">$1</span>
    <span style="color: #7a0874; font-weight: bold;">shift</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> application.properties <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_VERSION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> app.grails.version application.properties <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/.*=(.*)/\1/'</span><span style="color: #000000; font-weight: bold;">`</span>
        <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_HOME</span>=<span style="color: #ff0000;">&quot;/usr/local/grails-<span style="color: #007800;">$GRAILS_VERSION</span>&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties found, using <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME of <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">else</span> 
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties NOT found, leaving <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME as <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$GRAILS_HOME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ERROR: Unable to find <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME directory at <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
        <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
    <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>UPDATE: There are a few situations where aliases aren&#8217;t available (or are a pain to get available) such as when code is being executed as part of another application rather than from the command line.  To get around this, these scripts (created by a co-worker of mine, <a href="http://twitter.com/#!/sjurgemeyer" onclick="pageTracker._trackPageview('/outgoing/twitter.com/_/sjurgemeyer?referer=');">@sjurgemeyer</a>) could be put in your <code>PATH</code>, ahead of your <code>$GRAILS_HOME/bin</code> and used instead of the aliases above:</p>
<p>grails:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails-version grails <span style="color: #007800;">$*</span></pre></div></div>

<p>grails-debug:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails-version grails-debug <span style="color: #007800;">$*</span></pre></div></div>

<p>grails-version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #007800;">GRAILS_CMD</span>=<span style="color: #007800;">$1</span>
<span style="color: #7a0874; font-weight: bold;">shift</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-f</span> application.properties <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_VERSION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> app.grails.version application.properties <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #660033;">-E</span> <span style="color: #ff0000;">'s/.*=(.*)/\1/'</span><span style="color: #000000; font-weight: bold;">`</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_HOME</span>=<span style="color: #ff0000;">&quot;/usr/local/grails-<span style="color: #007800;">$GRAILS_VERSION</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties found, using <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME of <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">else</span> 
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;application.properties NOT found, leaving <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME as <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$GRAILS_HOME</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;ERROR: Unable to find <span style="color: #000099; font-weight: bold;">\$</span>GRAILS_HOME directory at <span style="color: #007800;">$GRAILS_HOME</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> <span style="color: #000000;">1</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span>
<span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GRAILS_CMD</span> <span style="color: #007800;">$*</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/09/26/smart-bash-zsh-aliases-to-run-appropriate-grails-version/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dynamically setting Grails Log4J levels with the Console Plugin</title>
		<link>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/</link>
		<comments>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/#comments</comments>
		<pubDate>Fri, 23 Sep 2011 18:41:59 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=414</guid>
		<description><![CDATA[If you&#8217;ve got Burt Beckwith&#8217;s great Grails Console Plugin installed, it&#8217;s easy to tweak the logging levels dynamically in your grails application. The quick and dirty way to switch your logging level dynamically, if you know the name of the logger is just to do this in your console window: import org.apache.log4j.* Logger.getLogger&#40;&#34;org.springframework&#34;&#41;.level = Level.DEBUG [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve got Burt Beckwith&#8217;s great <a href="http://grails.org/plugin/console" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/console?referer=');">Grails Console Plugin</a> installed, it&#8217;s easy to tweak the logging levels dynamically in your grails application.</p>
<p>The quick and dirty way to switch your logging level dynamically, if you know the name of the logger is just to do this in your console window:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.*</span>
Logger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;org.springframework&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> Level.<span style="color: #006600;">DEBUG</span></pre></div></div>

<p>Sometimes, a few helper methods can help you see what the current config is (especially if you&#8217;ve changed some things), as well as figure out what the right loggers are to tweak.  This sample script can be used in a grails console to make it easy to view and change the logging level to whatever you want, just cut and paste it into your application&#8217;s console window (in dev it defaults to: http://localhost:8080/yourAppName/console):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.Logger</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.apache.log4j.Level</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">static</span> org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">log4j</span>.<span style="color: #006600;">Level</span>.<span style="color: #66cc66;">*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> getRootLogger<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> Logger.<span style="color: #006600;">rootLogger</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">loggerRepository</span>.<span style="color: #006600;">currentLoggers</span>.<span style="color: #663399;">toList</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #663399;">sort</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">name</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> allLoggers.<span style="color: #663399;">findAll</span> <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">level</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> getLogger<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> logName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>logName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> setLevel<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> logName, Level level<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> rootLogger.<span style="color: #006600;">getLogger</span><span style="color: #66cc66;">&#40;</span>logName<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> level <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${logger.name} -&gt; ${logger.level}&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> printAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> allLoggers.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> printLogger<span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">def</span> printActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> activeLoggers.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> printLogger<span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This makes it easy to see what logs are currently active (those with a log level set):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">printActiveLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>prints something like:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">filters</span>.<span style="color: #006600;">LoggingFilters</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">filters</span>.<span style="color: #006600;">SecurityFilters</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> WARN
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">task</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
org.<span style="color: #006600;">apache</span>.<span style="color: #006600;">cxf</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
...</pre></div></div>

<p>You can also list all loggers, which also adds in those loggers who&#8217;s log level is currently `null`:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">printAllLoggers<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>prints:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span> <span style="color: #66cc66;">-&gt;</span> DEBUG
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">bootstrap</span>.<span style="color: #006600;">BootStrap</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">bootstrap</span>.<span style="color: #006600;">QuartzBootStrap</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">Base64Codec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">HTMLCodec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">codec</span>.<span style="color: #006600;">org</span>.<span style="color: #006600;">codehaus</span>.<span style="color: #006600;">groovy</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">codecs</span>.<span style="color: #006600;">HexCodec</span> <span style="color: #66cc66;">-&gt;</span> <span style="color: #000000; font-weight: bold;">null</span>
...</pre></div></div>

<p>You can also dynamically grab/create a logger and set it&#8217;s logging level to something more or less verbose than it&#8217;s current value:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> logger <span style="color: #66cc66;">=</span> getLogger<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;grails.app.service.grails.plugin.redis.RedisService&quot;</span><span style="color: #66cc66;">&#41;</span>
printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// initially WARN</span>
logger.<span style="color: #006600;">level</span> <span style="color: #66cc66;">=</span> INFO  
printLogger<span style="color: #66cc66;">&#40;</span>logger<span style="color: #66cc66;">&#41;</span>   <span style="color: #808080; font-style: italic;">// prints INFO</span></pre></div></div>

<p>prints:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> WARN
grails.<span style="color: #006600;">app</span>.<span style="color: #006600;">service</span>.<span style="color: #006600;">grails</span>.<span style="color: #006600;">plugin</span>.<span style="color: #006600;">redis</span>.<span style="color: #006600;">RedisService</span> <span style="color: #66cc66;">-&gt;</span> INFO</pre></div></div>

<p>It&#8217;d be easy to turn this into a simple gsp/controller that accepts changes and can list things out.  There are also a number of other plugins out there that let you view/change logging levels (including another one of Burt&#8217;s plugins, <a href="http://grails.org/plugin/app-info" onclick="pageTracker._trackPageview('/outgoing/grails.org/plugin/app-info?referer=');">app info</a>), but if you don&#8217;t have those installed, this is a quick way to see what&#8217;s going on with your application.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/09/23/dynamically-setting-grails-log4j-levels-with-the-console-plugin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>New Grails Redis Plugin Released</title>
		<link>http://naleid.com/blog/2011/08/08/new-grails-redis-plugin-released/</link>
		<comments>http://naleid.com/blog/2011/08/08/new-grails-redis-plugin-released/#comments</comments>
		<pubDate>Tue, 09 Aug 2011 03:46:23 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=406</guid>
		<description><![CDATA[I released the first version of the Grails Redis Plugin over the weekend. It&#8217;s a brand new plugin that takes the place of the previous redis plugin (which is being renamed to &#8220;redis-gorm&#8221; and has been refactored to use this plugin as a dependency). It&#8217;s version is 1.0.0.M7 just so it&#8217;s &#8220;higher&#8221; than the plugin [...]]]></description>
			<content:encoded><![CDATA[<p>I released the first version of the <a href="http://www.grails.org/plugin/redis" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/plugin/redis?referer=');">Grails Redis Plugin</a> over the weekend.  It&#8217;s a brand new plugin that takes the place of the previous redis plugin (which is being renamed to &#8220;redis-gorm&#8221; and has been refactored to use this plugin as a dependency).  It&#8217;s version is 1.0.0.M7 just so it&#8217;s &#8220;higher&#8221; than the plugin it&#8217;s replacing, though I&#8217;d probably make it a 0.9 release if I were releasing it under a new name till I get a little more community feedback.</p>
<p>Quick description of what Redis is from <a href="https://github.com/grails-plugins/grails-redis" onclick="pageTracker._trackPageview('/outgoing/github.com/grails-plugins/grails-redis?referer=');">the README</a>:</p>
<blockquote><p>
The best definition of Redis that I&#8217;ve heard is that it is a &#8220;collection of data structures exposed over the network&#8221;.</p>
<p>Redis is an insanely fast key/value store, in some ways similar to memcached, but the values it stores aren&#8217;t just dumb blobs of data. Redis values are data structures like strings, lists, hash maps, sets, and sorted sets. Redis also can act as a lightweight pub/sub or message queueing system.</p>
<p>Redis is used in production today by a number of very popular websites including Craigslist, StackOverflow, GitHub, The Guardian, and Digg.
</p></blockquote>
<p>The Grails Redis plugin makes a Redis connection pool available (and injectable as a spring bean) to your Grails application.  </p>
<p>To install the plugin, just execute this inside your application&#8217;s directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails install-plugin redis</pre></div></div>

<p>It allows you to transparently interact with your Redis instance by automatically handling retrieving a connection from the pool and ensuring that the connection is returned to the pool as it delegates to redis.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// overrides propertyMissing and methodMissing to delegate to redis</span>
<span style="color: #000000; font-weight: bold;">def</span> redisService
&nbsp;
redisService.<span style="color: #006600;">foo</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span>   
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">&quot;bar&quot;</span> <span style="color: #66cc66;">==</span> redisService.<span style="color: #006600;">foo</span>   
&nbsp;
redisService.<span style="color: #006600;">sadd</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;months&quot;</span>, <span style="color: #ff0000;">&quot;february&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">==</span> redisService.<span style="color: #006600;">sismember</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;months&quot;</span>, <span style="color: #ff0000;">&quot;february&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>One of the plugin&#8217;s greatest strengths is in the <a href="http://en.wikipedia.org/wiki/Memoization" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Memoization?referer=');">memoization</a> methods and tag libraries that it adds.   It&#8217;s a write-through cache (with optional TTL expiration).  Before executing the closure/tag, it will check Redis to see if we&#8217;ve already calculated that value.  If we have, we&#8217;ll just return the answer from Redis, otherwise, we&#8217;ll calculate it, and save it in Redis for future calls.</p>
<p>service method:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">redisService.<span style="color: #006600;">memoize</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;user:$userId:helloMessage&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #808080; font-style: italic;">// expensive to calculate method that returns a String</span>
    <span style="color: #ff0000;">&quot;Hello ${security.currentLoggedInUser().firstName}&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>taglib:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>redis:memoize key<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;mykey&quot;</span> expire<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3600&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;!--</span> 
        insert expensive to generate GSP content here 
&nbsp;
        taglib body will be executed once, subsequent calls 
        will pull from redis till the key expires
    <span style="color: #66cc66;">--&gt;</span>
    <span style="color: #66cc66;">&lt;</span>div id<span style="color: #66cc66;">=</span><span style="color: #ff0000;">'header'</span><span style="color: #66cc66;">&gt;</span>
        ... <span style="color: #006600;">expensive</span> header stuff here that can be cached ...
    <span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>/redis:memoize<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Check out the full <a href="https://github.com/grails-plugins/grails-redis" onclick="pageTracker._trackPageview('/outgoing/github.com/grails-plugins/grails-redis?referer=');">documentation on the github repository</a>.</p>
<p>If you&#8217;re new to using <a href="http://redis.io" onclick="pageTracker._trackPageview('/outgoing/redis.io?referer=');">Redis</a> with Groovy, I created an <a href="http://naleid.com/blog/2010/12/28/intro-to-using-redis-with-groovy/">introductory post</a> and gave a <a href="http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/">presentation at gr8conf</a> that are good starting places.</p>
<p>If you use OSX for development, you might also find <a href="http://naleid.com/blog/2011/03/05/running-redis-as-a-user-daemon-on-osx-with-launchd/">these instructions for automatically launching Redis on startup with launchd</a> useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/08/08/new-grails-redis-plugin-released/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Redis, Groovy and Grails presentation at gr8conf 2011 and GUM</title>
		<link>http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/</link>
		<comments>http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 04:12:22 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=378</guid>
		<description><![CDATA[A couple of weeks ago, I gave a talk on Redis, Groovy and Grails at the Groovy Users of Minnesota meeting. I&#8217;m giving that presentation again tomorrow at gr8conf 2011 and I wanted to post the slides so that people had access to them on the web. Redis and Groovy and Grails &#8211; gr8conf 2011 [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I gave a talk on Redis, Groovy and Grails at the Groovy Users of Minnesota meeting.  I&#8217;m giving that presentation again tomorrow at gr8conf 2011 and I wanted to post the slides so that people had access to them on the web.</p>
<div style="width:425px" id="__ss_8441813"><strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/tednaleid/redis-and-groovy-and-grails-gr8conf-2011" title="Redis and Groovy and Grails - gr8conf 2011" onclick="pageTracker._trackPageview('/outgoing/www.slideshare.net/tednaleid/redis-and-groovy-and-grails-gr8conf-2011?referer=');">Redis and Groovy and Grails &#8211; gr8conf 2011</a></strong><object id="__sse8441813" width="850" height="570"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=redis-gr8conf-110627225017-phpapp01&#038;stripped_title=redis-and-groovy-and-grails-gr8conf-2011&#038;userName=tednaleid" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed name="__sse8441813" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=redis-gr8conf-110627225017-phpapp01&#038;stripped_title=redis-and-groovy-and-grails-gr8conf-2011&#038;userName=tednaleid" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="850" height="570"></embed></object>
<div style="padding:5px 0 12px"></div>
</div>
<p>An original version of the keynote file that I presented can be found <a href="https://bitbucket.org/tednaleid/redis-presentation/src" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/redis-presentation/src?referer=');">on my bitbucket account</a>.  You can download the repo or grab the raw versions off bitbucket.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/06/27/redis-groovy-and-grails-presentation-at-gr8conf-2011-and-gum/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Replace Grails Console with the Editor of Your Choice</title>
		<link>http://naleid.com/blog/2011/04/25/replace-grails-console-with-the-editor-of-your-choice/</link>
		<comments>http://naleid.com/blog/2011/04/25/replace-grails-console-with-the-editor-of-your-choice/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 04:25:42 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=315</guid>
		<description><![CDATA[The grails console has a number of disadvantages that make it a little clunky to use: You can&#8217;t attach it to a running grails instance, you need to run grails console from scratch You can&#8217;t pass it parameters, such as the name of a script you&#8217;ve saved It&#8217;s a swing app, which is always a [...]]]></description>
			<content:encoded><![CDATA[<p>The grails console has a number of disadvantages that make it a little clunky to use:</p>
<ul>
<li>You can&#8217;t attach it to a running grails instance, you need to run <code>grails console</code> from scratch</li>
<li>You can&#8217;t pass it parameters, such as the name of a script you&#8217;ve saved</li>
<li>It&#8217;s a swing app, which is always a little wonky in it&#8217;s keybindings and it&#8217;s behavior</li>
<li>It&#8217;s not <i>&lt;insert your chosen editor&gt;</i></li>
</ul>
<p>I got tired of these limitations so I decided to do something about it.  I put together a groovy script that uses <a href="http://groovy.codehaus.org/modules/http-builder/download.html" onclick="pageTracker._trackPageview('/outgoing/groovy.codehaus.org/modules/http-builder/download.html?referer=');">HTTPBuilder</a> to POST groovy code to a running Grails app that has the <a href="http://www.grails.org/Console+Plugin" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/Console+Plugin?referer=');">grails console plugin</a> installed. </p>
<p>If you&#8217;re running this in any non-development environment, you&#8217;ll want to ensure that it&#8217;s behind some form of authentication.  The most popular grails security plugin is Burt Beckwith&#8217;s <a href="http://www.grails.org/plugin/spring-security-core" onclick="pageTracker._trackPageview('/outgoing/www.grails.org/plugin/spring-security-core?referer=');">spring security core</a>, so this script comes with built-in support for that.</p>
<p>This script makes it easy to call from any editor that allows you to execute scripts (pretty much every programmer&#8217;s editor on the market).   At the bottom of the post, I show how to integrate it with Vim (my editor of choice), but it&#8217;d be just as easy to script it from TextMate, JEdit, IntelliJ, Eclipse, etc.  Any editor that can take the file you&#8217;re currently editing and pass it to a shell script.</p>
<p>Here&#8217;s my Vim session with a grails script at the bottom and the output from that grails script in another window at the top.  </p>
<p><a href="http://naleid.com/blog/wp-content/uploads/2011/04/vim_grails_console1.png"><img src="http://naleid.com/blog/wp-content/uploads/2011/04/vim_grails_console1.png" alt="Using Vim as a Grails Console" title="vim_grails_console" width="725" height="417" class="aligncenter size-full wp-image-338" /></a></p>
<p><span id="more-315"></span>This is the script that makes the magic happen (<a href="https://github.com/tednaleid/groovy-util/blob/master/src/main/groovy/postCode.groovy" onclick="pageTracker._trackPageview('/outgoing/github.com/tednaleid/groovy-util/blob/master/src/main/groovy/postCode.groovy?referer=');">linked from github</a> so any future tweaks will be reflected below), just grab a copy and save it somewhere in your path.  </p>
<div style="background-color: #fff">
<script src="http://gist-it.appspot.com/github/tednaleid/groovy-util/raw/master/src/main/groovy/postCode.groovy"></script>
</div>
<p><br/></p>
<p>If you execute the script without any parameters, it&#8217;ll show you the usage syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> postCode.groovy 
usage: postCode.groovy <span style="color: #7a0874; font-weight: bold;">&#91;</span>-u username<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>-p password<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-b</span> baseUrl <span style="color: #c20cb9; font-weight: bold;">file</span>
 -b,--baseurl <span style="color: #000000; font-weight: bold;">&lt;</span>baseUrl<span style="color: #000000; font-weight: bold;">&gt;</span>     The required base url to auth<span style="color: #000000; font-weight: bold;">/</span>post to, ex:
                            http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span>
 -p,--password <span style="color: #000000; font-weight: bold;">&lt;</span>password<span style="color: #000000; font-weight: bold;">&gt;</span>   The password to authenticate with
 -u,--user <span style="color: #000000; font-weight: bold;">&lt;</span>user<span style="color: #000000; font-weight: bold;">&gt;</span>           The username to authenticate with, null
                            username<span style="color: #000000; font-weight: bold;">/</span>password skips authentication</pre></div></div>

<p>It uses the nifty CliBuilder that&#8217;s comes with recent versions of Groovy to parse command line arguments.   If your project doesn&#8217;t have any authentication, you can simply call it with the <code>-b</code> argument to let it know where your app is running and pass it a script to execute on that running instance:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">postCode.groovy <span style="color: #660033;">-b</span> http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span> scriptToRun.groovy 
... results of scriptToRun.groovy ...</pre></div></div>

<p>If your project IS using the spring security plugin for authentication, you can add a username and a password to use for authentication:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">postCode.groovy <span style="color: #660033;">-u</span> admin <span style="color: #660033;">-p</span> sekrit <span style="color: #660033;">-b</span> http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">/</span>myapp<span style="color: #000000; font-weight: bold;">/</span> scriptToRun.groovy
... results of scriptToRun.groovy ...</pre></div></div>

<p>The first time that you run it, it might take a little while to run because the Grape <code>Grab</code> annotation is downloading the HTTPBuilder jars and it&#8217;s dependencies.</p>
<p>UPDATE: 8/7/2011 The below is no longer necessary with the new @GrabExclude to ignore the groovy dependency.<br />
<strike><br />
One sort of screwy thing that I&#8217;ve found with the default Grape settings is that it really isn&#8217;t that smart about what dependencies it should download again when they&#8217;re already satisfied.  This can cause a ~30 second pause every time the script is run, which is unacceptable.  I posted a <a href="http://groovy.329449.n5.nabble.com/HTTP-Client-grape-resolve-takes-30-seconds-on-groovy-1-7-10-dependency-td4309356.html" onclick="pageTracker._trackPageview('/outgoing/groovy.329449.n5.nabble.com/HTTP-Client-grape-resolve-takes-30-seconds-on-groovy-1-7-10-dependency-td4309356.html?referer=');">message on the Groovy mailing list</a> and the <a href="http://spring-java-ee.blogspot.com/2011/04/fixing-slow-grab-dependencies-issue.html" onclick="pageTracker._trackPageview('/outgoing/spring-java-ee.blogspot.com/2011/04/fixing-slow-grab-dependencies-issue.html?referer=');">tl;dr fix</a> for it is to increase the <code> ivy.cache.ttl.default</code> timeout in your ~/.groovy/grapeConfig.xml file (use <a href="http://svn.codehaus.org/groovy/tags/GROOVY_1_7_10/src/main/groovy/grape/defaultGrapeConfig.xml" onclick="pageTracker._trackPageview('/outgoing/svn.codehaus.org/groovy/tags/GROOVY_1_7_10/src/main/groovy/grape/defaultGrapeConfig.xml?referer=');">this as a template</a> if you don&#8217;t have the file).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ivysettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ivy.cache.ttl.default&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;15m&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> 
...</pre></div></div>

<p></strike></p>
<h2>Example Use with Spring Security Core Plugin</h2>
<p>Here&#8217;s a quick demo project where the console plugin is protected by the Spring Security Core plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails create<span style="color: #66cc66;">-</span>app demo<span style="color: #66cc66;">-</span>post<span style="color: #66cc66;">-</span>code
cd demo<span style="color: #66cc66;">-</span>post<span style="color: #66cc66;">-</span>code
grails install<span style="color: #66cc66;">-</span>plugin console</pre></div></div>

<p>Install the spring-security-core plugin and create the default User and Role domain classes.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails install<span style="color: #66cc66;">-</span>plugin spring<span style="color: #66cc66;">-</span>security<span style="color: #66cc66;">-</span>core
grails s2<span style="color: #66cc66;">-</span>quickstart com.<span style="color: #006600;">example</span> User Role</pre></div></div>

<p>Edit <code>grails-app/conf/Config.groovy</code> and add a rule next to the other spring security configuration stating only <code>ROLE_ADMIN</code> users can get to the console plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">plugins</span>.<span style="color: #006600;">springsecurity</span>.<span style="color: #006600;">controllerAnnotations</span>.<span style="color: #006600;">staticRules</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span>
   <span style="color: #ff0000;">'/console/**'</span>: <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'ROLE_ADMIN'</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>Now we&#8217;ll need an <code>admin</code> user in <code>grails-app/conf/BootStrap.groovy</code> that has permission to execute code in the console.  Edit the bootstrap file to look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">com.example.*</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> BootStrap <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> springSecurityService
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> init <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span> servletContext <span style="color: #66cc66;">-&gt;</span>
        <span style="color: #000000; font-weight: bold;">def</span> adminRole <span style="color: #66cc66;">=</span> Role.<span style="color: #006600;">findByAuthority</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ROLE_ADMIN'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">?</span>: <span style="color: #000000; font-weight: bold;">new</span> Role<span style="color: #66cc66;">&#40;</span>authority: <span style="color: #ff0000;">'ROLE_ADMIN'</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError: <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">def</span> adminUser <span style="color: #66cc66;">=</span> User.<span style="color: #006600;">findByUsername</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">?</span>: <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #66cc66;">&#40;</span>
            username: <span style="color: #ff0000;">'admin'</span>, password: springSecurityService.<span style="color: #006600;">encodePassword</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'sekrit'</span><span style="color: #66cc66;">&#41;</span>, enabled: <span style="color: #000000; font-weight: bold;">true</span>
        <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError: <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>adminUser.<span style="color: #006600;">authorities</span>.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">&#40;</span>adminRole<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> UserRole.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span>adminUser, adminRole<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">def</span> destroy <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>As you can see, we&#8217;ve created an <code>admin</code> user with the password <code>sekrit</code>.</p>
<p>Now, create a sample script to run in the grails context, something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">com.example.*</span> 
&nbsp;
<span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;User count = ${User.count()}&quot;</span></pre></div></div>

<p>Now run the app with <code>grails run-app</code> and in another terminal window execute the postCode.groovy script and give it the credentials and the base url to post it to.  If you&#8217;ve got everything set up, you should see that there&#8217;s one User in the database (the admin user that we bootstrapped in :).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> postCode.groovy <span style="color: #660033;">-u</span> admin <span style="color: #660033;">-p</span> sekrit <span style="color: #660033;">-b</span> http:<span style="color: #000000; font-weight: bold;">//</span>localhost:<span style="color: #000000;">8080</span><span style="color: #000000; font-weight: bold;">/</span>demo-post-code test.groovy
&nbsp;
User count = <span style="color: #000000;">1</span></pre></div></div>

<h2>Integrating With Vim</h2>
<p>My vimscript skills are pretty much non-existent, and I&#8217;m sure there&#8217;s a better way to do this, but here&#8217;s the code that I&#8217;ve got in <a href="https://bitbucket.org/tednaleid/vimrc/src" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/vimrc/src?referer=');">my .vimrc</a> to bind F7 to executing the current buffer as plain groovy, and binds F8 to executing it as a grails script, with full binding.</p>
<p>Shift-F7/F8 will close the execution window.  If you highlight a part of your code in visual mode, it will just post the highlighted code rather than the whole file.</p>
<p>You can also override the default user/password/base url in a grails session by overriding any of these values, either directly in the vimscript or by setting the appropriate environment variable:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_user = <span style="color: #000000;">$</span>DEFAULT_GRAILS_USER
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_password = <span style="color: #000000;">$</span>DEFAULT_GRAILS_PASSWORD
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_base_url = <span style="color: #000000;">$</span>DEFAULT_GRAILS_BASE_URL</pre></div></div>

<p>So to test the above script, I&#8217;d use this in my .zshrc/.bashrc/.profile:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_USER</span>=<span style="color: #ff0000;">'admin'</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_PASSWORD</span>=<span style="color: #ff0000;">'sekrit'</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_BASE_URL</span>=<span style="color: #ff0000;">'http://localhost:8080/demo-post-code'</span></pre></div></div>

<p>and this script to temporarily change the values using the current directory for the BASE_URL and optionally passing in a user and password</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># example usage from root of grails directory: grailsBaseUrlReset myUsername myOtherPassword</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsBaseUrlReset<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_BASE_URL</span>=<span style="color: #ff0000;">&quot;http://localhost:8080/<span style="color: #007800;">${PWD##*/}</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;DEFAULT_GRAILS_BASE_URL set to <span style="color: #007800;">$DEFAULT_GRAILS_BASE_URL</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_USER</span>=<span style="color: #007800;">$1</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;DEFAULT_GRAILS_USER set to <span style="color: #007800;">$DEFAULT_GRAILS_USER</span>&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">DEFAULT_GRAILS_PASSWORD</span>=<span style="color: #007800;">$2</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;DEFAULT_GRAILS_PASSWORD set to <span style="color: #007800;">$DEFAULT_GRAILS_PASSWORD</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>You can change them temporarily in vim by changing a value for your current grails session, just hit colon and change the value:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #000000;">:</span><span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_password = <span style="color: #C5A22D;">&quot;reallyReallySekrit&quot;</span></pre></div></div>

<p>(or you could add them to a different file that you source independently)</p>
<p>Here&#8217;s the full script, just add it to your <code>~/.vimrc</code> and make sure the <code>postCode.groovy</code> script is in your path and you should be set to go:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"><span style="color: #804040;">function</span><span style="color: #000000;">!</span> s<span style="color: #000000;">:</span>copy_groovy_buffer_to_temp<span style="color: #000000;">&#40;</span>first, last<span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
  &quot; groovy/java scripts can't start with a # and tempname's normally do</span>
  <span style="color: #804040;">let</span> src = <span style="color: #25BB4D;">substitute</span><span style="color: #000000;">&#40;</span><span style="color: #25BB4D;">tempname</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #C5A22D;">&quot;[^<span style="">\/</span>]*$&quot;</span>, <span style="color: #C5A22D;">&quot;vim_&amp;.groovy&quot;</span>, <span style="color: #C5A22D;">&quot;&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;"> 
&nbsp;
  &quot; put current buffer's content in a temp file</span>
  silent <span style="color: #804040;">exe</span> <span style="color: #C5A22D;">&quot;: &quot;</span> <span style="color: #000000;">.</span> a<span style="color: #000000;">:</span>first <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot;,&quot;</span> <span style="color: #000000;">.</span> a<span style="color: #000000;">:</span>last <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot;w &quot;</span> <span style="color: #000000;">.</span> src
&nbsp;
  <span style="color: #804040;">return</span> src
endfunction
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> s<span style="color: #000000;">:</span>select_new_temp_buffer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #804040;">let</span> temp_file = <span style="color: #25BB4D;">tempname</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
  &quot; open the preview window to the temp file</span>
  silent <span style="color: #804040;">exe</span> <span style="color: #C5A22D;">&quot;:pedit! &quot;</span> <span style="color: #000000;">.</span> temp_file<span style="color: #adadad; font-style: italic;">
&nbsp;
  &quot; select the temp buffer as active </span>
  wincmd P<span style="color: #adadad; font-style: italic;">
&nbsp;
  &quot; set options for temp buffer</span>
  <span style="color: #804040;">setlocal</span> <span style="color: #668080;">buftype</span>=nofile
  <span style="color: #804040;">setlocal</span> <span style="color: #668080;">noswapfile</span>
  <span style="color: #804040;">setlocal</span> <span style="color: #804040;">syntax</span>=none
  <span style="color: #804040;">setlocal</span> <span style="color: #668080;">bufhidden</span>=<span style="color: #25BB4D;">delete</span>
&nbsp;
  <span style="color: #804040;">return</span> temp_file
endfunction
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> Groovy_eval_vsplit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #25BB4D;">range</span>
  <span style="color: #804040;">let</span> temp_source = s<span style="color: #000000;">:</span>copy_groovy_buffer_to_temp<span style="color: #000000;">&#40;</span>a<span style="color: #000000;">:</span>firstline, a<span style="color: #000000;">:</span>lastline<span style="color: #000000;">&#41;</span>
  <span style="color: #804040;">let</span> temp_file = s<span style="color: #000000;">:</span>select_new_temp_buffer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #adadad; font-style: italic;">
&nbsp;
  &quot; replace current buffer with groovy's output</span>
  silent <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">&quot;:%! groovy &quot;</span> <span style="color: #000000;">.</span> temp_source <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; 2&gt;&amp;1 &quot;</span>
&nbsp;
  wincmd p<span style="color: #adadad; font-style: italic;"> &quot; change back to the source buffer</span>
endfunction
&nbsp;
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy vmap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F7&gt;</span> <span style="color: #000000;">:</span><span style="color: #804040;">call</span> Groovy_eval_vsplit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #668080;">&lt;CR&gt;</span>
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy nmap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F7&gt;</span> mzggVG<span style="color: #000000;">&lt;</span>F7<span style="color: #000000;">&gt;</span>`z
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy imap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F7&gt;</span> <span style="color: #000000;">&lt;</span>Esc<span style="color: #000000;">&gt;&lt;</span>F7<span style="color: #000000;">&gt;</span>a
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy <span style="color: #804040;">map</span> <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #000000;">&lt;</span>S<span style="color: #000000;">-</span>F7<span style="color: #000000;">&gt;</span> <span style="color: #000000;">:</span>wincmd P<span style="color: #000000;">&lt;</span>CR<span style="color: #000000;">&gt;:</span>q<span style="color: #000000;">&lt;</span>CR<span style="color: #000000;">&gt;</span>
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy imap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #000000;">&lt;</span>S<span style="color: #000000;">-</span>F7<span style="color: #000000;">&gt;</span> <span style="color: #000000;">&lt;</span>Esc<span style="color: #000000;">&gt;&lt;</span>S<span style="color: #000000;">-</span>F7<span style="color: #000000;">&gt;</span>a<span style="color: #adadad; font-style: italic;">
&nbsp;
&nbsp;
&quot; set these up as environment variables on your system, or override</span><span style="color: #adadad; font-style: italic;">
&quot; per session by using ':let g:grails_user = foo'</span>
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_user = <span style="color: #000000;">$</span>DEFAULT_GRAILS_USER
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_password = <span style="color: #000000;">$</span>DEFAULT_GRAILS_PASSWORD
<span style="color: #804040;">let</span> g<span style="color: #000000;">:</span>grails_base_url = <span style="color: #000000;">$</span>DEFAULT_GRAILS_BASE_URL
&nbsp;
<span style="color: #804040;">function</span><span style="color: #000000;">!</span> Grails_eval_vsplit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #25BB4D;">range</span>
  <span style="color: #804040;">let</span> temp_source = s<span style="color: #000000;">:</span>copy_groovy_buffer_to_temp<span style="color: #000000;">&#40;</span>a<span style="color: #000000;">:</span>firstline, a<span style="color: #000000;">:</span>lastline<span style="color: #000000;">&#41;</span>
  <span style="color: #804040;">let</span> temp_file = s<span style="color: #000000;">:</span>select_new_temp_buffer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
  <span style="color: #804040;">if</span> <span style="color: #25BB4D;">strlen</span><span style="color: #000000;">&#40;</span>g<span style="color: #000000;">:</span>grails_user<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&gt;</span> <span style="color: #000000; font-weight:bold;">0</span><span style="color: #adadad; font-style: italic;">
      &quot; replace current buffer with grails' output</span>
      silent <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">&quot;:%! postCode.groovy -u &quot;</span> <span style="color: #000000;">.</span> g<span style="color: #000000;">:</span>grails_user <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; -p &quot;</span> <span style="color: #000000;">.</span> g<span style="color: #000000;">:</span>grails_password <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; -b &quot;</span> <span style="color: #000000;">.</span> g<span style="color: #000000;">:</span>grails_base_url <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; &quot;</span> <span style="color: #000000;">.</span> temp_source <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; 2&gt;&amp;1 &quot;</span>
  <span style="color: #804040;">else</span> 
      silent <span style="color: #804040;">execute</span> <span style="color: #C5A22D;">&quot;:%! postCode.groovy -b &quot;</span> <span style="color: #000000;">.</span> g<span style="color: #000000;">:</span>grails_base_url <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; &quot;</span> <span style="color: #000000;">.</span> temp_source <span style="color: #000000;">.</span> <span style="color: #C5A22D;">&quot; 2&gt;&amp;1 &quot;</span>
  <span style="color: #804040;">endif</span>
&nbsp;
  wincmd p<span style="color: #adadad; font-style: italic;"> &quot; change back to the source buffer</span>
endfunction
&nbsp;
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy vmap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F8&gt;</span> <span style="color: #000000;">:</span><span style="color: #804040;">call</span> Grails_eval_vsplit<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #668080;">&lt;CR&gt;</span>
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy nmap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F8&gt;</span> mzggVG<span style="color: #000000;">&lt;</span>F8<span style="color: #000000;">&gt;</span>`z
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy imap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #668080;">&lt;F8&gt;</span> <span style="color: #000000;">&lt;</span>Esc<span style="color: #000000;">&gt;&lt;</span>F8<span style="color: #000000;">&gt;</span>a
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy <span style="color: #804040;">map</span> <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #000000;">&lt;</span>S<span style="color: #000000;">-</span>F8<span style="color: #000000;">&gt;</span> <span style="color: #000000;">:</span>wincmd P<span style="color: #000000;">&lt;</span>CR<span style="color: #000000;">&gt;:</span>q<span style="color: #000000;">&lt;</span>CR<span style="color: #000000;">&gt;</span>
<span style="color: #804040;">au</span> <span style="color: #25BB4D;">BufNewFile</span>,<span style="color: #25BB4D;">BufRead</span> <span style="color: #000000;">*.</span>groovy imap <span style="color: #668080;">&lt;silent&gt;</span> <span style="color: #000000;">&lt;</span>S<span style="color: #000000;">-</span>F8<span style="color: #000000;">&gt;</span> <span style="color: #000000;">&lt;</span>Esc<span style="color: #000000;">&gt;&lt;</span>S<span style="color: #000000;">-</span>F8<span style="color: #000000;">&gt;</span>a</pre></div></div>

<p>If you&#8217;re not a Vim user, it should be easy to integrate with any other editor that allows you to pass the current file to a shell command and display the results.  I went a little extra with the vimscript to allow it to post the current buffer (or selected lines within that buffer) without having to save to disk first.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/04/25/replace-grails-console-with-the-editor-of-your-choice/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Creating New Instances of Spring &#8220;Singleton&#8221; Beans with Grails BeanBuilder</title>
		<link>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/</link>
		<comments>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 05:11:41 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=301</guid>
		<description><![CDATA[When I&#8217;m integration testing Grails service classes, I often want to mock off a part of the class so that a complicated code branch isn&#8217;t followed that I&#8217;m not trying to test. Grails will helpfully inject fully autowired Spring service beans into my test if I ask for them. Unfortunately, if I change the metaClass [...]]]></description>
			<content:encoded><![CDATA[<p>When I&#8217;m integration testing Grails service classes, I often want to mock off a part of the class so that a complicated code branch isn&#8217;t followed that I&#8217;m not trying to test.</p>
<p>Grails will helpfully inject fully autowired Spring service beans into my test if I ask for them.  Unfortunately, if I change the metaClass of the injected service, that change persists beyond where we want it to:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> MyService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #ff0000;">&quot;unmodified&quot;</span> <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyServiceTests <span style="color: #000000; font-weight: bold;">extends</span> GroovyTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myService  <span style="color: #808080; font-style: italic;">// injected automatically by spring/grails</span>
&nbsp;
    <span style="color: #993333;">void</span> testOne<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        myService.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">myMethod</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;modified&quot;</span> <span style="color: #66cc66;">&#125;</span>
        assertEquals <span style="color: #ff0000;">&quot;modified&quot;</span>, myService.<span style="color: #006600;">myMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span> 
&nbsp;
    <span style="color: #993333;">void</span> testTwo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        assertEquals  <span style="color: #ff0000;">&quot;unmodified&quot;</span>, myService.<span style="color: #006600;">myMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">// WTF!  Returns &quot;modified&quot;, pollution from first test</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Grails services are Spring &#8220;singleton&#8221; objects.  They&#8217;re not true singletons though, singleton&#8217;s are just cached in the application context and returned whenever <code>getBean</code> is called.  </p>
<p>Historically, if I wanted to mock part of my service manually, I&#8217;d need to &#8220;new&#8221; up my own instance of the service and manually inject any dependencies that the service might need to function.   This is both painful and fragile, if the service adds or removes dependencies, chances are that the tests are going to break.</p>
<p>I realized that if I could ask Grails/Spring for a new instance of the &#8220;singleton&#8221; service that I could muck with it all I wanted in my test without worrying about polluting other tests with my changes.   After some digging into the grails spring support, I came up with the following method that could be added to an integration test (or integration test base class):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// spring &quot;singleton&quot; objects really aren't they're just cached by their application context</span>
<span style="color: #000000; font-weight: bold;">def</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #aaaadd; font-weight: bold;">String</span> beanName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;prototype${clazz.name}&quot;</span>
    BeanBuilder beanBuilder <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BeanBuilder<span style="color: #66cc66;">&#40;</span>ApplicationHolder.<span style="color: #006600;">application</span>.<span style="color: #006600;">mainContext</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    beanBuilder.<span style="color: #006600;">beans</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #ff0000;">&quot;$beanName&quot;</span><span style="color: #66cc66;">&#40;</span>clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> bean <span style="color: #66cc66;">-&gt;</span>
            bean.<span style="color: #006600;">autowire</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'byName'</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    beanBuilder.<span style="color: #006600;">createApplicationContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getBean</span><span style="color: #66cc66;">&#40;</span>beanName<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This method uses the BeanBuilder to construct a temporary ApplicationContext with the Grails mainContext as a parent so that other dependencies can be resolved.   This method won&#8217;t work if your service has changes to how it&#8217;s wired up and configured by spring, but the majority of Grails service classes are simply autowired <code>byName</code>.</p>
<p>Here&#8217;s a more detailed example of use.  Given this service:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyService <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> injectedService
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> serviceMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> otherMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> otherMethod<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #ff0000;">&quot;original value&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>I&#8217;m able to generate per-test autowired instances of my service and mock out <code>otherMethod</code> without polluting other tests (or the Spring injected version of the bean):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #a1a100;">com.example</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.spring.BeanBuilder</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">org.codehaus.groovy.grails.commons.ApplicationHolder</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyServiceTests <span style="color: #000000; font-weight: bold;">extends</span> GroovyTestCase <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> myServiceInstance  <span style="color: #808080; font-style: italic;">// our new</span>
    <span style="color: #000000; font-weight: bold;">def</span> myService <span style="color: #808080; font-style: italic;">// spring injected version</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> setUp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">setUp</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        myServiceInstance <span style="color: #66cc66;">=</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> tearDown<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">tearDown</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">// spring &quot;singleton&quot; objects really aren't they're just cached by their application context</span>
    <span style="color: #000000; font-weight: bold;">def</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">Class</span> clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #aaaadd; font-weight: bold;">String</span> beanName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;prototype${clazz.name}&quot;</span>
        BeanBuilder beanBuilder <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BeanBuilder<span style="color: #66cc66;">&#40;</span>ApplicationHolder.<span style="color: #006600;">application</span>.<span style="color: #006600;">mainContext</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        beanBuilder.<span style="color: #006600;">beans</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #ff0000;">&quot;$beanName&quot;</span><span style="color: #66cc66;">&#40;</span>clazz<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> bean <span style="color: #66cc66;">-&gt;</span>
                bean.<span style="color: #006600;">autowire</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'byName'</span>
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        beanBuilder.<span style="color: #006600;">createApplicationContext</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getBean</span><span style="color: #66cc66;">&#40;</span>beanName<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testNewSingletonInstance<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        assertNotNull myServiceInstance <span style="color: #808080; font-style: italic;">// created uniquely for this test in setUp</span>
        assertNotNull myService         <span style="color: #808080; font-style: italic;">// spring injected into integration test</span>
&nbsp;
        assertNotSame myService, myServiceInstance
&nbsp;
        <span style="color: #808080; font-style: italic;">// we've got unique instances of MyService, but both are injected with the same singleton dependencies</span>
        assertSame myService.<span style="color: #006600;">injectedService</span>, myServiceInstance.<span style="color: #006600;">injectedService</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> testMessingWithMetaClassDoesNotAffectOriginalSingleton<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        myServiceInstance.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">otherMethod</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">-&gt;</span> <span style="color: #ff0000;">&quot;new value&quot;</span> <span style="color: #66cc66;">&#125;</span>
&nbsp;
        assertEquals <span style="color: #ff0000;">&quot;new value&quot;</span>, myServiceInstance.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        assertEquals <span style="color: #ff0000;">&quot;original value&quot;</span>, myService.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">def</span> anotherInstance <span style="color: #66cc66;">=</span> getNewSingletonInstanceOf<span style="color: #66cc66;">&#40;</span>MyService<span style="color: #66cc66;">&#41;</span>
&nbsp;
        assertEquals <span style="color: #ff0000;">&quot;original value&quot;</span>, anotherInstance.<span style="color: #006600;">serviceMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Using this technique lets you leverage Spring&#8217;s autowiring in your tests, but also gives you the flexibility to override areas not under test to improve test readability and maintainability.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/03/07/creating-new-instances-of-spring-singleton-beans-with-grails-beanbuilder/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using the Grails BeanBuilder to Set Arbitrary Properties From an External Config</title>
		<link>http://naleid.com/blog/2011/02/25/using-the-grails-beanbuilder-to-set-arbitrary-properties-from-an-external-config/</link>
		<comments>http://naleid.com/blog/2011/02/25/using-the-grails-beanbuilder-to-set-arbitrary-properties-from-an-external-config/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 04:42:39 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[redis]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=285</guid>
		<description><![CDATA[I&#8217;m working with an existing library (Jedis a Redis client library) that has a fairly complicated connection pool config file with a large variety of potential properties that could be worth setting depending on the environment that my Grails app is running in. I wanted the ability to define the set of properties that I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working with an existing library (<a href="https://github.com/xetorthio/jedis" onclick="pageTracker._trackPageview('/outgoing/github.com/xetorthio/jedis?referer=');">Jedis</a> a <a href="http://redis.io" onclick="pageTracker._trackPageview('/outgoing/redis.io?referer=');">Redis</a> client library) that has a <a href="https://github.com/xetorthio/jedis/blob/master/src/main/java/redis/clients/jedis/JedisPoolConfig.java" onclick="pageTracker._trackPageview('/outgoing/github.com/xetorthio/jedis/blob/master/src/main/java/redis/clients/jedis/JedisPoolConfig.java?referer=');">fairly complicated connection pool config file with a large variety of potential properties</a> that could be worth setting depending on the environment that my Grails app is running in.</p>
<p>I wanted the ability to define the set of properties that I wanted to override in the config file without having to call them all out explicitly in the Spring resources.groovy file.   If I missed one, or if the client library that I&#8217;m using added a new one that I don&#8217;t notice, I don&#8217;t want to have to release a new version of the code just to set it.</p>
<p>Grails allows you to load external config files simply by defining a reference to them in Config.groovy (this code is even commented out in the default Config.groovy file that gets generated automatically with a new grails app):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">grails.<span style="color: #006600;">config</span>.<span style="color: #006600;">locations</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;file:${userHome}/.grails/${appName}-config.groovy&quot;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>After a little playing around with the BeanBuilder syntax, I was able to come up with a solution that lets me set whatever values I want in the Config file and have them set on the bean that I have Spring/Grails build.</p>
<p>If you have a config like this:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">foo <span style="color: #66cc66;">&#123;</span>
    foo <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;bar&quot;</span>
    baz <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">4</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>You can populate your resources.groovy with something like this to set whatever<br />
values are set in your config file:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">beans <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">def</span> fooMap <span style="color: #66cc66;">=</span> application.<span style="color: #006600;">config</span><span style="color: #66cc66;">?</span>.<span style="color: #006600;">foo</span>
&nbsp;
    fooBean<span style="color: #66cc66;">&#40;</span>Foo<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        fooMap<span style="color: #66cc66;">?</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span> key, value <span style="color: #66cc66;">-&gt;</span>
            delegate.<span style="color: #006600;">setProperty</span><span style="color: #66cc66;">&#40;</span>key, value<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This will make a bean that has it&#8217;s `foo` set to &#8220;bar&#8221; and it&#8217;s `baz` set to 4.   </p>
<p>Later, if I find that I need to set the `baz` property on the fooBean in production, I just add that in my config file and everything works without any code changes.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2011/02/25/using-the-grails-beanbuilder-to-set-arbitrary-properties-from-an-external-config/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

