KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > toolagent > SOAPToolAgent


1 package org.enhydra.shark.toolagent;
2
3
4 import org.enhydra.shark.api.*;
5 import org.enhydra.shark.api.internal.toolagent.*;
6 import org.enhydra.shark.xpdl.XPDLConstants;
7
8 import org.apache.axis.Constants;
9 import org.apache.axis.encoding.ser.SimpleDeserializer;
10 import org.apache.axis.wsdl.gen.Parser;
11 import org.apache.axis.wsdl.symbolTable.BaseType;
12 import org.apache.axis.wsdl.symbolTable.BindingEntry;
13 import org.apache.axis.wsdl.symbolTable.Parameter;
14 import org.apache.axis.wsdl.symbolTable.Parameters;
15 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
16 import org.apache.axis.wsdl.symbolTable.SymTabEntry;
17 import org.apache.axis.wsdl.symbolTable.SymbolTable;
18 import org.apache.axis.wsdl.symbolTable.TypeEntry;
19
20 import javax.wsdl.Binding;
21 import javax.wsdl.Operation;
22 import javax.wsdl.Port;
23 import javax.wsdl.Service;
24 import javax.wsdl.extensions.soap.SOAPAddress;
25 import javax.xml.namespace.QName JavaDoc;
26 import javax.xml.rpc.Call JavaDoc;
27 import javax.xml.rpc.encoding.Deserializer JavaDoc;
28 import javax.xml.rpc.encoding.DeserializerFactory JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * Tool agent that executes WEB Service using SOAP.
39  * When calling invoke method, the wsdl document location is given as
40  * application name, the first parameter that is passed has to represent
41  * the WEB service method to be called.
42  */

