Monday, December 17, 2007

Top 5 reasons why Groovy is the best dynamic language for Java

  1. Offers seamless integration with Java
  2. Groovy generates same bytecode as Java. Once compiled, Groovy classes are nothing but Java classes for the JVM.

  3. Java like syntax with flat learning curve
  4. Groovy has Java like systax. Groovy code is more concise and readable than Java code. It comes with dynamic language features that Java language is lacking. Also brings in scripting capabilities.

  5. Mix and match Groovy and Java classes
  6. A Groovy class can extend a Java class and vice versa. A Java class can implement a Groovy interface and vice versa. Groovy shares same object model, libraries ans same security model as Java.

    JetBrains "joint compiler" eliminates the need for cleverly chosing the order of compilation while mixing groovy and java classes when one extending another or one implementing another and vice versa. It offers compilation of both classes with one go without having to worry about dependencies between both types of classes.

    When compiling from command line through groovyc, just specify -j for joint compilation. For e.g.

    groovyc *.groovy *.java -j -Jsource=1.4 -Jtarget=1.4

    ANT task and Maven plugin is available for groovy project. Maven archetype is also available for rapid bootstraping your project.
  7. Brings in Dynamic language features to Java
  8. Run-time method dispatch is one of the core feature of a dynamic language. It happens at run-time instead of compile-time.

  9. Brings in Fun with Productivity
  10. Groovy is fun and lot more productive to code than Java. Your code can be more precise, expressive and readable than Java code.


Groovy 1.5 gets more groovier with...

  • Support for JDK 5 features including annotations, generics and enums
  • Syntactic enhancements
  • Enhanced dynamic capabilities through Expando meta-class (contribution from grails project)
  • Improved performance

Saturday, December 15, 2007

Grails Up(to)date

Grails is getting ready for release 1.0. The release candidate RC3 is the current stable release. It is worth giving it a try to know all significant additions, fixes and changes went into this release after the last major release 0.6.

Grails 1.0 RCn Ships with:
  • Groovy 1.5 (Grails 0.6 shipped with 1.1-beta-3-SNAPSHOT)
  • Spring 2.5 (Grails 0.6 shipped with 2.0.2)
Some of Noteworthy changes and additions include:
  • The generated Config.groovy (grails application configuration) under config has added mime type configurations and war dependency configurations in addition to log4j configurations. Also has added support to specify additional config files that get merged into this main config file.

  • The generated DataSource.groovy under config has added default hibernate cache configurations in addition to environment DataSource configurations.

Sunday, September 30, 2007

