Welcome to my blog

I blog my interests and opinions here.

Thursday, September 8, 2011

Vacation in Kerala

A vacation in Kochi during Onam harvest festival time brings pleasant rains and good food. This was the day Mahabali rose from padalam (underground). The shopping centers greet you with giant Malabali baloons or a live person dressed as Mahabali a truly unique experience. Except for the bad roads and inching traffic this time of the year is really great here.

In five years the one rupee coin has disappeared and inflation has bumped up the cost of living significantly. Nevertheless a vacation well spent is vacation well spent.

Monday, July 18, 2011

What is polymorphism really!

It's been a while I blogged. With a new baby and things that come with it life kind of has become pretty hectic.

I always thought that polymorphism was the ability to implement the same behavior in different ways. An Animal would move (public void move()) but the way a Cat would implement move would be different from the way an Elephant would implement move. But that is not the case, polymorphism is the forcing the Cat class and Elephant class to implement move() method in their own way by having them implement an IAnimal interface with a public void move(); method. It is inheritance and method implementation that we see here. Method overriding does not mean the classes are exhibiting polymorphic behavior. See the Wiki for more details http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Monday, January 31, 2011

Using Spring and Hibernate makes developers life easier

A hibernate based application typically uses object relational mapping paradigm to map a database table
CREATE TABLE `hello` (
`id` int(11) NOT NULL,
`description` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
)

to an entity class in Java

package dto;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* Hello generated by hbm2java
*/
@Entity
@Table(name = "hello", catalog = "test")
public class Hello implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String description;

public Hello() {
}

public Hello(int id) {
this.id = id;
}

public Hello(int id, String description) {
this.id = id;
this.description = description;
}

@Id
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}

public void setId(int id) {
this.id = id;
}

@Column(name = "description", length = 45)
public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

}
Spring uses an application-context.xml file that specifies the bean dependency injection configurations for the data source, hibernate session factory, hibernate template and the Data Access Object (DAO)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>hello.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>

<bean id="myHelloDao" class="dao.HelloDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate"/>
</property>
</bean>

</beans>

The mapping between the entity and the database table is specified in the hibernate mapping file hello.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="dto.Hello" table="hello" catalog="test">
<id name="id" type="int">
<column name="id" />
<generator class="assigned" />
</id>
<property name="description" type="string">
<column name="description" length="45" />
</property>
</class>
</hibernate-mapping>

The Data Access Object (DAO) class would (uses the hibernate template callback mechanism in order to access the database) look like

public class HelloDao {

private HibernateTemplate hibernateTemplate;

public void setHibernateTemplate(HibernateTemplate hibernateTemplate){
this.hibernateTemplate = hibernateTemplate;
}

public HelloDao()
{

}


/**
* Gets all the rows from test.Hello table
* @return
*/
public List<Hello> getAll() throws DataAccessException
{
List<Hello> helloList = null;

try
{
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException,SQLException {
return session.createQuery("from Hello").list();
}
};
helloList = (List<Hello>)hibernateTemplate.execute(callback);
}
catch(Exception ex)
{
throw new DataAccessException("Error when getting all records.",ex);
}

return helloList;
}
}

Introducing Spring changes the way a typical hibernate application works by
1. Reducing the number of configuration files on the server side
2. Allowing multiple clients to connect to different databases (by injecting different data source configurations in the application-context.xml) without having to change the server side code.

Friday, January 7, 2011

Java Server Faces (JSF) 2.0

The advent of Java Server Faces (JSF) 2.0 has made the life of the UI developer very much easier. A simple JSF would have very minimal files namely XHTML files, a Java bean to hold properties and a web.xml file. Usage of ManagedBean annotations has helped to avoid creating and managing faces-config.xml. Bringing about XHTML files rather than JSP files has created a clear separation of logic between the page designer and the Java Developer. A simple hello world JSF project would have the following files -
1. Hello.xhtml - A well formed XHTML file that contains the JSF tags
2. web.xml - The web descriptor specifies the faces servlet and XHTML contexts
3. HelloBean java bean - A simple Java bean with the "ManagedBean" annotation that holds the properties

All the latest developments on JSF are available in the Mojarra project at http://javaserverfaces.java.net