Creating a dynamic Array Generator Web Service using AXIS

I just happened to create a webservice and a client using ASIX for a web service class. As I know that in the environment it is difficult to get things to work and minor issues can halt the process. I am posting a simple Service, Client and my configurations just in case they are helpful to any one. In my environment TOMCAT is the web container and I compile it with AXIS jar files in my classpath, deploy.wsdd is used for deployment:
I use the variable %AXISCLASSPATH% which is set in my environment variables to following:

%AXIS_LIB%\axis.jar;%AXIS_LIB%\commons-discovery-0.2.jar;%AXIS_LIB%\commons-logging-1.0.4.jar;%AXIS_LIB%\jaxrpc.jar;%AXIS_LIB%\saaj.jar;%AXIS_LIB%\log4j-1.2.8.jar;%AXIS_LIB%\wsdl4j-1.5.1.jar;%AXIS_LIB%\xml-apis.jar;%AXIS_LIB%\xercesImpl.jar;

AXIS_LIB is set to: %AXIS_HOME%\lib
AXIS_HOME is set to : C:\axis-1_4

Step 1 is to code the service: In the given scenario the service expects an integer passed to it in the GetDynamicArrayOfRandomNumbers method and it will return an array with the number of elements equal to the number passed in. It will populate the array with random numbers.

import java.util.Random;

/**
 * @author Muhammad Asad Siddiqi
 * 06/13/2009
 *  This class is used as the web service and the .class has to be copied under Tomcat/axis/webapps
 *  deploy.wsdd has to be used for deployment
 */

public class DynamicArrayGeneratorService
{   
    /*
     * This is the remote method that would be called by the client
     */
    public int [] GetDynamicArrayOfRandomNumbers (int nArraySize) throws Exception
    {
        int []arrRandomNumbers = new int[nArraySize];
       
        for (int nCount=0; nCount < nArraySize; nCount++)
        {
            Random randomGenerator = new Random();
            arrRandomNumbers[nCount] = randomGenerator.nextInt((nCount * 4) + 10);           
        }
       
        return arrRandomNumbers;       
    }
}

Step 2: Compiling the service can be done by running the following commands from where your code is:

javac -classpath %AXISCLASSPATH% DynamicArrayGeneratorService.java

Step 3 is coding the client, I am copying and pasting the code for the client below:

import java.lang.*;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import java.net.URL;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;

/**
 *
 * @author Asad Siddiqi
 * Assignment # 2 Web services
 * This class is the client for the service running on localhost
 */
 
public class DynamicArrayGeneratorClient
 {
 public static void main(String args[])
 {
     try
  {
   int nSizeToReturn = 0;
   
   if ( args == null || args.length != 1 )
   {
    System.out.println(“The client utility has to pass in the exactly 1 parameter [size of the array]”);
    System.exit(1);
   }
   
   nSizeToReturn = Integer.parseInt(args[0]);   

   // Create an instance of the service
          Service serArrayGenerator = new Service();
   
   // Create an instance of the Call to call the method
   Call call = (Call) serArrayGenerator.createCall();
   
   // Set the Address for connection
   call.setTargetEndpointAddress(new java.net.URL(“http://localhost:8080/axis/services/DynamicArrayGeneratorService“));
   
   // Specify the method to call
   call.setOperationName(“GetDynamicArrayOfRandomNumbers”);
   
   // Call the remote method
   int [] arrReturned = (int[]) call.invoke(new Object [] {new Integer(nSizeToReturn)});

   // Print the output

   System.out.println(“PRINTING THE NUMBERS RETURNED BY SERVICE :”);
   System.out.println(“——————————————“);
   
   for (int i = 0; i < arrReturned.length; i++)
   {
    System.out.println(arrReturned[i]);
   }
     }
     catch( Exception e )
  {
   // Error occured during processing
   System.out.println(“Error Occured during processing …”);
   e.printStackTrace();
     }
 }
};

Step 4: Compiling the Client can be done by executing the following command from the same directory as your client code:

javac -classpath %AXISCLASSPATH% DynamicArrayGeneratorClient.java

Step 5 : In your command prompt navigate to the directory where your deploy.wsdd file is located. Once you are in the right directory execute the following command:

java -cp %AXISCLASSPATH% org.apache.axis.client.AdminClient -lhttp://localhost:8080/axis/services/AdminService deploy.wsdd

Step 6: The client can be executed by again changing the directory to where the .class files were generated for the client and executing the following command:

java -cp .;%AXISCLASSPATH% edu.jhu.webservices.Siddiqui.Asad.DynamicArrayGeneratorClient 20

I am also posting the contents of deploy.wsdd file:

<deployment name=”hw2″ xmlns=”http://xml.apache.org/axis/wsdd/
    xmlns:java=”http://xml.apache.org/axis/wsdd/providers/java“>

  <service name=”DynamicArrayGeneratorService” provider=”java:RPC”>
    <parameter name=”className” value=”DynamicArrayGeneratorService”/>
    <parameter name=”allowedMethods” value=”GetDynamicArrayOfRandomNumbers”/>
    <parameter name=”wsdlServicePort” value=”GetDynamicArrayOfRandomNumbers”/>
  </service>

  </deployment>

If you find it useful or I was able to help you out or you have a question, feel free to comment and I will make sure to reply. Being a .Net developer this was a real pain and it sucks, I just posted to help anyone in the same boat as me.

Advertisements

Leave a comment

Filed under 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s