Saturday, February 26, 2011

Reading data from Hashmap using JSTL

The requirement was to retrieve date based on the key from a HashMap and to display it in the UI.This is a common scenario when using the foreign key data.Initially seemed quite straightforward to create a HashMap to store the key and Values.In the JSP front ,the data can be retrieved either by
(i) looping the contents ( construct ) to identify the match or
(ii) retrieve the data from HashMap using the key( map["key"]).
I preferred the later approach since it was more performance effective.

Map map = new HashMap();
map.put(1, "Monday");
map.put(2, "Tuesday");
map.put(3, "Wednesday");

This was not rendering the data in the page.But when looping the contents the match was found and displayed in the page.

By modifying the key type in HashMap to String the values were retrieved and displayed in the Page.
Map map = new HashMap();
map.put("1", "Monday");
map.put("2", "Tuesday");
map.put("3", "Wednesday");

My inference from this is that EL does not support the Interger wrapper class for HashMap.But I could not find any documentation to confirm this.

Saturday, January 15, 2011

NullPointerException when creating Spring Bean

If the class name of the Bean is missed (for some reason )in the context file,it gives rise to NullPointerException by the factory.The stack trace was like
Caused by: java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.hash(ConcurrentHashMap.java:157)
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:186)

Saturday, December 11, 2010

OptimisticLockingFailureException Attempt to update step execution id=1 with wrong version (n), where current version is n+1

My Spring batch application was intended to run in Single thread env and concurrent updates were not in its scope.But one of the Jobs ,kept encountering OptimisticLockingFailureException:Attempt to update step execution id=1 with wrong version (1), where current version is 2
On analyzing the Spring Logs found that this was generated by the MapStepExecutionDao.I am using MapJobRepositoryFactoryBean with ResourcelessTransactionManager which is NOT suppose to persist the data .
After testing with few scenarioes found that the exception was thrown only when the batch operation tries to insert duplicate records to the table with constraints.After removing the duplicate data the batch execution was successful.
In this case OptimisticLockingFailureException was misleading ,a more appropriate DB Exception would have be helpful identify the cause.

Friday, October 22, 2010

Constant Specific methods in Enumeration

Using Java 5 Enums,it is possible to define different behaviours for a method based on the value.This is known as Constant Specific Methods.The methods that requires different behavior to be declared as abstract and it should be overriden in each Enum Value.

public enum Game {
CHESS {
@Override
boolean isIndoor() {
return true;
}
},BADMINTON {
@Override
boolean isIndoor() {
return false;
}
},CHINESE_CHECKER {
@Override
boolean isIndoor() {
return true;
}
};

public abstract boolean isIndoor();
}

Thursday, July 22, 2010

Element 'bean' cannot have character [children], because the type's content type is element-only

Encountered this error yesterday and spend some time on figuring out what could be wrong with the config and later on comparision with the previous version found the bean definition had an additional '-' character.In the Spring context file ,if the bean definition needs any special characters ,it must be enclosed with the escape character else the forementioned exception is thrown.

Monday, July 19, 2010

Vaadin

Vaadin is framework for developing web applications in Java . The application can be deployed on any Java server (Tomcat, WebSphere,etc). Ajax and other web related stuff are automatically handled by the framework.Vaadin renders its UI using GWT but all the logic and data are on the server-side.For the Server Client communication Vaadin uses JSON since JSON has better performance when compared with XML.

Vaadin provides plugin for Eclipse.The documentation mentions this could be used in Eclipse version Galileo and Ganymede.But could not install the plugin with Ganymede .Tried with Galileo and it works like charm!

When trying to use csvalidation.jar for Client Side Validations ran into Run Time Exceptions later from the Vaadin team came to know it was due the Java version I was using.Upgrading to csvalidation-0.3.4.jar works for JRE 1.5 .More details on this on
http://vaadin.com/forum/-/message_boards/message/178918

The http://vaadin.com/tutorial/-/page/skeleton.html#skeleton.application is a good place to start a simple sample application.

Friday, July 2, 2010

Spring Web Services

Web services is the technology that integrates Web-based applications using the XML, SOAP, WSDL and UDDI open standards over the Internet . XML is used to define the data structure, SOAP is used to transfer the data, WSDL is used for describing the services available and UDDI is used for listing what services are available. Web services allow organizations to communicate data without the knowledge of each other's IT systems behind the firewall.

Web services do not provide the user with a GUI instead the services are shared through a programmatic interface across a network.

Web services allow different applications from different platforms to communicate with each other since all communication is in XML.

A detailed tutorial about implementing Spring Web Service which I found very useful is at
http://webcache.googleusercontent.com/search?q=cache:K3fszlCL9JQJ:www.lulu.com/content/content_download_redirect.php%3FmetaId%3D1090073+maven+dependecies+for+Spring+web+service&cd=2&hl=en&ct=clnk&gl=sg