Saturday, August 30, 2008

The web and the tools to use: From inception to perfection (Part 1)

So this entry is going to over several parts. It is basically a summarized brain dump of what i have learned over the last 12 years in web based development. It will really concentrate on the tools i use and why i use them.


Java Platform
: (http://java.sun.com/)
I have used Java for over 10 years now and i know it inside out. Admittedly the actual language is getting a bit old in the tooth now but the power of Java is so much more than the language. The reason i use Java is because i am buying into an entire eco-system of tools and frameworks that have evolved over the last 10 years plus and enormous web community of people who have used them. There is a Java library for anything you want to do on the web. So what does this mean for me... Well to put it in one word "Time". To put it in two words "Time Saving". As in saving me time by me not needing to write plumbing code when i start creating a new app. I know if i embark on a new Web project that some nice person somewhere has created a java library that does something i need. Some of my most top used java Libraries (frameworks) are:
  • Hibernate (awesome Object relational mapping ORM framework)
  • Spring (Glue code, provides a framework for easily wiring diverse components together in a consistent manner. But that is just scrapping the surface. This framework provides so much more)
  • Sitemesh (allows me to decorate my web page easily)
  • Quartz (scheduling framework)
  • IText (Pdf manipulation framework)
  • Apache Commons (enormous set of utility classes to help your everyday coding needs)
  • and many many more...

Groovy and Grails
Well where do i start with Groovy and Grails... Well first there is a difference... Groovy is a new language (about 4 years old) and Grails is a full stack web based framework. Now when i say Java saves me time i can multiply that by magnitudes when i use Grails and Groovy.

First Groovy: (http://groovy.codehaus.org/)
Essentially Groovy is Java and Java is Groovy. Well what i mean to say is the compiled version are binary compatible but the as far as the source is concerned in many ways they are in different leagues. Essentially Groovy injects magic into Java turning it from essentially static to dynamic. Without going into details it lets you do what you did already but faster, so much faster. Here are some Java Vs Groovy highlights:

Java:
List list = new ArrayList();
list.add("one");
list.add("two");
System.out.println(list)
list.add("three");

Groovy:
def list = ["one","two"]
println list
list << "three" println list

Java:
Map map = new HashMap();
map.put("name","peter")
map.put("age","30")
System.out.println(map)

Groovy:
println ["name":"peter","age":"30"]

Java:
String name = "peter";
String message = "Hello " + name + " how are you"; System.out.println(message)

Groovy:
def name = "Peter"
def message = "Hello $name how are you"
println message


Now Grails:(http://grails.org/)
Grails to be fair took a lot of inspiration from Ruby on Rails... However it has since evolved into a framework so much more powerful than anything else available in its domain. So this is Grails in a nutshell as i see it:

1) First: Take all the best tried and tested Java frameworks for full stack web development available today (Spring, Spring mvc, Spring webflow, Hibernate, sitemesh, quartz, commons)

2) Wire them all together in the most elegant way. Throwing in some magic custom code.

3) Sprinkle Groovy all the way through the framework and make it the language to use to control and manipulate the framework.

3) Throw in a built application server Jetty and an in memory database (Hypersonic)

