Tuesday, June 28, 2011

When we bought our first car.

Last month I bought a car, even though I did a lot of research on the internet about the various options that were in my budget limit it was still a difficult task to finally select a model.
Our primary reason to buy a car was that my wife was finding it difficult to commute in the thickly populated BMTC busses. The first thing that went through my head was that since she is not a experienced driver she might find it difficult to drive in Bangalore traffic so it would be a good idea to buy a second hand car as it won't pain that much if it gets few scratches or dents.
We went to a send hand car dealer in JP Nagar and we discovered that second hand cars in Bangalore are very expensive for instance a new Alto costs ~ 3.20 L while a 2007 model Alto costs 2.20 L and the same was seen with other cars like Wagon R, Hyundai etc.
We then decided to buy a new car, the thought that my wife is a beginner when it comes to driving still kept us on the track that we should not be spending too much on our first car. We decided to have a Tata Nano test drive, we went to a Tata showroom and saw Nano, when we sat in this car it was awesome, it feels spacious and the car is really good, the sales guy told us that the test drive car is gone to some place and that we can book a test drive and he can get the car at our place for a test drive, The same day after leaving from there we went for Maruti Showroom and had a look at Alto, Alto was much better the only issue was that it was costing almost as much as Wagon R ( ~4L ), we had a wagon r test drive and liked it we also booked it the same day. With Petrol prices touching 72 we discussed at home that when we are spending something like 4.5L why not try to get a Diesel car instead.
We had a look at Swift and Ritz, no one can "not like" Swift and Ritz was good but the speedometer and dashboard didn't look like a 6 L car to me, and we finalized Swift Diesel.
The only problem this time was that the waiting time was around 4 months. We thought of having a check at other dealers if someone has a lower waiting period, we found one where some one had just canceled his Swift Dzire Diesel version and even though it was slightly out of our reach we borrowed some money from family to get this car.
It has been one month now, I am 100% satisfied with this car.

Sunday, June 26, 2011

Spring 3 BlazeDS Google AppEngine Integration


I am finally able to get this thing working, this is how it can be done.

1. Follow what Martin's blog says about this issue.
2. You can take reference from my code .

Spring 3 Blazeds Integration.

This weekend I wanted to learn how to integrate Spring and Blazeds and test it using SoapUI.
Ok, before you get started the you need to make sure that you have the following softwares properly installed on your machine.
Java SDK 6, Maven 2.2, Eclipse Helios - (Plugins that I have installed are subeclipse, m2, ), SoapUI, Tomcat 6.

1. Create a Web Project
Open command prompt and type.

mvn archetype:create -DgroupId=org.example.webapp -DartifactId=example-webapp -DarchetypeArtifactId=maven-archetype-webapp

maven has created a simple web application, now we need create few eclipse files so that we can easily import this project in eclipse.

mvn -Dwtpversion=2.0 eclipse:eclipse

Next we have to import this project in eclipse.


Wednesday, June 22, 2011

Create Aspects in Spring for Logging DAO response time

Step One: Create a System architecture class which defines which methods to pointcut

@Aspect
public class SystemArchitecture {

/**
* Service Layer Aspect.
*/
@Pointcut("within(com.service..*)")
public void inServiceLayer() {

}

/**
* Service Implementation layer aspect.
*/
@Pointcut("execution(* com.service..*.*(..))")
public void businessService() {

}

/**
* DAO Layer aspect.
*/
@Pointcut("execution(* com.dao..*.*(..))")
public void dataAccessOperation() {

}
}

Step two: Create a Tracing Aspect class which contains the actual code that is executed at the point cuts

@Aspect
public class TracingAspects {

/**
* Around advice to log method entry and exits. Logs at TRACE level
*
* @param pjp ProceedingJoinPoint
* @return Object
* @throws Throwable if aspect throw any exception
*/
@Around("com.util.SystemArchitecture.dataAccessOperation()")
public Object traceMethodEntryExit(ProceedingJoinPoint pjp) throws Throwable {
Log log = LogFactory.getLog(pjp.getTarget().getClass());

MethodSignature mthdSignature = (MethodSignature) pjp.getSignature();
log.debug("Entering method " + mthdSignature.getName());

try {
return pjp.proceed();
} finally {
log.debug("Exiting method " + mthdSignature.getName());
}

}

/**
* round advice to log method execution time. Logs at DEBUG level
*
* @param pjp ProceedingJoinPoint
* @return Object
* @throws Throwable if aspect throw any exception
*/
@Around("com.util.SystemArchitecture.dataAccessOperation()")
public Object perfTrace(ProceedingJoinPoint pjp) throws Throwable {
Log log = LogFactory.getLog(pjp.getTarget().getClass());

MethodSignature mthdSignature = (MethodSignature) pjp.getSignature();
long startTime = System.currentTimeMillis();

try {
return pjp.proceed();
} finally {
long elapsedTime = System.currentTimeMillis() - startTime;
log.debug("Method " + mthdSignature.getName() + " took " + elapsedTime + "ms");
}

}


Step Three: Enable AOP in Spring configuration


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="tracingAspects" class="com.util.TracingAspects"/>
</beans>

Exception Handling in Spring MVC

Here is one good and easy way to do it
First of all We should create a user defined exception class

public class MyExceptionClass extends RuntimeException {
private String userMessage = "User message not set";

public MyExceptionClass() {
super();
}

public MyExceptionClass(String userMessage, Throwable cause) {
super(cause);
this.userMessage = userMessage;
}

public MyExceptionClass(String userMessage) {
super(userMessage);
this.userMessage = userMessage;
}

public MyExceptionClass(Throwable cause) {
super(cause);
userMessage = cause.getMessage();
}

public String getUserMessage() {
return userMessage;
}

public void setUserMessage(String userMessage) {
this.userMessage = userMessage;
}

@Override
public String toString() {
return "User message = " + userMessage + "\n" + super.toString();
}
}

Next step is to configure exception handling in application context file

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="MyExceptionClass">error</prop>
<prop key="java.lang.Exception">error</prop>
</props>
</property>
</bean>