Wednesday, June 17, 2015

Indexing (Cluster and non Cluster)

Indexing is similar to what we used day to day life while reading book. Our hand book has a index which help us to find the desired data in less amount of time. We use to search the string in the Index which specify us to go to page where if reside.


Same is the case with Indexing in Oracle. In Oracle data are stored in Table. When the data grows in huge quantity it becomes difficult for searching the data. For this purpose Oracle use the same Indexing concept.

There are two type of Indexing


1-      Cluster:- In cluster indexing data are arranged physically in the same fashion as it is indexed. Best example is primary key index. By doing Cluster index all the rows are sorted and arranged physically on Hard disk as per the sequence given by cluster index.


2-      Non-Cluster:- In non-cluster environment oracle maintain a list with values that store the pointer to the index values. Having said this we can have more than one non-cluster index for a single table. More over this non-cluster index is logical in nature means not stored physical on hard disk.
Now the concern is which one to us. Use this thum rule
-          If we want to perform select operation more go for Cluster index.

-          If we want to perform update, Insert etc operation go for Non-Cluster Index.

Normalization and Denormalization in terms of Oracle Database.

Normalization means storing of data in scatter manner. i.e. more than one table. This process describes to divide the table into many small table with reference to each other. This will help us to Write the data.



DeNormalization means storing of data in one manner. i.e. combining data of different table in one table. This will help us to read the data.

How to get ride of Two declarations cause a collision in the ObjectFactory class, ] A class/interface with the same name or This error is caused because on Windows you cannot have both

Generally we use wsimport to auto generated JAVA class for consuming WSDL base web service.

Many times it happens we use to see this specific type of error messages while auto generating code.

 [ERROR] Two declarations cause a collision in the ObjectFactory class.
[ERROR] A class/interface with the same name "XXXClassName" is already in use. Use a class customization to resolve this conflict.
[ERROR] This error is caused because on Windows you cannot have both "XXXClassName.java" and "xxxClassname.java" in the same directory.
[ERROR] This error is caused because on Windows you cannot have both "xxxClassName.java" and "PersistentObjectOfInt.java" in the same directory.

This are the possible ways which I tried and able to get ride of these errr:

Option 1:- This is simple option which works 90% of times. Use –B-XautoNameResolution.
Option 2:- This error is created because your XSD or WSDL is forcing to wsimport command to create java class of same name in same package. So if we not so strict in creating the auto generated source file in our specific folder neglect –p option.
Option 3:- Create binding.xml to get ride of this error. Through this we can tell wsimport to create class with the name specified by us rather than auto generating class name. But as stated in this if we had more than one schema in WSDL then for all name conflict we have to do the same.
Make sure when we create binding.xml  add it command prompt using –b option.

Example of binding.xml
        jxb:bindings node="//xs:element[@name='cxml.payment']/xs:complexType">
     name="CxmlInnerPaymentType" />




Monday, May 04, 2015

Deep Copy or Deep Cloning And Shallow Copy or Shallow Cloning

Cloning is very important aspect of OOPS in JAVA.

With the help of cloaning we can stop creating the huge number of object and can also create the object with specific value at specific time of execution.

All good thing comes with some problem same is the case with JAVA Clone.

Their are two type of clone in java

1- Shallow Clone
2- Deep Clone


1- Swallow Clone :- In this type of cloan we are copying the address of the class object inside other class. So when we create an clone object using super.clone() a new object is created but when the original object get change that changes is also reflected in cloned object. This is because when we do swallow clone we are copying the address of the previous object.

let me try to expain this is diagram some time single image is more than thousand word :)



Class 1
- int a;
- Class 2;


if I clone Class 1 object using super.clone() and create a new object lets say cloneObject. when ever  class2 object change the same thing is also reflected in clonedObject even though it is different.

2- Deep Clone:- In this type of cloan we are creating the new object instead of copying the address of the class object inside other class. So when we create an clone object using new operator in clone() method  a new object is created and this did not change even when the original object get change i.e. this change is not reflected in cloned object. This is because when we do deep clone we are not copying the address of the previous object but we are creating the object