Grails (0.6) Gotchas...

  • Multi-select dropdown box with size attribute doesn't work as expected

  • Though the documentation select tag in GSP tablibrary doesn't explicitely say anything about multiple select, it works by passing additional parameters ( multiple="multiple" size="4").
    e.g.

    <g:select name="stockPurchase.id" from="${StockPurchase.list()}" optionkey="id" multiple="multiple" size="4"></g:select>


    However, the result will produce a multi select drop-down but the size attribute is not honored. The bug lies in css. Open main.css under web-app/css and edit the following line and remove select,:

    input, select, textarea {


  • many-to-many stores relations in wrong columns

  • e.g.
    Problem domain
    A stock is sold that was purchased multiple times (in fractions).
    A stock is sold multiple times (in fractions) that was purchased in whole once.

    Domain Classes

    class StockPurchase {
    static belongsTo = [StockSale]
    static hasMany = [stockSales:StockSale]
    }

    class StockSale {
    static hasMany = [stockPurchases:StockPurchase]
    }

    Controller

    class StockSaleController {
    //code omitted for brevity
    //...
    def save = {
    def stockSale = new StockSale()
    stockSale.properties = params

    def stockPurchases = params.'stockPurchase.id'
    stockPurchases.each {
    stockSale.addToStockPurchases(StockPurchase.findById(it))
    }

    if(stockSale.save()) {
    flash.message = "StockSale ${stockSale.id} created."
    redirect(action:show,id:stockSale.id)
    }
    else {
    render(view:'create',model:[stockSale:stockSale])
    }
    }
    }


    In addition to stock_sale and stock_purchase tables, it also craetes a reference table named stock_sale_stock_purchase (with columns stock_sales_id, stock_purchases_id) automatically to support many-to-many relationship.

    However, when a stock sale is saved, the reference table ends up with ids switched. But the application works correctly as expected.

    I initially thought there could be a bug. But looks like there isn't and it is expected to work that way.

    Check for an explanation by Graeme Rocher here


Thursday, September 13, 2007

How to add a Usage message to maven builds

The easier way is to use the antrun plug-in's run goal, bind it to the validate phase (the first phase in the list of maven 2.0 phases), and configure the plug-in to echo the usage string.
e.g.


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelversion>4.0.0</modelversion>
<groupid>com.giri</groupid>
<artifactid>com.giri</artifactid>
<name>UsageExample</name>
<version>1.0.0</version>
<description>
MavenUsageExample-
Usage: mvn [-Dplatform.type=local/dev/qa/stage/prod]
--platform.type is taken for resources to package.
</description>
<properties> <!-- Defaults to local. To override, define like: mvn -Dplatform.type=dev -->
<platform.type>local</platform.type>
</properties>
<build>
<defaultgoal>assembly:assembly</defaultgoal>
<sourcedirectory>src/java</sourcedirectory>
<resources> <!-- only include platform related resources -->
<resource>
<directory>
src/resource/${platform.type}
</directory>
</resource>
</resources>
...
<plugins>
...
<plugin>
<artifactid>maven-antrun-plugin</artifactid>
<executions>
<execution>
<id>echo-usage</id>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>***********************</echo>
<echo>
Usage:
mvn [-Dplatform.type=local/dev/qa/stage/prod]
</echo>
<echo>
-Dplatform.type is optional.
If not specified, default is local
</echo>
<echo>***********************</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
</project>


Saturday, September 08, 2007

Fast Rail with Grails in just 2 minutes

  1. Install Groovy


  2. Install Grails


  3. Start a new Application

    • C:\grails-apps>grails create-app appname (appname is optional, if not given, it will ask anyway)
    • Check the directory appname and all sub-directories it creates under. The application is eclipse ready and ANT ready with .project and .classpath and build.xml files (it would have been more nicer if maven had been considered instead of ANT :) Hope to see MAVEN support added in coming releases).
    • Import the project into eclipse.


  4. Setup new application for MySql
    • Explode the zip and copy the jar to /lib directory under the newly created application

    • Create databases (dev, test, prod) in MySql database. E.g. gspin_dev, gspin_test and gspin_prod

    • Edit DataSource.groovy under /conf for MySql

    E.g.


    dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    username = "root"
    password = "root"
    }
    // environment specific settings
    environments {
    development {
    dataSource {
    dbCreate = "update" // one of 'create', 'create-drop','update'
    url = "jdbc:mysql://localhost/gspin_dev"
    }
    }
    test {
    dataSource {
    dbCreate = "update"
    url = "jdbc:mysql://localhost/gspin_test"
    }
    }
    production {
    dataSource {
    dbCreate = "update"
    url = "jdbc:mysql://localhost/gspin_prod"
    }
    }
    }

    • Create a domain class (.groovy)
      E.g. User as follows

    class User {
    String userId
    String password

    static constraints = {
    userId()
    password()
    }

    String toString() { this.userId }
    }

  5. Run the application

  6. E.g.

    C:\my-grails-app\gspin>grails run-app

    • Check the gspin_dev and notice the table User created.

  7. Check the gspin_dev and notice the table User created

  8. Generate source
    • C:\my-grails-app\gspin>grails generate-all

  9. Run the application and notice that you have got nice gui for all CRUD operations of your User domain object
    • C:\my-grails-app\gspin>grails run-app

  10. Test the application

Friday, August 17, 2007

Agile Practices

Agile Practices in Software development is nothing but learning and applying the learning experience iteratively and effectively.

