Web Services & Apache Axis
by Paul Tremblett

Listing One

package ca.tremblett.ddj;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.utils.Options;

import javax.xml.rpc.ParameterMode;
public class TemperatureConversionClient {
  public static void main(String [] args) throws Exception {
    if (args.length != 3) {
      System.err.println("Usage: " +
        "java TemperatureConversionClient url operation temp");
        System.exit(1);
    }

    if ((!"f2c".equals(args[1])) && (!"c2f".equals(args[1]))) {
      System.err.println(args[1] + " is not a valid operation");
      System.exit(1);
    }
    Double temp = null;
    try {
      temp = new Double(args[2]);
    }
    catch (NumberFormatException e) {
      System.err.println(args[2] + " is not a valid temperature");
      System.exit(1);
    }
    String endpoint = "http://" + args[0] + 
      "/axis/TemperatureConversion.jws";
    Service service = new Service();
    Call call = (Call) service.createCall();
    call.setTargetEndpointAddress( new java.net.URL(endpoint) );
    call.setOperationName( args[1] );
    call.addParameter( "temp", XMLType.XSD_DOUBLE, ParameterMode.IN );
    call.setReturnType( XMLType.XSD_DOUBLE );
    System.out.println(temp.toString() + " degrees " +
      (("f2c".equals(args[1])) ? "Farenheit" : "Celsius") + 
      " = " + (Double) call.invoke( new Object [] { temp }) +
      " degrees " + (("f2c".equals(args[1])) ? 
      "Celsius" : "Farenheit"));
  }
}



Listing Two

package org.random.www.RandomDotOrg_wsdl;

public class RandomClient {
  public static void main(String[] args) {
    org.random.www.RandomDotOrg_wsdl.RandomDotOrgLocator service =
      new org.random.www.RandomDotOrg_wsdl.RandomDotOrgLocator();
    try {
    org.random.www.RandomDotOrg_wsdl.RandomDotOrgPortType port =
      service.getRandomDotOrgPort();
    System.out.println("lrand48 returned " + 
      port.lrand48());
    System.out.println("mrand48 returned " +
      port.mrand48());
    }
    catch (Exception e) {
        System.err.println("web service failed");
    }
  }
}



Listing Three

/** CanadaInfoSoapBindingImpl.java
 * This file was auto-generated from WSDL by Apache Axis WSDL2Java emitter.
 */

package ca.tremblett.ddj.ws;

public class CanadaInfoSoapBindingImpl 
    implements ca.tremblett.ddj.ws.CanadaInfo{

  public java.lang.String[] provinces() 
      throws java.rmi.RemoteException {
    return null;
  }
  public java.lang.String[] provincesAndTerritories() 
      throws java.rmi.RemoteException {
    return null;
  }
  public java.lang.String[] territories() 
      throws java.rmi.RemoteException {
    return null;
  }
  public java.lang.String capital(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    return null;
  }
  public int population(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    return -3;
  }
  public java.lang.String premier(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    return null;
  }
  public int area(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    return -3;
  }
}



Listing Four

/** CanadaInfoSoapBindingImpl.java
 * This file was auto-generated from WSDL by Apache Axis WSDL2Java emitter.
 */

package ca.tremblett.ddj.ws;

import ca.tremblett.ddj.CanadaInfo;

public class CanadaInfoSoapBindingImpl 
      implements ca.tremblett.ddj.ws.CanadaInfo{
  public CanadaInfo ci = null;
  public CanadaInfoSoapBindingImpl() throws java.lang.Exception {
    ci = new CanadaInfo();
  }
  public java.lang.String[] provinces() 
      throws java.rmi.RemoteException {
    return ci.provinces();
  }
  public java.lang.String[] provincesAndTerritories() 
      throws java.rmi.RemoteException {
    return ci.provincesAndTerritories();
  }
  public java.lang.String[] territories() 
      throws java.rmi.RemoteException {
    return ci.territories();
  }
  public java.lang.String capital(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    try {
      return ci.capital(provinceOrTerritory);
    }
    catch (java.lang.Exception e) {
      throw new java.rmi.RemoteException();
    }
  }
  public int population(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    try {
      return ci.population(provinceOrTerritory);
    }
    catch (java.lang.Exception e) {
      throw new java.rmi.RemoteException();
    }
  }
  public java.lang.String premier(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    try {
      return ci.premier(provinceOrTerritory);
    }
    catch (java.lang.Exception e) {
      throw new java.rmi.RemoteException();
    }
  }
  public int area(java.lang.String provinceOrTerritory) 
      throws java.rmi.RemoteException {
    try {
      return ci.area(provinceOrTerritory);
    }
    catch (java.lang.Exception e) {
      throw new java.rmi.RemoteException();
    }
  }
}



Listing Five

package ca.tremblett.ddj;

public class CanadaInfoClient {
  public static void main(String [] args) throws Exception {
    try {
      ca.tremblett.ddj.ws.CanadaInfoService service = 
        new ca.tremblett.ddj.ws.CanadaInfoServiceLocator();
      ca.tremblett.ddj.ws.CanadaInfo ci = service.getcanadaInfo();
      System.out.println("Capital of Newfoundland is " + ci.capital("NL"));
    }
    catch (Exception e) {
      System.err.println("Web Service failed");
    }
  }
}







1


