Showing posts from 2009

Singleton Pattern with Lazy Initialization On Demand Holder

This is one of the better ways to implementing a singleton class (class which lets user create one and only one instance).It uses Initialization on demand holder pattern.

public class Singleton {
private static class LazyHolder {
private static final Singleton instance = new Singleton();

public static Singleton getInstance() {
return LazyHolder.instance;

private Singleton() {
//initialize singleton instance

Using Apache Thrift for RPC over TCP socket

Apache thrift is a framework for cross language RPC service/client implementation. Thrift has a small IDL (interface definition language) and comes with an IDL compiler that generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages. First you need to download thrift and then install Cygwin (i used 1.7 version of Cygwin).Since the thrift is distributed in source form so we first need to build the thrift.exe IDL compiler. Follow this document to generate the compiler exe on windows (mine is Windows 7 home premium x64).Following is an example usage. We intend to write a C# service and a Java client that implement the following interface (hello.thrift):namespace java hello
namespace csharp hello

enum Operation {
ADD = 1,

struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: string comment,

exception InvalidOperation {
1: i32 what,
2: string why

service HelloService {


JSON-RPC between .NET service and Java client

Recently i experimented with Jayrock .NET library for JSON-RPC and following its tutorial it was very simple to run a .NET JSON-RPC service on my Win7 Home Premium (IIS 7.5). From my earlier experience with JSON-RPC in Java, i wrote a Java client using json-rpc-client library and could get the RPC communication happening without any issue.

The ease with which the code samples worked for the below helloworld-ish sample, motivated me to write about it. JSON-RPC is simpler to debug and implement than SOAP and can be secured by implementing it over https. Most of the time in systems integration all we need is getting the .NET software to communicate with the Java software and in such simpler scenarios where RPC mechanism which is firewall friendly like SOAP is needed then thats where JSON-RPC is a good option.

helloworld.ashx - the JSONRPCHandler

using System;
using System.Web;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;

namespace jayrockserv
public class HelloWorl…

No screens found error for X server in Ubuntu Jaunty

I recently encountered an error when all of a sudden after a reboot X server will not start up.
It stated the following:(EE) intel(0): No kernel modesetting driver detected. (EE) Screen(s) found, but none have a usable configuration. My configuration at the time of this error was: 9.04 x64, kernel 2.6.28-15-generic, with intel GM965 graphics running xserver-xorg-video-intel: 2:2.9.0~git20091007.03e8e64f-0ubuntu0tormod3~jaunty First i selected recovery mode to boot into last good configuration.
I struggled with it for a while trying some of the suggestions i could find on google searches. In the process i tried upgrading to the linux kernel 2.6.30-020630 and tried enabling the KMS (Kernel Mode Setting) for Jaunty as described here and that fixed the issue.

Enabling RDP on Windows Vista Home Premium 64 Bit (SP2)

I found the hack mentioned in this blog work for me pretty well. Am able to RDP from Win XP Pro to my Vitsa Home Premium 64 Bit SP2 with no issue so far.

Update: For enabling RDP on Windows 7 Home Premium, refer to this forum post. It worked for me.

Some useful Eclipse shortcut keys

Cnrtl + O - show all methods and properties of the current editor's class.Cntl + Shift + O - fix import statements.Cntl + Shift + F - format code.Cntl + 1 - show quick fix recommendations.Cntl + F6 - toggle between editorsCntl + E - show all open editorsAlt + left/right arrow - move to last edit locationCntl + M - toggle maximize and minimize current editorCntl + J - incremental search (searches as you type)Cntl + Shift + J - incremental backwards search.Cntl + K - find nextCntl + Shift + K - find previousCntl + Shift + L - Shows currently defined shortcut keys (same as Help > Key Assist).Cntl + L - go to line.Cntl + Shift + G - generate getters/setters.Cntl + Alt + G - find text in workspace.

Eclipse 3.5 Galileo and clearcase plug-in

I installed the Eclipse Galileo recently and found that the Rational Clearcase plugin by IBM does not work anymore in 3.5. Of course the latest plug-in explicitly mentions its just for Eclipse 3.4. But i tried anyways and it did not work. Fortunately, there is a clearcase plugin that works with Eclipse 3.5 at this point ( May be soon IBM clearcase plug-in will also be released that is compatible with Eclipse 3.5.

Updated: IBM Rational clearcase SCM Adapter version is available too now for Eclipse 3.5.

Upgrade to Ubuntu 9.04

I was pleasantly surprised to see the dist upgrade to Ubuntu 9.04 from the previous 8.10 installation going well. It did not work so well last time during upgrade from 8.04 to 8.10. It crashed the root file system for me then. But the lazy me, hoped that it will work this time and it did and upgrade went fine. Web is already replete with praises for jaunty release.

Installing Subversion 1.5.3 on windows

Steps are: Download subversion installer from Install at location say C:\svn. At cmd prompt, create a repository: svnadmin create "C:\svnrepo"Edit C:\svnrepo\conf\svnserve.conf and uncomment the following lines: anon-access = read
auth-access = write
password-db = passwdEdit C:\svnrepo\conf\passwd file:add users in the format username = password one per line and remove the default users. Set environment variable SVN_EDITOR to your favorite text editor (say Textpad.exe). Set Subversion as a service with the following command at cmd prompt: sc create Subversion binpath= "c:\svn\bin\svnserve.exe --service --root c:\svnrepo --listen-port 9999" displayname= "Subversion Repo" depend= Tcpip start= autoGo to service control manager to start the Subversion service or type the following on : net start “Subversion Repo”

Import an existing project in subversion repo:

svn import C:\myproject svn://localhost:9999/my…

Introduction to Vim for Programmers

Vim editor:

It has 3 modes:

Ex mode <----> Command mode <-----> Insert mode.

You can never go from Insert to Ex mode directly or vice versa. You always
first need to go to command mode. Enter command mode by typing Esc and enter Ex mode by typing :.

For cursor movement:

h l
Cursor movement commands: {motion} commands:
G - Last line in file
gg - First line in file
% - Jump to matching brace (()}{[])
m{1etter} - example ma to mark a line. There can be at
max 26 marks in the file.
`{mark} - example `a to jump to the named mark.
`` - Jump to the last line where you jumped or searched from.
/expr - forward search where expr = is a regex.
?expr - backward search.
n - repeat last search in the same direction.
N - repeat last search in the opposite direction.
w - move cursor forward by one word.
b - move cursor backward by one word.
Some more cursor movement commands:
Cntl + F - move one screen forward.
Cntl + B - move one screen backward.
f{char} - positi…

A short introduction to GNU make

A make rule looks like the following:

target: prerequisite

.PHONY: all
all: program1 program2
program1: a.o b.o
gcc -o program1 a.o b.o
program2: c.o d.o
gcc -o program2 c.o d.o

This tells make not to search for a file named all and to assume that this target
is always obsolete.

Defining variables:

Variables in a make file are like in an ant build script but they
can only be set once unlike ant properties and dont change value.

FLAGS = first
@echo FLAGS=$(FLAGS)

FLAGS = second
@echo FLAGS=$(FLAGS)

The FLAGS variable is assigned the last value which is set in the build file
viz. second and so even if we run all target, the value echoed for FLAGS
will be second.
* So, a variable should be set just once in the make file. *

VAR = value
VAR := value

The := form allows variables to reference themselves without recursion.

VAR = value
# Wrong! Causes infinite recursion
VAR = $(VAR) more

# Okay, the := prevents recursion
VAR := $(VAR) more

# A GNU extension that does the same thing

VNCServer Configuration

In the ~/.vnc/xstartup we can configure vnc session to start in gnome or kde, as shown below:
# Uncomment the following two lines for normal desktop:
# exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
xterm -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &            
/usr/dt/bin/dtwm &
#startkde &
unset DBUS_SESSION_BUS_ADDRESS;gnome-session & Then start vnc server as:
$ vncserver  This will tell you the hostname:sessionnum and set the password:

$ vncpasswd Optionally you can change the screen size for your session as:
vncserver :displaynum-geometry 1920x1080 Now you are all set to connect with your favorite vnc client (i use ultravnc).

For XFCE desktop change the xstartup file to:
# Uncomment the following two lines for normal desktop:
 exec /etc/X11/xi…

Port Forwarding : Accessing your home computer from work

In order to access the home ubuntu desktop from work, following is what i needed to do:
Installed open-ssh demon on ubuntu with: sudo apt-get install open-sshTested that i could use a windows box on the home lan to access the sshd using putty.Once sshd is setup, followed the instructions at, which is broadly the following:
First we need to setup static/manual IP on the host computer to which we want to forward the port(s) to. Mine was setup to obtain IP address using DHCP so i first changed that to manual.Then login to the router (mine is an Air-link AR670W) and enable Port Forwarding for the SSH application port 22 for both TCP/UDP protocols and provided the static IP of my ubuntu desktop. Saved the configuration and router reboots after that.
Then i noted down the ISP assigned public IP to my router's external interface and used that IP (external IP of my router) from a windows box to putty to. Since the SSH port 22 on router is c…