Learning is a key aspect in every moment of life. How do we learn? We learn from our mistakes. If we face a situation the first time, we deal with it in a certain way and may take our own time dealing with it by even making mistakes. If we have to face it the second time and deal with it the same way, we can apply our experience and learning from our previous mistakes and face it faster than the first time. If we have to face it again and again, we learn again and again and improve each time and ultimately achieve effectiveness. Likewise, if we have to develop the same software application the second time, we will certainly be more productive and take less time than the first one. What makes us to be productive the second time is the learning experience that we apply. But, ‘developing the same application second time’ will never happen in Software industry. So, how can we make the application development highly productive the very first time itself? We can better achieve this by having several small cycles of iterative development and by applying the learning experience immediately in the next cycle. Agile practices advocate this practice of iterative learning, developing and improving the experience to achieve high productivity and produce the best result.

Nothing is new in agile practices; everything has been around for several years. Agile only distilled and brought things together. It is all about recognizing and responding to change, but in a faster, improved and effective way.

More on agile practices at: http://www.infoq.com/articles/learning_is_the_bottleneck

Wednesday, July 04, 2007

A True Framework

Java enterprise software development has become more comlex and unpleasant than ever with increasing stack of technologies added; fast, complex and ever changing specs coming out of JCP; more steap learning curve from version to version with increased complexity than simplicity; lack of good tooling support; and on top of all these more and more vendor specific extensions to know for making any project successful.

Spring framework has been gaining support among the Java community which is trying to simplify (and in many ways has succeeded) Java enterprise software development. Spring is a light-weight modular stack of Java frameworks. At its core is a light-weight POJO based IOC container that brings in a powerful design pattern - Inversion Of Control (IOC) or Dependancy Injection(DI) to Java applications. It is more nice for anybody to say that one is developing a Spring-based Java enterprise applicaton than to say J2EE-based (rather JavaEE-based) application.

Recently, Ruby On Rails (RoR) has been gaining popularity both inside and outside of Java community as an attractive alternative to Java-based web application complexities. In many ways RoR has clearly shown and proven to the developer community, what a "true framework" should look like. Even very mature enterprise technologies like Java with a wide variety of frameworks available for its community is shamelssly learning from this fast growing baby - RoR.

In my opinion "a true framework" should promote, automate and even force several best practices and make the development fast but simple, pleasant and more enjoyable.

RoR – a very well designed framework is based on THREE principles:
  • DRY – Do not Repeat Yourself
  • Convention over Configuration - which eliminates lot of configuration headaches
  • Code Generation – eliminates the need for repeated startup code

I was initially impressed by the simplicity and elegance of Java language when I first moved from C++ in 1999. It has been quite enjoyable developing Java applications. I have been trying to keep myself abreast with the latest and greatest things happening in Java community. Since then I never had to look back up until I have come across RoR that made me to think like "Java development is tedious and complex". Open source community has contributed an amazing list of quality frameworks to Java. Even with good open source frameworks like Jakarta, Struts, HIBERNATE, Spring etc., developing an enterprise Java application is still tedious and complex. Java development community deserves better frameworks for rapid development and truly making Java development a pleasant experience.

After learning and experiencing RoR, I started looking for such frameworks or initiatives in Java community. Though there is NONE currently available that is even close to RoR, I have come across a few initiatives that are happening along similar lines.

Following is a shot list of some of such initiatives for Rapid Java Application Development:

  • AppFuse - seems promising. Try AppFuse light/ Equinox and Jetty for even more rapid results.
  • Sculptor - a model driven development Java application framework
  • AndroMDA - another model driven development Java application framework.
  • Grails - a RoR like framework for Java web-based development.

Finally, it is a good sign to see that Java is heading towards simplicity with several open source frameworks and initiatives shifting developer community from traditional Java Enterprise Development towards simple more agile way - for a fast, better and pleasant experience. :)

A must have Eclipse plugins

Commonclipse - Elcipse plugin
It features automatic generation of the following methods using commons-lang builders:

  • toString()
  • hashcode()
  • equals(Object)
  • compareTo(Object)

This would be really useful if you are usine HIBERNATE for persisting domain objects.

GoToFile plugin