Compilation Failure is the best Failure!

What I am going to write may sound really obvious but surprisingly I see this problem at soo many places that it deserves a post.

Here's a Rule 1: "If you are doing an instanceof and you are in control of that class, you can instead delegate to the class"

Meaning, if you were making a decision of what code to execute by checking an instanceof and the class being checked is the one that you control, as in the one that you can modify, add methods change code, then you should delegate the decision of what should be executed to that class instead.

Lets look at an example,


public static void speak(Animal animal) 
    if (animal instanceof Dog) {
         System.out.println("bhow.. bhow...");
    } else if (animal instanceof Cat) {
         System.out.println("meow.. meow...");

    } else if (animal instanceof Tiger) {
         System.out.println("Grrrr...");
    }
}

could really be changed to,

Animal animal = getAnimal();
animal.speak();

Where classes Dog, Cat and Tiger now will implement the speak() method.

It sounds really obvious doesn't it? If it really does, just do a search of instanceof keyword in your code and ask the same question. The "speak" operation comes very naturally to the Animal but sometimes you will find that since we don't think something is natural to that Entity or class, we don't add it there.

There is an anti-pattern called "Anemic Domain Model" which will specially suffer this problem. This pattern encourages to keep the Behavior separate from the Data. Which is what is happening in above example. One very simple way to find it is to see how many classes are named "Entity" and "EntityService"? If the EntityService operates on a single instance of the Entity like single animal, then  you have a problem. Don't confuse this with the one's that operate on multiple instances like a "EntityManager" or an "EntityDAO"

So what is really wrong with writing an if else-if  and checking instanceof to make decisions? Lets consider that in above example, we added another Animal Cow. Now what can happen is that in our first version of "speak" one can forget to go and add code for a Cow. This is perfectly fine, code compiles! We have implemented the Animal interface but there are no complains. This would only lead to either runtime failure or no failure at all! which ultimately leads to bugs. Think about a larger project where the references are at a lot of places. If the "speak" method is at the level of Animal interface, you have no choice but to implement this method (God help people who will still keep it empty)

Which brings me to my Rule 2: "Compilation Failure is the best failure!"

Printer in Ubuntu "Cannot prompt for Authorization"

I was seeing this issue since loong time but never really got to the crux of it. After getting extremely annoyed with this message after every time I tried printing something from a GTK app, I finally decided to solve this problem. Interestingly this problem does not come for Qt (KDE) apps. Only GTK (Gnome) applications are affected.

I found a solution in this post http://ubuntuforums.org/archive/index.php/t-829706.html

Simple solution for this problem is to copy your printer. That will create a new copy of the existing configured printer. Now just print using the newly created printer. Everything works now!!

Don't ask me how!!!

Myth about InputStream.read(byte [], int, int) method

I know it may sound stupid but only yesterday I discovered that
InputStream.read(byte [] data, int offset, int length) 
method can end up reading lesser number of bytes than length.

What that means is if you are doing something like,
InputStream in = getInputStream(); // getting it from somewhere..
byte data = new byte[10];
int read = in.read(data, 0, data.length);
System.out.println("Bytes read: " + read);
The you cannot gurantee that in.read() call will read 10 bytes even when the stream is connected / not broken (applicable particularly in sockets). My understanding was that if it ever returns lesser bytes than lenght, then the channel us broken and we should assume that connection is dropped from counter-party.

There is good explanation in javadocs about this method. The default implementation of this method at the level of InputStream class does this,
  1. It tries to read the first byte from the stream. If the read fails due to any other reason other than end of stream, then IOException is thrown. If end of stream is detected, -1 is returned.
  2. After the first read, if any of the subsequent read throws and IOException, It is swallowed and end of stream is assumed and the number of bytes read until this point are returned. If any of the subsequent reads detects end of stream, again whaterver bytes are read are returned. 
The javadoc also suggests the extensions of InputStream to provide a better implementation of this method. In SocketInputStream (not public in java api) which is what I was dealing with, read(byte [] data, int offset, int length) call delegates to a native method.

So out of my curiosity, I wrote a sample where I had a ServerSocket as a produces producing 2 bytes and waiting for 1 second. And a consumer using Socket which would attempt to read 4 bytes at a time without waiting.  The problem was easily reproduced and I could see my every read call in consumer only reading 2 bytes at a time.

BufferedInputStream provides a more convinient implementation of the read method. It repeatedly invkoes the multibyte read method on the underlying stream until
  1. Length number of bytes are read
  2. End of stream is reached
  3. Subsequent call to read will block. This is identified by calling available method on the stream.
