From scm-return-37985-apmail-geronimo-scm-archive=geronimo.apache.org@geronimo.apache.org Tue Aug 18 01:57:04 2009 Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 29464 invoked from network); 18 Aug 2009 01:57:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 18 Aug 2009 01:57:04 -0000 Received: (qmail 34504 invoked by uid 500); 18 Aug 2009 01:57:23 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 34428 invoked by uid 500); 18 Aug 2009 01:57:22 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 34419 invoked by uid 99); 18 Aug 2009 01:57:22 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 Aug 2009 01:57:22 +0000 X-ASF-Spam-Status: No, hits=-1994.1 required=10.0 tests=ALL_TRUSTED,HTML_MESSAGE,MIME_HTML_ONLY,OBFUSCATING_COMMENT X-Spam-Check-By: apache.org Received: from [140.211.11.140] (HELO brutus.apache.org) (140.211.11.140) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 Aug 2009 01:57:20 +0000 Received: from brutus (localhost [127.0.0.1]) by brutus.apache.org (Postfix) with ESMTP id 14EFC234C044 for ; Mon, 17 Aug 2009 18:57:00 -0700 (PDT) Date: Mon, 17 Aug 2009 18:57:00 -0700 (PDT) From: confluence@apache.org To: scm@geronimo.apache.org Message-ID: <11170837.532.1250560620070.JavaMail.www-data@brutus> Subject: [CONF] Apache Geronimo v2.2 > Developing a simple Calculator Web service MIME-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Auto-Submitted: auto-generated X-Virus-Checked: Checked by ClamAV on apache.org

Developing a simple Calculator We= b service

Page edited by Ying Tang

=20

This tutorial will walk you through building and developing the JAX-WS c= alculator that ships with the samples package for Geronimo. After completin= g this tutorial, you should be able to understand how to build simple Web s= ervices with Apache Geronimo
using the Eclipse development environment.

To run this tutorial, as a minimum you will be required to have installe= d the following prerequisite software.

    =09
  1. Sun JDK 6.0+ (J2SE 1.6)
  2. =09
  3. Eclipse IDE for Java EE Developers, which is platform specific
  4. =09
  5. Apache Geronimo Eclipse Plugin 2.2.x
  6. =09
  7. Apache Geronimo Server 2.2.x
    3D""

    Geronimo version 2.2, Java 1.6 runtim= e, and Eclipse Ganymede are used is used in this tutorial but other version= s can be used instead (for example, Geronimo version 2.1, Java 1.5, Eclipse= Europa)

Details on installing eclipse are provided in the Development environment section. This tutorial is organized in the f= ollowing sections:

Setting Eclipse for application development

Create a Dynamic Web project

    =09
  1. Select File -> New, select Other....


  2. =09
  3. In the popup window select Web -> Dynamic Web Project cate= gory and click Next.


  4. =09
  5. Type in jaxws-calculator as the Project name and s= elect Next.


  6. =09
  7. Make sure Generate Deployment Descriptor is selected and clic= k Next.


  8. =09
  9. Modify the Group Id to: org.apache.geronimo.samples.jws= and Artifact Id to: Calculator.


    3D""Useful Information
      =09
    • Group ID: A name that identifies the group of a module. The default = value is default.
    • =09
    • Artifact ID: A name of the module in a group. The default value is &= lt;blank>.
    • =09
    • Version: The version of module. The default value is 1.0
    • =09
    • Type: The type of module, such as system module (car), jar module (j= ar), web archive (war), enterprise archive (ear), and so on. The default va= lue is car. A module is stored inside a repository as repository/groupId/ar= tifactId/version/artifactIdversion.
      type. This repository structure gives a more standardized and easily mainta= inable
      structure to Geronimo applications.

Axis2 configuration

    =09
  1. Select Window -> Preferences.


  2. =09
  3. Select Web Services -> Axis2 Preferences.
  4. =09
  5. Under Axis2 Runtime tab, browse to the installation root of= the Axis2 runtime installation to be used in Eclipse. Select OK.
    3D""Useful Information

    A = separate Axis2 runtime is required for use by the Eclipse Axis2 project fac= et. You can use the pre-integrated Axis2 1.4 binaries (axis-1.4.jar) from t= he local Geronimo repository under <Geronimo_home>/repository/axis/ax= is/1.4

  6. =09
  7. Righ click on the jaxws-calculator project and select Prop= erties, then select Project Facets.
  8. =09
  9. On the next screen check the box for Axis2 Web Services.


  10. =09
  11. Select Finish.
    This completes the configuration of Eclipse for application development.

Creating the Web services implementation code

