Sunday, February 28, 2016

The Story of JNU And Why I love Smriti Irani


As I was listening the speech of our HRD Minister, Mrs. Smriti Irani, a catchy phrase, taken directly from a news channel, came to my mind – Saas Bahu Aur Saazish. For those who don’t understand Hindi, Saas = Mother in law, Bahu = Daughter in Law, Saazish = Conspiracy. For every household story, Saas and Bahu represents two sides. For the same incident if you listen the narration of Saas, it will be very different from Bahu. Every story has at least two sides. Sometimes even more. But in this era of social media we want to stick with one side of story. The story which suits our ideological prejudice. So much so that we don’t shy away in revealing our foolishness to everyone. I was not surprised when I saw literate IT professionals sharing a post claiming that Ratan Tata has said that they will not hire anyone from JNU. Another one said that Umar Khalid is a terrorist linked with Jaish e Mohammed, another IT professional gave a breaking news that Manish Tiwari (Congress spokesperson) is not a Hindu indicating that his wife has a Muslim name (Actually she is Parsi, but should it matter?). I have come to terms with the fact that education may not necessarily translate to wisdom.

The story of JNU has three sides:

First side of the story is anti-India sloganeering. In the capital of a sovereign country, to hear any anti-national slogan is heartbreaking to say the least. It is insane to compare this with anti-India sloganeering in Kashmir. Kashmir has a history and we know that there is militancy in that region. But that does not mean that we can let them go who have audacity to raise anti India slogans in the very capital of the nation. I wonder why they are using any facility in India if they have so much problem with this country.

Second side of the story is the event which was organized by a group of students against the death penalty of Afzal Guru. This is the gray side of the story. No matter how much we like or dislike but the fact is that having a dissent view on the death penalty of a convict is not a crime. A section of society thinks that Afzal Guru was the victim of circumstances and that he was made a scapegoat to satisfy the collective conscious of the nation. I do not want to go in the merit of these arguments. But the point is that if someone has this view, he has every right to express the same. It does not make anyone anti national. If someone believes that Afzal guru actually conspired the attack on Parliament and that attack was the right thing to do then certainly it is anti-national. But if one has a view that Parliament attack was wrong but Afzal Guru was not guilty then it does not make anyone anti national. We have people, including lawmakers, who believe that Nathuram Godse did the right thing by killing Father of the nation. We may like or dislike this view but the fact is that we are accommodating this view. It is not that they think that killing of Father of Nation was wrong but Nathuram Godse was the victim of circumstances. They openly endorse the killing itself. Those who want sedition charges to be imposed on these JNU students, do they have the same view for these Godse Lovers? Personally i support these hangings but i am ready to listen anyone who think otherwise without labeling him as anti national.

Third side of the story is Kanhaiya Kumar himself. A video was aired where Kanhaiya Kumar was demanding Azaadi. Later it came out to be a doctored one. In fact the original speech is quiet patriotic unless for you the constitution of Nagpur is greater than the constitution of India. Unless for you being anti RSS is equivalent to being anti India.

Our nation is going through a phase where patriotic jingoism is confused with nationalism. Where national flag has become a stick to beat others. We may or may not like Smriti Irani. But she is a great reminder of our Indian Saas Bahu Soap. Which tells that every story has two extreme sides narrated by the Saas and the Bahu, but the truth lies somewhere in between. I love Smriti Irani because her face reminds me of this fact. So let us stop behaving like Saas and Bahu of Hindi Soap, and understand the conspiracy of our political class.

Wednesday, June 18, 2014

Being Siddhu

