<?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>Carsten Block&#039;s Blog</title>
	<atom:link href="http://blog.block-consult.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.block-consult.com</link>
	<description>Thoughts and Ideas from the web for the web...</description>
	<lastBuildDate>Tue, 13 Mar 2012 10:12:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Registering Custom Mongo User Types Part 2</title>
		<link>http://blog.block-consult.com/2012/03/registering-custom-mongo-user-types-part-2/</link>
		<comments>http://blog.block-consult.com/2012/03/registering-custom-mongo-user-types-part-2/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 10:12:25 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=104</guid>
		<description><![CDATA[It turns out that the registring process described in my previous blog post currently doesn&#8217;t work. The reason: Datastore mapping is already finished before Bootstrap is called. Digging through the grails-datastore-core source code I found out that AbstractMappingContextFactoryBean has a &#8230; <a href="http://blog.block-consult.com/2012/03/registering-custom-mongo-user-types-part-2/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It turns out that the registring process described in my previous blog post currently doesn&#8217;t work. The reason: Datastore mapping is already finished before Bootstrap is called. </p>
<p>Digging through the grails-datastore-core source code I found out that <code><a href="https://github.com/SpringSource/grails-data-mapping/blob/master/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/bean/factory/AbstractMappingContextFactoryBean.groovy" title="AbstractMappingContextFactoryBean" target="_blank">AbstractMappingContextFactoryBean</a></code> has a method <code>registerCustomTypeMarshallers</code> which scans all spring beans and automatically registers beans of type <code>org.grails.datastore.mapping.engine.types.CustomTypeMarshaller</code> as custom type marshallers.</p>
<p>So the simple solution for registering Custom Type Marshaller (for now) is to first write the  marshaller class and then to register it as spring bean. For the custom Birthday type from the <a href="http://springsource.github.com/grails-data-mapping/mongo/manual/guide/3.%20Mapping%20Domain%20Classes%20to%20Mongo%20Collections.html#3.6%20Custom%20User%20Types" title="mongodb plugin documentation" target="_blank">mongodb plugin documentation</a> this would be:</p>
<p>In grails-app/conf/spring/resources.groovy:</p>
<pre class="brush: groovy; title: ;">
// Place your Spring DSL code here
beans = {
  customBirthdayType(CustomBirthdayType)
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2012/03/registering-custom-mongo-user-types-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Registering Custom Mongo User Type during Grails Bootstrap</title>
		<link>http://blog.block-consult.com/2012/02/registering-custom-mongo-user-type-during-grails-bootstrap/</link>
		<comments>http://blog.block-consult.com/2012/02/registering-custom-mongo-user-type-during-grails-bootstrap/#comments</comments>
		<pubDate>Wed, 15 Feb 2012 09:56:51 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=96</guid>
		<description><![CDATA[Short note to self for future reference: Creating custom user types for grails mongodb plugin is nicely desribed in the user guide. Actually registering it is not. So here we go&#8230; In grails-app/conf/Bootstrap.groovy: class BootStrap { MongoDatastore mongoDatastore def init &#8230; <a href="http://blog.block-consult.com/2012/02/registering-custom-mongo-user-type-during-grails-bootstrap/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Short note to self for future reference: Creating custom user types for <a href="http://grails.org/plugin/mongodb" title="Grails MongoDB Plugin" target="_blank">grails mongodb plugin</a> is <a href="http://springsource.github.com/grails-data-mapping/mongo/manual/guide/3.%20Mapping%20Domain%20Classes%20to%20Mongo%20Collections.html#3.6%20Custom%20User%20Types" title="Grails MongoDB Plugin Documentation" target="_blank">nicely desribed</a> in the user guide. Actually registering it is not. So here we go&#8230;</p>
<p>In grails-app/conf/Bootstrap.groovy:</p>
<pre class="brush: groovy; title: ;">
class BootStrap {
  MongoDatastore mongoDatastore

  def init = { servletContext -&gt;
   //register sample user type from plugin documentation
   mongoDatastore.mappingContext.mappingFactory.registerCustomType(new BirthdayType())
  }
  def destroy = {
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2012/02/registering-custom-mongo-user-type-during-grails-bootstrap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Update: Concurrent user sessions management in grails 2 using spring-security-core plugin</title>
		<link>http://blog.block-consult.com/2012/01/update-concurrent-user-sessions-management-in-grails-2-using-spring-security-core-plugin/</link>
		<comments>http://blog.block-consult.com/2012/01/update-concurrent-user-sessions-management-in-grails-2-using-spring-security-core-plugin/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 19:56:50 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=89</guid>
		<description><![CDATA[While the approach towards managing concurrent users sessions in grails 2 using Spring Security Plugin described in my previous post works well, I just noticed that the configuration breaks some of the custom config options from Spring Security Core plugin. &#8230; <a href="http://blog.block-consult.com/2012/01/update-concurrent-user-sessions-management-in-grails-2-using-spring-security-core-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While the approach towards managing concurrent users sessions in grails 2 using Spring Security Plugin described in my <a href="http://blog.block-consult.com/2012/01/restricting-concurrent-user-sessions-in-grails-2-using-spring-security-core-plugin/" title="Restricting concurrent user sessions in grails 2 using spring-security-core plugin">previous post</a> works well, I just noticed that the configuration breaks some of the custom config options from Spring Security Core plugin.</p>
<p>The reason for that is simple: We replaced the <code>authenticationProcessingFilter</code> that comes with Spring Security Core plugin with our custom implementation in order to set our concurrentSessionControlStrategy:</p>
<pre class="brush: groovy; title: ;">
authenticationProcessingFilter(UsernamePasswordAuthenticationFilter) {
  sessionAuthenticationStrategy=concurrentSessionControlStrategy
  authenticationManager=ref(&quot;authenticationManager&quot;)
}
</pre>
<p>A more elegant way to achieve the same is to <strong>not</strong> redefine the complete bean in resources.groovy but instead to only set the sessionAuthenticationStrategy in Boostrap.groovy.<br />
For your reference, here is how the updated <code>resources.groovy</code> looks like:</p>
<pre class="brush: groovy; title: ;">
import org.springframework.security.core.session.SessionRegistryImpl
import org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy
import org.springframework.security.web.session.ConcurrentSessionFilter

beans = {
  sessionRegistry(SessionRegistryImpl)

  concurrencyFilter(ConcurrentSessionFilter) {
    sessionRegistry = sessionRegistry
    logoutHandlers = [ref(&quot;rememberMeServices&quot;), ref(&quot;securityContextLogoutHandler&quot;)]
    expiredUrl='/login/concurrentSession'
  }
  concurrentSessionControlStrategy(ConcurrentSessionControlStrategy, sessionRegistry) {
    alwaysCreateSession = true
    exceptionIfMaximumExceeded = false
    maximumSessions = 1
  }
  /*
  authenticationProcessingFilter(UsernamePasswordAuthenticationFilter) {
    sessionAuthenticationStrategy=concurrentSessionControlStrategy
    authenticationManager=ref(&quot;authenticationManager&quot;)
  }
  */
}
</pre>
<p>And here is the updated Bootstrap.groovy:</p>
<pre class="brush: groovy; title: ;">
class BootStrap {

  def authenticationManager
  def concurrentSessionController
  def securityContextPersistenceFilter
  def authenticationProcessingFilter
  def concurrentSessionControlStrategy

  def init = { servletContext -&gt;
    SpringSecurityUtils.clientRegisterFilter('concurrencyFilter', SecurityFilterPosition.CONCURRENT_SESSION_FILTER)
    authenticationProcessingFilter.sessionAuthenticationStrategy = concurrentSessionControlStrategy
  }

  def destroy = {
  }
}
</pre>
<p>Now all spring security related config options from Config.groovy are left untouched and only out authentication processing strategy is updated.</p>
<p>I&#8217;ll put all this into a short demo app and then publish it on github as soon as I have time for it again&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2012/01/update-concurrent-user-sessions-management-in-grails-2-using-spring-security-core-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Restricting concurrent user sessions in grails 2 using spring-security-core plugin</title>
		<link>http://blog.block-consult.com/2012/01/restricting-concurrent-user-sessions-in-grails-2-using-spring-security-core-plugin/</link>
		<comments>http://blog.block-consult.com/2012/01/restricting-concurrent-user-sessions-in-grails-2-using-spring-security-core-plugin/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 15:13:53 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=67</guid>
		<description><![CDATA[A common requirement in web application development is to restrict multiple concurrent user logins. In other words, a person who subscribed to your shiny new web app using the cheap single-user-subscription should not be able to login from several different &#8230; <a href="http://blog.block-consult.com/2012/01/restricting-concurrent-user-sessions-in-grails-2-using-spring-security-core-plugin/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A common requirement in web application development is to restrict multiple concurrent user logins. In other words, a person who subscribed to your shiny new web app using the cheap single-user-subscription should not be able to login from several different computers at the same time.</p>
<p>Let&#8217;s see who we can implement that functionality using <a href="http://blog.springsource.org/2011/12/15/grails-2-0-released/" title="Grails 2.0" target="_blank">Grails 2.0</a> and <a href="http://grails.org/plugin/spring-security-core" title="Grails Spring Security Core Plugin" target="_blank">Spring Security Core Plugin</a> (v1.2.7.1, which uses <a href="http://static.springsource.org/spring-security/site/" title="Spring Security" target="_blank">Spring Security 3.0.7</a> under the hood). While digging for extisting solutions I cam across several slightly outdated posts on the grais mailing lists (<a title="post1" href="http://grails.markmail.org/message/26ltkjjuegvuuycm?q=ConcurrentSessionFilter" target="_blank">post1</a> <a title="post2" href="http://grails.1312388.n4.nabble.com/dealing-with-concurrent-session-in-acegi-and-grails-td1356904.html#a1356906" target="_blank">post2</a> <a title="post3" href="http://grails.1312388.n4.nabble.com/Spring-Security-Plugin-and-concurrent-sessions-lt-lt-Solved-gt-gt-td3276336.html" target="_blank">post3</a>), an unanswered question on <a title="stackoverflow" href="http://stackoverflow.com/questions/4571010/grails-spring-security-core-plugin-how-do-i-limit-concurrent-sessions" target="_blank">stackoverflow</a>, as well as the excellent <a title="Spring Security 3" href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/session-mgmt.html" target="_blank">Spring Security 3</a> and <a title="Grails Spring Security Plugin" href="http://grails-plugins.github.com/grails-spring-security-core/docs/manual/guide/single.html" target="_blank">Grails Spring Security Plugin</a> documentations.</p>
<p>Based on all this I worked my way towards the solution described below.</p>
<p>First we need to enable the <a title="HttpSessionEventPublisher" href="http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/session/HttpSessionEventPublisher.html" target="_blank"><code>HttpSessionEventPublisher</code></a> in order to participate in session events. Simply add</p>
<pre class="brush: groovy; title: ;">
grails.plugins.springsecurity.useHttpSessionEventPublisher = true
</pre>
<p>to <code>Config.groovy</code> and spring security plugin takes care of the rest. As soon as the listener is registered an <code>ApplicationEvent</code> will be published to Spring <code>ApplicationContext</code> each time a <code>HttpSession</code> commences or ends. This event can then be consumed by a ConcurrentSessionFilter as described below.</p>
<p>Then, in <code>resources.groovy</code>, we define a <a title="sessionRegistry" href="http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/core/session/SessionRegistry.html" target="_blank"><code>sessionRegistry</code></a> bean (refering to the default <code>SessionRegistryImpl</code> provided by Spring Security), a <a title="concurrencyFilter" href="http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/session/ConcurrentSessionFilter.html" target="_blank"><code>concurrencyFilter</code></a> bean, and a <a title="concurrentSessionControlStrategy" href="http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/session/ConcurrentSessionControlStrategy.html" target="_blank"><code>concurrentSessionControlStrategy</code></a> bean.</p>
<pre class="brush: groovy; title: ;">
import org.springframework.security.core.session.SessionRegistryImpl
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
import org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy
import org.springframework.security.web.session.ConcurrentSessionFilter

beans = {
  sessionRegistry(SessionRegistryImpl)

  concurrencyFilter(ConcurrentSessionFilter) {
    sessionRegistry = sessionRegistry
    logoutHandlers = [ref(&quot;rememberMeServices&quot;), ref(&quot;securityContextLogoutHandler&quot;)]
    expiredUrl='/login/concurrentSession'
  }
  concurrentSessionControlStrategy(ConcurrentSessionControlStrategy, sessionRegistry) {
    alwaysCreateSession = true
    exceptionIfMaximumExceeded = false
    maximumSessions = 1
  }
  // Better not to override the bean but to just set the strategy in Bootstrap.groovy (Details  below)
  /*
  authenticationProcessingFilter(UsernamePasswordAuthenticationFilter) {
    sessionAuthenticationStrategy=concurrentSessionControlStrategy
    authenticationManager=ref(&quot;authenticationManager&quot;)
  }
  */
}
</pre>
<p>The <strong><code>concurrentSessionControl</code></strong> strategy allows us to (1) define the maximum permitted number of concurrent sessions a user is allowed to have, and (2) the consequences if this number is exceeded by adjusting <strong><code>exceptionIfMaximumExceeded</code></strong>. It this property is set to false, old sessions of a user are quietly invalidated as soon as he creates a new session (i.e. he logs in on another computer). Otherwise the new login attempt would fail with an exception as long as other valid sessions exist.</p>
<p>The <strong><code>concurrencyFilter</code></strong> does two things. (1) Update <strong><code>sessionRegistry</code></strong> so that the registered session&#8217;s &#8220;last update&#8221; time is always correct, and (2) Check if the session is marked as expired and &#8211; if that is the case &#8211; call the configured <code>logoutHandlers</code>.</p>
<p><strong>Note:</strong> We explicitly configure <code>[ref("rememberMeServices"), ref("securityContextLogoutHandler")]</code> as logoutHandlers because by default Spring Security would inject only the <code>securityContextLogoutHandler</code> into the filter.<br />
Why is that important? If we leave the defaults untouched the following can happen:<br />
User Alice logs in on computer A with &#8220;remember-me&#8221;-box on the default login page checked. Afterwards Alice also logs in on computer B using the same credentials as before. Upon login on computer B our <code>securityContextLogoutHandler</code> invalidates Alice&#8217;s session for computer A and then lets her proceed. If Alice now returns to computer A her session is invalid so that the system triggers a new login. But as it finds a valid remember-me cookie it automatically re-authenticates alice and lets her proceed on Computer A too as long as the cookie remains valid. Clearly not what we wanted to achieve&#8230; So always remember to register  <code>rememberMeServices</code> as logout handler too whenever your system uses cookie based authentication.  </p>
<p><del datetime="2012-01-20T19:57:09+00:00">Finally, we override <code>authenticationProcessingFilter</code> (which ships readily configured with spring security core plugin) so that it uses our custom <code>sessionAuthenticationStrategy</code>.<br />
Note that we use the name <strong><code>authenticationProcessingFilter</code></strong> although in Spring Security 3 this class was renamed into <strong><code><a href="http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/UsernamePasswordAuthenticationFilter.html" title="UsernamePasswordAuthenticationFilter" target="_blank">UsernamePasswordAuthenticationFilter</a></code></strong>. But as the spring-security-core plugin (v1.2.7.1) still uses the old name in its default filter chain and as we want to override that particular bean with our new configuration we stick to the old bean name.</del><br />
<strong>Update:</strong> Overwriting this bean would break some of the Spring Security Plugin config options. A better approach is to just set the <code>sessionAuthenticationStrategy</code> in Bootstrap.grooby <a href="http://blog.block-consult.com/2012/01/update-concurrent-user-sessions-management-in-grails-2-using-spring-security-core-plugin/" title="Update: Concurrent user sessions management in grails 2 using spring-security-core plugin" target="_blank">as described in my other blog post.</a>. </p>
<p>And that&#8217;s pretty much it!! As a very last step we register our <code>concurrencyFilter</code> in the spring security filter chain and then we&#8217;re done.</p>
<pre class="brush: groovy; title: ;">
class BootStrap {

  def authenticationManager
  def concurrentSessionController
  def securityContextPersistenceFilter

  def init = { servletContext -&gt;
    SpringSecurityUtils.clientRegisterFilter('concurrencyFilter', SecurityFilterPosition.CONCURRENT_SESSION_FILTER)
  }

  def destroy = {
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2012/01/restricting-concurrent-user-sessions-in-grails-2-using-spring-security-core-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inject springSecurityService into grails domain class for controller unit testing</title>
		<link>http://blog.block-consult.com/2011/08/inject-spring-security-service-into-domain-class-for-controller-unit-testing/</link>
		<comments>http://blog.block-consult.com/2011/08/inject-spring-security-service-into-domain-class-for-controller-unit-testing/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 07:55:01 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=53</guid>
		<description><![CDATA[With grails Spring Security Core plugin release 1.2 password encryption logic shifted into the Person domain class. This is great as it simplifies password handling. Still its not totally obvious on how to unit test these domain classes and accompanying &#8230; <a href="http://blog.block-consult.com/2011/08/inject-spring-security-service-into-domain-class-for-controller-unit-testing/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>With grails <a title="Spring Security Core Plugin" href="http://grails.org/plugin/spring-security-core" target="_blank">Spring Security Core</a> plugin release 1.2 password encryption logic shifted into the Person domain class. This is great as it simplifies password handling. Still its not totally obvious on how to unit test these domain classes and accompanying controllers. </p>
<p>In particular, an instance of <code>grails.plugins.springsecurity.SpringSecurityService</code> needs to be injected into Person domain class in order for password encryption to work properly. But with the new unit testing framework introduced in grails 2 this is easy to accomplish:</p>
<p>The trick is to inject spring security service into the unit test:</p>
<pre class="brush: groovy; title: ;">
defineBeans {
  springSecurityService(SpringSecurityService)
}
</pre>
<p>Also we need to mock Person domain class and PersonController manually <em>after</em> the springSecurityService bean has been defined. In the scaffolded controller unit test we thus replace the <code>@TestFor(PersonController)</code> and <code>@Mock(Person)</code> annotation with <code>@TestMixin([ControllerUnitTestMixin,DomainClassUnitTestMixin])</code>.<br />
Now we can define mocked controller and domain instances inside our test methods with <code>def controller = testFor(PersonController)</code> and <code>mockDomain(Person)</code>.<br />
And that&#8217;s it! Controller and domain class now have full access to a properly injected <code>springSecurityService</code> instance. </p>
<p>Please find a complete example below:</p>
<pre class="brush: groovy; highlight: [11,12,13,14,15]; title: ;">
import grails.plugins.springsecurity.SpringSecurityService
import grails.test.mixin.TestMixin
import grails.test.mixin.domain.DomainClassUnitTestMixin
import grails.test.mixin.web.ControllerUnitTestMixin
import javax.servlet.http.HttpServletResponse

@TestMixin([ControllerUnitTestMixin,DomainClassUnitTestMixin])
class PersonControllerTests {

void testSave() {
    defineBeans {
      springSecurityService(SpringSecurityService)
    }
    mockDomain(Person)
    def controller = testFor(PersonController)
    controller.save()
    assert response.status == HttpServletResponse.SC_METHOD_NOT_ALLOWED

    response.reset()
    request.method = 'POST'
    controller.save()

    assert model.personInstance != null
    assert view == '/person/create'

    response.reset()

    params[&quot;username&quot;] = 'someUser'
    params[&quot;password&quot;] = 'somePassword'
    params[&quot;enabled&quot;] = true
    controller.save()

    assert response.redirectedUrl == '/person/show/1'
    assert controller.flash.message != null
    assert Person.count() == 1
  }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2011/08/inject-spring-security-service-into-domain-class-for-controller-unit-testing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Deploy Grails App to Cloud Foundry via Command Line</title>
		<link>http://blog.block-consult.com/2011/05/deploy-grails-app-to-cloud-foundry-via-command-line/</link>
		<comments>http://blog.block-consult.com/2011/05/deploy-grails-app-to-cloud-foundry-via-command-line/#comments</comments>
		<pubDate>Tue, 03 May 2011 09:12:09 +0000</pubDate>
		<dc:creator>cblock</dc:creator>
				<category><![CDATA[Groovy & Grails]]></category>

		<guid isPermaLink="false">http://blog.block-consult.com/?p=4</guid>
		<description><![CDATA[A few days ago I received my beta subscription for the great new Cloud Foundry PaaS Platform. I immediately tried to publish one of my Grails projects using the cloud foundry grails plugin as described in Peter Ledbrook&#8217;s excellent blog post. Single command &#8230; <a href="http://blog.block-consult.com/2011/05/deploy-grails-app-to-cloud-foundry-via-command-line/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few days ago I received my beta subscription for the great new <a href="http://cloudfoundry.com" target="_blank">Cloud Foundry</a> PaaS Platform. I immediately tried to publish one of my Grails projects using the <a title="cloud foundry grails plugin" href="grails.org/plugin/cloud-foundry" target="_blank">cloud foundry grails plugin</a> as described in Peter Ledbrook&#8217;s excellent <a title="blog post" href="http://blog.springsource.com/2011/04/12/one-step-deployment-with-grails-and-cloud-foundry/" target="_blank">blog post</a>. Single command deployment of the app works nicely though grails environment startup somewhat slows down deployment process. Also dislike keeping my cloud foundry user credentials in Config.groovy (as it increases the likelihood of accidentally exposing them through version control).<br />
<em><strong>Update:</strong></em> See Burt&#8217;s comments below on externalizing config and using grails interactive mode to address these issues. </p>
<p>That&#8217;s why I decided to use vmc command line tool instead for managing my cloud foundry instances. I didn&#8217;t find a specific grails vmc tutorial, so I figured it out on my own and now record the steps here for reference.</p>
<p>First install vmc. If you have ruby and ruby gem installed (vmc is written in ruby; go to the <a href="http://www.ruby-lang.org/en/downloads/" target="_blank">ruby download page</a> if you need to install ruby on your system) this as simple as</p>
<pre class="brush: bash; title: ;">computer:~ user$ gem install vmc</pre>
<p>Next, log in to Cloud Foundry:</p>
<pre class="brush: bash; highlight: [1,4]; title: ;">
computer:~ user$ vmc target api.cloudfoundry.com
Succesfully targeted to [http://api.cloudfoundry.com

computer:~ user$ vmc login email@myhost.com
Password: ************
Successfully logged into [http://api.cloudfoundry.com]
</pre>
<p>Now you&#8217;re ready to deploy your application. First create a grails war and then point vmc to the war directory. vmc automatically analyzes the local war file and the remotely available resources in order to only upload those resources not available on Cloud Foundry.</p>
<pre class="brush: bash; highlight: [1,2,3,8]; title: ;">
computer:~ user$ cd /my/grails/demoproject
computer:~ user$ rm -rf target/
computer:~ user$ grails prod war
Welcome to Grails 1.3.7 - http://grails.org/
[...]
Done creating WAR /my/grails/demoproject/target/demoproject-0.1-SNAPSHOT.war

computer:~ user$ vmc push demoproject --path target/
Application Deployed URL: 'demoproject.cloudfoundry.com'?
Detected a Java SpringSource Grails Application, is this correct? [Yn]:
Memory Reservation [Default:512M] (64M, 128M, 256M, 512M or 1G)
Creating Application: OK
Would you like to bind any services to 'demoproject'? [yN]: y
Would you like to use an existing provisioned service [yN]? N
The following system services are available::
1. mongodb
2. mysql
3. redis
Please select one you wish to provision: 2
Specify the name of the service [mysql-xyz]:
Creating Service: OK
Binding Service: OK
Uploading Application:
  Checking for available resources: OK
  Processing resources: OK
  Packing application: OK
  Uploading (16K): OK
Push Status: OK
Staging Application: OK
Starting Application: OK
</pre>
<p>And that&#8217;s it! Great! Some things to note, though:</p>
<p><span style="font-size: 16px; font-family: Georgia, 'Bitstream Charter', serif; line-height: 24px;"> </span></p>
<ol>
<li>vmc searches for a war file in the place specified by the <code>--path</code> parameter. That&#8217;s why you have to run <code>grails prod war</code> first.</li>
<li>vmc simply chooses the first war file it finds in the specified directory. So make sure that you delete old warfiles first</li>
<li>vmc examines the war Meta Data (manifest.mf) to determine required resources. It then uploads only those resources not yet available on Cloud Foundry, which leads to optimized upload sizes. A more detailed description of the magic in behind can be found in this <a title="blog post" href="http://blog.cloudfoundry.com/post/4737632136/what-happens-when-you-vmc-push-an-application-to-cloud" target="_blank">blog post</a>.</li>
</ol>
<p>After repeating the above steps several times I wrote a minimal shell file (<code>deploy.sh</code> in my grails app root folder) that bundles target folder cleanup, war generation, and deployment for me:</p>
<pre class="brush: bash; title: ; toolbar: true;">
#!/bin/bash
rm -rf target
grails prod war
vmc update demoproject --path target
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.block-consult.com/2011/05/deploy-grails-app-to-cloud-foundry-via-command-line/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

