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:- 




JVM (Java Virtual Machine)

1- What is JVM

- JVM = Java Virtual Machine

It is software component that provide runtime environment for java byte code to be executed. JVM is available on many hardware and software platforms.
Its implementation is know as JRE (Java Runtime Environment) and when ever user write java command on the command prompt to run the java class, and instance of JVM is created.

It performs operation like Load code, Verifies Code, Execute Code.



Now Lets discuss about few main point on JVM Architecture:-





1) Classloader:

Load class in to this area so that it can be executed in JRE.

2) Class(Method) Area:

It store stores code for methods.

3) Heap:

It is the runtime data area in which objects are allocated.

4) Stack:

Java Stack stores reference variable.

5) Program Counter Register:

It contains the address of the Java virtual machine instruction currently being executed.

6) Native Method Stack:

It contains all the native methods used in the application.

7) Execution Engine:

It contains virtural processor, Interepreter that read our *.class byte file and JIT-Just-In-Time(JIT) compiler :- Actually this is used to improve the performace in JAVA. Our *.class byte code is fragmented into many small segment and all the similar component is compiled only once at run time. Compile means translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.


Wednesday, March 25, 2015

JAVA Class Loader

Applications written programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. But in case of java specific application this is different. JAVA is dynamically compiled programming languages. In Java, compiler generate the .class files and it remain as-is until loaded into the Java Virtual Machine (JVM) . Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.

Lets try to understand using our famous HelloWorld

public class HelloWorld {
   public static void main(String argv[]) {
      System.out.println("Hello World");
   }
}
If you run this class specifying the -verbose:class command-line option, so that it prints what classes are being loaded

java -verbose:class HelloWorld



[Opened C:\Program Files\Java\jre1.5.0\lib\rt.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jsse.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jce.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
---
---
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded HelloWorld from
Hello World
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
As you can see, the Java runtime classes required by the application class (HelloWorld) are loaded first.

How to add external JS in GWT Project

Let say your GWT project name is barchart


Add following line in BarChart.html

Create a public folder and keep all your *.js file in it. Make sure to have same flow structure of packages.




Folder wise screen shot





How to add Fusion chart js to gwt project

Add following line to your Project.html





Add following line to your Project.html