Tuesday, March 8, 2011

Yet another Singleton - Java !

Multithreading is undoubtedly one of the most interesting topics in Java and also Java's one of the best features or abilities. Code involving multithreading are difficult of debug and visualize.However with careful state diagram of the program under multiple threads makes the life of a programmer much easier. Of course debuggers come in handy to tackle these issues.

Long time back I read quite a lot about singleton pattern and how DCL effects it and other things related to it. Of course the syncronized version of it would certainly work but cost involved is quite high , knowing the fact that after the singleton instance is initialized , the syncronized method call to getInstance is fruitless and the overhead of acquiring the lock is useless . Finally read about the new pattern - demand holder pattern which solves the purpose.It's the most elegant and neat code for the DCL problem.However I was still looking achieving a singleton , without using syncronization (lock based syncronization) and without using the static inner class way (demad-nholder pattern). Below is my work in similar lines , its not neat (its clumsy) , but it solves the purpose. However if I have to use then I would go for demand holder pattern , after all what good is a code which is complicated and developers have to break their head understanding it.

public class Singleton {

private static final AtomicBoolean WORK_IN_PROGRESS = new AtomicBoolean(false);
private static boolean initialized = false;
private static Singleton instance = null;

private Singleton(){}

public static Singleton getSingleton(){
if(WORK_IN_PROGRESS.compareAndSet(false, true) && initialized == false) {
instance = new Singleton();
initialized = true;
} else if (initialized == false) {
while(WORK_IN_PROGRESS.get());
}
return instance;
}
}

[Compiled with JDK 1.5+ ]
I haven't run any tests on it. If you have any scenario in which it would break then drop a comment.

Thursday, November 25, 2010

How RESTful should it be ?

Can restful programming methodology be followed religiously under all circumstances or it should be violated for good [whenever required] ?

This is the question that I am trying to deal at the moment. Actually I do not consider it to be a technical challenge , nor a tedious design. However , this discussion is all about how religiously it should be followed.

Issue : Suppose there exists a service which takes a username , password and returns a list of all the entities which this user has under his account. The method should only return the entities after the credentials are verified.

Now , we need to expose this service as a RESTful service.

Ideally , it should be something like the following , GET because , its for only fetching , no change to the data is made.

GET /context/serv?username=uname&password=pwd

Issue : The credentials are in clear text.

Below are some of the solutions that I could think of , please comment if you have any better idea.

Solution 1: use POST over HTTPS. However , POST is a bad choice of method for this read only service. However it’s the simplest one that I can think of.

Solution 2: Make a request to another service (call it AUTH service - POST over HTTPS) with credentials , the service would return a key/hash , this key is to be passed along with the subsequent service call. So our service would be something like,

GET /context/serv?username=uanme&hash=2452klh52hj5=

The issues here is hijacking of the hash. Another issues is maintaining the hash value in the service and taking care of timeouts of this and invalidating it after timeout or on request of the AUTH service.

Solution 3: Let the authentication be handled by the web server hosting the service. Any of the authentication measures provided to us by the vendor can be used to do it . BASIC , DIGEST,CLIENT-CERT and our main service url looks like.

GET /context/serv?username=uname

The authentication will mostly use the HTTP headers and will be transparent to user.CLIENT-CERT would be good considering the strength of it.

The problem with this is , that it becomes fairly hard for the service consumers to code the client application.

Considering the above mentioned solutions , I think the solution 1 is the simplest one. However , you now no longer call it a perfect RESTful service (as it uses POST of read only stuffs). But who cares as long as it’s properly documented and works way we want it to. So I concluded that it might not be good to follow some standards , unless it's tightly driven by some industry standard , for everything else – BREAK IT.

Wednesday, October 6, 2010

It's killing me.

From the last few days I been thinking about a question and it is now literally haunting me.I do not know why I cannot stop thinking about it.So thought of writing with the hope to get some pointers on this from someone knowledgeable or from someone who thinks that he has a logical explanation for it.

Here goes it (sigh)!

Say suppose there is a very huge site (terms of traffic that it gets) , like Facebook.com , which draws a lot of users.This question is about scalability. Below I have mentioned some of the technical details , which might be worth going through to understand the question that I am going to state later.