But note the point 3, it still does not gurantee that it will always return length number of bytes unless end of stream is reached.

In my case our SocketInputStream was wrapped in the BufferedInputStream, which relatively shielded us from this problem but it aggrevated the problem as we saw this problem very rarely. Only after some code review is what we identified this issue.

Now a few questions in your mind maybe why call the multibyte read? why not just keep calling single byte read and track how many bytes we read? One of the grey hair in my company answered this question saying, 
Whe we use to do it in C, the multibyte read made sure that there were fewer stack pushes and pops as we would end up doing fewer read calls. 
I havent personally measured the performance gain by using multibyte read but Ithink it never hurts using it if you understand how it works.

So when does the multibyte read call wait then? Since it is capable of returing lesser bytes than requested, it can always just return with lesser data. My guess is that it will only block when there are 0 bytes avaialble to read. In a way multibyte read call will never return 0, as it will make an attempt to read at least one byte.

Good comparison of OpenESB and ServiceMix

This link gives very good comparison between OpenESB and ServiceMix ESBs

Quick way to untar and bunzip files in java

Have you ever felt a need to deal with bzip2 compressed tarballs in Java? Recently I compressed a lot of test resources in our source tree using bzip2 compression. The resources were static and were meant to change very rarely. bzip2 was the best choice in terms of amount of compression. What it also meant was my SVN download of the source tree would take much lower time. I planned to extract the resources and make them available at runtime while running tests.

If you already know, there is no way to handle bzip compression in Java core API. But at the back of my mind I knew that you can create bzipped tarballs using Ant. So I looked at the Ant tasks and figured that there was untar task which can be instructed to also process it through bzip2 uncompression.

Here is the code snippet that can untar and bunzip the file using Java code.
Untar untar = new Untar();
untar.setSrc(new File("./src/test/resources/files.tar.bz2"));
untar.setDest(new File("./target"));
UntarCompressionMethod compression = new UntarCompressionMethod();
compression.setValue("bzip2");
untar.setCompression(compression);
untar.setOverwrite(true);
untar.execute();

Also make sure that you put ant jar on the classpath.

Maven users can simply add following dependency,

<dependency>
   <groupId>org.apache.ant</groupId>
   <artifactId>ant</artifactId>
   <version>1.7.0</version>
   <scope>test</scope>
</dependency>

Disabling test under JUnit 4.4

Recently I had to disable a unit test in our test system. We use Maven as our build tool and JUnit 4.4 for unit testing. I had a few options,

  1. Exclude that particular class from tests under surefire plugins configuration in my pom.xml
  2. Remove @Test annotation from test method in the test class. Which fails as it finds a Test class but does not find any test methods
  3. Rename the class from say MyTest to something that does not end with "Test" say "Tezt"
I remembered that in TestNG you can just disable a test by saying @Test (enable = false) and I was desperately trying to find how to do this in JUnit 4.4. But to my dissappointment Test annotation only allows a couple of attributes, timeout and expected which did not do what I want.

After looking at the JUnit javadocs, I stumbled upon an annotation @Ignore it is indeed an annotation to be used if you want to skip the test.

So if you want to disable your test case in JUnit 4.4 just annotate your test methond with @Ignore @Test annotations.

What it also did was that Maven started reposting one test as being "Skipped" which is nicer as it keeps reminding you that you need to look after the test that you have disabled.

Wow! Thunderbird now reminds me to attach the file

Thunderbird 3.0.3 introduces a great feature that reminds you to attach a document in case you type words like "attaching" in your email. It scans through the matter of the composed email finds relevant word that may indicate that you may want to attach some file. Here is a screenshot.

Thunderbird 3.0.3 migration

I recently migrated to Thunderbird 3.0.3 There is a specific problem with Thunderbird that I have seen at times. Ever faced a problem of loosing your mail data and accounts after migrating to new Thunderbird? Thunderbird stores data under directory named .thunderbird in your home. But maybe just in Ubuntu or some versions of Thunderbird store it under directory named .mozilla-thunderbird. So  if after installing a new Thunnderbird and restarting it, if you are not able to see your e-mails, it might well be the problem of directory names.

To resolve, try renaming the directory (Whatever applies to you),

> mv .mozilla-thunderbird .thunderbird

or

> mv .thunderbird .mozilla-thunderbird 

Restart your Thunderbird and now you should be able to see your e-mails.

