Tuesday, October 16, 2012

Trip to Jog Falls, Sakrebailu and Kemmannu gundi

Recently I had been to Jog falls along with my family. This was my third visit to Jog falls. First two have been with friends. This was first time with my family. The trip was planned for two days. So plan was to see Jog falls, Sakrebailu, Mandagadde and other surrounding places of Shimoga.

So, we started early morning on Saturday, filled-up the car tank and started to Shimoga. The route we had planned was Bangalore -> Tumkur -> Tiptur -> Arasikere -> Kadur -> Birur -> Tarikere -> Bhadravati -> Shimoga.

One of my friend had suggested to have a breakfast at "Preeti Canteen" in Birur. He had informed that it is small canteen as you enter Birur. But we did not find it and hence moved on to have breakfast at Tarikere. It was pretty ordinary. Moved to Shimoga after having breakfast. I had already booked rooms for a day. The hotel was "Jewel Rock". I heard that it was good, hence I had just booked it. We checked-in hotel and rooms were okay. We took much needed rest.

Now, the plan was to visit "Tavarekoppa Lion and Tiger Safari" and then Jog Falls in the evening. The plan to visit Jog falls in the evening was prompted by the news about the newly introduced musical fountain. Hence, we started to "Tavarekoppa" after having lunch. At the entrance, the guard said the Safari would start only at 2:30 PM. We calculated that if we spend an hour in Zoo and Safari, it would be 3:30 PM and it would take approximately 1 hr : 30 mins to travel to Jog falls from there (the Jog falls is 90 KMs from Shimoga) and we would be there by 5:00 PM and we can enjoy at least 2 hours and see the musical fountain. It was good Safari, except that there are very few animals to see. We could see few tigers very closely. There are 3 lions in a cage. But there activities seems to be limited. And there are lots of Deers, Black bucks.
Tigers at Tavarekoppa Lion and Tiger Safari

As planned, we reached Jog falls around 5:00 PM. It was raining all the way. When we reached Jog, the falls was fully covered with fog not even single falls of the four was visible. After some time everything was clear and was able to see the falls.
We can climb down till the water falls and initially I did not had plan go down. As I had been till down and also at the top of Raja in my earlier visits. However, this was first time I had visited Jog after steps were made till down. So, climbed down few steps and I was fully wet. Slowly I was inspired (or tempted) by few people who were enjoying their trip down to Jog falls on top it was raining. So, I too decided get down. It was easy walk down and knew it would be difficult to get to the top again.
Jog falls
It was nice experience to get down in the rain and watching the jog falls and surroundings. I took some snaps at the down even though it was raining.
It was nice to see the whole of the water falling..., spent some time and returned to top. As we reached at top it was very satisfying.
 
Me at the down of Jog falls Water falling at the down of Jog falls.
In the evening around 7:30 PM and the musical fountain started. It was nice, however, since it was still under construction, the area is not covered with sheets and there were no seating arrangements and on top of it, it was raining. Also, there were lots of mosquitoes and it was getting difficult to stand. So, we decided to return back to Shimoga.
Musical fountain at Jog falls Musical fountain at Jog falls

We reached back to hotel around 9:30 PM and hotel restaurant was closed. Hence, we were forced to eat in Bar / Restaurant. He took hell of time to serve the simplest order you can order. So, advice is not eat at that place. We went to bed with lot of tiredness and with fulfillment of achieving what was planned. Next day plan was to getup early, fresh'n up and start to Sakrebailu Elephant Camp. I had read that there we can see the elephants bathing, eating and we too can try our hand. So, I was expecting some fun, particularly for my 4 year old daughter.

Got up early, checked-out hotel, settled the bill and we were out of the hotel by 8:00 AM.

Sakrebailu is around 14 KMs from Shimoga, hence we knew that this can be reached early. Hence, we decided to have breakfast. Visited a small hotel (if I am correct, its name is "Woodlands") and tiffin was good.
We asked localities for the route and they provided all the information. We drived soon to the Sakrebailu Elephant Camp. They charged around Rs. 30/- per adult and free for children. As I had read, already few of the elephants were getting bath. I too tried my hands at the elephant bathing.

Elephant at the camp
Elephants at the Sakrebailu Elephant campMe washing an elephant

