Reading publications for my proposal

Posted: August 29th, 2009 | Author: | Filed under: Data Mining | Tags: | No Comments »

Haven’t had time to do any blogging since I am extremely busy with finding relative publications for my proposal and reading them. On top of that I am reading Fractals Everywhere 1983 edition. Its all about fractal geometry and this is the first time I am reading a math book after a very long time.


Monsoon Like rain in Minneapolis

Posted: August 20th, 2009 | Author: | Filed under: Cycling | Tags: | No Comments »

I love rain. But when it rains I cant get on the saddle and enjoy a good bike ride. Past three days in Minneapolis reminded me Monsoon like rain that we get back home (Sri Lanka). Mother nature has its own plan and no one can disobey to that, so we have to wait and see until it gives a chance for the sun to show its power. Till then my Trek will be having a well needed rest.


Best use of ThreadLocal class to handle Selenium Session

Posted: August 12th, 2009 | Author: | Filed under: Java Ruled, Selenium | Tags: , | No Comments »

Dealing with threads in Java is not the easiest but I was in the process of implementing a custom Java Selenium Framework and I decided “Why not use the help of the ThreadLocal class to ease my pain”. So, this is my attempt to take stab at ThreadLocal class.

    Why Did I need to use the ThreadLocal class?

  • In nutshell, The Selenium Framework I was building designed based upon pages of the web application. ex: Lets say we have login page and then once we login, use get navigated to about page. The framework I built have two separate Java classes to handle function in login page and about page. Since both these classes live from selenium session, I wanted to save the selenium session where each class can freely access the session without session getting passed around back and forth between those class.
  • Only thing you need to know about my implementation.
    As I mentioned, I have created class that extends DefualtSelenium.java (lets call it OurDefaultSelenium.java, this class has method that start the selenium session)

      So, just because I think I should use the ThreadLocal class does it mean is it the best mechanism to use..?

    Most typical scenario of using ThreadLocal would be as an alternative to an object or resource pool, when we don’t mind creating one object per thread.

    So When should I really use the Thread Local

  • The objects are non-trivial to construct;
  • An instance of the object is frequently needed by a given thread;
  • The application pools threads, such as in a typical server (if every time the thread-local is used it is from a new thread, then a new object will still be created on each call!);
  • Ok enough of lecturing, lets get to the bottom of it, shall we.

  • I am a big fan of Java generics so this example uses Java 5, Java generics.
    1. Lets call our Class, SeleniumSession make sense right we are dealing with selenium session.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    public class SeleniumSession {
        private static final Logger log = Logger.getLogger(SeleniumSession.class.getName());

        private static ThreadLocal<HashMap<String, OurDefaultSelenium>> session=
            new ThreadLocal<HashMap<String, OurDefaultSelenium >>() {
            protected synchronized HashMap<String, OurDefaultSelenium > initialvalue() {
                return new HashMap<String, OurDefaultSelenium >();
            }
        };
       
        private static ThreadLocal< OurDefaultSelenium  > selenium = new ThreadLocal< OurDefaultSelenium>();
       
        /**
         * Get the current selenium session.
         * @return
         */
        public static OurDefaultSelenium get() {
            return selenium.get();
        }
      Note that there is still a single, static instance of SeleniumSession shared by all threads. But that single instance uses the ThreadLocal variable session, which has a per-thread value. Inside get(), the call to selenium.get()  will always operate on our thread-private “instance” of the variable, with synchronization.

    Since we are using Java generics, subsequent get() method doesn’t need an explicit cast. (That is, the cast is inserted automatically by the compiler.)

    But lets focus our eyes on to

    1
    <strong>initialValue </strong>

    method. We actually subclass ThreadLocal and override

    1
    initialValue()

    to provide an appropriate object each time a new one is required (i.e. when

    1
    get()

    is called for the first time on a particular thread). I know what you thinking, You could add a logic to check the null inside the

    1
    get()

    method. Like this,

        OurDefaultSelenium ourSelenium = session.get();
        if (ourSelenium == null) {
          session.set(cal = new DefaultSelenium());
        }
        return ourSelenium;
    

  • But You don’t need to do this since we are overriding
    1
    ThreadLocal.initialValue()

    automatically handles this logic and makes our code a bit neater– especially if we’re calling

    1
    get()

    in multiple places.

    1. In this implementation I also have methods to set the session and end the selenium session.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
        /**
         * Set the current selenium session.
         * @param classInstance
         */
        public static void set(OurDefaultSelenium classInstance) {
            selenium.set(classInstance);
        }
        /**
         * Method to end the session, should use after the test.
         */
        public static void endSession() {
            selenium.get().stop();
        }

    More pictures of Tour De Tonka

    Posted: August 9th, 2009 | Author: | Filed under: Cycling | Tags: , | No Comments »

    Finally had sometime to clean up pictures from my Android and uploaded them into picasa.


    Return the Baby Elephants to their mother

    Posted: August 6th, 2009 | Author: | Filed under: Life in Sri Lanka, Wild Life | Tags: , | No Comments »

    If you haven’t seen the story here is in the nut shell . Sri Lanka has the largest and well maintained Elephants orphanage in the world. Its called Pinnawala Elephant Orphanage. Recently two baby elephants were taken away from their mother where all were happily living at the orphanage and given to one of the main temple in Sri Lanka. I am really ashamed of Sri lanka’s highest Buddhist institute. As the followers of great philosophy Asgiriya and Malwatte Temples had the opportunity to refuse the donation from heartless minister. But they didn’t. Load Buddha was person who understood the relationship between mother and child. He went further by putting regulations where a young child can not enter to a life of Bikku with out permission of their parents. Its sad to see a Buddhist Country like Sri Lanka taking away child from a mother. Yes, it can be an animal but I doubt mother love changes from humans to animals. Especially when you talk elephants who are extremely smart.

    I am bias because elephants are my favorite animals in this planet. I have had many close calls, but I still love them, who couldn’t, even poets have written poems about elephants. My biggest frustration was President Mahinda Rajapaksha whom I thought would do the right thing went and officially handed over the elephants to the temple.

    Hardly 10 people read my blog in a given day, month, or even year but if any one read this please put a comment requesting elephants to be return to their mother and I am planning to print all the comments and forward them to both presidents office and both the temples.


    Rider’s Log: Day after 75 Mile ride.

    Posted: August 4th, 2009 | Author: | Filed under: Cycling, Travel arround the world | Tags: , | No Comments »

    Surprisingly I felt very good on Sunday morning no pain in my legs, more that my legs it was my ass which was hurting after sitting just over 5 hours on the saddle. Evening I went for a short 25mile ride just to stretch out the legs as well as to get the lactic acid that was build up. During the 25mile ride I felt like I was riding some crazy mountain, i think it was my lactic acid.


    Ride for Charity: Tour De Tonka 75 Mile Ride

    Posted: August 2nd, 2009 | Author: | Filed under: Cycling | No Comments »

    Did a 75 mile charity ride Tour De Tonka a charity for Minnetonka Community ride. Entire ride we had to deal with head wind in the range of 20-25mph. Climbing became hard especially against the head wind. Last 8-12 miles were uphill. Last two climbs were harder than I thought and I think they were 8%-10%.

    Here we are before group start, 2200 cyclist participated the event and it was well organized.
    tourDeTonka

    Here is the Tour route, Hopefully I will get to do another tour before the summer ends, Its going fast and here in Minnesota we are already talking about the state fair which means end of the summer.


    View Larger Map


    Get Adobe Flash playerPlugin by wpburn.com wordpress themes