Even after staying in US for more than four years now, I still feel that my sense of humor is pretty much desi. It’s not about understanding the language; it’s about aligning your sense of humor with a different culture. There are people who do this seamlessly. At least they pretend to do it. But for me it needs effort which is no less than developing a high end algorithm (I don't remember when last time I did that).

Today I was in one such trap. The occasion was a party in buffalo wild wings thrown by my client. I am used to these types of parties and ready to take usual kind of jokes. I don't think that I have developed any taste for these jokes but I have learned to laugh at the right time and in the right amount. But I had a bigger problem in hand, the FIFA world cup. While I am still coming to terms why football is called Soccer in America and why the game which is called football here has hardly any use of foot. I am up with a new challenge. I not only need to enjoy FIFA world cup, I also need to know the history and Geography of each player. My client loves Soccer and my manager has already told me that though I am good in technology but I need to work hard on relationship management. I am sure I am not good in technology but I love when someone praises me. I am also sure that my manager’s presumption about my relationship management is correct. And if my client loves Soccer then I must have some view, if not love, for this game. I should be able to appreciate the game, its nuances and the jokes attached. I needed a guide for dummies on Football teams. I googled it, found one and went through it quickly. I realized that it is not worth. I should continue my technique of smiling and laughing as it is.


So at the end I was sitting in front of my client who was cracking some joke related to one player (I don't remember even his name) and I was laughing like anything. The trick is timing. You need to understand when the right time to laugh is. Once you get hold of this you need not to worry about what the person is saying. While I was laughing with full intensity, all of a sudden I felt like Navjot Singh Siddhu. What is the difference between him and me? Why I used to hate this person for his nonsense laughter? His primary job is to laugh and he laughs. I am not the anchor of any laughter challenge show. So to laugh is not my primary job and still I am laughing for my job. While I was returning from that party I developed a sense of respect for Siddhu. It is not easy to laugh on something which doesn't make sense. But it is even harder to lose your sense and start making sense out of all nonsense. Siddhu has achieved this state. I have yet to achieve Nirvana.
At The Corridor of Death


Going to that homeopathy doctor was always a big task for me. More often because he was very unfriendly and rude. But for my cousin sister, who was suffering from cancer in its critical stage, this was the only hope. The blindness that she developed because of brain tumor, not only sieged her vision but also her dreams. All attempts had already failed and probably this was the only thing which was giving her, if not others, some sort of assurance. As I was about to go to that doctor, I called her to know about her health (The doctor is in Bangalore where I live and my cousin was in Ghazipur, a small town with almost no medical facility). She started talking about her pain and helplessness, and in between she said something which I was not expecting in that course of discussion....."Help others as much as possible because once life goes out of hand, it never comes back". I was sort of taken aback. I asked her again to repeat her sentence, pretending I could not hear, but she never repeated it. May be she was thinking that the sentence was too preachy to repeat. This was a suggestion which for some reason left a permanent mark in my heart and mind. May be because it came spontaneously from someone who has already lost control over her life. Someone who, all of a sudden, at an early age, finds herself at the corridor of death. This was my last conversation with her. After few days, while I was busy in my overseas worldly affairs, I got the news about her death.

Whenever I am in doubt, whether I should lend a helping hand to others, whether I should bear a loss simply to help someone without expecting any return, whether I should put myself in a mess which I can easily avoid. I remember that one suggestion given by her and it clears all my doubts.

Though she has left us, but I wish, may that one sentence never leave my heart...Amen

Thursday, February 12, 2009

Interpreter Design Pattern:

This pattern provides a definition of macro language or syntax and parsing into objects in a program. You use this pattern when you need your own parser generator. It is also used when you have to translate a specific expression or when you need to handle a tree related information.

Proxy Design Pattern

Proxy is not a new term for a person like me who has dubious record in school days in terms of attendence. Me and my similar thinking friends were helpful for each other in giving attendence. When one of us would be absent, other will give his attendence. This other person is called a proxy for the original one who is absent in the class and enjoying good times. Thus proxies acts as a place holder for original object.

Example:
1) Using Java XML Pack, you use proxies to access web services with JAX-RPC
2) Loading image icon which is taking time, we first contruct a frame for it using proxy object and later load image.

Facade Design Pattern:

Facade means concealment or hiding the details. Facade pattern make a complex system simpler for the client by providing a unified or general interface which at a higher level to these subsystems. Facade pattern gives an easy entry point to an otherwise complex system.

Example:
JDBC design:
A database design is complicated. JDBC is used to connect the database and manipulate data without exposing details to the clients.

Builder Design Pattern:

Process of step by step construction of complex object using simple objects. The Builder pattern allows a client object to construct a complex object by specifying only its type and content. The client is shielded from the details of the objects construction. It gives a finer control to the client over construction of the object.

Abstract Factory Design Pattern