Then we visited a place called "Mandgadde", it is small bird sanctuary, which is another 10 KMs from Sakrebailu. When we went it was 10:00 AM and there very few birds and that too we have to see them from a quite distance. Hence, we did not spend much time and decided to drive back to Bangalore.
On the way back we spent some time at "Gajanur Dam", its small dam build across river "Tunga".
Birds at Mandagadde Bird Sanctuary
Later, on the way back we reached Tarikeri and we had enough time and we just checked if we can make the trip to Kemmannugundi. We wanted to check the route to Kemmannu Gundi, which is a nice hill station. In my previous visit, the road was bad and hence it was not in my list. But localities informed the road is good enough to drive. So, first we decided to visit "Kalhatti falls" (also known as Kallatha Giri falls). It is a small falls and there is a temple dedicated to lord Veerabhadreshwar. We took the blessings and headed towards Kemmadnnugundi. The road from this falls was bad and was getting worst. We could see some cement and sand. Expecting that they would be going to repair this road.

Veerbhadreshwar temple at Kallatha GiriKallatha Giri or Kallhatti falls

But once we reached at the top, it was really nice and very cold. There is a small garden and there are guest houses for the people who wants to make a stay. Also, there is falls called "Hebbe" falls. It is around 8 KMs from Kemmannu Gundi. One can trek to this place or there are local Jeeps available. However, we did not had plan to visit any of these, we decided to return to Bangalore.

 
Kemmanna Gundi Scene from a view point

On the way back we stopped for a dinner and then reached our home around 10:00 PM and just wanted to sleep...
Overall, it was a very nice trip and enjoyed.

Tuesday, September 25, 2012

Custom JAXBContextResolver


JSON representation for single record element which is declared as a java array or a collection, using jersey would display it as a single element rather than JSON array.

For example:


For single record:

"employee":
         {
           "id": "16",
           "name": "Sunil"
         }

Where as it is expected to display it as:

"employee":
      [ 
         {
           "id": "16",
           "name": "Sunil"
         }
      ]



If you need to handle JSON produced by Jersey for a single element collection, then we need to customize the JAXBContext Resolver. A provider class annotated with @provider needs to extend the ContextResolver of javax.ws.rs.ext package.


/**
 * This helps in configuring the JSON output format.
 */
@Provider
public class JAXBContextResolver extends ContextResolver {

    /**
     * The JAXBContext object.
     */
    private JAXBContext context;
    /**
     * The class types.
     */
    private final Class[] types = {Employee.class};

    /**
     * Constructs the JSON output format for the specified class types.
     *
     * @throws Exception the exception
     */
    public JAXBContextResolver() throws Exception {
        this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("employee").build(), types);
    }

    /**
     * Returns the current context for the given class.
     *     
     * @param objectType the JAXBContext for the given class object.
     * @return the JAXBContext.
     */
    @Override
    public JAXBContext getContext(final Class objectType) {
        for (Class type : types) {
            if (type == objectType) {
                return context;
            }
        }
        return null;
    }
}


In the above class, the Employee.java is a simple Java Bean class which has 'employee' attribute which is a Java arrays or a collection. If that element contains multiple values then JSON conversion would be displayed:

For single record:


"employee":
      [ 
         {
           "id": "16",
           "name": "Sunil"
         }
      ]

  For Multiple Records

  "employee":
       [
           {
               "id": "16",
               "name": "Sunil"
           },
           {
               "id": "17",
               "name": "Ajeesh"
           },
           {
               "id": "18",
               "name": "Sanjay"
           }
       ]



One issue with this implementation is one need to maintain the affected classes/items manually in their implementation. So, you need to add as many as classes that have Java collection to the above class and get the configuration 'mapped' to JSON array.


private final Class[] types = {Employee.class, Company.class};

and

this.context = new JSONJAXBContext(JSONConfiguration.mapped().arrays("employee", "company").build(), types);

JAXB with an example

JAXB with an example.

What is JAXB?
JAXB stands for Java Architecture for XML Binding. It can be used to convert Java Object to XML and XML back to Java Objects.

With JDK Annotation, it is very simple for implementation. Lets directly jump into implementation.