let me try to expain this is diagram some time single image is more than thousand word :)



Class 1
- int a;
- Class 2;


if I clone Class 1 object using new operator in Clone() method and create a new object lets say cloneObject. when ever  class2 object change now the same thing is will not be  reflected in clonedObject even though it is different.

Now lets try to expain with Code.

class Standard {

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String s) {
    name = s;
  }

  public Standard(String s) {
    name = s;
  }
}

class Child implements Cloneable {
  //Contained object
  private Standard stand;

  private String name;

  public Standard getstand() {
return stand;
  }

  public String getName() {
return name;
  }

  public void setName(String s) {
name = s;
  }

  public Child(String s, String sub) {
name = s;
stand = new Standard(sub);
  }

  public Object clone() {
//shallow copy
try {
 return super.clone();
} catch (CloneNotSupportedException e) {
 return null;
}
  }
}

public class CopyTest {

  public static void main(String[] args) {
//Original Object
 Child stud = new Child("one", "1st");

System.out.println("Original Object: " + stud.getName() + " - "
+ stud.getstand().getName());

//Clone Object
Child clonedStud = (Child) stud.clone();

System.out.println("Cloned Object: " + clonedStud.getName() + " - "
+ clonedStud.getstand().getName());

stud.setName("second");
stud.getstand().setName("2nd");

System.out.println("Original Object after it is updated: " 
+ stud.getName() + " - " + stud.getstand().getName());

System.out.println("Cloned Object after updating original object: "
+ clonedStud.getName() + " - " + clonedStud.getstand().getName());

  }
}

/*Original Object: one - 1st
Cloned Object: one - 1st
Original Object after it is updated: second - 2nd
Cloned Object after updating original object: one - 2nd
*/

Now lets see the same example using Deep cloaning. In deep cloaning event though our Standard object is changed then also their is no impact in Cloned object.

class Child implements Cloneable {
  //Contained object
  private Subject subj;

  private String name;

  public Subject getSubj() {
return subj;
  }

  public String getName() {
return name;
  }

  public void setName(String s) {
name = s;
  }

  public Child(String s, String sub) {
name = s;
subj = new Subject(sub);
  }

  public Object clone() {
//Deep copy
Child s = new Child(name, subj.getName());
return s;
  }
}


Original Object: one - 1st
Cloned Object: one - 1st
Original Object after it is updated: second - 2nd
Cloned Object after updating original object: one - 1st

What is the use of comparable and comparator interfaces in java.When should developer use the same.



What is the use of comparable and comparator interfaces in java.When should developer use the same.

Sorting is one of the main event which any developer need to face in his carrier. That sorting might be on int value or string value, in array, or Collections etc.

Java API gives us beautiful two interface comparable and comparator which assist doing the same.

Comparable interface contains method compareTo(Object  o) which is used to compare the same class Object.

Comparator interface contain method compare(Object  o1, Object  o2) which is used to compare two different object either of same class or of different class.

Lets apply force and try to understand use of this interface using simple java programe.

Lets say we had class Student having id and name as a field.

We got a requirement in which we need to sort this Student class object filled in collections  in both way by id and by name.

Lets  go first with sorting by default using id

public class Student implements Comparable {
    private int id = 0;
    private String firstName = null;
    private String lastName = null;


    public Student(int id, String fName, String lName) {
        this.id = id;
        this.firstName = fName;
        this.lastName = lName;
        
    }

    @Override
    public int compareTo(Student o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Student : " + id + " - " + firstName + " - " + lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSorting {
    public static void main(String[] args) {
    Student e1 = new Student(1, "afirstname", "aLastName");
    Student e2 = new Student(2, "cfirstname", "cLastName");
        Student e3 = new Student(3, "bfirstname", "bLastName");
         
        List Students = new ArrayList();
        Students.add(e2);
        Students.add(e3);
        Students.add(e1);
         
        // UnSorted List
        System.out.println(Students);

        Collections.sort(Students);
        // Default Sorting by Student id
        System.out.println(Students);
    }
}


Out Put:-

[Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName, Student : 1 - afirstname - aLastName]
[Student : 1 - afirstname - aLastName, Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName]


Now lets check the condition when we need to sort the same student class by name i.e. firstName and lastName.

For this we need to use Comparator interface having compareTo(Object o1, Object o2).


Lets us create two class

import java.util.Comparator;

public class FirstNameSorter implements Comparator{

@Override
public int compare(Student o1, Student o2) {
return o1.getFirstName().compareTo(o2.getFirstName());
}
}


import java.util.Comparator;

public class LastNameSorter implements Comparator {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.getLastName().compareTo(o2.getLastName());
    }

}

