RSS
 

Archive for the ‘Groovy’ Category

Cloud Foundry + Grails = lightning fast deployment

07 Jan

The purpose of this article is to show how quickly and easily deploy Grails application to Cloud Foundry platform.

What is Cloud Foundry?

From cloudfoundry.com:

Cloud Foundry is an open platform as a service, providing a choice of clouds, developer frameworks and application services. Initiated by VMware, with broad industry support, Cloud Foundry makes it faster and easier to build, test, deploy and scale applications. It is an open source project and is available through a variety of private cloud distributions and public cloud instances, including CloudFoundry.com.

Prerequisites

To make a use of this article you’ll need to have Grails framework installed.

To check if Grails is properly installed, use

grails -version

You should see something like

Grails version: 2.0.0

If you haven’t, just follow the Grails Getting Started Guide.

Let’s the fun begin

The whole process is extraordinary simple! Just do the following steps.

  1. Create Grails app

    grails create-app cloud_foundry_example
    
  2. Change your working directory to a new application directory

    cd cloud_foundry_example
    
  3. Install Cloud Foundry plug-in

    grails install-plugin cloud-foundry
    
  4. Configure your Cloud Foundry credentials

    You can configure it in both grails-app/conf/Config.groovy and in ~/.grails/settings.groovy. Since the file will contain sensitive, it’s not recommended to put it in source control. That’s why the second option is considered the best practice.

    So, configure your credentials using

    grails.plugin.cloudfoundry.username = "<your_username>"
    grails.plugin.cloudfoundry.password = "<pass>"
    
  5. Test your config

    grails cf-info
    

    The output should look like

    VMware's Cloud Application Platform
    For support visit http://support.cloudfoundry.com
    Target:   http://api.cloudfoundry.com (v0.999)
    
    User:     <your_username>
    Usage:    Memory   (0B of 2.0G total)
              Services (0 of 16 total)
              Apps     (0 of 20 total)
    
  6. Deploy your app!

    grails prod cf-push
    

    You’ll be asked about the application URL, and also about some persistence services (in the time of writing: MySQL and PostgreSQL). I advise to accept default address and chose one of these services.

    If you want to re-deploy after some changes, just do

    grails prod cf-update
    

That’s it!

The app is deployed and ready to work. A proof? PartyPlanner app.

In next part I’ll describe how to configure different services in Cloud Foundry.

 
2 Comments

Posted in Grails

 

Django and Rails and Grails comparison

08 Nov

I’ve recently found a link to an interesting presentation at zenzire.com. Its author, Jaime Buelta, speaks about differences between Django, Rails and Grails frameworks.

I know Django and Grails. Personally, I like both of them. I can’t only disagree with one argument about Grails – it’s rather poorly documented. But still, it deserves to give it a shot.

Just take a look at it.

Django and Rails and Grails, Oh my! (Jaime Buelta) from whykay on Vimeo.

 

Null Value on Save Issue in Grails

22 Jul

If you’ve used Grails, you’re probably familiar with a domain class and its “constraints” block. There you can define conditions which have to be met by class’s fields. For example, you can enforce that a field have to be not-empty using “blank: false” condition. It’d be intuitive not to define conditions for the field you don’t care of. Unfortunately, there is a small trap here. Let’s use an example to explain it.

Let’s define an Invoice class with a few fields: number, draw date and payment date. The number and draw data are obligatory while the payment date isn’t, because you’re able to pay for an invoice by cash.

class Invoice {
	String number;
	Date drawDate;
	Date paymentDate;
	static constraints = {
		number(blank: false);
		drawDate(blank: false);
	}
}

It looks good, doesn’t it? You can generate controller and views for that class and test it in a browser. Try to leave a payment date field empty and save an invoice. Success – it works! OK, so let’s create a BootStrap entry for our new class. Again, try to omit its optional field – paymentDate.

def invoice = new Invoice(number: “1/2010”, drawDate: new Date()).save();

What value does invoice variable have? Null! What’s wrong? Why does it work in a browser and not directly in a code?

The answer is quite straight, but I haven’t found it in a documentation. The default, implicit value of a field’s constraint is “nullable: false”. When you fills in a form in your browser, you really sends a blank value – empty string. This string isn’t null so it meets “nullable: false” criteria. On the other hand, if you creates a new object in your code and you omits a field, you pass a null value and hence the validator doesn’t allow to create an object! Unfortunately, Grails doesn’t provide any descriptive message on what’s really going under the hood.

What can you do? You can set an explicit constraint for such a field: “nullable: true”. The complete class would be:

class Invoice {
	String number;
	Date drawDate;
	Date paymentDate;
	static constraints = {
		number(blank: false);
		drawDate(blank: false);
		paymentDate(nullable: true);
	}
}

Personally, I suggest to set “nullable: true” constraint for every field which is optional and “blank: false” for every obligatory field.

This article was previously posted at Groovy Zone.

 

Initial value of primary key in Grails

22 Jul

I’ve recently came across a small issue in my Grails application. I had to set an initial value of auto-generated identifiers of my objects. Moreover, I had to use two different values in two classes. For example, identifiers in one table in database should start from 50, while in another table – from 1000. How to get this in Grails domain classes?

You have to use SequenceStyleGenerator with its parameter – initial_value. You can do this with generator phrase in mapping clousure:

class FirstClass {
  static mapping = {
    id(generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator',
       params: [initial_value: 50])
  }
  ...
}

class SecondClass {
  static mapping = {
    id(generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator',
       params: [initial_value: 1000])
  }
  ...
}

Unfortunately, in this case you’ll finish with the same sequence generator in both classes. That means that you’ll get one sequence shared between two tables. How to avoid this? Just specify sequence name using next parameter – sequence_name:

class FirstClass {
  static mapping = {
    id(generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator',
       params: [sequence_name: 'first_seq', initial_value: 50])
  }
  ...
}

class SecondClass {
  static mapping = {
    id(generator: 'org.hibernate.id.enhanced.SequenceStyleGenerator',
       params: [sequence_name: 'second_seq', initial_value: 1000])
  }
  ...
}

What do you think about my solution? Do you know another one?


This article was previously posted at Groovy Zone.