To implement the Calculator Web service we are going to create a = package called org.apache.geronimo.samples.jws. This package will = contain two classes: a Calculator interface, and a CalculatorS= ervice which implements the Calculator interface. Let's go step= by step with creating the package, interface class and implementation clas= s.

    =09
  1. Right click on Java Resources: src and select New -> Pa= ckage.


  2. =09
  3. Name the package as org.apache.geronimo.samples.jws. Select= Finish.


  4. =09
  5. Right click on the new package and Select New -> Interface= .


    <= br clear=3D"all" />
  6. =09
  7. Name the interface as Calculator. Select Finish.


    =
  8. =09
  9. Add the following code to the Calculator interface class:
    =
    Calculator.class
    package org.apache.geronimo.samples.jws=
    ;
    
    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    
    @WebService(name=3D"CalculatorPortType",
                targetNamespace =3D "http://jws.samples.geronimo.apache.org")
    public interface Calculator {
    
        @WebMethod
        public =
    int add(@WebParam(name =3D "value1") int value1,
                       @WebParam(name =3D "value2") int value2);
    
    }
    
  10. =09
  11. Right click on the package org.apache.geronimo.samples.jws an= d select New -> Class.
  12. =09
  13. Name the class CalculatorService.
  14. =09
  15. Accept all the defaults and Select Finish..


  16. =09
  17. Add the following code to CalculatorService class:
    =
    CalculatorService.class
    package org.apache.geronimo.samples.jws=
    ;
    
    import javax.annotation.Resource;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    
    @WebService(serviceName =3D "Calculator",
                portName=3D"CalculatorPort",
                endpointInterface =3D "org.apache.ge=
    ronimo.samples.jws.Calculator",
                targetNamespace =3D "http://jws.samples.geronimo.apache.org"
                )
    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;
        }
    }
    

Let us try to understand each annotation.

    =09
  • @WebService- This annotation can be used with a Java class as= well as with interface. In our case we used it with both interface as well= as the POJO. This annotation declares the POJO as a WebService. @WebServic= e annotation is utilized in generating the WSDL file. =09
      =09=09
    • serviceName is same as the WSDL element service
    • =09=09
    • name is same as the WSDL element <portType name> =09=09
    • endpointInterface suggests the user defined name for the S= ervice Endpoint Interface (SEI).
    • =09=09
    • portName is the element portName
    • =09=09
    • targetNamespace is the XML namespace of the WSDL and some = of the XML elements generated from the WebService
    • =09
    =09
  • =09
  • @WebMethod- This annotation is applied to a method to expose = it as a WebService method. In case you have multiple methods you can use th= is annotation to selectively expose methods as WebService method. If you do= not use this annotation all the public methods will be exposed as WebServic= e.
  • =09
  • @WebParam- This annotation is used along with @WebMethod anno= tation to define the WebService. It is used to customize parameter used in = the message part of the WSDL.

This completes the development of the Web Service Implementation code.

Generating the WSDL for the Web service

Geronimo provides a new jaxws-tools.bat which helps in generating the WS= DL file from service endpoint interface.

    =09
  • Export the source files to a jar as shown in the following three fig= ures.




    <= br clear=3D"all" />

    <= br clear=3D"all" />
  • =09
  • Open a command prompt and point it to the bin directory of se= rver installation. =09
      =09=09
    1. Run the command:
      =
      WSDL Generation command
      Administrator@T60J9:/> jaxws-tools.bat wsgen -classpath C:/WSDL/source.j=
      ar -d C:/WSDL/ -wsdl:soap1.1 org.apache.geronimo.samples.jws.CalculatorServ=
      ice
      

      In this command -classpath is used to set the source.jar e= xported from Eclipse in the classpath, -d defines the location where= all the generated artifacts will be placed, -wsdl:soap1.1 suggests = a WSDL generation following soap1.1 protocol, and org.apache.geronimo.sa= mples.jws.CalculatorService is the SEI used to generate the WSDL.

      =09=09
    2. Once the above command is run Calculator_schema1.xsd and= Calculator.wsdl will be generated at C:/WSDL/.

    3. =09=09
    4. Rename Calculator.wsdl as CalculatorService.wsdl and add both the generated files to WEB-INF directory of the project. =09
    =09

Developing a Web client for Calculator