public class Student implements Comparable {
    private int id = 0;
    private String firstName = null;
    public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

private String lastName = null;
    

    public Student(int id, String fName, String lName) {
        this.id = id;
        this.firstName = fName;
        this.lastName = lName;
        
    }

    @Override
    public int compareTo(Student o) {
        return this.id - o.id;
    }

    @Override
    public String toString() {
        return "Student : " + id + " - " + firstName + " - " + lastName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    
}

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSorting {
public static void main(String[] args) {
    Student e1 = new Student(1, "afirstname", "aLastName");
    Student e2 = new Student(2, "cfirstname", "cLastName");
        Student e3 = new Student(3, "bfirstname", "bLastName");
         
        List Students = new ArrayList();
        Students.add(e2);
        Students.add(e3);
        Students.add(e1);

        // UnSorted List
        System.out.println(Students);

        Collections.sort(Students);
        // Default Sorting by Student id
        System.out.println(Students);

        Collections.sort(Students, new FirstNameSorter());
        // Sorted by firstName
        System.out.println(Students);

        Collections.sort(Students, new LastNameSorter());
        // Sorted by lastName
        System.out.println(Students);
         
    }
}




OUT PUT:- 
[Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName, Student : 1 - afirstname - aLastName]
[Student : 1 - afirstname - aLastName, Student : 2 - cfirstname - cLastName, Student : 3 - bfirstname - bLastName]
[Student : 1 - afirstname - aLastName, Student : 3 - bfirstname - bLastName, Student : 2 - cfirstname - cLastName]
[Student : 1 - afirstname - aLastName, Student : 3 - bfirstname - bLastName, Student : 2 - cfirstname - cLastName]

Friday, March 27, 2015

WSImport Command Example for Consuming JAVA Web Service

WSImport command is presend in Bin folder of JDK.
Using this command we can create a glue code from exposed/Published Web service.

We can use this glue code from the consumer or client code to consume result of exposed web service.


Step-1- Check Web service is exposed by hitting WSDL url from browser.
Step-2- Create a Glue code using WSImport command as shown below

wsimport -keep http://localhost:9999/ws/hello?wsdl





Once you execute above command glue code is created at the respective folder.

we will get follwing two class


package com.test.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 *
 */
@WebService(name = "HelloWorld", targetNamespace = "http://ws.test.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorld {


    /**
     *
     * @param arg0
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://ws.test.com/HelloWorld/getHelloWorldRequest", output = "http://ws.test.com/HelloWorld/getHelloWorldResponse")
    public String getHelloWorld(
        @WebParam(name = "arg0", partName = "arg0")
        String arg0);

}




package com.test.ws;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.4-b01
 * Generated source version: 2.2
 *
 */
@WebServiceClient(name = "HelloWorldImplService", targetNamespace = "http://ws.test.com/", wsdlLocation = "http://localhost:9999/ws/hello?wsdl")
public class HelloWorldImplService
    extends Service
{

    private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException HELLOWORLDIMPLSERVICE_EXCEPTION;
    private final static QName HELLOWORLDIMPLSERVICE_QNAME = new QName("http://ws.test.com/", "HelloWorldImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://localhost:9999/ws/hello?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
        HELLOWORLDIMPLSERVICE_EXCEPTION = e;
    }

    public HelloWorldImplService() {
        super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME);
    }

    public HelloWorldImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), HELLOWORLDIMPLSERVICE_QNAME, features);
    }

    public HelloWorldImplService(URL wsdlLocation) {
        super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME);
    }

    public HelloWorldImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, HELLOWORLDIMPLSERVICE_QNAME, features);
    }

    public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public HelloWorldImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     *
     * @return
     *     returns HelloWorld
     */
    @WebEndpoint(name = "HelloWorldImplPort")
    public HelloWorld getHelloWorldImplPort() {
        return super.getPort(new QName("http://ws.test.com/", "HelloWorldImplPort"), HelloWorld.class);
    }

    /**
     *
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the features parameter will have their default values.
     * @return
     *     returns HelloWorld
     */
    @WebEndpoint(name = "HelloWorldImplPort")
    public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://ws.test.com/", "HelloWorldImplPort"), HelloWorld.class, features);
    }

    private static URL __getWsdlLocation() {
        if (HELLOWORLDIMPLSERVICE_EXCEPTION!= null) {
            throw HELLOWORLDIMPLSERVICE_EXCEPTION;
        }
        return HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
    }

}