43 public class SOAPToolAgent extends AbstractToolAgent {
44
45    public void invokeApplication (SharkTransaction t,
46                                   long handle,
47                                   String JavaDoc applicationName,
48                                   String JavaDoc procInstId,
49                                   String JavaDoc assId,
50                                   AppParameter[] parameters,
51                                   Integer JavaDoc appMode)
52       throws ApplicationNotStarted, ApplicationNotDefined,
53       ApplicationBusy, ToolAgentGeneralException {
54
55       super.invokeApplication(t,handle,applicationName,procInstId,assId,parameters,appMode);
56
57       try {
58          status=APP_STATUS_RUNNING;
59
60          if (appName==null || appName.trim().length()==0) {
61             readParamsFromExtAttributes((String JavaDoc)parameters[0].the_value);
62          }
63
64          AppParameter[] aps=new AppParameter[parameters.length-1];
65          System.arraycopy(parameters,1,aps,0,aps.length);
66
67
68          String JavaDoc operationName = (String JavaDoc)aps[0].the_value;
69          String JavaDoc portName = null;
70          try {
71             portName = operationName.substring(operationName.indexOf("(") + 1,
72                                                operationName.indexOf(")"));
73             operationName = operationName.substring(0,operationName.indexOf("("));
74          } catch (Exception JavaDoc ignored) {}
75
76          Parser wsdlParser = null;
77          wsdlParser = new Parser();
78          cus.info("SOAPToolAgent -> Reading WSDL document from '" + appName + "'");
79          wsdlParser.run(appName);
80
81
82          HashMap JavaDoc map = invokeMethod(wsdlParser,operationName,portName,aps);
83
84          // print result
85
cus.info("SOAPToolAgent -> Result:");
86          for (Iterator JavaDoc it = map.keySet().iterator(); it.hasNext();) {
87             String JavaDoc name = (String JavaDoc) it.next();
88             cus.info(" " + name + "=" + map.get(name));
89          }
90
91          prepareResult(parameters,map);
92          cus.info("\nSOAPToolAgent -> Done!");
93
94
95          status=APP_STATUS_FINISHED;
96
97       } catch (Throwable JavaDoc ex) {
98          cus.error("SOAPToolAgent -> application "+appName+" terminated incorrectly: "+ex);
99          status=APP_STATUS_INVALID;
100          throw new ToolAgentGeneralException(ex);
101       }
102    }
103
104    public String JavaDoc getInfo (SharkTransaction t) throws ToolAgentGeneralException {
105       String JavaDoc i="Executes WEB service operation."+
106          "\nWhen you map XPDL application to this tool agent, you should set application "+
107          "\nname to the location of the WSDL file that defines WEB service to be called." +
108          "\nAlso, this tool agent requires that the first parameter defined in XPDL Application's "+
109          "\nformal parameters represent the name of WEB service operation to be called."+
110          "\n"+
111          "\nThis tool agent is able to understand the extended attribute with the following name:"+
112          "\n * AppName - value of this attribute should represent the location of WSDL file where WEB service is defined"+
113          "\n"+
114          "\n NOTE: Tool agent will read extended attributes only if they are called through"+
115          "\n Default tool agent (not by shark directly) and this is the case when information "+
116          "\n on which tool agent to start for XPDL application definition is not contained in mappings";
117       return i;
118    }
119
120    /**
121     */

122    public HashMap JavaDoc invokeMethod(Parser wsdlParser,String JavaDoc operationName,
123                                String JavaDoc portName,AppParameter[] args) throws Exception JavaDoc {
124       String JavaDoc serviceNS = null;
125       String JavaDoc serviceName = null;
126       String JavaDoc operationQName = null;
127
128       cus.info("SOAPToolAgent -> Preparing Axis dynamic invocation");
129       Service service = getService(wsdlParser,serviceNS,serviceName);
130       Operation operation = null;
131       org.apache.axis.client.Service dpf =
132          new org.apache.axis.client.Service(wsdlParser,service.getQName());
133
134       List JavaDoc inputs = new ArrayList JavaDoc();
135       Port port = getPort(service.getPorts(),portName);
136       if (portName == null) {
137          portName = port.getName();
138       }
139       Binding binding = port.getBinding();
140       Call JavaDoc call = dpf.createCall(QName.valueOf(portName),QName.valueOf(operationName));
141       //((org.apache.axis.client.Call)call).setTimeout(new Integer(15*1000));
142
// Output types and names
143
List JavaDoc outNames = new ArrayList JavaDoc();
144
145       // Input types and names
146
List JavaDoc inNames = new ArrayList JavaDoc();
147       List JavaDoc inTypes = new ArrayList JavaDoc();
148       SymbolTable symbolTable = wsdlParser.getSymbolTable();
149       BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
150       Parameters parameters = null;
151       Iterator JavaDoc i = bEntry.getParameters().keySet().iterator();
152
153       while (i.hasNext()) {
154          Operation o = (Operation) i.next();
155          if (o.getName().equals(operationName)) {
156             operation = o;
157             parameters = (Parameters) bEntry.getParameters().get(o);
158             break;
159          }
160       }
161       if ((operation == null) || (parameters == null)) {
162          throw new RuntimeException JavaDoc(operationName + " was not found.");
163       }
164
165       // loop over paramters and set up in/out params
166
for (int j=0; j<parameters.list.size(); ++j) {
167          Parameter p = (Parameter) parameters.list.get(j);
168
169          if (p.getMode() == 1) { // IN
170
inNames.add(p.getQName().getLocalPart());
171             inTypes.add(p);
172          } else if (p.getMode() == 2) { // OUT
173
outNames.add(p.getQName().getLocalPart());
174          } else if (p.getMode() == 3) { // INOUT
175
inNames.add(p.getQName().getLocalPart());
176             inTypes.add(p);
177             outNames.add(p.getQName().getLocalPart());
178          }
179       }
180
181       // set output type
182
if (parameters.returnParam != null) {
183          // Get the QName for the return Type
184
QName JavaDoc returnType = org.apache.axis.wsdl.toJava.Utils.getXSIType(
185             parameters.returnParam);
186          QName JavaDoc returnQName = parameters.returnParam.getQName();
187
188          outNames.add(returnQName.getLocalPart());
189       }
190
191       if (inNames.size() > args.length - 1)
192          throw new RuntimeException JavaDoc("Need " + inNames.size() + " arguments!!!");
193
194       for (int pos=0; pos<inNames.size(); ++pos) {
195          AppParameter arg = args[pos+1];
196          Parameter p = (Parameter)inTypes.get(pos);
197          inputs.add(getParamData((org.apache.axis.client.Call)call,p,arg));
198       }
199       cus.info("SOAPToolAgent -> Executing operation " + operationName + " with parameters:");
200       for (int j=0; j<inputs.size(); j++) {
201          cus.info(" "+ inNames.get(j) + "=" + inputs.get(j));
202       }
203       java.lang.Object JavaDoc ret = call.invoke(inputs.toArray());
204       Map JavaDoc outputs = call.getOutputParams();
205       HashMap JavaDoc map = new HashMap JavaDoc();
206
207       for (int pos=0; pos<outNames.size(); ++pos) {
208          String JavaDoc name = (String JavaDoc) outNames.get(pos);
209          java.lang.Object JavaDoc value = outputs.get(name);
210
211          if ((value == null) && (pos == 0)) {
212             map.put(name, ret);
213          } else {
214             map.put(name, value);
215          }
216       }
217       return map;
218    }
219
220    private void prepareResult (AppParameter[] context, HashMap JavaDoc hashret) throws Exception JavaDoc {
221       int n = 0;
222       Set JavaDoc set = hashret.entrySet();
223       Iterator JavaDoc it = set.iterator();
224
225       java.lang.Object JavaDoc[] val = new java.lang.Object JavaDoc[hashret.size()];
226       while (it.hasNext()) {
227          Map.Entry JavaDoc mapentry = (Map.Entry JavaDoc)it.next();
228          val[n] = (java.lang.Object JavaDoc)mapentry.getValue();
229          n++;
230       }
231
232       if (context!=null) {
233          int en = 0;
234          for (int i = 0; i < context.length; i++) {
235             if(context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_OUT) || context[i].the_mode.equals(XPDLConstants.FORMAL_PARAMETER_MODE_INOUT)) {
236                context[i].the_value=convertToProperType(val[en],context[i].the_class);
237                en++;
238             }
239          }
240       }
241    }
242
243    private Object JavaDoc convertToProperType (Object JavaDoc toConvert,Class JavaDoc desiredType) throws Exception JavaDoc {
244       if (desiredType.isInstance(toConvert)) return toConvert;
245
246       if (desiredType.equals(Long JavaDoc.class)) {
247          return new Long JavaDoc((new Double JavaDoc(toConvert.toString())).longValue());
248       } else if (desiredType.equals(Boolean JavaDoc.class)) {
249          return new Boolean JavaDoc(toConvert.toString());
250       } else if (desiredType.equals(Double JavaDoc.class)) {
251          return new Double JavaDoc(toConvert.toString());
252       } else if (desiredType.equals(java.util.Date JavaDoc.class)) {
253          return new java.util.Date JavaDoc(toConvert.toString());
254       }
255       return toConvert;
256    }
257
258    /**
259     */