I have mostly seen this problem  when I install Thunderbird externally i.e. not through package manager. By installing I mean simply extracting it to /opt and not doing "./configure make install"

RIA frameworks and HTML 5

Recently when Steve Jobs wrote off Adobe Flash support in iPad, I went ahead and read more about HTML 5.0 and I am convinced that once HTML 5.0  is out, there will be no need of the fancy RIA frameworks like Apache Pivot and Adobe Flex.

HTML 5.0 introduces everything from videos, audio as well as shapes as part of the specification (introduces tags

But indeed HTML 5.0 is the future. If you are planning to develop your products based on Adobe Flex or Apache Pivot then think twice.

Why I like Twitter!

With only about 30 odd posts in last 4 years over here, and already about a 35 tweets in a couple of months, I really have started liking twitter. I ask myself why did I not blog so much? Answer is simple, everytime I tried to write a blog entry, I never had a restriction of number of words. I always felt as if I needed to fill the space (for no reason) With Twitter its just soo much more easy.

Animal Sniffer Maven Plugin

While looking at the Maven plugins at Codehaus, I just stumbled upon a maven plugin named Animal Sniffer. This plugin could be a great tool for people working on frameworks where API's are published to the user. There is always a problem of API loosing the compatibility across versions (for eg. making some method final looses backward compatibility). These kind of problems are very very difficult to trap.

I wanted to evaluate this plugin for instrumenting our internal API against which following versions can check compatibility. Firstly I am not sure if Animal Sniffer can be used in this way but I believe it could be.

I tried to play with the plugin a little where I figured out that vresion 1.5-SNAPSHOT cannot be downloaded from the Codehaus snapshot repository and older versions 1.4, 1.3 encounter an NPE when I run animal-sniffer:build goal.

Does anyone have experience with Animal Sniffer?

Apache Pivot, Platform for building Rich Internet Appplications

Apache Pivot is a framework for building Rich Internet Applications. Pivot seems to have taken inspiration from Flex and Silverlight.

Here is a snippet of overview from Pivot website,

"Pivot applications are written using a combination of Java and XML and can be run either as an applet or as a standalone (optionally offline) desktop application. Pivot includes features that make building modern GUI applications much easier, including declarative UI, data binding, effects and transitions, and web services integration." 

I would personally choose Pivot over Flex or Silverlight as it is easier to integrate builds into Continuous Integration and Maven. Not sure where Pivot will head to but it surely seems a promising technology.

Log4j 1.2.15 with Maven

I had a problem with Log4j version 1.2.15 where after adding a Log4j dependency, maven would try to download dependencies like java mail, jmx etc. The download never seem to finish as it is from some external repository. I am not sure if it was a temporary problem. I figured the problem was version 1.2.15 when I looked at the pom of that version and I could see some dependencies with specific repository.

Migrating to version 1.2.14 seem to fix this problem.

How I tricked my Ubuntu

My Ubunut was giving me problems starting up yesterday. After investigating for a while, I figured that it was due to corrupt reiserfs on /var So I set to fix the problem by running reiserfsch --check on my /var partition. I rebooted Ubuntu in Recovery Mode. To My surprise, I could not do fsck on my /var as it was mounted and some applications seemed to be using it. I always thought that Recovery Mode does not boot the installed kernel instead it boots an alternate image. This was merely a misconception.

Now with no Ubuntu live CD I could virtually not solve the problem! So I thought about tricking my Ubuntu. I edited my /var/fstab, commented out the mount /var and rebooted my machine. Now the /var was not mounted and I could run fsck on that partition. Some applications did complain about missing files etc. but it at least allowed me to run fsck on my /var partition. After fixing problems by doing reiserfsck --rebuild-tree I removed the commented line from fstab, mounted /var and switched to runlevel 5 and my Ubuntu is back fixed again

Runnable jar using maven assembly plugin

I faced a nagging problem of not able to create a runnable jar using Maven assembly plugin. I had a couple of dependencies which were required at runtime. I used the jar-with-dependecies assembly to achieve this. But my Main-Class attribute from the MANIFEST.MF was lost every time.

The solution to this problem is archive configuration under assembly plugin as described here

One thing I miss from KDE

About 6 months back I migrated to Ubuntu from OpenSUSE. I explicitly did not choose Kubunut as I did not really like KDE 4. It actually sucked! Before that my default window manager was always KDE and I thought about GNOME being lame. But not anymore!

There were a number of things that I missed from KDE most of which had alternatives in GNOME. But I still miss the cool feature of KDE where you could just open the "Run Dialogue" (Alt + F2) and use it as a simple calculator. You could say type "2*2" and press enter. It would do the calculation and show you the output instead of starting any application.

Havent found an alternative to that yet in GNOME!

Common we are in 2009! Use an IDE!!

I have seen a number of people using a naming convention of prefixing letter 'I' to names of any interfaces in Java like IHuman, IMonster. This was a very famous and essential practice about a couple of decades ago (I believe. Not that I am soo old!) I remember reading about prefixes like "lpstr" indicating that it is a "Long Pointer to the String". But all this was followed in C and C++. It was important at that time since the languages were not type safe and there were no mature IDE's. It would be easier to recognize that this pointer is to this this type of variable etc. which would prevent a lot of silly mistakes too. Seems like a few people with inclination towards Microsoft standards in .Net still like it and recommend it.

Personally I think we are in year 2009, I use IDE (Eclipse) for development and I have never needed to really see 'I' prefixed to an interface. It adds very less value. Only thing you can accidently end up doing is say do new Interface(); but your IDE will complain the very next moment anyway. I can always see what methods I can call by simply typing '.' In case I have any doubts. I generally use F4 to see hierarchy which will clearly show me that it is an Interface by coloring it purlple or I would directly F3 to get into code to see whats there.

People who still follow this beautiful practice, I am sorry but I absolutely hate it! for others, keep your Alt + Shift + R ready ;)

