Monday, October 15, 2007

User defined types with JsonMarshaller

I came across the JsonMarshaller project while searching for a Java library for unmarshalling JSON. This is a nifty little framework and uses Java 5 annotations for marshalling and unmarshalling JSON.

JsonMarshaller provides simple annotations @Entity and @Value for marking up your classes for marshalling and unmarshalling. However, I wanted something more than what the base framework provides - the ability to convert dates back and forth. The framework supports a so called type option to specify a user defined type for a value. This is a cool idea for extending the basic types supported out of the box. The (simplified) code for my DateType class that solved my problem is below:



public class DateType implements Type {

@Override
public Class getReturnedClass() {
return Date.class;
}

@Override
public Object marshall(Date entity) {
SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
return format.format(entity);
}

@Override
public Date unmarshall(Object object) {
String date = (String) object;
Date d = null;
try {
d = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return d;
}
}




This simple class can read a String representing a date in most commonly used formats and return a java.util.Date. It can also read a java.util.Date and return a String representing that date.

Create your family tree with Geni

I came across this really cool website that let's you create your family tree. It's got a cool interface that let's you and family members you invite add new members to your tree. Really cool - loved it.

Claiming my blog in Technorati

Technorati Profile

Thursday, October 11, 2007

TightVNC client getting killed after Windows locked?

If you are wondering, like I was, why your TightVNC viewer dies after your Windows workstation running the server is locked, here's the answer.

Briefly, from the link above:
"This is a standard behavior. You have to run TightVNC Server as a service if you want to avoid this problem."
So there.

Pidgin + Pidgin plugin pack on Ubuntu

Pidgin is an open source multi-protocol chat client. Since I use AIM at work and most of my friends listen in on GTalk, I decided to install Pidgin. A quick Google search unearthed this link. But hey, what's the point of being on Linux and not performing the configure-make-make-install drill? Besides, I wanted the Pidgin Plugin Pack as well (which btw can also be installed by following the instructions here without building the sources). This pack has an extremely useful plugin - Mystatusbox - that allows you to selectively set the status of each of your IM accounts. So here's how I got around to getting both Pidgin and Pidgin plugin pack on my box:
  1. download pidgin 2.2.1 source from sourceforge
  2. downloaded various dependent sources (libxml-dev, libgtk-dev) and installed these
  3. ./configure && make && sudo make install
  4. downloaded the pidgin plugin pack from here
  5. ./configure && make && sudo make install
I threw in a desktop launcher for good measure, and I was good to go.

Friday, September 21, 2007

Refactoring your tests

Martin Fowler's seminal book on refactoring speaks about the various code smells that you need to watch out for to keep your code healthy. Along with refactoring your code however, it's essential that you refactor your unit tests as well. Here's a nice paper that I found that describes various unit-test smells.

Refactoring and unit tests

It's an established fact that refactoring needs a strong scaffolding in the form of unit tests. If you have a good unit test suite, it gives you that extra boost of confidence to refactor your code, knowing fully well that you are not breaking anything. All this is well documented by several people way smarter than me, but here's an instance where unit tests saved the day for me.

What I had was a class similar to the one below:



public class Foo {
public Foo() {
initThis();
initThat();
initTheOther();
}

public void someOtherMethod() {
}
...
...
}




I figured that the code above might not be the most optimum since all the initXXX() methods were not exactly cheap and calling them eagerly on class construction was not exactly necessary. Therefore, I fired up eclipse and moved the three calls into a single public method init() that could be invoked by a callee before using an instance of foo(). I was happy that I had achieved a neat little refactoring by postponing heavy initialization to when it was needed.



public class Foo {
public Foo() {
//nothing here now
}

/* Call this before you use this instance */
public void init() {
initThis();
initThat();
initTheOther();
}

public void someOtherMethod() {
}
}




But, on running my unit tests for someOtherMethod(), I started getting inexplicable NPEs all over the place. This made me realize that something that happened in one of the initXXX() methods was required for executing someOtherMethod() successfully. That led me to add a call to init() in the constructor - something that wouldn't have come to my notice until somebody had actually run the application and reported a bug. Unit tests saved me a lot of time that I would have had to spend much later.

Agreed, I probably should've redesigned my class to call init() from someOtherMethod(), but that probably wouldn't have been the best choice if all the other methods in my class depended upon init() being called beforehand.

Saturday, August 25, 2007

Hello, World!

Everyone and their uncle blogs these days. So why not me? I intend to publish things here that strike me as interesting.

Stay tuned...