260    private static java.lang.Object JavaDoc getParamData(org.apache.axis.client.Call c,
261                                                 Parameter p,
262                                                 AppParameter ap) throws Exception JavaDoc {
263       // Get the QName representing the parameter type
264
QName JavaDoc paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p);
265       String JavaDoc arg=null;
266
267       TypeEntry type = p.getType();
268       if (type instanceof BaseType && ((BaseType) type).isBaseType()) {
269          DeserializerFactory JavaDoc factory = c.getTypeMapping().getDeserializer(paramType);
270          Deserializer deserializer = factory.getDeserializerAs(Constants.AXIS_SAX);
271          if (deserializer instanceof SimpleDeserializer) {
272             java.lang.Object JavaDoc obj=ap.the_value;
273             arg=String.valueOf(obj);
274             return ((SimpleDeserializer)deserializer).makeValue(arg);
275          }
276       }
277       throw new RuntimeException JavaDoc("don't know how to convert '" + arg + "' into " + c);
278    }
279
280    /**
281     */

282    public static Service getService (Parser wsdlParser,String JavaDoc serviceNS,String JavaDoc serviceName) throws Exception JavaDoc {
283       QName JavaDoc serviceQName = null;
284       if (serviceNS != null && serviceName != null) {
285          serviceQName=new QName JavaDoc(serviceNS, serviceName);
286       }
287       ServiceEntry serviceEntry = (ServiceEntry)
288          getSymTabEntry(wsdlParser,serviceQName,ServiceEntry.class);
289       return serviceEntry.getService();
290    }
291
292    /**
293     */