4) Finally Apply strict agile principals to the framework just as DRY (Don't Repeat Yourself) , COC (Convention over configuration), domain driven development, test infected.

What you end up with is nothing short of a "Revolutionary" new Full Stack web framework. The Holy Grail.

The way i see it all these technologies existed independently on there own. However the Grails guys bring them together in such a unique way the something Magical happens. Grails really is more than the sum of all its parts. A bit like Xtream Programming is.

Here are some of the high lights for me:

Convention over configuration
This saves me untold amount of time. Grails has a place for everything well everything you need. Domain objects, controller, views, services

GORM:
This is grails ORM. It used hibernate underneath but provides a dynamic groovy interface over the top of it.

Save an object:

book.save()

query and object

Book.findAllByAuthor(author)

Those methods don't exist at compile time they are added at runtime.

GSP:
Similar to jsp but exponentially more powerful.

Much much more...

I will leave it there for now: next in part 2 i will talk about

Firefox and firebug and friends

Tuesday, August 26, 2008

Return the collection from a many to many relationship in grails

I thought i would blog about this one so that it is recorded somewhere. It is a tricky problem but is so easily solved with groovy.

Say i have a many to many relationship between User and Account that is controlled by the class AccountAccess. This would be my domain:

class User {

static hasMany = [accountAccesses:AccountAccess]

Date dateCreated
Date lastUpdated
String name;

}

class Account {

static hasMany = [accountAccesses:AccountAccess]

Date dateCreated
Date lastUpdated
String name

}

class AccountAccess {

static belongsTo = User;

Date dateCreated
Date lastUpdated

User user;
Account account;

}


If i wanted to get all the Accounts a user has access to i can do this:

def accounts = user.accountAccesses.collect {it.account}


It uses the collect closure of groovy. I iterate through the AccountAccess objects and add the Account object referenced by each one. The end result is i get a list of the Accounts a user has access to.

Hope this helps

Using Back References with String replaceAll method

This is small problem i was facing the other day and couldn't find much information about it on the web so thought i would blog about it.

The problem is how to use back references in Java regular expressions.

The problem is this. Say i have a String like so

"orderM8orderA3orderX2NoReturn"

and i want to turn it into a String like so:

"order M#8 order A#3 orderX#2 NoReturn"

i can do this:

String test = "orderM8orderA3orderX2NoReturn";

String replaced = test.replaceAll("([A-Z])([0-9])", " $1#$2 ");

What happens here is:

  1. first create two regular expression for matching all capital letters [A-Z] and all single digits [0-9]
  2. Next i then put each of these in a group. using ( ) brackets. The grouping means that the match is remembered and can be referenced by the replace string.
  3. In the replace string i can then reference the matches via the $n notation where n = the number of the group.

So what happens is: The regular expression processor moves along the string looking for cases of a capital letter next to a digit. When it find them it stores the capital letter in a group 1 and the digit in group 2.

So i want to replace the original match with another string i can.

Also note. The whole expression is automatically added to an implicit group zero 0 that is a group of the whole expression.

String replaced = test.replaceAll("([A-Z])([0-9])", " '$0' ");

will give

order 'M8' order 'A3' order 'X2' NoReturn
IMPORTANT NOTE:

The javadoc says that you reference back references with '\n' (were n = number) but that is not true. That does not work you need to use '$n'. The javadoc is wrong and needs to be updated.

Pattern javadoc

Hope this helps :)

Thursday, August 21, 2008

Amazon put the icing on the cake...

I received an email to from Amazon web service. This was the title:

"Amazon Elastic Block Store for Amazon EC2 is Now Available"

Yes finally after a long long wait amazon have finally released a persistence storage service for EC2. If you are not familiar with Amazon webservices check this out.

Amazon Web services

Basically amazon now provides a set of services to allow host all your new "next big thing" websites ideas on. And best of all they are pay as you go.

These are the main ones:
Amazon Elastic Compute Cloud
Amazon SimpleDB
Amazon Simple Storage Service
Amazon Simple Queue Service

So the top of the list is EC2 the "Elastic Compute Cloud". Basically this service is a pay as you go virtual hosting service. You open an account, take an off the shelf linux image and press run.

Then you effectively
have your hosting platform for your new website that you can connect to via ssh. You then configure it with your new application needs. (mysql, java, tomcat, apache, etc) then take a snapshot of the image. When you need to scale just turn on the new image instances. But only PAY AS YOU SCALE.

Anyway one of the reasons i was holding back on EC2 was that is kind of a an in memory instance. This means that if the instance goes down for any reason then you loose everything. This means if you have a mysql database running then you loose the data too. This is not a good thing right. Well until now the only options you had to get around this was some 3rd party companies that offer fail over and periodical back of the the database. Here are some listed below:

http://www.persistentfs.com/
http://www.elasticdrive.com/
http://www.rightscale.com

Of which http://www.rightscale.com seems to be the best. However this costs $2500 setup and $500 per month plus the cost of running multiple ec2 instances. Now this is a lot of money for a one man startup.

So along comes Elastic Block Store

This means you can setup your EC2 instances now with persistence storage. Ideal for a database. Whats more you only pay for what you need which is currently $0.10 per GB per month. Smiley faces all around...

Anyway i am going to get this setup and give it ago so it will be ready for my next web startup venture which is all most built. Stay tuned.