Saturday, August 1, 2009

AOP - Simple Example

Aspect Oriented Programming (AOP) promotes separation of cross-cutting concerns like logging, transaction management, security, etc from the components in a software system. The components may be performing these system services apart from the core logic they are entrusted with. These concerns tend to cut across multiple components in a system, hence called cross-cutting concerns of a system. These concerns introduce levels of complexity to your code as -
  • systemwide duplicated code for implementing these system concerns, necessitating multiple code changes if any of the concerns change
  • components are burdened with more code than that which deals with their core functionality
AOP modularizes these services and applies them declaratively to the required components in the system. Hence, components become more specific and cohesive. It covers the core application with layers of functionality without the application even knowing it exists.

Let us get a feel of AOP through a simple example.
A class People who sings praiseworthy songs about a class King. Let us see how it works without AOP.

Here, each time the King goes to a War, it must stop and tell his People to Praise him before he can start fighting the War. Similary, after the War, the King must remember to tell the People again to Praise him about his achievements.

Let us now introduce the concept of Aspect here and also, a configuration file praise.xml.

The functionality of the Aspect is defined in the bean referred to by the ref attribute. The aspect is made up of pointcuts and advice. Here, we declare a pointcut which is triggered by the execution of a startWar() method. Advices are provided in 2 forms here - before to call singBefore() method of People before the pointcut and after to call singAfter() method of People after the pointcut.

This simplifies the code in class King as follows:

reference: spring in action

Sunday, July 26, 2009

Simple usage of Spring

Let us look into the concept of Dependency Injection through a small example:-

Here we are trying to print out a string "Hello World" using Spring's techniques. The interface HelloService defines the contract for the service class:-