294    public static Port getPort (Map JavaDoc ports,String JavaDoc portName) throws Exception JavaDoc {
295       Iterator JavaDoc valueIterator = ports.keySet().iterator();
296       while (valueIterator.hasNext()) {
297          String JavaDoc name = (String JavaDoc) valueIterator.next();
298          if ((portName == null) || (portName.length() == 0)) {
299             Port port = (Port) ports.get(name);
300             List JavaDoc list = port.getExtensibilityElements();
301
302             for (int i = 0; (list != null) && (i < list.size()); i++) {
303                java.lang.Object JavaDoc obj = list.get(i);
304                if (obj instanceof SOAPAddress) {
305                   return port;
306                }
307             }
308          } else if ((name != null) && name.equals(portName)) {
309             return (Port) ports.get(name);
310          }
311       }
312       return null;
313    }
314
315    /**
316     */

317    public static SymTabEntry getSymTabEntry(Parser wsdlParser,QName JavaDoc qname,Class JavaDoc cls) {
318       HashMap JavaDoc map = wsdlParser.getSymbolTable().getHashMap();
319       Iterator JavaDoc iterator = map.entrySet().iterator();
320
321       while (iterator.hasNext()) {
322          Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
323          QName JavaDoc key = (QName JavaDoc) entry.getKey();
324          Vector JavaDoc v = (Vector JavaDoc) entry.getValue();
325
326          if ((qname == null) || qname.equals(qname)) {
327             for (int i = 0; i < v.size(); ++i) {
328                SymTabEntry symTabEntry = (SymTabEntry) v.elementAt(i);
329
330                if (cls.isInstance(symTabEntry)) {
331                   return symTabEntry;
332                }
333             }
334          }
335       }
336       return null;
337    }
338
339
340    public static void main (String JavaDoc[] args) {
341       SOAPToolAgent soapTA=new SOAPToolAgent();
342       test1(soapTA);
343       test2(soapTA);
344       test3(soapTA);
345       test4(soapTA);
346    }
347
348    public static void test1 (SOAPToolAgent soapTA) {
349       try {
350
351          SessionHandle handle=soapTA.connect(null,"","","","");
352
353          String JavaDoc appName="http://www.ebi.ac.uk/xembl/XEMBL.wsdl";
354          AppParameter[] aps=new AppParameter[5];
355
356          AppParameter ap=new AppParameter();
357          ap.the_value="";
358          ap.the_mode="IN";
359          ap.the_class=String JavaDoc.class;
360          aps[0]=ap;
361
362          ap=new AppParameter();
363          ap.the_value="getNucSeq";
364          ap.the_mode="IN";
365          ap.the_class=String JavaDoc.class;
366          aps[1]=ap;
367
368          ap=new AppParameter();
369          ap.the_value="Bsml";
370          ap.the_actual_name="nuc_format";
371          ap.the_formal_name="format";
372          ap.the_mode="IN";
373          ap.the_class=String JavaDoc.class;
374          aps[2]=ap;
375
376          ap=new AppParameter();
377          ap.the_value="HSERPG U83300 AC000057";
378          ap.the_actual_name="nuc_ids";
379          ap.the_formal_name="ids";
380          ap.the_mode="IN";
381          ap.the_class=String JavaDoc.class;
382          aps[3]=ap;
383
384          ap=new AppParameter();
385          ap.the_value=null;
386          ap.the_actual_name="nuc_formula";
387          ap.the_formal_name="formula";
388          ap.the_mode="OUT";
389          ap.the_class=String JavaDoc.class;
390          aps[4]=ap;
391
392          soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer JavaDoc(0));
393       } catch (Exception JavaDoc ex) {
394          ex.printStackTrace();
395       }
396    }
397
398
399    public static void test2 (SOAPToolAgent soapTA) {
400       try {
401          SessionHandle handle=soapTA.connect(null,"","","","");
402
403          String JavaDoc appName="http://samples.gotdotnet.com/quickstart/aspplus/samples/services/MathService/VB/MathService.asmx?WSDL";
404          AppParameter[] aps=new AppParameter[5];
405
406          AppParameter ap=new AppParameter();
407          ap.the_value="";
408          ap.the_mode="IN";
409          ap.the_class=String JavaDoc.class;
410          aps[0]=ap;
411
412          ap=new AppParameter();
413          ap.the_value="Add";
414          ap.the_mode="IN";
415          ap.the_class=String JavaDoc.class;
416          aps[1]=ap;
417
418          ap=new AppParameter();
419          ap.the_value=new Double JavaDoc(3);
420          ap.the_actual_name="par1";
421          ap.the_formal_name="p1";
422          ap.the_mode="IN";
423          ap.the_class=Double JavaDoc.class;
424          aps[2]=ap;
425
426          ap=new AppParameter();
427          ap.the_value=new Double JavaDoc(44);
428          ap.the_actual_name="par2";
429          ap.the_formal_name="p2";
430          ap.the_mode="IN";
431          ap.the_class=Double JavaDoc.class;
432          aps[3]=ap;
433
434          ap=new AppParameter();
435          ap.the_value=null;
436          ap.the_actual_name="res";
437          ap.the_formal_name="r";
438          ap.the_mode="OUT";
439          ap.the_class=Double JavaDoc.class;
440          aps[4]=ap;
441
442          soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer JavaDoc(0));
443       } catch (Exception JavaDoc ex) {
444          ex.printStackTrace();
445       }
446    }
447
448    public static void test3 (SOAPToolAgent soapTA) {
449       try {
450          SessionHandle handle=soapTA.connect(null,"","","","");
451
452          String JavaDoc appName="http://www.xmethods.net/sd/2001/TemperatureService.wsdl";
453          AppParameter[] aps=new AppParameter[4];
454
455          AppParameter ap=new AppParameter();
456          ap.the_value="";
457          ap.the_mode="IN";
458          ap.the_class=String JavaDoc.class;
459          aps[0]=ap;
460
461          ap=new AppParameter();
462          ap.the_value="getTemp";
463          ap.the_mode="IN";
464          ap.the_class=String JavaDoc.class;
465          aps[1]=ap;
466
467          ap=new AppParameter();
468          ap.the_value="02067";
469          ap.the_actual_name="par1";
470          ap.the_formal_name="p1";
471          ap.the_mode="IN";
472          ap.the_class=String JavaDoc.class;
473          aps[2]=ap;
474
475          ap=new AppParameter();
476          ap.the_value=null;
477          ap.the_actual_name="res";
478          ap.the_formal_name="r";
479          ap.the_mode="OUT";
480          ap.the_class=Double JavaDoc.class;
481          aps[3]=ap;
482
483          soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer JavaDoc(0));
484       } catch (Exception JavaDoc ex) {
485          ex.printStackTrace();
486       }
487    }
488
489    public static void test4 (SOAPToolAgent soapTA) {
490       try {
491          SessionHandle handle=soapTA.connect(null,"","","","");
492
493          String JavaDoc appName="http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl";
494          AppParameter[] aps=new AppParameter[4];
495
496          AppParameter ap=new AppParameter();
497          ap.the_value="";
498          ap.the_mode="IN";
499          ap.the_class=String JavaDoc.class;
500          aps[0]=ap;
501
502          ap=new AppParameter();
503          ap.the_value="getQuote";
504          ap.the_mode="IN";
505          ap.the_class=String JavaDoc.class;
506          aps[1]=ap;
507
508          ap=new AppParameter();
509          ap.the_value="IBM";
510          ap.the_actual_name="par1";
511          ap.the_formal_name="p1";
512          ap.the_mode="IN";
513          ap.the_class=String JavaDoc.class;
514          aps[2]=ap;
515
516          ap=new AppParameter();
517          ap.the_value=null;
518          ap.the_actual_name="res";
519          ap.the_formal_name="r";
520          ap.the_mode="OUT";
521          ap.the_class=Double JavaDoc.class;
522          aps[3]=ap;
523
524          soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer JavaDoc(0));
525       } catch (Exception JavaDoc ex) {
526          ex.printStackTrace();
527       }
528    }
529
530    //java samples.client.DynamicInvoker http://mssoapinterop.org/asmx/xsd/round4XSD.wsdl echoString "Hello World!!!"
531
}
532
533
Popular Tags