Environmental variables, Maven and Eclipse

If you use m2eclipse for building Maven projects in Eclipse, there are some issues while building projects having variables defined which refer Environmental Variables.

<properties>
<appserver.home>${env.APPSERVER_HOME}</appserver.home>
</properties>


Some of our developers came out with an idea of replacing the value locally to point to the directory on their machine like


<properties>
<appserver.home>/home/developer/software/appserver/</appserver.home>
</properties>


This lead to numerous incidents where developers checked in the hard coded path for this variable in our code repository, which lead to failed builds on our Hudson.

A better way to manage this is using profiles. You can create a profile in your settings.xml which resides under .m2 directory in your home directory on Linux (not sure where on Windows). Example profile configuration is as follows,


<profiles>
<profile>
<id>eclipse</id>
<properties>
<appserver.home>/home/developer/software/appserver</appserver.home>
</properties>
</profile>
</profiles>


Now you have a profile named eclipse which points to the absolute path for the varialbe appserver.home.

Now checking out / importing a maven project you can specify profile as eclipse. Or in an already checked out Maven project (note that the project needs to be checked out as Maven Project. Same thing will not work on simple java projects even if they use maven), go to project properties -> Maven and set the profile to "eclipse" (the same as id parameter in your settings.xml)

Now what this does is when you make build from eclipse, it uses the profile "eclipse" and resolves the variable to the right value. Advantage of this approach is that you do not need to change pom.xml of the project so it avoids the problem of checking in the hard coded values by mistake.

Spell check under Openoffice

Are you unable to do auto spell check in the OpenOffice applications? To make it work, you need to go to the application (oowriter, oocalc etc) menu, go to Tools -> Options -> Language Settings -> Languages and set the "Default language for the documents" to something standard. In my case "English (USA)" just worked fine.

Reliance Netconnect on Ubuntu

I am absolutely delighted as I write this post. I am using Reliance Netconnect on my Ubunut 9.04.

And if you are under impression that it was achieved after hours of googling, trial and error and after running hundreds of commands, then you are wrong.

To get it working,
  1. Plug in the dongle.
  2. Wait till the network manager applet menu shows "Auto Mobile Broadband (CDMA) connection"
  3. Best way to make it work is to select the "Auto Mobile Broadband (CDMA) connection" and let it figure out the settings
  4. After step 4, it tries to establish connection but obviously fails since the user and password are not provided
  5. Now, right click on the network manager applet and select edit connections.
  6. Go to "Mobile Broadband" tab
  7. Now there is already a configuration created named "Auto Mobile Broadband (CDMA) connection"
  8. Edit that configuration and add your user name and password.
  9. Now connect like in step 8 by clicking on Network manager applet and selecting the "Auto Mobile Broadband (CDMA) connection"
  10. Enjoy high speed mobile broadband on Ubuntu!
I am sure that same procedure will work with any latest mature Linux distribution. There could be issues bootstrapping the hardware but once its detected, NetworkManager will just work fine.