package com.hello;
public interface HelloService {
void sayHello();

HelloServiceImpl implements the above interface as follows:

package com.hello;
public class HelloServiceImpl implements HelloService {
private String hello;
public HelloServiceImpl() {}
public HelloServiceImpl(String hello) {
this.hello = hello;
public void sayHello() {
public void setHello(String hello) {
this.hello = hello;

The property "hello" in this class can be set either through the constructor or the setter method. Now, let us write the Spring Configuration file, named here as hello.xml

Here, the bean named "helloService" of class "HelloServiceImpl" has a property "hello", which is injected with a value of "Hello World" using the setter injection. In effect, this does the following:-

HelloServiceImpl helloService = new HelloServiceImpl();
helloService.setHello("Hello World");

If we were opting for constructor injection, then the tag in the xml file would have to be replaced with -

In effect, this does the following:-

HelloServiceImpl helloService = new HelloServiceImpl("Hello World");

Now, let us write a main class for this entire application, -

package com.hello;
//add imports and handle exceptions as and when necessary
public class HelloMain {
public static void main (String[] args) {
BeanFactory factory = new XmlBeanFactory(
new FileSystemResource("hello.xml"));

HelloService helloService = (HelloService) factory.getBean("helloService");


Here, the BeanFactory is the Spring container. The configuration file "hello.xml" is loaded, "getBean()" retrieves a reference to the bean with id "helloService" and this reference is used to print "Hello World" to the screen.

This example depicts Dependency Injection or Inversion of Control in Spring clearly. Typically, in almost all applications, each object would be responsible for obtaining its own references to the objects it collaborates with, which leads to highly-coupled and hard-to-test code. But here, objects are given their dependencies at creation time by the xml file (in the above example) or dependencies are injected into objects, hence enabling loose-coupling. Here, the interface helps the class to remain loose-coupled at all times.

reference :- Spring in Action

Friday, July 24, 2009

Spring - What is it?

Java was in the process of maturing during the year of 1996. Many rich and dynamic web applications were being developed. But, people started to realize the shortcomings of Java Applets. This paved the way for the invention of Java Beans - a software component model for Java. They were primarily used for building user interface widgets and seemed too simple to be able to do any "real" work. Later in the year if 1998, Enterprise Java Beans (EJB) extended the normal beans to the server side, even though losing its simplicity. Its complexity reduced its popularity and easiness. This is where Aspect Oriented Programming (AOP) and Dependency Injection (DI) found spot to flourish. These methodologies furnish the POJOs (Plain Old Java Object) with a mirror image of an EJB using a declarative programming model, but completly avoiding all the complexities. Better late than never !!!

Leading the development based on lightweight POJOs is the Spring Framework.

Spring is an open source framework, created by Rod Johnson benefitting from its features such as simplicity, testability and loose coupling. Basically, it is a lightweight dependency injection and aspect-oriented container and framework. Breaking it down into simpler terms:-

  • Lightweight -> in size (distributable JAR less than 2.5MB) and overhead of processing
  • Dependency Injection -> loose coupling by means of objects not creating or looking up for dependent objects on their own, instead giving their dependencies during instantiation without being asked by them
  • Aspect-oriented -> separating business logic from system services (logging, security, transaction management) where objects deal with only things they are supposed to do
  • Container -> contains and controls the lifecycle and configuration of application objects
  • Framework -> configures and composes complex applications from simpler components with the help from an XML configuration file
Let us have a peep into the Spring Modules:-
  • Core -> provides the fundamental functionality of Spring, onbased on the BeanFactory
  • ApplicationContext -> builds on top of core and makes Spring a framework, adding support for internationalization messages, application life-cycle events, email, JNDI access, remoting, scheduling, validation, etc.
  • AOP -> decoupling application-wide concerns from objects to which they are applied
  • JDBC abstraction (DAO) -> abstracts away the boilerplate code of JDBC keeping the database code clean and simple, also building meaningful exceptions over the normal SQL Exceptions
  • ORM -> supports various ORM frameworks like Hibernate, JPA, JDO, iBatis, etc in addition to JDBC
  • JMX -> helps expose application's beans as Java Management Extensions beans supporting monitoring and reconfiguring a running application
  • JCA (Java EE Connector API) -> integrates Java applications with various information systems like databases and mainframes, all scattered in disparate servers and platforms
  • MVC Framework -> separates user interface from application logic, also supporting existing MVCs in market like Struts, JSF, etc
  • Portlet MVC -> aggregating several bits of functionality on a single web page providing a view into several applications at once
  • Web -> support classes for MVC applications
  • Remoting -> access applications over the network, expose objects' functionality as remote objects, all considering the objects as simple POJOs
  • JMS -> guarantees message delivery throught JMS message queues and topics and helps create message-driven POJOs capable of consuming asynchronous messages

Thursday, July 9, 2009

EasyMock IllegalStateException: missing behavior definition for last method call

When you encounter this error, please check whether you tried to use the mock object before invoking EasyMock.replay() method. I did that encountered the IllegalStateException and spent quite some time until I figured out the reason.

I had written the following expect() ;


the culprit was the following statement which I had put before the expect().


Hope it helps someone!

JUnit with EasyMock

Ever wondered how could you test your classes without waiting for your colleague to complete his classes.. I think EasyMock is one of the answers..

EasyMock helps to create mock objects by passing an interface as argument. It is very helpful when you have complex web application that involves lot of custom objects and you want to test a particular class which is dependent on ten other classes.

It has 2 simple steps to follow.

1. Create mock objects
2. Set the expected values in mock object so that the methods of the object return the same values

It is very easy to use. (Steps below:-)

1. Add easymock libraries
2. Create JUnit test case
3. Create mock objects using EasyMock.createStrictMock(MyInterface.class)
4. Set expected values using EasyMock.expect(myObj.myMethod()).andReturn("ReturnvaluefromMyMethod").
This statement means that when myMethod() is invoked, return the value "ReturnvaluefromMyMethod". In essence, whichever method you are planning to invoke later, set the values expected to be returned from the method.
5. call EasyMock.replay(myObj);

Our mock objects are ready to use.

1. Do not call any methods of the mock object until you call replay(). Otherwise it will throw an IllegalStateException at that point.

2. If your interface contains getter/setter methods, instruct the getter() [not the setter()] with the expected return values. In general, we are used to invoke the setter method with the value to be set and use the getter methods to retrieve the value.

Hope it helps!

Wednesday, June 24, 2009

BIRT report - database resource localization

I just thought of explaining how to display the localized resources from database. Basically, we create a mapping between the data in a column and the resource key in the resource bundle.

1. Add resource bundle to your report.
In the Layout tab, click anywhere on the blank space. Go to Resources tab. Select your resource bundle file.

2. Select the table where you contains the columns from your dataset. Go to the "Map" tab. Create a new mapping by clicking on the "Add" button.

3. Click on the "..." button to choose the property key from the resource bundle. The screenshot is given below.

When the column "city" in the dataset contains the value "Nantes", retrieve the value from the resource bundle whoose property key is dblocalization.prop1. This will instruct the report engine to display "LocalizedValue1" when it encounters "Nantes" in the dataset.

This should be done for all the values that you expect to be localized and shown in the report.

4. All set. Preview the report.

It would have been nicer if there is a only one mapping needed to values in the column and the resource bundle file name. Lets wait BIRT's upcoming features..

Tuesday, June 23, 2009

How to pass a ResultSet to BIRT report

I tried to pass a JDBC ResultSet (java.sql.ResultSet) and could not find a straight forward way like passing the ResultSet as an argument to the report during generationd. However this is what I finally arrived at. This solution could be used to pass a collection of objects from a java class to the BIRT report.


  1. In the java application where we are planning to generate the report using BIRT engine, execute the sql query and fetch the resultset. Add the resultset as a parameter to the report.

      ResultSet rs = getResultSet() // a function that executes the query and returns resultset

      HashMap params = new HashMap();

      params.put(myResultSet”, rs);

  2. In the report, create a scripted data source. Create a scripted data set with the column names that you wish to use in the report. For e.g lets create a data set with one column “EMP_NAME”.

  3. This step is to glue the columns in the resultset that came as parameter and the columns in the scripted data set. This can be achieved in two ways.

      a) Using javascript function

        1. In the scripted data set, implement the open(), fetch() methods. I'll explain in this another post.

      b) Using a java event handler that extends ScriptedDataSetEventAdapter class ( and implement the fetch(), open() and beforeOpen() methods. Lets handle one by one.

    1. Create a class called TestHandler (test.TestHandler)
    2. Declare a member variable for the ResultSet

        ResultSet resultSet;

    3. Retrieve the parameter myResultSet set from the java class in Step 1 and store it in the member variable.

      beforeOpen(IDataSetInstance dataSet, IReportContext reportContext) {

        super.beforeOpen(dataSet, reportContext);

        //retrieve the parameter myResultSet set from the java class in Step 1

        resultSet = (ResultSet)reportContext.getParameterValue(“myResultSet”);


    4. Position to resultset to the first row in the open()

      public void open(IDataSetInstance dataSet) {;

        if(resultSet != null)



    5. Map the column of the resultset and the column of the scripted data set.

      public boolean fetch(IDataSetInstance datSet, IUPdatableDataSetRow row) {


          return false;// to indicate that there are no more rows left to process

        row.setColumnValue(“EMP_NAME”, resultSet.getString(1));;

        return true; // to indicate that more rows to follow


  4. Do the cleanup activities. Close the resultset, statement and release the connection.

  5. Go to the report designer. Go to Outline window. Select the scripted data set created in Step 2. Go to the Property Editor and select "Event Handler" tab. In the Event Handler Class, add the fully qualified class name created in step 3 (test.TestHandler).
  6. All set. Run the java program to initiate the engine and invoke the report and see the results.

My experiments with truth.. oops.. no.. BIRT and Jasper

BIRT vs Jasper

Over the past couple of weeks I was trying to explore the features supported in BIRT and Jasper. To a beginner, both the tools seem to be difficult. I mean, to get a working sample on them, it takes some time. I went through numerous blogs, tutorials and my firefox bookmarks started becoming unmanageable. So I thought of publishing my experiences so that it will help someone.

The basic principle behind both reporting tools is the same.

1. Design a report using a report designer (Save it in a xml file)

2. Use the report engine to parse the xml file and fill the data in the report

3. Render the report in a format of your choice (pdf, html, csv, etc..)

Performance metrics

BIRT seems to be 10-25% slower than Jasper for as long as the caching mechanism is used. Once the caching (virtualizer) is used, BIRT gives better performance (around 50% more) when compared to Jasper.

BIRT Gotchas

· Always sorts the records once a group is created. BIRT 2.3 talks about a field for turning off the sort, there is no difference on the turn-around time.

· Using BIRT in a web application requires you to add 30MB of libraries to your deployable (ear or war).

· The pdf rendering mechanism generated huge (really hhhhhuuuuggggeeee) pdf documents.

· Integration with java does not seem to be straightforward. For instance, passing a collection of objects to a BIRT report requires you to browse through numerous blogs and tutorials on birt-exchange and then apply your brain.

· Report rendering in xml format does not yield a good quality xml.

Jasper Gotchas

· iReport. The report designer does not seem to be intuitive.

· Localization of database resources does not seem to be possible

· Csv export gives extra blank columns (extra commas) in the output file

· Excel output does not include the graphic images (logos etc..)

· Does not provide a good facility for component based model, object libraries.

· Lacks concept of theme in the report elements except charts.

· Does not have a default style per element (one style for table, another one for textfield etc..). It has a default style for the report.

· Once virtualizer is used to overcome outofmemory error, the performance degrades drastically