DynamicInvoker



import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.xml.namespace.QName;
import javax.xml.rpc.Call;
import javax.xml.rpc.encoding.Deserializer;
import javax.xml.rpc.encoding.DeserializerFactory;

import org.apache.axis.Constants;
import org.apache.axis.encoding.ser.SimpleDeserializer;
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.log4j.Logger;


/**
* Web Service Dynamic Invoker
*/
public class DynamicInvoker {

private static Logger logger = Logger.getLogger(DynamicInvoker.class);

private Parser wsdlParser = null;

private Map services = null;

public DynamicInvoker(String wsdlURL) throws Exception {

wsdlParser = new Parser();
try{

wsdlParser.run(wsdlURL);

}catch(Exception e){
e.printStackTrace();
}
services = enumSymTabEntry(ServiceEntry.class);
}

public Map invoke(String serviceName, String portName,
String operationName, Vector parameterValues) throws Exception {
logger.info("--------"+serviceName+"-----"+portName+"-----"+operationName+"---------begin-------");
logger.info(parameterValues.toString());
logger.info("--------"+serviceName+"-----"+portName+"-----"+operationName+"---------end-------");
Vector inputs = new Vector();
String returnName = null;

ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Service service = serviceEntry.getService();
org.apache.axis.client.Service clientService = new org.apache.axis.client.Service(
wsdlParser, service.getQName());

Call call = clientService.createCall(QName.valueOf(portName), QName
.valueOf(operationName));
((org.apache.axis.client.Call) call).setTimeout(new Integer(50 * 1000));

BindingEntry bindingEntry = getBindingEntry(serviceName, portName);
Operation o = getOperation(bindingEntry, operationName);
Parameters parameters = bindingEntry.getParameters(o);

if (parameters.returnParam != null) {
QName returnType = org.apache.axis.wsdl.toJava.Utils
.getXSIType(parameters.returnParam);
QName returnQName = parameters.returnParam.getQName();
returnName = returnQName.getLocalPart();
}

int size = parameters.list.size();
for (int i = 0; i < size; i++) {
Parameter p = (Parameter) parameters.list.get(i);
switch (p.getMode()) {
case Parameter.IN:
inputs.add(getParamData((org.apache.axis.client.Call) call, p,
(String) parameterValues.elementAt(i)));
break;
case Parameter.OUT:
break;
case Parameter.INOUT:
inputs.add(getParamData((org.apache.axis.client.Call) call, p,
(String) parameterValues.elementAt(i)));
break;
}
}

Object ret = call.invoke(inputs.toArray());
Map outputs = call.getOutputParams();
HashMap map = new HashMap();
if (ret != null && returnName != null) {
map.put(returnName, ret);
}
if (outputs != null) {
for (Iterator i = outputs.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
String name;
Object value;
if (obj.getClass().getName().equals("java.lang.String")) {
name = (String) obj;
} else {
name = ((QName) obj).getLocalPart();
}
value = outputs.get(obj);
map.put(name, value);
}
}
logger.info("--------"+serviceName+"-----"+portName+"-----"+operationName+"---------result begin-------");
logger.info(map.toString());
logger.info("--------"+serviceName+"-----"+portName+"-----"+operationName+"---------result end-------");
return map;
}

public Vector enumServiceNames() {
Vector v = new Vector();
Iterator i = services.keySet().iterator();
while (i.hasNext()) {
String s = (String) i.next();
v.addElement(s);
}
return v;
}

public Vector enumPortNames(String serviceName) {
Vector v = new Vector();
ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Map ports = serviceEntry.getService().getPorts();
Iterator i = ports.keySet().iterator();
while (i.hasNext()) {
String s = (String) i.next();
v.addElement(s);
}
return v;
}

public Vector enumOperationNames(String serviceName, String portName) {
Vector v = new Vector();
BindingEntry entry = getBindingEntry(serviceName, portName);
Set operations = entry.getOperations();
Iterator i = operations.iterator();
while (i.hasNext()) {
Operation o = (Operation) i.next();
String s = o.getName();
v.addElement(s);
}
return v;
}

public Parameters enumParameters(String serviceName, String portName,
String operationName) {
BindingEntry entry = getBindingEntry(serviceName, portName);
Operation o = getOperation(entry, operationName);
Parameters parameters = entry.getParameters(o);
return parameters;
}

public String getParameterModeString(Parameter p) {
String ret = null;
switch (p.getMode()) {
case Parameter.IN:
ret = "[IN]";
break;
case Parameter.INOUT:
ret = "[IN, OUT]";
break;
case Parameter.OUT:
ret = "[OUT]";
break;
}
return ret;
}

private BindingEntry getBindingEntry(String serviceName, String portName) {
ServiceEntry serviceEntry = (ServiceEntry) services.get(serviceName);
Port port = serviceEntry.getService().getPort(portName);
Binding binding = port.getBinding();
SymbolTable table = wsdlParser.getSymbolTable();
return table.getBindingEntry(binding.getQName());
}

private Operation getOperation(BindingEntry entry, String operationName) {
Set operations = entry.getOperations();
Iterator i = operations.iterator();
while (i.hasNext()) {
Operation o = (Operation) i.next();
if (operationName.equals(o.getName())) {
return o;
}
}
return null;
}


/**
* return Map of <QName.getLocalPart, SymTabEntry>
*/
private Map enumSymTabEntry(Class cls) {
HashMap ret = new HashMap();
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();
int size = v.size();
for (int i = 0; i < size; ++i) {
SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
if (cls.isInstance(symTabEntry)) {
ret.put(key.getLocalPart(), symTabEntry);
}
}
}
return ret;
}

private Object getParamData(org.apache.axis.client.Call c, Parameter p,
String arg) throws Exception {
// 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("Can not convert '" + arg
+ "' into " + c);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值