Import that folder as in Eclipse
Create new project with same name of the folder and automatically all the files will be added in the package

Step-3- Create Client code that will consume the this glue code to call Published W/S.

Create a package com.test.client and file named as HelloWorldClient.

package com.test.client;
import com.test.ws.HelloWorld;
import com.test.ws.HelloWorldImplService;

public class HelloWorldClient{

public static void main(String[] args) {

HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();

System.out.println(hello.getHelloWorld("siddhu calling from WSImport Glue code"));

    }

}

Execute this class and we will be able to call the Web service using glue code created by WSImport command.

Out Put :- Hello World JAX-WS example calling Web Service using Java API for AML using Web Service:siddhu calling from WSImport Glue code


Client --(request)-> Glue code created by WSImport -(request)-> Web service Exposed by publisher

Client <-- by="" code="" created="" exposed="" glue="" p="" publisher="" response="" service="" web="" wsimport="">

Thursday, March 26, 2015

JAX-WS Hello World Example

JAX-WS bundle come in JDK 1.6 it help to develop and test JAVA W/S.


Web service is a concpet which helps two different technoloyg to talk and share data with each other without technology barrier.
For Web service implmentation two entity take active part
1- Sender - Publisher
2- Receiver - Client

1- Sender - Publisher :-  This entity is responsible for publishing the W/S to the outer world.
2- Receiver - Client :- This entity is responsible to consume the data published by publisher.


In general words, “endpoint” is a service which is published by Published to the outside  user to access; where “client” access the published service.


1. Create a Web Service Endpoint Interface

package com.test.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//This class is used as Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

@WebMethod String getHelloWorld(String name);

}


2. Create a Web Service Endpoint Implementation


package com.test.ws;
import javax.jws.WebService;

//This class is used to implement Service.
@WebService(endpointInterface = "com.test.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

@Override
public String getHelloWorld(String name) {
return "Hello World JAX-WS example calling Web Service using Java API for AML using Web Service:" + name;
}


}

3. Create a Endpoint Publisher

package com.test.endpoint;
import javax.xml.ws.Endpoint;
import com.test.ws.HelloWorldImpl;

//this class is used to publish 
public class HelloWorldPublisher{

public static void main(String[] args) {
  Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
    }


}

4. Testing

Once you start the publisher.. our web service will be exposed. To check the same use this url and we will be able to see the WSDL.“http://localhost:9999/ws/hello?wsdl” .



1. Java Web Service Client

package com.test.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.test.ws.HelloWorld;

public class HelloWorldClient{

public static void main(String[] args) throws Exception {

URL url = new URL("http://localhost:9999/ws/hello?wsdl");

       
        QName qname = new QName("http://ws.test.com/", "HelloWorldImplService");

        Service service = Service.create(url, qname);

        HelloWorld hello = service.getPort(HelloWorld.class);

        System.out.println(hello.getHelloWorld("Siddhu"));

    }

}




First execute HelloWorldPublisher and then check publisher is able to expose desired W/S by hitting this WSDL url:-
“http://localhost:9999/ws/hello?wsdl” .

Out Put:-


Hello World JAX-WS example calling Web Service using Java API for AML using Web Service:Siddhu

WSDL Image:-