Thursday 5 November 2009

XStream : The Simplest XML serializer

Serializing XML to (Java) objects and back just became simpler with what is being touted as the fastest and the simplest XML serializer, XStream.

According to this bench marking excercise, XStream is the fastest Serializer to hit our Java objects. While marshalling and unmarshalling is a time consuming task, using XStream would definitely be a performance boost. But while speed and its low memory foot print are talking points, it is simply easy to use. The XStream two minute tutorial takes TWO MINUTES to read and digest. The only slightly disconcerting aspect is that releases are few and far apart.

One a final note, if you are looking for an XML serializer that serialises with no hang ups, XStream might fit the bill but be aware that using XStream is like traveling in the economy class of a budget airline and while you might get home, you cannot expect too many features. For features, pick up JaxB 2.1 or XMLBeans.

Tuesday 20 October 2009

One JAR for all your JARs

Sometimes it is more convenient to package your Java application into a single JAR which includes all dependencies and works just like an executable. This seemingly simple objective becomes complicated when the dependencies include other JAR files as one can run into JAR Hell with the Java Class Loader. One possibility of achieving the goal of having a single executable jar is using One Jar.
The unique aspect of OneJar is that dependencies can be bundled into the executable Jar in their native form without unpacking the classes and the custom class loader resolves and loads required classes from within the dependent jars. The default Java class loader can only load classes from the file system, it cannot look inside other jars for the required classes. To achieve loading of classes from within other jars, a custom class loader is required which is provided by One Jar.One Jar also provides an Ant and a Maven2 task and can be easily integrated into your build cycle.

Tuesday 6 October 2009

Scalable Vector Graphics

Scalable Vector Graphics provide a rich source of functionality that allow applications to manipulate and respond to the user interactions with the image. Creating and modifying SVG images is possible using one of the several open source tools available.

Batik SVG Toolkit is an impressive Java based open source SVG toolkit that provides libraries for the dynamic manipulation of SVG objects from within a Java application. While Batik provides a rich array of functionality, it somewhat lacks a good WYSIWYG editor.

A good WYSIWYG editor for SVG Graphics is provided by Inkscape which is another open source SVG editor with several advanced editing features such as (markers, clones, alpha blending, etc.)

Monday 6 July 2009

Wimbledon 2009: Federer won but Roddick didn't lose

Last night, Federer created history by overtaking Sampras as the all time winner of 15 Grand Slam titles. In doing so, he set another record by appearing in his seventh consecutive Wimbledon final but spare a thought for the man who stood in his way and threatened to spoil the party, Andy Roddick.
Roddick came to this final a record of 18 loses and only 2 wins against Federer. This was hardly encouraging however, armed with a new strategy that attempted to stifle the free flowing Federer and a fitter body, Roddick unleased a barrage of serves that kept him toe to toe with the FedEx. Infact throughout the match, it was Roddick who on several occasions threatened to seize control and run away with the match, however Federer being the great Champion that he is used all his experience to finally overcome Roddick in the longest fifth set in the annals of the competition. Well Done Federer (for the win and for also regaining the number one ranking) but also Well played Roddick!!

Monday 8 June 2009

French Open 2009: The FedEx is a Champion..AGAIN!!

Federrer finally got what he wanted.A complete Slam!True, he didn't have to overcome the challenge of having to face his greatest nemesis, Rafa over the net in the finals, yet there was no question about his form and ability. After a scratchy beginning to the tournament during which he dropped two sets against Tommy Haas and nearly went to the brink, Federrer regained control and played down the nerves to take the Championship in three sets against Robin Soderling. The final was a bit of an anti-climax with Federrer completely dominating the match but for a Federrer fan like me, it was a sweet win.Well Done Federrer!!

Saturday 6 June 2009

Mapping Hibernate to Computed Columns

Most applications require a unique id to be assigned to the record. While a unique id can be implemented in several ways, SQL Server offers the option of using a Computed Column to generate and maintain these values.

Computed Columns enable the implementation of some complex logic via a User Defined Function (UDF) in generating the value of the corresponding row. For example: Consider an Employee table that has Employee_Location as a unique field. The Employee_Location changes every time, the employee's department is changed. This field can have values such as E001,Y001 and T009. While there are several ways of implementing this scenario, the Computed Column feature offered by SQL Server is an effective option. Values stored in a Computed Column can be persisted or kept virtual. Thus, it gives you a performance boost as you don't need to store data on the disk that is changing frequently but is required. On the other hand, if the data does need to be stored, the Computed Column can be persisted. This article explains the creation of indexes on Computed Columns.

If your Computed Column is being refrenced from within the application via an ORM such as Hibernate. The corresponding column mapping in the Hibernate POJO has to be annotated using the following set of annotations on the getter of the associated column attribute.
@Column(name = "Employee_Location", length = 25, insertable = false, updatable = false)
@Generated(GenerationTime.INSERT)

