| Standards for Technology in Automotive Retail |
| Home - News Feed

6.2. Apache Axis 2
Prev | Chapter 6. Java Examples | Next

## 6.2. Apache Axis 2

In this example we will use the Apache Axis2 framework to generate a simple Web Service and client. The homepage for Axis2 is http://ws.apache.org/axis2/ . The target runtime for the service is the Apache Tomcat server. The client will be a standalone application.

This example was created using the JDK 5.0 Update 14, Apache Ant 1.6.5, Apache Axis2 release 1.3, and Tomcat 5.5.

### 6.2.1. Prerequisites

1. Prerequisites: Follow the instructions on downloading and installing the Axis2 binary distribution ( http://ws.apache.org/axis2/1_3/installationguide.html#standalone1 ).

2. Since our target runtime is Tomcat, refer to the Tomcat homepage ( http://tomcat.apache.org/ ) for download and installation instructions. Once Tomcat is installed, follow the instructions to install Axis2 ( http://ws.apache.org/axis2/1_3/installationguide.html#servlet_container ).

Both service and client will be generated using the “WSDL2Java” script included with the Axis2 binary distribution. This script takes a WSDL file as input and generates the appropriate Java classes as output. The “Axis2 Reference Guide” ( http://ws.apache.org/axis2/1_3/reference.html ) lists the parameters for the script and shows example usage.

### 6.2.2. Apache Axis 2 Walkthrough

#### 6.2.2.1. Axis 2 Service

Procedure 6.4. Axis 2 Service

1. If the WSDL was retrieved from the “STAR Web Services Specifications” document, it is missing a crucial section in order for the script to properly generate the code. This is the service section, which contains information specific to your Web Service implementation, including the address of the Web Service. Assuming you will be running a local instance of Tomcat for this example, the following information can be added to the STAR WSDL:

       <wsdl:service name="StarQuickStartServiceAxis2">
          <wsdl:port name="StarTransport" binding="starbindings:starTransport">
             <soap:address location="http://localhost:8080/StarQuickStartServiceAxis2/StarTransport"/>
          </wsdl:port>
       </wsdl:service>

2. Generating server code: Using the aforementioned WSDL2Java script, generate the Java code for the service. At a windows command prompt, move to the bin directory of your Axis2 installation. Execute the WSDL2Java script with the -uri option to specify the location of the STAR WSDL, the -ss and -sd options to generate the server code and service descriptor, and the -o option to specify the output location. By default the script generates client code. This is why the options are needed for the service artifacts.

   Figure 6.3. Axis 2 Generate Service

   The script will create a src folder for the source code, a resources folder for the services.xml and a generated wsdl, and an ant build script. These are all the files needed for a simple service implementation. Note that the generated wsdl is identical to the one specified as input to the script, though formatting changes have been made.

3. Modify Service Skeleton with Implementation Code: One of the Java classes generated by WSDL2Java is a “skeleton” for the code you will eventually implement to handle Web Service calls. If you modified the WSDL with the service information from above, this class will be named “StarQuickStartServiceAxis2Skeleton.” Add code to this class to indicate the service is being invoked. If you do not modify this class, the methods will throw an UnsupportedOperationException when called since this is what has been generated by default.

   Figure 6.4. Axis 2 Modify Skelton

4. Generate Axis Archive (*.aar) for the Web Service

   Once the skeleton class has been modified, the Web Service is ready to be packaged as an Axis Archive. Using the ant build file generated by WSDL2Java, execute the “jar.server” target.

   Figure 6.5. Axis 2 Generate AAR

   Once the script is complete, StarQuickStartServiceAxis2.aar will be in the build/lib folder.

Procedure 6.5. Deploy Web Service to Tomcat

1. The service is ready to be deployed to the application server using the Axis2 Web application you installed as a part of the prerequisites. If you are using defaults, this application can be accessed at http://localhost:8080/axis2/ . The page will look similar to the screenshot below.

   Figure 6.6. Axis 2 Deploy Service

2. Click on the “Administration” link. When prompted to login, the initial username/password is admin/axis2. Refer to the “Apache Axis2 Web Administrator’s Guide” (http://ws.apache.org/axis2/1_3/webadminguide.html) for instructions on how to change the default login as well as other information regarding the application. Below is a screenshot of the administration console.

   Figure 6.7. Axis 2 Admin Page

3. Use the “Available Services” link to check the deployment of the Web Service. You should see the “StarQuickStartServiceAxis2” listed:

   Figure 6.8. Axis 2 Available Services

#### 6.2.2.2. Axis 2 Client

Procedure 6.6. Generating Client Code

- Using the WSDL2Java script again, generate the Java code for the client. Follow the same steps used to generate the service, but exclude the -ss and -sd options that were used to generate the service-specific code. You can also change the output location in order to separate your service and client code.

  Figure 6.9. Axis 2 Generate Client Code

Procedure 6.7. Calling the Web Service

1. Use Generated Stub to call Web Service.

   The output of WSDL2Java generating a client will be a “Stub” class that will be used to call the Web Service. For this example, we will use the Stub class to transmit a ProcessPartsOrder BOD via a PutMessage call. Below is an example of a main method using the StarQuickStartServiceAxis2Stub generated by WSDL2Java. This code reads the BOD from a file.

       package org.star.quickstart.client;

       import java.io.*;
       import javax.xml.stream.*;
       import org.apache.axiom.om.*;
       import org.apache.axiom.om.impl.builder.*;
       import org.apache.axis2.*;
       import org.apache.axis2.databinding.types.*;
       import org.starstandards.www.webservices._2005._10.transport.bindings.*;
       import org.starstandards.www.webservices._2005._10.transport.bindings.StarQuickStartServiceAxis2Stub.*;

       public class TestClient {
          public static void main(String[] args) {
             try {
                String endpoint = "http://localhost:8080/axis2/services/StarQuickStartServiceAxis2";
                StarQuickStartServiceAxis2Stub stub = new StarQuickStartServiceAxis2Stub(endpoint);

               //Create Payload Manifest
               PayloadManifest0 payloadManifest0 = new PayloadManifest0();

               PayloadManifest payloadManifest = new PayloadManifest();
               Manifest manifest = new Manifest();

               manifest.setContentID(new IDRef("Content0"));
               manifest.setElement("ProcessPartsOrder");
               manifest.setNamespaceURI(new URI("http://www.starstandard.org/STAR/5"));
               manifest.setVersion("5.2.1");
               Manifest[] manifestAry = new Manifest[] { manifest };

               payloadManifest.setManifest(manifestAry);
               payloadManifest0.setPayloadManifest(payloadManifest);

               PutMessage putMessage = new PutMessage();
               Payload payload = new Payload();

               //Create Content
               Content content = new Content();
               content.setId(new Id("Content0"));
               //Read in Process Parts Order BOD
               XMLStreamReader parser = XMLInputFactory.newInstance()
                  .createXMLStreamReader(
                     new FileInputStream("C:\\axis2-1.3\\bin\\STAR\\Rev5.2.1\\BODExamples\\ProcessPartsOrder.xml"));

               StAXOMBuilder builder = new StAXOMBuilder(parser);
               OMElement documentElement = builder.getDocumentElement();

               content.setExtraElement(documentElement);
               payload.addContent(content);
               putMessage.setPayload(payload);

               //Call PutMessage operation
               stub.PutMessage(putMessage, payloadManifest0);
            } catch (Exception e) {
                 e.printStackTrace();
            }
         }
       }

2. Once this class is complete, it can be used to call the Web Service deployed to Tomcat. If you added a println statement to the PutMessage method of your skeleton, you should now see the message in the Tomcat stdout log. The client may receive an error on the response from the service; this is because additional work is necessary to complete the Web Service and client.

Prev | Up | Next
Chapter 6. Java Examples | Home | Chapter 7. Windows Communication Framework Examples