You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 9 Next »

The Calculator interface defines the Service Endpoint Interface (SEI) for the Web Service.

Calculator.java
package org.apache.geronimo.samples.jws;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

@WebService
public interface Calculator {

    @WebMethod
    public int add(@WebParam(name = "value1") int value1,
                   @WebParam(name = "value2") int value2);

}

The CalculatorService class implements the Web Service business logic. It implements all the methods defined in the SEI. The class does not need to implement the Calculator interface but must reference it through the @WebService.endpointInterface annotation. This class will be exposed as a Servlet through web.xml file even though it does not extend the javax.servlet.Servlet class.
The context variable marked with the @Resource annotation will be injected at runtime. The WebServiceContext can be used to obtain the message context and security information relative to the call.

CalculatorService.java
package org.apache.geronimo.samples.jws;

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;

@WebService(endpointInterface = "org.apache.geronimo.samples.jws.Calculator")
public class CalculatorService implements Calculator {

    @Resource
    private WebServiceContext context;

    public int add(int value1, int value2) {
        System.out.println("User Principal: " + context.getUserPrincipal());
        return value1 + value2;
    }
}

The web.xml descriptor is used to deploy the Web Service.

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:calc="urn:geronimo-samples-jws" 
                xmlns="http://java.sun.com/xml/ns/javaee"
		version="2.5">
	<servlet>
		<display-name>CalculatorService</display-name>
		<servlet-name>CalculatorService</servlet-name>
		<servlet-class>
			org.apache.geronimo.samples.jws.CalculatorService
		</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>CalculatorService</servlet-name>
		<url-pattern>/calculator</url-pattern>
	</servlet-mapping>
	
</web-app>

Currently, the web.xml deployment descriptor is required to configure the service but it will not be necessary in the future.

The geronimo-web.xml descriptor is optional but here it is used to specify the module id.

geronimo-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1">
  <dep:environment xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.1">
    <dep:moduleId>
      <dep:groupId>org.apache.geronimo.samples.jws</dep:groupId>
      <dep:artifactId>Calculator</dep:artifactId>
      <dep:version>1.0</dep:version>
      <dep:type>car</dep:type>
    </dep:moduleId>
</web-app>

The webservices.xml file describes the Web Service. It configures the WSDL file for the service, the name of the WSDL port that the service implements, the SEI, and also contains a link to the Servlet that implements the business logic.

webservices.xml
<?xml version="1.0" encoding="UTF-8"?>
<webservices xmlns="xmlns="http://java.sun.com/xml/ns/javaee" version="1.2">
 <webservice-description>
  <webservice-description-name>CalculatorService</webservice-description-name>
  <wsdl-file>CalculatorService.wsdl</wsdl-file>
  <port-component>
	  <port-component-name>CalculatorServicePort</port-component-name>
	  <wsdl-port>CalculatorPort</wsdl-port>
	  <service-endpoint-interface>
                org.apache.geronimo.samples.jws.Calculator
	  </service-endpoint-interface>
	  <service-impl-bean>
	      <servlet-link>CalculatorService</servlet-link>
	  </service-impl-bean>
  </port-component>
 </webservice-description>
</webservices>

Currently, the webservices.xml deployment descriptor is required to configure the service but it will not be necessary in the future.

The CalculatorClient class is a client for the CalculatorService Web Service. The client automatically generates a dynamic proxy from the service WSDL and the SEI which then is used to invoke the service.

CalculatorClient.java
package org.apache.geronimo.samples.jws.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.geronimo.samples.jws.Calculator;

public class CalculatorClient {

    public static void main(String[] args) throws Exception {
        int value1 = Integer.parseInt(args[0]);
        int value2 = Integer.parseInt(args[1]);

        int sum = add(value1, value2);

        System.out.println("Sum of " + value1 + " and " + value2 + " is " + sum);
    }

    public static int add(int value1, int value2) throws Exception {
        URL url = new URL("http://localhost:8080/jaxws-calculator-1.0/CalculatorService.wsdl");
        QName qname = new QName("http://jws.samples.geronimo.apache.org", "Calculator");
        Service service = Service.create(url, qname);
        
        Calculator calc = (Calculator)service.getPort(Calculator.class);
        
        return calc.add(value1, value2);
    }
}

Tools used

The tools used for developing and building the Calculator sample application are:

Maven 2.0.x

Maven is used for building the Calculator application. Maven can be downloaded from the following URL:
http://maven.apache.org/

Building

Compile Source Code

From command prompt execute the following command in the Calculator folder:

mvn -Dmaven.test.skip=true install

After the code is successfully compiled a jaxws-calculator-1.0.war file will be created in the target subfolder.

Deploying

Deploying the application is pretty straight forward, since we are going to using Geronimo Console.

  1. Scroll to Deploy New from the Console Navigation panel.
  2. Load jaxws-calculator-1.0.war from Calculator/targer folder in to the Archive input box.
  3. Press Install button to deploy application in the server.
  • No labels