FileDocCategorySizeDatePackage
DynamicInvoker.javaAPI DocApache Axis 1.412611Sat Apr 22 18:56:52 BST 2006samples.client

DynamicInvoker

public class DynamicInvoker extends Object
This sample shows how to use Axis for completely dynamic invocations as it is completely stubless execution. It supports both doc/lit and rpc/encoded services. But this sample does not support complex types (it could if there was defined a to encode complex values as command line arguments).
author
Davanum Srinivas (dims@yahoo.com)

Fields Summary
private org.apache.axis.wsdl.gen.Parser
wsdlParser
Field wsdlParser
Constructors Summary
public DynamicInvoker(String wsdlURL)
Constructor DynamicInvoker

param
wsdlURL
throws
Exception


               
         
        // Start by reading in the WSDL using Parser
        wsdlParser = new Parser();
        System.out.println("Reading WSDL document from '" + wsdlURL + "'");
        wsdlParser.run(wsdlURL);
    
Methods Summary
private java.lang.ObjectgetParamData(org.apache.axis.client.Call c, org.apache.axis.wsdl.symbolTable.Parameter p, java.lang.String arg)
Method getParamData

param
c
param
arg

        // Get the QName representing the parameter type
        QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);

        TypeEntry type = p.getType();
        if (type instanceof BaseType && ((BaseType) type).isBaseType()) {
            DeserializerFactory factory = c.getTypeMapping().getDeserializer(paramType);
            Deserializer deserializer = factory.getDeserializerAs(Constants.AXIS_SAX);
            if (deserializer instanceof SimpleDeserializer) {
                return ((SimpleDeserializer)deserializer).makeValue(arg);
            }
        }
        throw new RuntimeException("not know how to convert '" + arg
                                   + "' into " + c);
    
public org.apache.axis.wsdl.symbolTable.SymTabEntrygetSymTabEntry(javax.xml.namespace.QName qname, java.lang.Class cls)
Method getSymTabEntry

param
qname
param
cls
return

        HashMap map = 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 java.util.HashMapinvokeMethod(java.lang.String operationName, java.lang.String portName, java.lang.String[] args)
Method invokeMethod

param
wsdlLocation
param
operationName
param
inputName
param
outputName
param
portName
param
args
return
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;
        org.apache.axis.client.Service dpf = new org.apache.axis.client.Service(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));
        ((org.apache.axis.client.Call)call).setTimeout(new Integer(15*1000));
        ((org.apache.axis.client.Call)call).setProperty(ElementDeserializer.DESERIALIZE_CURRENT_ELEMENT, Boolean.TRUE);
        
        // Output types and names
        Vector outNames = new Vector();

        // Input types and names
        Vector inNames = new Vector();
        Vector inTypes = new Vector();
        SymbolTable symbolTable = 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.");
        }

        // loop over paramters and set up in/out params
        for (int j = 0; j < parameters.list.size(); ++j) {
            Parameter p = (Parameter) parameters.list.get(j);

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

        // set output type
        if (parameters.returnParam != null) {

            if(!parameters.returnParam.getType().isBaseType()) {
                ((org.apache.axis.client.Call)call).registerTypeMapping(org.w3c.dom.Element.class, parameters.returnParam.getType().getQName(),
                            new ElementSerializerFactory(),
                            new ElementDeserializerFactory());
            }

            // Get the QName for the return Type
            QName returnType = org.apache.axis.wsdl.toJava.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((org.apache.axis.client.Call) call, p, arg));
        }
        System.out.println("Executing operation " + operationName + " with parameters:");
        for (int 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 static voidmain(java.lang.String[] args)
Method main

param
args
throws
Exception

        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 ignored) {
        }

        DynamicInvoker invoker = new DynamicInvoker(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("=========================");
            } else {
                System.out.println(key + "=" + value);
            }
        }
        System.out.println("\nDone!");
    
public javax.wsdl.PortselectPort(java.util.Map ports, java.lang.String portName)
Method selectPort

param
ports
param
portName
return
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();

                for (int i = 0; (list != null) && (i < list.size()); i++) {
                    Object obj = list.get(i);
                    if (obj instanceof SOAPAddress) {
                        return port;
                    }
                }
            } else if ((name != null) && name.equals(portName)) {
                return (Port) ports.get(name);
            }
        }
        return null;
    
public javax.wsdl.ServiceselectService(java.lang.String serviceNS, java.lang.String serviceName)
Method selectService

param
def
param
serviceNS
param
serviceName
return
throws
Exception

        QName serviceQName = (((serviceNS != null)
                && (serviceName != null))
                ? new QName(serviceNS, serviceName)
                : null);
        ServiceEntry serviceEntry = (ServiceEntry) getSymTabEntry(serviceQName,
                                                                  ServiceEntry.class);
        return serviceEntry.getService();
    
private static voidusage()
Method usage

        System.err.println(
                "Usage: java " + DynamicInvoker.class.getName() + " wsdlLocation "
                + "operationName[(portName)] "
                + "[argument1 ...]");
        System.exit(1);