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 ; 26 import javax.xml.rpc.Call ; 27 import javax.xml.rpc.encoding.Deserializer ; 28 import javax.xml.rpc.encoding.DeserializerFactory ; 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Vector ; 35 import java.util.Set ; 36 37 43 public class SOAPToolAgent extends AbstractToolAgent { 44 45 public void invokeApplication (SharkTransaction t, 46 long handle, 47 String applicationName, 48 String procInstId, 49 String assId, 50 AppParameter[] parameters, 51 Integer 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 )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 operationName = (String )aps[0].the_value; 69 String portName = null; 70 try { 71 portName = operationName.substring(operationName.indexOf("(") + 1, 72 operationName.indexOf(")")); 73 operationName = operationName.substring(0,operationName.indexOf("(")); 74 } catch (Exception 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 map = invokeMethod(wsdlParser,operationName,portName,aps); 83 84 cus.info("SOAPToolAgent -> Result:"); 86 for (Iterator it = map.keySet().iterator(); it.hasNext();) { 87 String name = (String ) 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 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 getInfo (SharkTransaction t) throws ToolAgentGeneralException { 105 String 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 122 public HashMap invokeMethod(Parser wsdlParser,String operationName, 123 String portName,AppParameter[] args) throws Exception { 124 String serviceNS = null; 125 String serviceName = null; 126 String 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 inputs = new ArrayList (); 135 Port port = getPort(service.getPorts(),portName); 136 if (portName == null) { 137 portName = port.getName(); 138 } 139 Binding binding = port.getBinding(); 140 Call call = dpf.createCall(QName.valueOf(portName),QName.valueOf(operationName)); 141 List outNames = new ArrayList (); 144 145 List inNames = new ArrayList (); 147 List inTypes = new ArrayList (); 148 SymbolTable symbolTable = wsdlParser.getSymbolTable(); 149 BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName()); 150 Parameters parameters = null; 151 Iterator 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 (operationName + " was not found."); 163 } 164 165 for (int j=0; j<parameters.list.size(); ++j) { 167 Parameter p = (Parameter) parameters.list.get(j); 168 169 if (p.getMode() == 1) { inNames.add(p.getQName().getLocalPart()); 171 inTypes.add(p); 172 } else if (p.getMode() == 2) { outNames.add(p.getQName().getLocalPart()); 174 } else if (p.getMode() == 3) { inNames.add(p.getQName().getLocalPart()); 176 inTypes.add(p); 177 outNames.add(p.getQName().getLocalPart()); 178 } 179 } 180 181 if (parameters.returnParam != null) { 183 QName returnType = org.apache.axis.wsdl.toJava.Utils.getXSIType( 185 parameters.returnParam); 186 QName returnQName = parameters.returnParam.getQName(); 187 188 outNames.add(returnQName.getLocalPart()); 189 } 190 191 if (inNames.size() > args.length - 1) 192 throw new RuntimeException ("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 ret = call.invoke(inputs.toArray()); 204 Map outputs = call.getOutputParams(); 205 HashMap map = new HashMap (); 206 207 for (int pos=0; pos<outNames.size(); ++pos) { 208 String name = (String ) outNames.get(pos); 209 java.lang.Object 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 hashret) throws Exception { 221 int n = 0; 222 Set set = hashret.entrySet(); 223 Iterator it = set.iterator(); 224 225 java.lang.Object [] val = new java.lang.Object [hashret.size()]; 226 while (it.hasNext()) { 227 Map.Entry mapentry = (Map.Entry )it.next(); 228 val[n] = (java.lang.Object )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 convertToProperType (Object toConvert,Class desiredType) throws Exception { 244 if (desiredType.isInstance(toConvert)) return toConvert; 245 246 if (desiredType.equals(Long .class)) { 247 return new Long ((new Double (toConvert.toString())).longValue()); 248 } else if (desiredType.equals(Boolean .class)) { 249 return new Boolean (toConvert.toString()); 250 } else if (desiredType.equals(Double .class)) { 251 return new Double (toConvert.toString()); 252 } else if (desiredType.equals(java.util.Date .class)) { 253 return new java.util.Date (toConvert.toString()); 254 } 255 return toConvert; 256 } 257 258 260 private static java.lang.Object getParamData(org.apache.axis.client.Call c, 261 Parameter p, 262 AppParameter ap) throws Exception { 263 QName paramType = org.apache.axis.wsdl.toJava.Utils.getXSIType(p); 265 String arg=null; 266 267 TypeEntry type = p.getType(); 268 if (type instanceof BaseType && ((BaseType) type).isBaseType()) { 269 DeserializerFactory factory = c.getTypeMapping().getDeserializer(paramType); 270 Deserializer deserializer = factory.getDeserializerAs(Constants.AXIS_SAX); 271 if (deserializer instanceof SimpleDeserializer) { 272 java.lang.Object obj=ap.the_value; 273 arg=String.valueOf(obj); 274 return ((SimpleDeserializer)deserializer).makeValue(arg); 275 } 276 } 277 throw new RuntimeException ("don't know how to convert '" + arg + "' into " + c); 278 } 279 280 282 public static Service getService (Parser wsdlParser,String serviceNS,String serviceName) throws Exception { 283 QName serviceQName = null; 284 if (serviceNS != null && serviceName != null) { 285 serviceQName=new QName (serviceNS, serviceName); 286 } 287 ServiceEntry serviceEntry = (ServiceEntry) 288 getSymTabEntry(wsdlParser,serviceQName,ServiceEntry.class); 289 return serviceEntry.getService(); 290 } 291 292 294 public static Port getPort (Map ports,String portName) throws Exception { 295 Iterator valueIterator = ports.keySet().iterator(); 296 while (valueIterator.hasNext()) { 297 String name = (String ) valueIterator.next(); 298 if ((portName == null) || (portName.length() == 0)) { 299 Port port = (Port) ports.get(name); 300 List list = port.getExtensibilityElements(); 301 302 for (int i = 0; (list != null) && (i < list.size()); i++) { 303 java.lang.Object 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 317 public static SymTabEntry getSymTabEntry(Parser wsdlParser,QName qname,Class cls) { 318 HashMap map = wsdlParser.getSymbolTable().getHashMap(); 319 Iterator iterator = map.entrySet().iterator(); 320 321 while (iterator.hasNext()) { 322 Map.Entry entry = (Map.Entry ) iterator.next(); 323 QName key = (QName ) entry.getKey(); 324 Vector v = (Vector ) 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 [] 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 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 .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 .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 .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 .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 .class; 390 aps[4]=ap; 391 392 soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer (0)); 393 } catch (Exception 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 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 .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 .class; 416 aps[1]=ap; 417 418 ap=new AppParameter(); 419 ap.the_value=new Double (3); 420 ap.the_actual_name="par1"; 421 ap.the_formal_name="p1"; 422 ap.the_mode="IN"; 423 ap.the_class=Double .class; 424 aps[2]=ap; 425 426 ap=new AppParameter(); 427 ap.the_value=new Double (44); 428 ap.the_actual_name="par2"; 429 ap.the_formal_name="p2"; 430 ap.the_mode="IN"; 431 ap.the_class=Double .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 .class; 440 aps[4]=ap; 441 442 soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer (0)); 443 } catch (Exception 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 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 .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 .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 .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 .class; 481 aps[3]=ap; 482 483 soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer (0)); 484 } catch (Exception 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 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 .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 .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 .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 .class; 522 aps[3]=ap; 523 524 soapTA.invokeApplication(null,handle.getHandle(),appName,"1","1",aps,new Integer (0)); 525 } catch (Exception ex) { 526 ex.printStackTrace(); 527 } 528 } 529 530 } 532 533 | Popular Tags |