First, Let us define a Address.java class with basic fields.

package baseproject.jaxb;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlType;


/**
 *
 * @author
 */

@XmlType

public class Address {

    private String street;

    private String lane;
    private String city;
    private String state;

    /**

     * Get the value of street
     *
     * @return the value of street
     */
    @XmlElement
    public String getStreet() {
        return street;
    }

    /**

     * Set the value of street
     *
     * @param street new value of street
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**

     * Get the value of lane
     *
     * @return the value of lane
     */
    @XmlElement
    public String getLane() {
        return lane;
    }

    /**

     * Set the value of lane
     *
     * @param lane new value of lane
     */
    public void setLane(String lane) {
        this.lane = lane;
    }

    /**

     * Get the value of city
     *
     * @return the value of city
     */
    @XmlElement
    public String getCity() {
        return city;
    }

    /**

     * Set the value of city
     *
     * @param city new value of city
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**

     * Get the value of state
     *
     * @return the value of state
     */
    @XmlElement
    public String getState() {
        return state;
    }

    /**

     * Set the value of state
     *
     * @param state new value of state
     */
    public void setState(String state) {
        this.state = state;
    }

    @Override

    public String toString() {
        return "Address: {" + "street=" + street + ", lane=" + lane + ", city=" + city + ", state=" + state + '}';
    }
}

Now, let us define a new Class Employee.java with basic fields and also the 'Address' object.

package baseproject.jaxb;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**

 *
 * @author
 */
@XmlRootElement
public class Employee {

    private String id;

    private String name;
    private String company;
    private Address address;

    /**

     * Get the value of id
     *
     * @return the value of id
     */
    @XmlElement
    public String getId() {
        return id;
    }

    /**

     * Set the value of id
     *
     * @param id new value of id
     */
    public void setId(String id) {
        this.id = id;
    }

    /**

     * Get the value of name
     *
     * @return the value of name
     */
    @XmlElement
    public String getName() {
        return name;
    }

    /**

     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {

        this.company = company;
    }

    @XmlElement

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {

        this.address = address;
    }

    @Override

    public String toString() {
        return "Employee: {" + "id=" + id + ", name=" + name + ", company="
                + company + ", address=" + address + '}';
    }
}

Now, define the class which converts an Object to XML. For that first we create an object 'Employee' class and set the attributes for the same.

package baseproject.jaxb;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author
 */
public class JAXBObjectToXML {