Abstract Factory Design Pattern:
In the Factory design pattern, there is a Factory class which create objects. Simply speaking Factory class is the factory of objects. Abstract Design Pattern is one level above the Factory design pattern and provides Factory of factory. Thus Abstract Factory Design Pattern is used to return one of several factories.

Examples:
In java 1.2, the pluggable look-and-feel classes accomplish this at the system level so that instances of the visual interface components are returned correctly once the program selects the type of look and feel

Factory Design Pattern

Factory Design Pattern:
Factory design pattern is all about having "factory of objects". Here we declare a class (factory) whose job is to instantiate appropriate object out of various objects in parallel hierarchy based on the input given by calling program.

Implementation:
Consider a base class Namer. Suppose it has two subclasses LastFirst and FirstFirst. Then we have a factory class NameFactory. The factory class instantiates either LastFirst or FirstFirst depending upon the String name parameter received from the client.

public class Namer
{
private String firstName;
private String lastName;
public String getFirst()
{
return firstName;
}
public String getLast()
{
return lastName;
}
}

public class FirstFirst extends Namer
{
pubic FirstFirst(String s)
{
int i = s.lastIndexOf(" ");
if(i > 0)
{
firstName = s.substring(0,i).trim();
lastName = s.substring(i+1).trin();
}
else
{
firsName = "";
lastName = s;
}
}
}

public class SecondFirst extends Namer
{
public SecondFirst(String s)
{
int i = s.indexOf(",");
lastName = s.substring(0,i).trim();
firstName = s.substring(i+1).trim();
}
}

class NameFactory
{
public static Name getNamer(String name)
{
int i = name.indexOf(",");
if(i > 0)
{
return new LastFirst(name);
}
else
{
return new FirstFirst(name);
}
}

class Client
{
NameFactory.getNamer(shakil,shahnawaz).getFirst();
NameFactory.getNamer(shakil,shahnawaz).getLast();
}


As we can see in the above example, client is printing the first and last name without knowing which class is being called by the factory class.

Examples of Factory Design Pattern:
1) COM Application
The factory role is poayed by IClassFactory
2) Standard Template Library (STL)
3) EJB:
Creation of entity beans
4) CORBA
CosNaming.Factory plays the role of factory.

Adapter Design Pattern

Adaper Design Pattern or Wrapper Pattern:
Adapter is not a new word in english dictionary. You use adapter to connect your laptop with the electric socket. You can view your laptop as a class and the electric socket as an interface. Your laptop (class) wants to get some functionality via the interface (electric socket) but it does not understand this interface (electric socket). In other words you can say that laptop and electric sockets are incompatible. To make them compatible you require adapter.
Thus adapter design patter convert the existing interface (electric socket) into an interface(adapter) which the client(laptop) can understand. Thus Adapter design pattern achieves compatibility and reusability of the unrelated classes in an application.

Examples of Adapter Design Pattern:
The famous adapter classes in Java API are WindowAdapter, ComponentAdapter, ContainerAdapter, FocusAdapter, KeyAdapter, MouseAdapter and MouseMotionAdapter

Singleton Design Pattern

Singleton Pattern:
This pattern is used to ensure that only one instance of a class is created which is accessible globaly in an application.

Implementation:
The definition of singleton pattern is probably the easiest among all design patterns. However its implementation requires a bit of care, as we will see here. Consider the below code:

Program 1:
public class MySingleton{
private static MySingleton instance = null;
private MySingleton() //This constructor will ensure that default constructor is not created
{
}
public static MySingleton getInstance()
{
if(instance == null)
{
instance = new MySingleton();
}
return instance;
}
}


Here we are declaring MySingleton as static to make it available globaly in an application. The constructor is declared as private so that any client creatin object of this class is bound to call getInstance() function instead of calling the constructor. Inside the getInstance() method we are creating the instance of MySingleton only if the instance is null. Thus once an instance is created, other instances of MySingleton class cannot be created.
Above implementation of Singleton pattern is correct to a large extent. However this implemenation may fail in certain scenarios as following:
1) If class is loaded by different classloaders.
2) If a Singleton object is serialized and then subsequently deserialized
3) If two "threads" call getInstance() method at the same time.

