123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- /*
- * Copyright 2001-2004 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.sinosoft.lz.system.sms.dao.ws;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- 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.Constants;
- import org.apache.axis.encoding.ser.ElementDeserializer;
- 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.message.SOAPHeaderElement;
- 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;
- /**
- * 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)
- * <p/>
- * History: 12/24/2007 hujinjun 修改invokeMethod方法,实现通用登录.
- */
- public class DynamicInvoker {
- /** Field wsdlParser */
- private Parser wsdlParser = null;
- // private static final int TIME_OUT = 300 * 1000; // Time out after 5
- // minute
- private Integer time_out = 300 * 1000;
- /**
- * @return Returns the time_out.
- */
- public Integer getTime_out() {
- return time_out;
- }
- /**
- * @param time_out
- * The time_out to set.
- */
- public void setTime_out(Integer time_out) {
- this.time_out = time_out;
- }
- /**
- * Constructor DynamicInvoker
- *
- * @param wsdlURL
- *
- * @throws Exception
- */
- public DynamicInvoker(String wsdlURL) throws Exception {
- // Start by reading in the WSDL using Parser
- wsdlParser = new Parser();
-
- wsdlParser.run(wsdlURL);
- }
- /**
- * Method usage
- */
- private static void usage() {
- System.err.println("Usage: java " + DynamicInvoker.class.getName()
- + " wsdlLocation " + "operationName[(portName)] "
- + "[argument1 ...]");
- System.exit(1);
- }
- /**
- * Method main
- *
- * @param args
- *
- * @throws Exception
- */
- public static String invoke(String wsdlLocation, String operationName,
- String[] args, String username, String password,
- boolean httpbasicAuth) throws Exception {
- 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,
- username, password, httpbasicAuth);
- String methodName = "";// webservice方法
- int argnum = 0;// xml含调用参数
- for (int i = 0; i < args.length; i++) {
- if (args[i] != null && args[i].indexOf("<?xml") == -1) {
- methodName = methodName + "?" + args[i];
- if (methodName.length() >= 255) {
- // 调用方法字段不能超过255长度
- methodName = methodName.substring(1,
- methodName.length() >= 255 ? 255 : methodName
- .length());
- break;
- }
- } else if (args[i] != null && args[i].indexOf("<?xml") >= 0) {
- // 如有是含xml的调用参数
- argnum = i;
- }
- }
-
-
- StringBuffer invokeResult = new StringBuffer();
- for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- String key = (String) entry.getKey();
- Object value = entry.getValue();
- invokeResult.append(value);
- }
- return invokeResult.toString();
- }
- /**
- * Method main(time_out)
- *
- * @param args
- *
- * @throws Exception
- */
- public static String invoke(String wsdlLocation, String operationName,
- String[] args, String username, String password,
- boolean httpbasicAuth, Integer time_out) throws Exception {
- 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,
- username, password, httpbasicAuth, time_out);
- String methodName = "";// webservice方法
- int argnum = 0;// xml含调用参数
- for (int i = 0; i < args.length; i++) {
- if (args[i] != null && args[i].indexOf("<?xml") == -1) {
- methodName = methodName + "?" + args[i];
- if (methodName.length() >= 255) {
- // 调用方法字段不能超过255长度
- methodName = methodName.substring(1,
- methodName.length() >= 255 ? 255 : methodName
- .length());
- break;
- }
- } else if (args[i] != null && args[i].indexOf("<?xml") >= 0) {
- // 如有是含xml的调用参数
- argnum = i;
- }
- }
-
- StringBuffer invokeResult = new StringBuffer();
- for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
- Map.Entry entry = (Map.Entry) it.next();
- String key = (String) entry.getKey();
- Object value = entry.getValue();
- invokeResult.append(value);
- }
- return invokeResult.toString();
- }
- /**
- * Method invokeMethod
- *
- * @param wsdlLocation
- * @param operationName
- * @param inputName
- * @param outputName
- * @param portName
- * @param args
- *
- * @return
- *
- * @throws Exception
- */
- public HashMap invokeMethod(String operationName, String portName,
- String[] args, String username, String password,
- boolean httpbasicAuth) throws Exception {
- String serviceNS = null;
- String serviceName = null;
- String operationQName = null;
- 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(this
- .getTime_out()));
- ((org.apache.axis.client.Call) call).setProperty(
- ElementDeserializer.DESERIALIZE_CURRENT_ELEMENT, Boolean.TRUE);
- if (username != null) {
- if (httpbasicAuth) {
- ((org.apache.axis.client.Call) call).setUsername(username);
- ((org.apache.axis.client.Call) call).setPassword(password);
- } else {
- ((org.apache.axis.client.Call) call)
- .addHeader(new SOAPHeaderElement("Authorization","username", username));
- ((org.apache.axis.client.Call) call)
- .addHeader(new SOAPHeaderElement("Authorization",
- "password", password));
- }
- ((org.apache.axis.client.Call) call).setMaintainSession(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) {
- throw new RuntimeException("Need " + inNames.size()
- + " arguments!!!");
- }
- for (int pos = 0; pos < inNames.size(); ++pos) {
- String arg = args[pos];
- Parameter p = (Parameter) inTypes.get(pos);
- inputs
- .add(getParamData((org.apache.axis.client.Call) call, p,
- arg));
- }
-
- 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;
- }
- /**
- * Method invokeMethod(time_out)
- *
- * @param wsdlLocation
- * @param operationName
- * @param inputName
- * @param outputName
- * @param portName
- * @param args
- *
- * @return
- *
- * @throws Exception
- */
- public HashMap invokeMethod(String operationName, String portName,
- String[] args, String username, String password,
- boolean httpbasicAuth, Integer time_out) throws Exception {
- if (time_out != null) {
- this.setTime_out(time_out);
- }
- String serviceNS = null;
- String serviceName = null;
- String operationQName = null;
- 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(this
- .getTime_out()));
- ((org.apache.axis.client.Call) call).setProperty(
- ElementDeserializer.DESERIALIZE_CURRENT_ELEMENT, Boolean.TRUE);
- if (username != null) {
- if (httpbasicAuth) {
- ((org.apache.axis.client.Call) call).setUsername(username);
- ((org.apache.axis.client.Call) call).setPassword(password);
- } else {
- ((org.apache.axis.client.Call) call)
- .addHeader(new SOAPHeaderElement("Authorization",
- "username", username));
- ((org.apache.axis.client.Call) call)
- .addHeader(new SOAPHeaderElement("Authorization",
- "password", password));
- }
- ((org.apache.axis.client.Call) call).setMaintainSession(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) {
- throw new RuntimeException("Need " + inNames.size()
- + " arguments!!!");
- }
- for (int pos = 0; pos < inNames.size(); ++pos) {
- String arg = args[pos];
- Parameter p = (Parameter) inTypes.get(pos);
- inputs
- .add(getParamData((org.apache.axis.client.Call) call, p,
- arg));
- }
-
- 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;
- }
- /**
- * Method getParamData
- *
- * @param c
- * @param arg
- */
- 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("not know how to convert '" + arg
- + "' into " + c);
- }
- /**
- * Method selectService
- *
- * @param def
- * @param serviceNS
- * @param serviceName
- *
- * @return
- *
- * @throws Exception
- */
- 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();
- }
- /**
- * Method getSymTabEntry
- *
- * @param qname
- * @param cls
- *
- * @return
- */
- public SymTabEntry getSymTabEntry(QName qname, Class cls) {
- 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;
- }
- /**
- * Method selectPort
- *
- * @param ports
- * @param portName
- *
- * @return
- *
- * @throws Exception
- */
- 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();
- 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;
- }
- private static void saveLog(String interfaceName, String serverAddress,
- Date beginTime, Date endTime, String username, String remoteAddr,
- String content) {
-
- }
- }
|