This section will take you through the creation of two JSP's: index.= jsp and result.jsp. index.jsp will prompt the user t= o enter two whole number values to add together. After submitting the form,= the action will be forwarded to the result.jsp, which will call t= he Calculator Web Service.

    =09
  1. Right click on the Web Content folder and Select New --> JSP.

  2. =09
  3. Name the JSP as index.jsp and select Finish.


  4. =09
  5. Add the following code to index.jsp:
    =
    index.jsp
    <%@ page
    import=3D"java.net.URL,javax.xml.namesp=
    ace.QName,javax.xml.ws.Service,org
    .apache.geronimo.samples.jws.Calculator"%>
    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"=
    en">
    <head>
    <title>Calculator</title>
    </head>
    <body>
    <form action=3D"result.jsp">
    Please enter 2 whole numbers to add: <input type=3D"text"
    name=3D"value1"> + <input type=3D"text" name=3D"v=
    alue2"> <input type=3D"submit"
    value=3D"=3D">
    </form>
    </body>
    </html>
    
  6. =09
  7. Right click on the Web Content folder and Select New -> JSP.

  8. =09
  9. Name the JSP as result.jsp and select Finish.

  10. =09
  11. Add the following code to result.jsp:
    =
    result.jsp
    <%@ page
    import=3D"java.net.URL,javax.xml.namesp=
    ace.QName,javax.xml.ws.Service,org
    .apache.geronimo.samples.jws.Calculator"%>
    <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"=
    en">
    <head>
    <title>Calculator Result</title>
    </head>
    <%
    int value1 =3D 0;
    int value2 =3D 0;
    int sum =3D 0;
    try {
    System.out.println( request.getParameter=
    ( "value1" ) + " " +
    request.getParameter( "value2" ) );
    value1 =3D Integer.parseInt( request.get=
    Parameter( "value1" ) );
    value2 =3D Integer.parseInt( request.get=
    Parameter( "value2" ) );
    URL url =3D new URL("http://localhost:8080/jaxws-calculator-
    1.0/calculator?wsdl");
    QName qname =3D new
    QName("http://jws.s=
    amples.geronimo.apache.org", "Calculator"=
    );
    Service service =3D Service.create(url, qname);
    Calculator calc =3D
    (Calculator)service.getPort(Calculator.class);
    sum =3D calc.add(value1, value2);
    } catch ( Exception e ) {
    e.printStackTrace();
    }
    %>
    <body>
    The result is: <%=3Dvalue1%>+<%=3Dvalue2%>=3D<%=3Dsum%>
    <br>
    <a href=3D"index.jsp">Back</a>=
    ;
    </body>
    </html>
    

    This finishes the development of Web client.

Setting up the Deployment Descriptor and D= eployment Plan

    =09
  1. Double click WEB-INF/web.xml and add the following code:
    =
    web.xml
    <?xml version=3D"1.0" encoding=3D"UTF-8"?>
    <web-app xmlns:calc=3D"urn:geronimo-samples-j=
    ws"
    xmlns=3D"http://jav=
    a.sun.com/xml/ns/javaee"
    version=3D"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>
    <service-ref>
                <service-ref-name>services/Calculator</service-ref-nam=
    e>
                <service-interface>ja=
    vax.xml.ws.Service</service-interface>
                <wsdl-file>WEB-INF/CalculatorService.wsdl</wsdl-file&g=
    t;
            </service-ref>
    </web-app>
    
  2. =09
  3. Similarly double click geronimo-web.xml and add the following= code:
    =
    geronimo-web.xml
    <?xml version=3D"1.0" encoding=3D"UTF-8"?>
    <web:web-app xmlns:dep=3D"http://geronimo.apache.org/xml/ns/deployment-1.2"=20
                 xmlns:conn=3D"http://geronimo.apache.org/xml/ns/j2ee/connector-1.2"=
    =20
                 xmlns:name=3D"http://geronimo.apache.org/xml/ns/naming-1.2"=20
                 xmlns:ejb=3D"http://openejb.apache.org/xml/ns/openejb-jar-2.2"=20
                 xmlns:pkgen=3D"http://openejb.apache.org/xml/ns/pkgen-2.1"=20
                 xmlns:app=3D"http://geronimo.apache.org/xml/ns/j2ee/application-2.0"=20
                 xmlns:sec=3D"http://geronimo.apache.org/xml/ns/security-2.0"=20
                 xmlns:web=3D"http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1"=20
                 xmlns:pers=3D"http://java.sun.com/xml/ns/persistence"=20
                 xmlns:client=3D"http://geronimo.apache.org/xml/ns/j2ee/application-client-=
    2.0">
    
        <dep:environment>
            <dep:moduleId>
                <dep:groupId>org.apache.geronimo.samples.jws</dep:grou=
    pId>
                <dep:artifactId>Calculator</dep:artifactId>
                <dep:version>1.0</dep:version>
                <dep:type>car</dep:type>
            </dep:moduleId>
            <dep:dependencies>
            </dep:dependencies>
        </dep:environment>
    
        <context-root>/jaxws-calculator-1.0</context-root>
    
        <service-ref>
            <service-ref-name>services/Calculator</service-ref-name>=
    ;
            <port>
                <port-name>CalculatorPort</port-name>
                <protocol>http</protocol>
                <host>localhost</host>
                <port>8080</port>
                <uri>/jaxws-calculator/calculator</uri>
            </port>
        </service-ref>
    </web:web-app>
    

Depl= oy and Run

    =09
  1. Right click on jaxws-calculator project. Select Run As -&g= t; Run On Server.



  2. =09
  3. On the next window select Apache Geronimo V2.2 and Select Finish.=



  4. =09
  5. Once the application is deployed on the server launch a browser and = run the following URL http://localhost:8080/jaxws-calculator-1.0/ind= ex.jsp.




  6. =09
  7. Give the values as 5 and 7 and then select the =3D<= /b> button:




  8. =09
  9. The result is 12 as shown in the figure:
    =