Here we will ignore the first two scenarios because I want to give you an opportunity to do research. Now let us consider the third scenario. I think by the time you have read these lines, and if you are a true Java nerd, your suggestion should be ready. The obvious suggestion is simply synchronise the method getInstance() something as following:

Program 2:
public class MySingleton{
private static MySingleton instance = null;
private MySingleton() //This constructor will ensure that default constructor is not created
{
}
public synchronized static MySingleton getInstance()
{
if(instance == null)
{
instance = new MySingleton();
}
return instance;
}
}


Brilliant !! ... so the issue is resolved and we have just implemented a singleton design pattern which is thread safe.
Though above implementation is thread safe but synchronisation is very expensive thing to do as far as performance is concerned. There is an easy and performance wise better approach to do this. See Program 3:

Program 3:

public class MySingleton{
public final static MySingleton instance = new MySingleton();
private MySingleton()
{
}
}


Since static member variables are guaranteed to be created, the first time they are accessed, so this implementation is thread safe. Both Program 2 and Program 3 have there own advantages/disadvantages. Program 2 is performance wise not good but it gives flexibility of changing the number of allowable instances. Suppose later you decided that i should allow three instances of this object to be created, all you will need is a change in the if condition of Program 2 without touching the client side program. Program 3 does not give this flexibility. But as we know when it comes to performance then Program 3 has a definite edge.

Examples of Singleton Design Pattern:
1) One file system, one window manager, one printer spooler, one Test engine, one I/O socket etc
2) Java serial port package viz javax.comm

Tuesday, February 10, 2009

EJB Basics

Enterprise Java Beans:
EJB is both a set of specifications (often called contract) and API. Any application server which is EJB conformant should follow this specification and provide implementation to the API.

What is the use of EJB?
Suppose you have to develop a huge distributed application having complex business logics. While developing such an application you need to take care of several factors like security, transaction management, Remote method invocation, Threading, system management etc to name a few. Thanks to application servers which take care of all these issues. This is possible because these application servers follow certain specifications. If this specification is EJB specification then they are called EJB conformant application servers (e.g. BEA Weblogic, IBM websphere). Further they also support EJB API and all you need to do is to call or override the required functions. Thus you can see that EJB simplifies the development of business component. However if your application is very small having very less business logic then using EJB is not a good idea. The purpose of using EJB is to simplify the development of complex distributed business components and not to increase the complexity of simple business components!!

Types of EJB:
1) Session Bean
a) Stateful
b) Stateless
2) Entity Bean
a) Container managed persistence
b) Bean managed persistence
3) Message Driven Bean

Session Beans:
You write business logic in session bean.
Non persistent
Short lived – life terminates with the termination of client
It is non sharable i.e. it can have only one client at a time.

Stateless Session Bean:
Stateless session beans, as the name suggests, don’t maintain state information.
Because these beans don’t contain state information, same been can be used by more than one client (but one client at a time)
Stateless session beans can be pooled by the container
Since stateless session beans can support multiple clients the offer better scalability
Since stateless session beans are never written to secondary storage, they give better performance.
They can implement web service.

Writing Stateless Session bean:
Step 1:
Write the Remote interface:

ShakilRemote.java (Remote interface)

import javax.ejb.*;
import java.rmi.RemoteException;
import java.rmi.Remote;

public interface ShakilRemote extends EJBObject{

public String hello()throws RemoteException;
}

Step 2:
Write Home interface:

HelloHome.java (Home interface)
import javax.ejb.*;
import java.rmi.RemoteException;

public interface ShakilHome extends EJBHome{
Hello create() throws RemoteException,CreateException;
}

Step 3:
Write the stateless Session bean


ShakilBean.java (stateless session bean)

import javax.ejb.*;
public class ShakilBean implements SessionBean
{
private SessionContext ctx;
public void ejbCreate()
{
System.out.println("inside ejbCreate");
}
public void ejbRemove(){
System.out.println("inside ejbRemove");
}
public void ejbActivate(){
System.out.println("inside ejbActivate");
}
public void ejbPassivate(){
System.out.println("inside ejbPassivate");
}
public void setSessionContext(SessionContext ctx){
System.out.println("inside setSessionContext()");
}
public String hello()
{
System.out.println("Hello world");
}

}

Step 4:
Create ejb-jar.xml