What the annotations do is tell Hibernate, that the Computed Column is not to be updated by the application AND that the value in the Computed Column is created when the record is inserted. That's it. Hibernate will not attempt to insert any value into the Employee_Location column but will retrieve the value stored in the column and associate it with any related mapping.

Sunday 17 May 2009

Forcing comments in Subversion

While administering Subversion is a simple task, getting all developers to add comments to their subversion is not. The importance of having comments while checking-in their code helps especially in an Agile environment where in there may be several check ins during the day and comments are an important tool in communicating what was done. Fortunately for those forgetful developers, there is help at hand to enforce commenting during a check-in.

A simple script available in a dormant form with every Subversion installation works the magic. The operation of the script is explained very clearly in this blog post. The script is present in the pre-commit.tmpl file in the /{repository-location}/hooks/ directory. To get it to fire, make sure the script has commands that are valid DOS commands (if you are in a Windows environment) AND re-name the file to pre-commit.bat. That's it. Every check-in will require a comment!!

Sunday 10 May 2009

Hibernate Annotations : A powerful addition to the Hibernate family.

I recently used Hibernate Annotations in a project and was impressed by the power and relative flexibility offered by annotations and the difference it made with regards to cleaning up the project build. While the major advantage of using annotations was that I could completely get rid of the hibernate mapping xmls, it also made life easier when it came to specifying relationships between my entity beans.

To get started with Hibernate Annotations, follow the Hibernate learning trail and download the required jars and set up your project. If you are using Maven, you can specify the dependencies in a Maven POM as outlined in this example. Once you have a project set up, take a look at this reference guide. Mapping entity bean associations and relationships is simple and this is a good example that demonstrates the annotation entries for mapping different relationships. One major advantage of using Annotations is that several mappings and associations can be configured by default. For example, if I defined a primary key mapping of an entity bean to a database as follows:
@Id
public Long getId() {
return id;
}
and left out @GeneratedValue(strategy=GenerationType.AUTO)

Hibernate would assume that the application will provide the Primary Key or in other words, the strategy for the primary key, if I were to specify the same in an hbm file would be equivalent to "assigned". Similarly, If the Annotation @Table(name="EMPLOYEE") on a bean can be used to map the bean to the table EMPLOYEE but if not given, the name of the bean would be used as a key by Hibernate for mapping the table to a bean.

Thus, Hibernate Annotations reduces the setup time in adding Hibernate to a project while simplifying the maintenance of the associated code.

Saturday 21 March 2009

Integrating Spring Security with Active Directory on JBOSS 4.0.5

Spring Security (formely ACEGI) is a fairly robust and flexible framework that fits in well with a J2EE solution stack. Some of the main features that made us choose Spring was its flow transition authorization policy and its database backed 'remember me' implementation.
Based on this, we decided to use Spring Security 2.0.4 on a JBOSS 4.0.5 server. The Spring component authenticates and authorizes a user against a Active Directory through its LDAP authenticator component (org.springframework.security.providers.ldap.authenticator.BindAuthenticator) and authorize the user using its LDAP authorities search classes (org.springframework.security.ldap.populator.DefaultLdapAuthoritiesPopulator & org.springframework.security.providers.ldap.LdapAuthenticationProvider).

The important thing to remember is that Spring security matches a group name with the prefix "ROLE_" so if you have a user belonging to a group called "PG_JAVA_DEV), it would get mapped as ROLE_PG_JAVA_DEV within the security context. You can get more information on how the Spring security LDAP component works in Chapter 10 of the Spring Security guide.A detailed example of how to set up Spring Security to talk to the Active Directory is given here.

To set up Spring Security, you will need Spring Security 2.0.4 and Spring LDAP 1.3 (which has some additional dependencies mapped in them).Just map these to your Maven POM and you'll be good to go.

Monday 2 February 2009

Australian Open 2009: Rafa aces the FedEx yet again

Incredible! This one word sums up Rafa's game in the Australian Open 2009 final. As Jim Courier put it "How is this guy even on his feet?". But on his feet he was and for more than 18 hours 54 minutes of playing time which included close to 10 hours in the last 48 hours of the Open.
Incredible athleticism combined with a die-hard must win attitude cracked Federer's normally impassive veneer to make him visibly nervous and suddenly the 14th major for the FedEx looked further away. Federer's first serve percentage kept dropping and the double faults crept up to hurt him at crucial junctures and when he hit one long after saving three championship points, he handed the Australian Open to Rafa.
While Rafa has been gracious enough to acknowledge Federer as the 'greatest ever', it now appears as if it will be Rafa who now has a realistic chance of completing a Grand Slam in 2009. Also, Rafa has now beaten Federer in their last five encounters. If Federer wants to confirm his place as the 'greatest ever' in history, he will have to break this wall down or stand by and witness Rafa's elevation to that epithet.