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"));
  }
}