When a web page opens up in our browser , it establishes a persistent TCP/IP connection.It’s persistent because it keeps the connection open and uses the same connection for multiple HTTP request response pairs.It a connection remains open for certain amount of time , then the client/browser closes it by sending a TCP packet with it’s fin bit set. So there exists a communication channel with a port in the user’s workstation and a port in the server hosting the website. Now the port number is specified using 16 bits unsigned type , which means that there can be at most 65536 ports in a computer. I am ignoring the fact that ports below 1024 are reserverd.

So the question is , is it the fact that at any given point of time no more that 65K users are online concurrently ?

I am sure that it’s possible but how ? If you have any pointers then please leave a comment.

Tuesday, July 21, 2009

HTML readonly vs disabled.

Here is a quick note about the differences between readonly and disabled HTML element.

Both the elements are rendered into the browser space , but disabled looks like a litter faded out.If a text field is readonly then you can copy the text (no question of writing to a disabled or a readonly text field.) , but in case its disabled you cannot even copy the text from the text box.If you have a HTML form with one of the fields as disabled then when browser sends the data to server on submit , then it doesn't even send the data of the disabled fields , but the data of the read only fields are sent to the server.

Tuesday, July 7, 2009

typeof method of javascript.

Continuation to Mind Unleashed: undefined and null w.r.t javascript.

I was trying to do some checks using typeof operator , but that didn't work.The reason was that I didn't know the return type of the typeof method and compare was failing.

Here is what I tried ,

var a ;
if(a == undefined){
alert('a is undefined'); ==> prints 'a is undefined'.
}

now I tried ,

var a;
if(typeof(a) == undefined){
alert('a is undefined'); ==> nothing.
}

the reason is typeof method returns the typeof parameter a , but the type itself is a string.

var a;
if(typeof(a) == 'undefined'){
alert('a is undefined'); ==> prints 'a is undefined'.
}

however the easiest way to check for undefined would be to use == operator.But using the typeof method is still better.Why ?

If a variable is not defined at all or a variable is defined but not initialized then that variable is considered to be undefined.

consider the following two snippets of code where the variable is not declared at all.

if(a == undefined){
alert('a is undefined'); ==> error ! from the javascript interpretor.
}

and ,

if(typeof(a) == 'undefined'){
alert('a is undefined'); ==> prints 'a is undefined'.
}

so, using the second approach would be a better as it check both the cases. ie , variable defined , but not initialized and the variable not defined at all.

Tuesday, June 30, 2009

Twitter Twitter Twitter !!

I have just started using twitter.I am not very attracted to other social web communities like orkut , facebook. Though Twitter isn't like that but its quite cool.You can have updates about your self and people wanting to get those updates can follow you.No much of personal information is required to register for a Twitter account and its quite simple to use.

Follow me on twitter , http://twitter.com/rahulrecdgp
--Rahul

Friday, June 19, 2009

undefined and null w.r.t javascript.

This is more of like web notes that I am putting in here. I usually do not work a lot in javascript , but recently I been amazed with the capabilities of javascript.So though of putting posts here which I think might be useful to me later point of time for reference as might help others as well.

Initially I was irritated with the debugging process of javascript.Though its name is javascript , there are a lot of things which are totally for javascript without any relation with Java , but syntactically , its just like Java.You might google to get more information about the history of javascript and how it evolved over the time.

Initially I was quite confused about the differences between null and undefined in javascript language.Programmers of other programming languages might be aware of null , but undefined is quite new.In javascript world anything that hasn't been defined yet or a variable which has been defined but have not been initialized is considered to be undefined and its a keyword and not a object. Well , this is the main difference between null and undefined.null is an object , whereas undefined isn't.

There are two useful operators in javascript which are very useful in order to do some coding experiments with js.
1) typeof A - this returns the type of an variable A.
2) A instanceof B - this returns if A is a type of B or not.

Try the following ,

1) alert(typeof a); ==> undefined
2) var a ;
alert(typeof a); ==> undefined as a has been declared , but not initialized.
3) var a = null;
alert(typeof a); ==> object as null is a type of object.
[use if to check for null , like if(a == null){}]

I try to initialize every variable with null , so reducing the undefined checks from the code and replace null (placeholder) using the appropriate object at later point of time.

Like java , all the javascript objects extend properties and methods from javascript's object class.
The javascript's Object has a toString method.Now if null is an object then why the following gives an error.

var a = null;
alert(typeof a); ==> Object
alert(a.toString()); ==> error ! -> a is not an object or null.

why is that so ? I think its a special exception in javascript for the null object.