e214342bd35ba8aa4ead707b08bf05d718308698.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.sinosoft.lz.system.sms.dao.ws;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Vector;
  23. import javax.wsdl.Binding;
  24. import javax.wsdl.Operation;
  25. import javax.wsdl.Port;
  26. import javax.wsdl.Service;
  27. import javax.wsdl.extensions.soap.SOAPAddress;
  28. import javax.xml.namespace.QName;
  29. import javax.xml.rpc.Call;
  30. import javax.xml.rpc.encoding.Deserializer;
  31. import javax.xml.rpc.encoding.DeserializerFactory;
  32. import org.apache.axis.Constants;
  33. import org.apache.axis.encoding.ser.ElementDeserializer;
  34. import org.apache.axis.encoding.ser.ElementDeserializerFactory;
  35. import org.apache.axis.encoding.ser.ElementSerializerFactory;
  36. import org.apache.axis.encoding.ser.SimpleDeserializer;
  37. import org.apache.axis.message.SOAPHeaderElement;
  38. import org.apache.axis.wsdl.gen.Parser;
  39. import org.apache.axis.wsdl.symbolTable.BaseType;
  40. import org.apache.axis.wsdl.symbolTable.BindingEntry;
  41. import org.apache.axis.wsdl.symbolTable.Parameter;
  42. import org.apache.axis.wsdl.symbolTable.Parameters;
  43. import org.apache.axis.wsdl.symbolTable.ServiceEntry;
  44. import org.apache.axis.wsdl.symbolTable.SymTabEntry;
  45. import org.apache.axis.wsdl.symbolTable.SymbolTable;
  46. import org.apache.axis.wsdl.symbolTable.TypeEntry;
  47. /**
  48. * This sample shows how to use Axis for completely dynamic invocations as it is
  49. * completely stubless execution. It supports both doc/lit and rpc/encoded
  50. * services. But this sample does not support complex types (it could if there
  51. * was defined a to encode complex values as command line arguments).
  52. *
  53. * @author Davanum Srinivas (dims@yahoo.com)
  54. * <p/>
  55. * History: 12/24/2007 hujinjun 修改invokeMethod方法,实现通用登录.
  56. */
  57. public class DynamicInvoker {
  58. /** Field wsdlParser */
  59. private Parser wsdlParser = null;
  60. // private static final int TIME_OUT = 300 * 1000; // Time out after 5
  61. // minute
  62. private Integer time_out = 300 * 1000;
  63. /**
  64. * @return Returns the time_out.
  65. */
  66. public Integer getTime_out() {
  67. return time_out;
  68. }
  69. /**
  70. * @param time_out
  71. * The time_out to set.
  72. */
  73. public void setTime_out(Integer time_out) {
  74. this.time_out = time_out;
  75. }
  76. /**
  77. * Constructor DynamicInvoker
  78. *
  79. * @param wsdlURL
  80. *
  81. * @throws Exception
  82. */
  83. public DynamicInvoker(String wsdlURL) throws Exception {
  84. // Start by reading in the WSDL using Parser
  85. wsdlParser = new Parser();
  86. wsdlParser.run(wsdlURL);
  87. }
  88. /**
  89. * Method usage
  90. */
  91. private static void usage() {
  92. System.err.println("Usage: java " + DynamicInvoker.class.getName()
  93. + " wsdlLocation " + "operationName[(portName)] "
  94. + "[argument1 ...]");
  95. System.exit(1);
  96. }
  97. /**
  98. * Method main
  99. *
  100. * @param args
  101. *
  102. * @throws Exception
  103. */
  104. public static String invoke(String wsdlLocation, String operationName,
  105. String[] args, String username, String password,
  106. boolean httpbasicAuth) throws Exception {
  107. String portName = null;
  108. try {
  109. portName = operationName.substring(operationName.indexOf("(") + 1,
  110. operationName.indexOf(")"));
  111. operationName = operationName.substring(0, operationName
  112. .indexOf("("));
  113. } catch (Exception ignored) {
  114. }
  115. DynamicInvoker invoker = new DynamicInvoker(wsdlLocation);
  116. HashMap map = invoker.invokeMethod(operationName, portName, args,
  117. username, password, httpbasicAuth);
  118. String methodName = "";// webservice方法
  119. int argnum = 0;// xml含调用参数
  120. for (int i = 0; i < args.length; i++) {
  121. if (args[i] != null && args[i].indexOf("<?xml") == -1) {
  122. methodName = methodName + "?" + args[i];
  123. if (methodName.length() >= 255) {
  124. // 调用方法字段不能超过255长度
  125. methodName = methodName.substring(1,
  126. methodName.length() >= 255 ? 255 : methodName
  127. .length());
  128. break;
  129. }
  130. } else if (args[i] != null && args[i].indexOf("<?xml") >= 0) {
  131. // 如有是含xml的调用参数
  132. argnum = i;
  133. }
  134. }
  135. StringBuffer invokeResult = new StringBuffer();
  136. for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
  137. Map.Entry entry = (Map.Entry) it.next();
  138. String key = (String) entry.getKey();
  139. Object value = entry.getValue();
  140. invokeResult.append(value);
  141. }
  142. return invokeResult.toString();
  143. }
  144. /**
  145. * Method main(time_out)
  146. *
  147. * @param args
  148. *
  149. * @throws Exception
  150. */
  151. public static String invoke(String wsdlLocation, String operationName,
  152. String[] args, String username, String password,
  153. boolean httpbasicAuth, Integer time_out) throws Exception {
  154. String portName = null;
  155. try {
  156. portName = operationName.substring(operationName.indexOf("(") + 1,
  157. operationName.indexOf(")"));
  158. operationName = operationName.substring(0, operationName
  159. .indexOf("("));
  160. } catch (Exception ignored) {
  161. }
  162. DynamicInvoker invoker = new DynamicInvoker(wsdlLocation);
  163. HashMap map = invoker.invokeMethod(operationName, portName, args,
  164. username, password, httpbasicAuth, time_out);
  165. String methodName = "";// webservice方法
  166. int argnum = 0;// xml含调用参数
  167. for (int i = 0; i < args.length; i++) {
  168. if (args[i] != null && args[i].indexOf("<?xml") == -1) {
  169. methodName = methodName + "?" + args[i];
  170. if (methodName.length() >= 255) {
  171. // 调用方法字段不能超过255长度
  172. methodName = methodName.substring(1,
  173. methodName.length() >= 255 ? 255 : methodName
  174. .length());
  175. break;
  176. }
  177. } else if (args[i] != null && args[i].indexOf("<?xml") >= 0) {
  178. // 如有是含xml的调用参数
  179. argnum = i;
  180. }
  181. }
  182. StringBuffer invokeResult = new StringBuffer();
  183. for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
  184. Map.Entry entry = (Map.Entry) it.next();
  185. String key = (String) entry.getKey();
  186. Object value = entry.getValue();
  187. invokeResult.append(value);
  188. }
  189. return invokeResult.toString();
  190. }
  191. /**
  192. * Method invokeMethod
  193. *
  194. * @param wsdlLocation
  195. * @param operationName
  196. * @param inputName
  197. * @param outputName
  198. * @param portName
  199. * @param args
  200. *
  201. * @return
  202. *
  203. * @throws Exception
  204. */
  205. public HashMap invokeMethod(String operationName, String portName,
  206. String[] args, String username, String password,
  207. boolean httpbasicAuth) throws Exception {
  208. String serviceNS = null;
  209. String serviceName = null;
  210. String operationQName = null;
  211. Service service = selectService(serviceNS, serviceName);
  212. Operation operation = null;
  213. org.apache.axis.client.Service dpf = new org.apache.axis.client.Service(
  214. wsdlParser, service.getQName());
  215. Vector inputs = new Vector();
  216. Port port = selectPort(service.getPorts(), portName);
  217. if (portName == null) {
  218. portName = port.getName();
  219. }
  220. Binding binding = port.getBinding();
  221. Call call = dpf.createCall(QName.valueOf(portName), QName
  222. .valueOf(operationName));
  223. ((org.apache.axis.client.Call) call).setTimeout(new Integer(this
  224. .getTime_out()));
  225. ((org.apache.axis.client.Call) call).setProperty(
  226. ElementDeserializer.DESERIALIZE_CURRENT_ELEMENT, Boolean.TRUE);
  227. if (username != null) {
  228. if (httpbasicAuth) {
  229. ((org.apache.axis.client.Call) call).setUsername(username);
  230. ((org.apache.axis.client.Call) call).setPassword(password);
  231. } else {
  232. ((org.apache.axis.client.Call) call)
  233. .addHeader(new SOAPHeaderElement("Authorization","username", username));
  234. ((org.apache.axis.client.Call) call)
  235. .addHeader(new SOAPHeaderElement("Authorization",
  236. "password", password));
  237. }
  238. ((org.apache.axis.client.Call) call).setMaintainSession(true);
  239. }
  240. // Output types and names
  241. Vector outNames = new Vector();
  242. // Input types and names
  243. Vector inNames = new Vector();
  244. Vector inTypes = new Vector();
  245. SymbolTable symbolTable = wsdlParser.getSymbolTable();
  246. BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
  247. Parameters parameters = null;
  248. Iterator i = bEntry.getParameters().keySet().iterator();
  249. while (i.hasNext()) {
  250. Operation o = (Operation) i.next();
  251. if (o.getName().equals(operationName)) {
  252. operation = o;
  253. parameters = (Parameters) bEntry.getParameters().get(o);
  254. break;
  255. }
  256. }
  257. if ((operation == null) || (parameters == null)) {
  258. throw new RuntimeException(operationName + " was not found.");
  259. }
  260. // loop over paramters and set up in/out params
  261. for (int j = 0; j < parameters.list.size(); ++j) {
  262. Parameter p = (Parameter) parameters.list.get(j);
  263. if (p.getMode() == 1) { // IN
  264. inNames.add(p.getQName().getLocalPart());
  265. inTypes.add(p);
  266. } else if (p.getMode() == 2) { // OUT
  267. outNames.add(p.getQName().getLocalPart());
  268. } else if (p.getMode() == 3) { // INOUT
  269. inNames.add(p.getQName().getLocalPart());
  270. inTypes.add(p);
  271. outNames.add(p.getQName().getLocalPart());
  272. }
  273. }
  274. // set output type
  275. if (parameters.returnParam != null) {
  276. if (!parameters.returnParam.getType().isBaseType()) {
  277. ((org.apache.axis.client.Call) call).registerTypeMapping(
  278. org.w3c.dom.Element.class, parameters.returnParam
  279. .getType().getQName(),
  280. new ElementSerializerFactory(),
  281. new ElementDeserializerFactory());
  282. }
  283. // Get the QName for the return Type
  284. QName returnType = org.apache.axis.wsdl.toJava.Utils
  285. .getXSIType(parameters.returnParam);
  286. QName returnQName = parameters.returnParam.getQName();
  287. outNames.add(returnQName.getLocalPart());
  288. }
  289. if (inNames.size() != args.length) {
  290. throw new RuntimeException("Need " + inNames.size()
  291. + " arguments!!!");
  292. }
  293. for (int pos = 0; pos < inNames.size(); ++pos) {
  294. String arg = args[pos];
  295. Parameter p = (Parameter) inTypes.get(pos);
  296. inputs
  297. .add(getParamData((org.apache.axis.client.Call) call, p,
  298. arg));
  299. }
  300. Object ret = call.invoke(inputs.toArray());
  301. Map outputs = call.getOutputParams();
  302. HashMap map = new HashMap();
  303. for (int pos = 0; pos < outNames.size(); ++pos) {
  304. String name = (String) outNames.get(pos);
  305. Object value = outputs.get(name);
  306. if ((value == null) && (pos == 0)) {
  307. map.put(name, ret);
  308. } else {
  309. map.put(name, value);
  310. }
  311. }
  312. return map;
  313. }
  314. /**
  315. * Method invokeMethod(time_out)
  316. *
  317. * @param wsdlLocation
  318. * @param operationName
  319. * @param inputName
  320. * @param outputName
  321. * @param portName
  322. * @param args
  323. *
  324. * @return
  325. *
  326. * @throws Exception
  327. */
  328. public HashMap invokeMethod(String operationName, String portName,
  329. String[] args, String username, String password,
  330. boolean httpbasicAuth, Integer time_out) throws Exception {
  331. if (time_out != null) {
  332. this.setTime_out(time_out);
  333. }
  334. String serviceNS = null;
  335. String serviceName = null;
  336. String operationQName = null;
  337. Service service = selectService(serviceNS, serviceName);
  338. Operation operation = null;
  339. org.apache.axis.client.Service dpf = new org.apache.axis.client.Service(
  340. wsdlParser, service.getQName());
  341. Vector inputs = new Vector();
  342. Port port = selectPort(service.getPorts(), portName);
  343. if (portName == null) {
  344. portName = port.getName();
  345. }
  346. Binding binding = port.getBinding();
  347. Call call = dpf.createCall(QName.valueOf(portName), QName
  348. .valueOf(operationName));
  349. ((org.apache.axis.client.Call) call).setTimeout(new Integer(this
  350. .getTime_out()));
  351. ((org.apache.axis.client.Call) call).setProperty(
  352. ElementDeserializer.DESERIALIZE_CURRENT_ELEMENT, Boolean.TRUE);
  353. if (username != null) {
  354. if (httpbasicAuth) {
  355. ((org.apache.axis.client.Call) call).setUsername(username);
  356. ((org.apache.axis.client.Call) call).setPassword(password);
  357. } else {
  358. ((org.apache.axis.client.Call) call)
  359. .addHeader(new SOAPHeaderElement("Authorization",
  360. "username", username));
  361. ((org.apache.axis.client.Call) call)
  362. .addHeader(new SOAPHeaderElement("Authorization",
  363. "password", password));
  364. }
  365. ((org.apache.axis.client.Call) call).setMaintainSession(true);
  366. }
  367. // Output types and names
  368. Vector outNames = new Vector();
  369. // Input types and names
  370. Vector inNames = new Vector();
  371. Vector inTypes = new Vector();
  372. SymbolTable symbolTable = wsdlParser.getSymbolTable();
  373. BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
  374. Parameters parameters = null;
  375. Iterator i = bEntry.getParameters().keySet().iterator();
  376. while (i.hasNext()) {
  377. Operation o = (Operation) i.next();
  378. if (o.getName().equals(operationName)) {
  379. operation = o;
  380. parameters = (Parameters) bEntry.getParameters().get(o);
  381. break;
  382. }
  383. }
  384. if ((operation == null) || (parameters == null)) {
  385. throw new RuntimeException(operationName + " was not found.");
  386. }
  387. // loop over paramters and set up in/out params
  388. for (int j = 0; j < parameters.list.size(); ++j) {
  389. Parameter p = (Parameter) parameters.list.get(j);
  390. if (p.getMode() == 1) { // IN
  391. inNames.add(p.getQName().getLocalPart());
  392. inTypes.add(p);
  393. } else if (p.getMode() == 2) { // OUT
  394. outNames.add(p.getQName().getLocalPart());
  395. } else if (p.getMode() == 3) { // INOUT
  396. inNames.add(p.getQName().getLocalPart());
  397. inTypes.add(p);
  398. outNames.add(p.getQName().getLocalPart());
  399. }
  400. }
  401. // set output type
  402. if (parameters.returnParam != null) {
  403. if (!parameters.returnParam.getType().isBaseType()) {
  404. ((org.apache.axis.client.Call) call).registerTypeMapping(
  405. org.w3c.dom.Element.class, parameters.returnParam
  406. .getType().getQName(),
  407. new ElementSerializerFactory(),
  408. new ElementDeserializerFactory());
  409. }
  410. // Get the QName for the return Type
  411. QName returnType = org.apache.axis.wsdl.toJava.Utils
  412. .getXSIType(parameters.returnParam);
  413. QName returnQName = parameters.returnParam.getQName();
  414. outNames.add(returnQName.getLocalPart());
  415. }
  416. if (inNames.size() != args.length) {
  417. throw new RuntimeException("Need " + inNames.size()
  418. + " arguments!!!");
  419. }
  420. for (int pos = 0; pos < inNames.size(); ++pos) {
  421. String arg = args[pos];
  422. Parameter p = (Parameter) inTypes.get(pos);
  423. inputs
  424. .add(getParamData((org.apache.axis.client.Call) call, p,
  425. arg));
  426. }
  427. Object ret = call.invoke(inputs.toArray());
  428. Map outputs = call.getOutputParams();
  429. HashMap map = new HashMap();
  430. for (int pos = 0; pos < outNames.size(); ++pos) {
  431. String name = (String) outNames.get(pos);
  432. Object value = outputs.get(name);
  433. if ((value == null) && (pos == 0)) {
  434. map.put(name, ret);
  435. } else {
  436. map.put(name, value);
  437. }
  438. }
  439. return map;
  440. }
  441. /**
  442. * Method getParamData
  443. *
  444. * @param c
  445. * @param arg
  446. */
  447. private Object getParamData(org.apache.axis.client.Call c, Parameter p,
  448. String arg) throws Exception {
  449. // Get the QName representing the parameter type
  450. QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
  451. TypeEntry type = p.getType();
  452. if (type instanceof BaseType && ((BaseType) type).isBaseType()) {
  453. DeserializerFactory factory = c.getTypeMapping().getDeserializer(
  454. paramType);
  455. Deserializer deserializer = factory
  456. .getDeserializerAs(Constants.AXIS_SAX);
  457. if (deserializer instanceof SimpleDeserializer) {
  458. return ((SimpleDeserializer) deserializer).makeValue(arg);
  459. }
  460. }
  461. throw new RuntimeException("not know how to convert '" + arg
  462. + "' into " + c);
  463. }
  464. /**
  465. * Method selectService
  466. *
  467. * @param def
  468. * @param serviceNS
  469. * @param serviceName
  470. *
  471. * @return
  472. *
  473. * @throws Exception
  474. */
  475. public Service selectService(String serviceNS, String serviceName)
  476. throws Exception {
  477. QName serviceQName = (((serviceNS != null) && (serviceName != null)) ? new QName(
  478. serviceNS, serviceName)
  479. : null);
  480. ServiceEntry serviceEntry = (ServiceEntry) getSymTabEntry(serviceQName,
  481. ServiceEntry.class);
  482. return serviceEntry.getService();
  483. }
  484. /**
  485. * Method getSymTabEntry
  486. *
  487. * @param qname
  488. * @param cls
  489. *
  490. * @return
  491. */
  492. public SymTabEntry getSymTabEntry(QName qname, Class cls) {
  493. HashMap map = wsdlParser.getSymbolTable().getHashMap();
  494. Iterator iterator = map.entrySet().iterator();
  495. while (iterator.hasNext()) {
  496. Map.Entry entry = (Map.Entry) iterator.next();
  497. QName key = (QName) entry.getKey();
  498. Vector v = (Vector) entry.getValue();
  499. if ((qname == null) || qname.equals(qname)) {
  500. for (int i = 0; i < v.size(); ++i) {
  501. SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
  502. if (cls.isInstance(symTabEntry)) {
  503. return symTabEntry;
  504. }
  505. }
  506. }
  507. }
  508. return null;
  509. }
  510. /**
  511. * Method selectPort
  512. *
  513. * @param ports
  514. * @param portName
  515. *
  516. * @return
  517. *
  518. * @throws Exception
  519. */
  520. public Port selectPort(Map ports, String portName) throws Exception {
  521. Iterator valueIterator = ports.keySet().iterator();
  522. while (valueIterator.hasNext()) {
  523. String name = (String) valueIterator.next();
  524. if ((portName == null) || (portName.length() == 0)) {
  525. Port port = (Port) ports.get(name);
  526. List list = port.getExtensibilityElements();
  527. for (int i = 0; (list != null) && (i < list.size()); i++) {
  528. Object obj = list.get(i);
  529. if (obj instanceof SOAPAddress) {
  530. return port;
  531. }
  532. }
  533. } else if ((name != null) && name.equals(portName)) {
  534. return (Port) ports.get(name);
  535. }
  536. }
  537. return null;
  538. }
  539. private static void saveLog(String interfaceName, String serverAddress,
  540. Date beginTime, Date endTime, String username, String remoteAddr,
  541. String content) {
  542. }
  543. }