Skip to content

Writing a Java Client Wrapper for a RPC-encoded Web Service

Writing a Java wrapper for non-WS-I-compliant Web services requires that you have the proper environment configured on your computer. There are other options available, but these examples use Eclipse 3.6 (Helios) as the Java IDE.

Requirements

  • You must have Java installed. This example uses JDK 1.6.0_16 and assumes that Java and Javac are in your system path.
  • Axis version 1.4, which is available from the Apache web site.
  • VHI 6.5 with the CICSAcctsDemo model deployed and a Web service generated.

Optional

A Java Integrated Development Environment (IDE). This example uses Eclipse 3.6 (Helios).

Basic steps

  1. Generate the client stub source files using Axis
  2. Write and compile the service wrapper client
  3. Test the class

Generating the client stub source files

  • After you download Axis, add an environment variable, AXIS_HOME, that points to the location where Axis is installed.
  • Axis includes a command line utility, WSDL2Java that consumes a WSDL, and then outputs the source code required to write a Java client.

This is a sample command line for the utility:

java -cp 
"%AXIS_HOME%/lib/axis.jar;
%AXIS_HOME%/lib/log4j-1.2.8.jar;
%AXIS_HOME%/lib/commons-logging-1.0.4.jar; 
%AXIS_HOME%/lib/commons-discovery-0.2.jar;
%AXIS_HOME%/lib/jaxrpc.jar;
%AXIS_HOME%/lib/wsdl4j-1.5.1.jar" 
org.apache.axis.wsdl.WSDL2Java -o C:\temp\wsdlgenclient\src -p com.attachmate.wrappedservices http://<server>:<port>/axis/services/CICSAcctsDemoWebService?wsdl
  • The Axis documentation explains the different options for WSDL2Java, but two that are in the example are:
    • -o the output location for the generated source code
    • -p the package name for the Java classes
  • The URL to the RPC-encoded Web service WSDL is the last parameter passed to WSDL2Java.
  • When the command completes, you should see these source files in the output folder, C:\temp\wsdlgenclient\src\com\attachmate\wrappedservices:
    • CICSAcctsDemoSession.java
    • CICSAcctsDemoSessionService.java
    • CICSAcctsDemoSessionServiceLocator.java
    • CICSAcctsDemoSoapBindingStub.java
    • GetAccountDetailRecord.java
    • GetAccountHistoryRecord.java
    • GetRecordSetDetailsBySearchNameRecord.java
    • SearchByNameRecord.java
    • SQLField.java
    • SQLRecord.java

Writing and compiling the service wrapper client

Using a text editor and Javac Java compiler you could use the generated source code to write a client. If you executed the call to javac from the C:\temp\wsdlgencleint folder, it would look similar to this:

javac -cp
"%AXIS_HOME%/lib/axis.jar;
%AXIS_HOME%/lib/log4j-1.2.8.jar;
%AXIS_HOME%/lib/commons-logging-1.0.4.jar;
%AXIS_HOME%/lib/commons-discovery-0.2.jar;
%AXIS_HOME%/lib/jaxrpc.jar;\
%AXIS_HOME%/lib/wsdl4j-1.5.1.jar" -d ".classes" .\src\com\attachmate\wrappedservices\*.java

However, using a Java IDE, such as Eclipse 3.6 (Helios), makes things much easier.

Using Eclipse 3.6 Helios to write the wrapper client

  1. Open Eclipse and create a Java project.

    New Eclipse project

  2. To add the generated source files to the project, copy the com folder from C:\temp\wsdlgenclient\src to the source folder in the Eclipse Project Explorer.

    Add source files to project

    Any errors that occur will be fixed in the next step.

  3. To fix the errors, right-click on the project in the Project Explorer, and select Properties.

    Open Properties panel

  4. Click on the Libraries tab, and then Add External JARs.... In the file dialog box that opens navigate to the lib folder in your Axis installation and add all the jars listed in the Java command.

    After you dismiss the Properties dialog box the errors should resolve and the Project Explorer should look like this:

    Project Explorer classes

    At this point in the process, there are only stub classes. To call the Web service's operations you need a wrapper class.

  5. Right-click on the com.attachmate.wrappedservices package in the Project Explorer, select New, and then class.

  6. Enter the name ServiceWrapper for the class and then click Finish.

    Java class dialog box

  7. To this new class, add a variable of type CICSAcctsDemoSoapBindingStub named stub and a constructor with no parameters.

    • In the constructor use the CICSAcctsDemoSessionServiceLocator.getCICSAcctsDemo() method to initialize the stub variable.
    • Add a method to execute one of the operations such as GetAccountDetail.

Example code for the wrapper class

// sample source code
package com.attachmate.wrappedservices;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class ServiceWrapper 
{
    CICSAcctsDemoSoapBindingStub stub;
    public ServiceWrapper()
    {
        try
        {
            CICSAcctsDemoSessionServiceLocator locator = new                            
            CICSAcctsDemoSessionServiceLocator();

            stub = (CICSAcctsDemoSoapBindingStub)locator.getCICSAcctsDemo();
        }
        catch (ServiceException afe)
        {
            afe.printStackTrace();
        }
    }
    public GetAccountDetailRecord[] getAccountDetail(int acctNum)
    {
        GetAccountDetailRecord[] retVal = null;
        try
        {
            retVal = stub.getAccountDetail(acctNum);
        }
        catch (RemoteException re)
        {
            re.printStackTrace();
        }
        return retVal;
    }
}

Other operations will require similar wrapper methods. Next create a test class by adding a new class as before only this time with a main method. In the main method of the new class instantiate an instance of ServiceWrapper and test the wrapper by calling ServiceWrapper.getAccountDetail(int acctNum).

This is an example of source code for the test class:

// sample source code
package com.attachmate.wrappedservices;
public class ServiceWrapperTestRunner
{
    public static void main(String[] args) 
    {
        ServiceWrapper wrapper = new ServiceWrapper();
        GetAccountDetailRecord[] records = wrapper.getAccountDetail(20000);
        for (GetAccountDetailRecord record : records)
        {
            System.out.println("Name on account with account number 20000 is " + 
    record.getFirstName() + " " + record.getLastName());
        }
    }
}

Running the test class

To run the test class:

  • Verify that the class is displaying in the editor.
  • From the Eclipse toolbar, expand the Run drop-down menu and select Run As, and then Java Application.
  • Check that the following output displays in the Eclipse Console

Eclipse Console


More Information