ESB Was 測試類

package Test;

import csmp.mediation.TotalAmountHandlerImpl;
import csmp.util.XmlUtil;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Vector;
import javax.wsdl.Binding;
import javax.wsdl.Operation;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.encoding.Deserializer;
import javax.xml.rpc.encoding.DeserializerFactory;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.TypeMapping;
import org.apache.axis.encoding.ser.ElementDeserializerFactory;
import org.apache.axis.encoding.ser.ElementSerializerFactory;
import org.apache.axis.encoding.ser.SimpleDeserializer;
import org.apache.axis.utils.XMLUtils;
import org.apache.axis.wsdl.gen.Parser;
import org.apache.axis.wsdl.symbolTable.BaseType;
import org.apache.axis.wsdl.symbolTable.BindingEntry;
import org.apache.axis.wsdl.symbolTable.Parameter;
import org.apache.axis.wsdl.symbolTable.Parameters;
import org.apache.axis.wsdl.symbolTable.ServiceEntry;
import org.apache.axis.wsdl.symbolTable.SymTabEntry;
import org.apache.axis.wsdl.symbolTable.SymbolTable;
import org.apache.axis.wsdl.symbolTable.TypeEntry;
import org.apache.axis.wsdl.toJava.Utils;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WebServiceTest
{
  private Parser wsdlParser = null;

  public static void main(String[] args)
    throws Exception
  {
    String file = "E:\\e\\XXXX.xml";

    TotalAmountHandlerImpl t = new TotalAmountHandlerImpl();

    DOMParser parser = new DOMParser();
    parser.parse(file);
    Document doc = parser.getDocument();

    String url = "http://IP:port /XXX/XX/XXX/wsdl/XXXX.wsdl";
    String method = "methodName";
    String param = XmlUtil.document2String(doc);

    args = new String[3];
    args[0] = url;
    args[1] = method;
    args[2] = param;

    if (args.length < 2)
      usage();

    String wsdlLocation = (args.length > 0) ? args[0] : null;
    String operationName = (args.length > 1) ? args[1] : null;
    String portName = null;
    try {
      portName = operationName.substring(operationName.indexOf("(") + 1,
        operationName.indexOf(")"));
      operationName = operationName.substring(0,
        operationName.indexOf("("));
    }
    catch (Exception localException) {
    }
    WebServiceTest invoker = new WebServiceTest(wsdlLocation);
    HashMap map = invoker.invokeMethod(operationName, portName, args);

    for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
      Map.Entry entry = (Map.Entry)it.next();
      String key = (String)entry.getKey();
      Object value = entry.getValue();
      if (value instanceof Element) {
        System.out.println("====== " + key + " ======");
        XMLUtils.ElementToStream((Element)value, System.out);
        System.out.println("========================="); break label309:
      }
      label309: System.out.println(key + "=" + value);
    }

    System.out.println("\nDone!");
  }

  private static void usage() {
    System.err.println("Usage: java " + WebServiceTest.class.getName() +
      " wsdlLocation " + "operationName[(portName)] " +
      "[argument1 ...]");
    System.exit(1);
  }

  public WebServiceTest(String wsdlURL) throws Exception
  {
    this.wsdlParser = new Parser();
    System.out.println("Reading WSDL document from '" + wsdlURL + "'");
    this.wsdlParser.run(wsdlURL);
  }

  public HashMap invokeMethod(String operationName, String portName, String[] args) throws Exception
  {
    String serviceNS = null;
    String serviceName = null;
    String operationQName = null;

    System.out.println("Preparing Axis dynamic invocation");
    Service service = selectService(serviceNS, serviceName);
    Operation operation = null;
    Service dpf = new Service(
      this.wsdlParser, service.getQName());

    Vector inputs = new Vector();
    Port port = selectPort(service.getPorts(), portName);
    if (portName == null)
      portName = port.getName();

    Binding binding = port.getBinding();
    Call call = dpf.createCall(QName.valueOf(portName),
      QName.valueOf(operationName));
    ((Call)call).setTimeout(new Integer(15000));
    ((Call)call).setProperty(
      "DeserializeCurrentElement", Boolean.TRUE);

    Vector outNames = new Vector();

    Vector inNames = new Vector();
    Vector inTypes = new Vector();
    SymbolTable symbolTable = this.wsdlParser.getSymbolTable();
    BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
    Parameters parameters = null;
    Iterator i = bEntry.getParameters().keySet().iterator();

    while (i.hasNext()) {
      Operation o = (Operation)i.next();
      if (o.getName().equals(operationName)) {
        operation = o;
        parameters = (Parameters)bEntry.getParameters().get(o);
        break;
      }
    }
    if ((operation == null) || (parameters == null)) {
      throw new RuntimeException(operationName + " was not found.");
    }

    for (int j = 0; j < parameters.list.size(); ++j) {
      Parameter p = (Parameter)parameters.list.get(j);

      if (p.getMode() == 1) {
        inNames.add(p.getQName().getLocalPart());
        inTypes.add(p);
      } else if (p.getMode() == 2) {
        outNames.add(p.getQName().getLocalPart());
      } else if (p.getMode() == 3) {
        inNames.add(p.getQName().getLocalPart());
        inTypes.add(p);
        outNames.add(p.getQName().getLocalPart());
      }

    }

    if (parameters.returnParam != null)
    {
      if (!(parameters.returnParam.getType().isBaseType())) {
        ((Call)call).registerTypeMapping(
          Element.class,
          parameters.returnParam.getType().getQName(),
          new ElementSerializerFactory(),
          new ElementDeserializerFactory());
      }

      QName returnType =
        Utils.getXSIType(parameters.returnParam);
      QName returnQName = parameters.returnParam.getQName();

      outNames.add(returnQName.getLocalPart());
    }

    if (inNames.size() != args.length - 2)
      throw new RuntimeException("Need " + inNames.size() +
        " arguments!!!");

    for (int pos = 0; pos < inNames.size(); ++pos) {
      String arg = args[(pos + 2)];
      Parameter p = (Parameter)inTypes.get(pos);
      inputs.add
        (getParamData((Call)call, p,
        arg));
    }
    System.out.println("Executing operation " + operationName +
      " with parameters:");
    for (j = 0; j < inputs.size(); ++j)
      System.out.println(inNames.get(j) + "=" + inputs.get(j));

    Object ret = call.invoke(inputs.toArray());
    Map outputs = call.getOutputParams();
    HashMap map = new HashMap();

    for (int pos = 0; pos < outNames.size(); ++pos) {
      String name = (String)outNames.get(pos);
      Object value = outputs.get(name);

      if ((value == null) && (pos == 0))
        map.put(name, ret);
      else
        map.put(name, value);
    }

    return map;
  }

  public Service selectService(String serviceNS, String serviceName) throws Exception
  {
    QName serviceQName = ((serviceNS != null) && (serviceName != null)) ?
      new QName(serviceNS, serviceName) :
      null;
    ServiceEntry serviceEntry = (ServiceEntry)getSymTabEntry(serviceQName,
      ServiceEntry.class);
    return serviceEntry.getService();
  }

  public SymTabEntry getSymTabEntry(QName qname, Class cls) {
    HashMap map = this.wsdlParser.getSymbolTable().getHashMap();
    Iterator iterator = map.entrySet().iterator();

    while (iterator.hasNext()) {
      Map.Entry entry = (Map.Entry)iterator.next();
      QName key = (QName)entry.getKey();
      Vector v = (Vector)entry.getValue();

      if ((qname == null) || (qname.equals(qname)))
        for (int i = 0; i < v.size(); ++i) {
          SymTabEntry symTabEntry = (SymTabEntry)v.elementAt(i);

          if (cls.isInstance(symTabEntry))
            return symTabEntry;
        }

    }

    return null;
  }

  public Port selectPort(Map ports, String portName) throws Exception {
    Iterator valueIterator = ports.keySet().iterator();
    while (valueIterator.hasNext()) {
      String name = (String)valueIterator.next();

      if ((portName == null) || (portName.length() == 0)) {
        Port port = (Port)ports.get(name);
        List list = port.getExtensibilityElements();

        int i = 0; break label90:
        do { Object obj = list.get(i);
          if (obj instanceof SOAPAddress)
            return port;
          ++i; label136: label90: if (list == null) break label136;  }
        while (i < list.size());
      }
      else if ((name != null) && (name.equals(portName))) {
        return ((Port)ports.get(name));
      }
    }
    return null;
  }

  private Object getParamData(Call c, Parameter p, String arg)
    throws Exception
  {
    QName paramType = Utils.getXSIType(p);

    TypeEntry type = p.getType();
    if ((type instanceof BaseType) && (((BaseType)type).isBaseType())) {
      DeserializerFactory factory = c.getTypeMapping().getDeserializer(
        paramType);
      Deserializer deserializer = factory.getDeserializerAs
        ("Axis SAX Mechanism");
      if (deserializer instanceof SimpleDeserializer)
        return ((SimpleDeserializer)deserializer).makeValue(arg);
    }

    throw new RuntimeException("not know how to convert '" + arg +
      "' into " + c);
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章