Tuesday, 5 November 2013

Dynamic Evaluation of expression in java

                                 Dynamically Evaluate expression in java

I am sure many people had already stumbled on this and recently I too became one of them . I was working on an assignment which needed some run time evaluation of expression. These expressions were not very complex of nature. They were pretty much like assign a variable with value 0 if that variable was null

something on the lines of :   abc=abc==null?0:abc

or something like  abc=abc=='D'?abc+'B':abc+'C'

now there was the way to build a kind of parser which parses these equations and provide the output , but every new requirement might need a enhancement to the parser.

Then going back to the basics I started googling around for solutions and stumbled across


This is basically a script engine (what the browsers internally might be using ) to do evaluation of javascript method like Eval.

And to my surprise it works pretty well . I understand there are pretty limitations to this engine ( as I read in of the blogs that it has issues with some regular expressions , not exactly sure which case was he mentioning ) , but it satisfies all the basic expressions.

This allowed me to store these expressions in some external properties files and invoke it dynamically and calculate the expressions.

May be worth trying if someone is looking for some utility under java.

Sunday, 27 October 2013

Flex-Java SWFLoader issue

                                    SWF Loader issue with  clustered env

Last week i got stuck in a strange issue:

We had a application (which under normal development work ) was being built on a single machine system  with no clustered env.

It had a Web application built using Flex 4.6 framework integrated with spring and blaze-ds to contact back end layer built in java. This looked good all on dev system but started troubling on integration system which mimicked more like production having multiple boxes and each box having multiple servers clustered together.

In the application , we had complex situation in which a swf application was loading another swf application within a popup (when I say swf application , it was APPLICATION not the MODULES). So when the primary application tried to open the secondary swf within for the first time , there was no response from the server . The secondary swf bytes were fetched but nothing  was shown on the screen. But subsequent hits to open the pop up with secondary swf resulted in success . So the question was the reason of first time failure.

On deeper analysis showed that , when the first time secondary swf was loaded from the server , there was no session ID passed as cookies to the load balancer , but still swf bytes were loaded. On the first invocation of blaze ds request within the secondary SWF , resulted in the create of session id . due to this the first invocation failed but subsequent worked well. This phenomenon was not noticed in single server env (like local dev systems) but were only found in clustered system with load balancer in place.

So the solution was to somehow create the session id even before first request of blaze ds was made .

The easiest way to do this was make a dummy HTTP request from the secondary swf immediate after its loaded (basically just after creation complete ) event. As this dummy HTTP request to any servlet on server would surely create a session ( until or unless there is request.getSession() method in servlet ).

So any subsequent request of balze ds would pick this session id and continue its work as normal.

This worked for us , hope it helps some one else too.

Thursday, 21 March 2013

Real To Java for fun

I had this weird observation yesterday thought of sharing with you:

I was on the way from my home station (Journal Square) to office (33d Street NY) in train  and as always I had taken the  corner most seat of the wagon and was busy reading some newspaper article . After couple of stops I realized a super hot blonde girl with the minimal of formal outfit you could think of  was sitting opposite to me . As most of us do , I too tried avoiding a stare towards her but again as always couldn’t avoid and somehow or the other kept on looking at her ( for some reason , there was this James blunt’s song , You’r beautiful  started playing in my head  )  . Anyways 23rd street arrived and again as always she left the train without even noticing me and the respect I had grown for her beauty J . So with heavy heart my stop arrived and I got down thinking of her and felt I LOVE BLONDE GIRLS.

Then as every day morning I proceeded towards a star bucks coffee shop next to my office to pick my latte ( as I have realized that if you want to be noticed in NY , you need to carry star bucks coffee even if you end up dumping it in your restroom, but somehow I like their latte). And as I entered I saw Christine again , a dark eyed ,dark haired beautiful Mexican girl, smiling and asked me do I need latte again ? ( as she suggests me many times to try some other coffee’s too at that place , but I don’t end up taking risk) . I picked my coffee , paid my bill and came out of the shop thinking I LOVE MEXICAN GIRLS.

At this moment , I realized what happened to my love for blonde girls ? Why did it change suddenly?

The first thing that came to my mind was , BLONDE and MEXICAN are 2 classes in my brains which are loaded and with every girl of that type and instance is created for them and if they are beautiful they are persisted else made available for garbage collection.  So when I saw the girl in train , my JVM loaded a  class called Blonde and quickly made an instance and filled all the variables in it J . Then when I went to coffee shop , it loaded a class called MEXICAN , again made a instance  of it with features of
Christine . But what happened to my class and object of blonde , it should have been still live. How does brain takes care of so many different classes of girls ?

The only reason I thought was if classes were getting unloaded back to main storage from my RAM . So does it even happen in JAVA . But I have never faced it in JAVA. neither I do take care of it while coding .

So I searched JVM doc to find the answer and I found it on a short note up which says A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector’

So if we write a custom class loader without much care and this gets unloaded so all the classes it loaded will get unloaded.

The dangers could be : If any class which was loaded before get unloaded ( due to classloader) but gets loaded again , all the static blocks would run again . And if you had some DB calls or Business logic present , this might be even more dangerous .

I understand it’s not a daily activity to write a classloader but thought of sharing with you if you some day end up finding a issue like this.