    public static void main(String args[]) {
   
        // First define the address of the employee.
        Address address = new Address();
        address.setStreet("Main Street");
        address.setLane("Main Lane");
        address.setCity("Bangalore");
        address.setState("Karnataka");

        // Define the employee object
        Employee e = new Employee();
        e.setId("1");
        e.setName("Sunil");
        e.setCompany("Sunil Systems.");
        e.setAddress(address);  // Setting employee address object.
        try {
            File file = new File("src/baseproject/jaxb/emp.xml");

            // Now the JAXB
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(e, file);
            marshaller.marshal(e, System.out); // displays the output on screen for System.out
        } catch (JAXBException ex) {
            System.out.println("JAXB Exception: " + ex);
        }
    }
}

Once you run the JAXBObjectToXML, we see the following output.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
    <address>
        <city>Bangalore</city>
        <lane>Main Lane</lane>
        <state>Karnataka</state>
        <street>Main Street</street>
    </address>
    <id>1</id>
    <name>Sunil</name>
</employee>

Similarly,  It is very easy to convert an XML to an object. 

Now, we shall define a JAXBXmlToObject.java class which constructs an object from an XML file. In the following class, we are reading 'emp.xml' file and converting it to 'Employee' object.

package baseproject.jaxb;

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author
 */
public class JAXBXmlToObject {
    public static void main(String args[]) {
        try {
            File file = new File("src/baseproject/jaxb/emp.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            Unmarshaller unmarhsaller = jaxbContext.createUnmarshaller();
            Employee e = (Employee) unmarhsaller.unmarshal(file);
            System.out.println(e);
        } catch (JAXBException ex) {
            System.out.println("Jaxb Exception... " + ex);
        }
    } 
}


For the same, we shall define XML file similar to one we want for our objects.

Input XML file: emp.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
    <address>
        <city>Bangalore</city>
        <lane>2nd lane</lane>
        <state>Karnataka</state>
        <street>2nd Street</street>
    </address>
    <id>2</id>
    <name>Sunil</name>
</employee>


Upon successful run, we shall have 'Employee' lobject being constructed with values in the XML file. Following output confirms the same.

Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}

Please note the output of address object.

Even if the XML contains additional elements for which the attributes does not exists in the class, they would be simply ignored.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
    <address>
        <city>Bangalore</city>
        <lane>2nd lane</lane>
        <state>Karnataka</state>
        <street>2nd Street</street>
        <cross>2nd cross</cross>  <!-- Will be ignored -->
    </address>
    <id>2</id>
    <name>Sunil</name>
    <dummy>Dummy, will not be used</dummy>   <!-- Will be ignored -->
</employee>


The output would still look the same as above.

Employee: {id=2, name=Sunil, company=Sunil Systems., address=Address: {street=2nd Street, lane=2nd lane, city=Bangalore, state=Karnataka}}

Similarly, if the elements are missing for the attribute in the class, then their default values would be used.

Input XML: emp.xml. (Please see that the element <name/> and <street /> are missing.)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee company="Sunil Systems.">
    <address>
        <city>Bangalore</city>
        <lane>2nd lane</lane>
        <state>Karnataka</state>
    </address>
    <id>2</id>
</employee>

and the output would be:

Employee: {id=2, name=null, company=Sunil Systems., address=Address: {street=null, lane=2nd lane, city=Bangalore, state=Karnataka}}

From the output, we can see that both 'name' and 'street' values are 'null' which are their default values.

Monday, August 23, 2010

My trip to Udupi

My weekends were just passing... without much happening anything in life... and I got tired of watching TV at home.. so decided to travel to Udupi (Karnataka) where my cousin stays.. and he had invited me several times but could not manage to visit. So just decided to make this time and called him to check whether he is available over the weekend and got a positive reply from him. Booked KSRTC volvo bus from Bangalore to Udupi and back. So, after office work on Friday directly left to Kempegouda bus stop in Majestic. Bus was on right time and was standing at the said platform. Just got in the bus after verifying the ticket with bus conductor. Bus started moving just 10 minutes late than said time which is quite okay. I put-in my ipod and started the journey and do not know when I had slept. Got up some time in the night and my ipod was still playing. Removed it and kept in the bag and again slept back. Got up in the early morning and got to know that bus has just passed Mangalore and would be reaching Udupi in an hour. I called my cousin and asked him to reach the Udupi bus stop. He came in and picked up me to his house.

Since it was a unplanned journey, we decided to visit few places around Udupi and cover as much as possible in two days. So morning we visited the famous Krishna temple in Udupi.



Then moved on to visit other temples around this Krishna temple. Then we went to hotel and had breakfast and tea. It was getting hot and decided to visit some places in the evening, so came back to home and took some rest. However, again watching TV was never on my mind.. so asked him where to go. So, my cousin suggested to watch a movie. I said no, but he convinced me and I somehow agreed to watching a new movie than watching TV. So, we left home to a near by theater, where 3 idiots was running. After that we had lunch then went to famous Malpe beach.

I took some Sun set photos.

Sun set at Malpe beach


















Came back to house after having dinner. That was our end of day one. We went to bed discussing about next day's plan and places to visit (we both knew it would change).

Got up early morning, after having breakfast, went to Light house in Koppa. Spent some time there and moved to some hotel had lunch. In the evening we decided to go to St. Mary's island. To go to this island, we have to go by a boat near by a Malpe port. As we were waiting for the boat to come and pick us, I took some snaps of a new ship being built.






St. Mary's island is special in kind where see different kinds of stones. Looks like some pillars rocks.
and here are some more photos of the same stones.


Once back to Udupi, evening we had some snacks and moved to Manipal end point. Manipal is near by town to Udupi famous for some educational institute. Manipal end point a very well maintained garden in the background of river flowing.














and nature scenes like this one!!!















It was all fun and enjoyed my trip to Udupi. Got into the return bus to Bangalore to reach the Bangalore early morning..... again life started as usual and was preparing to go to office...