1 57 58 package org.apache.wsif.base; 59 60 import java.lang.reflect.InvocationHandler ; 61 import java.lang.reflect.Method ; 62 import java.lang.reflect.Proxy ; 63 import java.util.ArrayList ; 64 import java.util.HashMap ; 65 import java.util.Iterator ; 66 import java.util.List ; 67 import java.util.Map ; 68 69 import javax.activation.DataHandler ; 70 import javax.wsdl.Definition; 71 import javax.wsdl.Input; 72 import javax.wsdl.Message; 73 import javax.wsdl.Operation; 74 import javax.wsdl.Output; 75 import javax.wsdl.Part; 76 import javax.wsdl.PortType; 77 import javax.xml.namespace.QName ; 78 79 import org.apache.wsif.WSIFConstants; 80 import org.apache.wsif.WSIFException; 81 import org.apache.wsif.WSIFMessage; 82 import org.apache.wsif.WSIFOperation; 83 import org.apache.wsif.WSIFPort; 84 import org.apache.wsif.compiler.util.TypeMapping; 85 import org.apache.wsif.logging.Trc; 86 import org.apache.wsif.providers.WSIFDynamicTypeMap; 87 import org.apache.wsif.providers.WSIFDynamicTypeMapping; 88 import org.apache.wsif.util.WSIFUtils; 89 90 106 public class WSIFClientProxy implements InvocationHandler { 107 protected Class iface = null; 108 protected Definition def = null; 109 protected String serviceNS = null; 110 protected String serviceName = null; 111 protected String portTypeNS = null; 112 protected String portTypeName = null; 113 protected WSIFDynamicTypeMap typeMap = null; 114 protected Map simpleTypeReg = null; 115 protected PortType portType = null; 116 protected WSIFPort wsifport = null; 117 protected Object proxy = null; 118 private Map wsdlOperationTable = null; 119 120 132 public static WSIFClientProxy newInstance( 133 Class iface, 134 Definition def, 135 String serviceNS, 136 String serviceName, 137 String portTypeNS, 138 String portTypeName, 139 WSIFDynamicTypeMap typeMap) 140 throws WSIFException { 141 Trc.entry( 142 null, 143 iface, 144 def, 145 serviceNS, 146 serviceName, 147 portTypeNS, 148 portTypeName, 149 typeMap); 150 151 if (!iface.isInterface()) 152 throw new WSIFException( 153 "Cannot get a stub for " + iface + " because it is not an interface"); 154 155 WSIFClientProxy clientProxy = 156 new WSIFClientProxy( 157 iface, 158 def, 159 serviceNS, 160 serviceName, 161 portTypeNS, 162 portTypeName, 163 typeMap); 164 165 Object proxy = 166 Proxy.newProxyInstance( 167 iface.getClassLoader(), 168 new Class [] { iface }, 169 clientProxy); 170 171 clientProxy.setProxy(proxy); 172 173 if (Trc.ON) 174 Trc.exit(clientProxy.deep()); 175 return clientProxy; 176 } 177 178 181 private WSIFClientProxy( 182 Class iface, 183 Definition def, 184 String serviceNS, 185 String serviceName, 186 String portTypeNS, 187 String portTypeName, 188 WSIFDynamicTypeMap typeMap) 189 throws WSIFException { 190 Trc.entry( 191 this, 192 iface, 193 def, 194 serviceNS, 195 serviceName, 196 portTypeNS, 197 portTypeName, 198 typeMap); 199 this.iface = iface; 200 this.def = def; 201 this.serviceNS = serviceNS; 202 this.serviceName = serviceName; 203 this.portTypeNS = portTypeNS; 204 this.portTypeName = portTypeName; 205 this.typeMap = typeMap; 206 this.portType = WSIFUtils.selectPortType(def, portTypeNS, portTypeName); 207 208 simpleTypeReg = WSIFUtils.getSimpleTypesMap(); 209 wsdlOperationTable = new HashMap (); 210 Trc.exit(); 211 } 212 213 private void setProxy(Object proxy) { 214 this.proxy = proxy; 215 } 216 217 public Object getProxy() { 218 return this.proxy; 219 } 220 221 224 public void setPort(WSIFPort wsifport) { 225 Trc.entry(this, wsifport); 226 this.wsifport = wsifport; 227 Trc.exit(); 228 } 229 230 239 public Object invoke(Object proxy, Method method, Object [] args) 240 throws WSIFException { 241 Trc.entry(this, method, args); 243 Operation operation = findMatchingOperation(method, args); 244 245 Input input = operation.getInput(); 247 Output output = operation.getOutput(); 248 String inputName = (input == null) ? null : input.getName(); 249 String outputName = (output == null) ? null : output.getName(); 250 Message inputMessage = (input == null) ? null : input.getMessage(); 251 Message outputMessage = (output == null) ? null : output.getMessage(); 252 253 WSIFOperation wsifOperation = 254 wsifport.createOperation(method.getName(), inputName, outputName); 255 256 String inputMsgName = ""; 258 if (input != null) { 259 Message m = input.getMessage(); 260 if (m != null) { 261 QName qn = m.getQName(); 262 inputMsgName = (qn == null) ? "" : qn.getLocalPart(); 263 } 264 } 265 266 String outputMsgName = ""; 267 if (output != null) { 268 Message m = output.getMessage(); 269 if (m != null) { 270 QName qn = m.getQName(); 271 outputMsgName = (qn == null) ? "" : qn.getLocalPart(); 272 } 273 } 274 275 WSIFMessage wsifInputMessage = 277 wsifOperation.createInputMessage(inputMsgName); 278 279 WSIFMessage wsifOutputMessage = null; 281 WSIFMessage wsifFaultMessage = null; 282 if (output != null) { 283 wsifOutputMessage = wsifOperation.createOutputMessage(inputMsgName); 284 wsifFaultMessage = wsifOperation.createFaultMessage(inputMsgName); 285 } 286 287 List inputParts = (inputMessage == null) 288 ? new ArrayList () 289 : inputMessage.getOrderedParts(null); 290 if (args != null) { 291 if (inputParts.size() != args.length) { 292 unWrapIfWrappedDocLit(inputParts, operation.getName()); 293 } 294 Iterator partIt = inputParts.iterator(); 295 for (int argIndex = 0; partIt.hasNext(); argIndex++) { 296 Part part = (Part) partIt.next(); 297 String partName; 298 if (isWrappedInContext()) { 299 QName qn = part.getElementName(); 300 partName = (qn == null) ? "" : qn.getLocalPart(); 301 } else { 302 partName = part.getName(); 303 } 304 wsifInputMessage.setObjectPart(partName, args[argIndex]); 305 } 306 } 307 308 if (output == null) 309 wsifOperation.executeInputOnlyOperation(wsifInputMessage); 310 else { 311 boolean success = 312 wsifOperation.executeRequestResponseOperation( 313 wsifInputMessage, 314 wsifOutputMessage, 315 wsifFaultMessage); 316 if (!success) { 317 StringBuffer sb = new StringBuffer (); 318 Iterator it = wsifFaultMessage.getPartNames(); 319 while (it.hasNext()) { 320 String name = (String ) it.next(); 321 sb.append(name).append(":").append( 322 wsifFaultMessage.getObjectPart(name)).append( 323 " "); 324 } 325 throw new WSIFException(sb.toString()); 326 } 327 } 328 329 Object result = null; 331 if (outputMessage != null) { 332 List outputParts = outputMessage.getOrderedParts(null); 333 if (outputParts != null && outputParts.size() > 0) { 334 Iterator outPartIt = outputParts.iterator(); 336 Part returnPart = (Part) outPartIt.next(); 337 try { 338 result = wsifOutputMessage.getObjectPart(returnPart.getName()); 339 } catch (WSIFException e) { 340 Trc.ignoredException(e); 341 unWrapIfWrappedDocLit(outputParts, operation.getName()+"Response"); 342 outPartIt = outputParts.iterator(); 343 returnPart = (Part) outPartIt.next(); 344 result = wsifOutputMessage.getObjectPart(returnPart.getName()); 345 } 346 if (outPartIt.hasNext()) { 349 Object [] inPartArr = 350 inputMessage.getOrderedParts(null).toArray(); 351 Part nextOutPart = (Part) outPartIt.next(); 352 353 for (int argIndex = 0; argIndex < args.length; argIndex++) { 354 if (((Part) (inPartArr[argIndex])) 355 .getName() 356 .equals(nextOutPart.getName())) { 357 args[argIndex] = 358 wsifOutputMessage.getObjectPart( 359 nextOutPart.getName()); 360 if (outPartIt.hasNext()) 361 nextOutPart = (Part) outPartIt.next(); 362 else 363 break; } 365 } } 367 } 368 } 369 Trc.exit(result); 370 return result; 371 } 372 373 414 private Operation findMatchingOperation(Method method, Object [] args) 415 throws WSIFException { 416 417 String key = createWSDLOperationKey(method, args); 419 420 Operation previousOp = (Operation) wsdlOperationTable.get(key); 423 if (previousOp != null) { 424 return previousOp; 425 } 426 427 Method [] allMethods = iface.getMethods(); 429 int i; 430 for (i = 0; i < allMethods.length; i++) 431 if (allMethods[i].equals(method)) 432 break; 433 if (i >= allMethods.length || !method.equals(allMethods[i])) 434 throw new WSIFException( 435 "Method " 436 + method.getName() 437 + " is not in interface " 438 + iface.getName()); 439 440 String methodName = method.getName(); 441 Class [] types = method.getParameterTypes(); 442 List opList = portType.getOperations(); 443 Iterator opIt = opList.iterator(); 444 Operation matchingOperation = null; 445 446 while (opIt.hasNext()) { 449 Operation operation = (Operation) opIt.next(); 450 if (!methodName.equalsIgnoreCase(operation.getName())) 452 continue; 453 454 Input input = operation.getInput(); 455 Message inputMessage = (input == null) ? null : input.getMessage(); 456 List inputParts = (inputMessage == null) 457 ? new ArrayList () 458 : inputMessage.getOrderedParts(null); 459 460 int numInputParts = inputParts.size(); 461 462 if (numInputParts == 0 && types.length == 0) { 464 wsdlOperationTable.put(key, operation); 465 return operation; 466 } 467 468 if (types != null && (numInputParts != types.length)) { 470 unWrapIfWrappedDocLit(inputParts, operation.getName()); 471 numInputParts = inputParts.size(); 472 if (numInputParts != types.length) { 473 continue; 474 } 475 } 476 477 Iterator partIt = inputParts.iterator(); 479 boolean foundAllArgs = true; 480 boolean exactMatchAllArgs = true; 481 for (int argIndex = 0; 482 partIt.hasNext() && foundAllArgs; 483 argIndex++) { 484 485 Part part = (Part) partIt.next(); 486 QName partTypeName = part.getTypeName(); 487 if (partTypeName==null) { 488 partTypeName = part.getElementName(); 489 } 490 491 494 QName partTypeNameWrapped = 495 new QName (partTypeName.getNamespaceURI(), ">" + partTypeName.getLocalPart()); 496 497 boolean foundThisArg = false; 498 boolean exactMatchThisArg = false; 499 500 for (Iterator mapIt = typeMap.iterator(); 502 mapIt.hasNext() && !foundThisArg; 503 ) { 504 WSIFDynamicTypeMapping mapping = 505 (WSIFDynamicTypeMapping) mapIt.next(); 506 if (mapping.getXmlType().equals(partTypeName) 507 || (mapping.getXmlType().equals(partTypeNameWrapped))) { 508 if (mapping 509 .getJavaType() 510 .isAssignableFrom(types[argIndex]) 511 || (args[argIndex] != null 512 && mapping.getJavaType().isAssignableFrom( 513 args[argIndex].getClass()))) { 514 foundThisArg = true; 515 if (mapping.getJavaType().equals(types[argIndex]) 516 || (args[argIndex] != null 517 && mapping.getJavaType().equals( 518 args[argIndex].getClass()))) 519 exactMatchThisArg = true; 520 } else 521 break; 522 } 523 } 524 525 TypeMapping tm = 527 (TypeMapping) (simpleTypeReg.get(partTypeName)); 528 if (!foundThisArg) { 529 if (tm != null) { 530 String simpleType = tm.javaType; 531 if (types[argIndex].toString().equals(simpleType)) { 532 foundThisArg = true; 534 exactMatchThisArg = true; 535 } else 536 try { 538 Class simpleClass = 539 Class.forName( 540 simpleType, 541 true, 542 Thread 543 .currentThread() 544 .getContextClassLoader()); 545 if (simpleClass 546 .isAssignableFrom(types[argIndex])) { 547 foundThisArg = true; 548 if (simpleClass.equals(types[argIndex])) 549 exactMatchThisArg = true; 550 } 551 } catch (ClassNotFoundException ignored) { 552 Trc.ignoredException(ignored); 553 } 554 } else if (types[argIndex].equals(DataHandler .class)) 555 foundThisArg = true; 561 } 562 563 if (!foundThisArg) 564 foundAllArgs = false; 565 if (!exactMatchThisArg) 566 exactMatchAllArgs = false; 567 } 568 569 if (foundAllArgs) { 570 if (exactMatchAllArgs) { 571 wsdlOperationTable.put(key, operation); 572 return operation; 573 } 574 575 matchingOperation = operation; 577 } 578 } 580 if (matchingOperation != null) { 581 wsdlOperationTable.put(key, matchingOperation); 582 return matchingOperation; 583 } 584 585 String argString = new String (); 587 if (types != null) 588 for (i = 0; i < types.length; i++) { 589 if (i != 0) 590 argString += ", "; 591 argString += types[i]; 592 } 593 594 throw new WSIFException( 595 "Method " 596 + methodName 597 + "(" 598 + argString 599 + ") was not found in portType " 600 + portType.getQName()); 601 } 602 603 606 private String createWSDLOperationKey(Method method, Object [] args) { 607 Trc.entry(this, method, args); 608 609 StringBuffer sb = new StringBuffer (); 610 sb.append(method.getName()).append(":"); 611 612 Class [] types = method.getParameterTypes(); 613 for (int i = 0; i < types.length; i++) 614 sb.append(types[i].getName()).append(":"); 615 616 if (args != null) 617 for (int i = 0; i < args.length; i++) { 618 if (args[i] == null) 619 sb.append("null"); 620 else 621 sb.append(args[i].getClass().getName()); 622 sb.append(":"); 623 } 624 625 Trc.exit(sb.toString()); 626 return sb.toString(); 627 } 628 629 632 private String createWSIFOperationKey( 633 String operationName, 634 String inputName, 635 String outputName) { 636 Trc.entry(this,operationName,inputName,outputName); 637 638 StringBuffer sb = new StringBuffer (); 639 sb.append(operationName).append(inputName).append(outputName); 640 641 Trc.exit(sb.toString()); 642 return sb.toString(); 643 } 644 645 648 private void unWrapIfWrappedDocLit(List parts, String operationName) 649 throws WSIFException { 650 Part p = WSIFUtils.getWrappedDocLiteralPart(parts, operationName); 651 if (p != null) { 652 List unWrappedParts = WSIFUtils.unWrapPart(p, def); 653 parts.remove(p); 654 parts.addAll(unWrappedParts); 655 } 656 } 657 658 private boolean isWrappedInContext() throws WSIFException { 659 WSIFMessage context = wsifport.getContext(); 660 String style = null; 661 try { 662 style = 663 (String ) context.getObjectPart( 664 WSIFConstants.CONTEXT_OPERATION_STYLE); 665 } catch (WSIFException e) { 666 Trc.ignoredException(e); 667 } 668 boolean wrappedInContext; 669 if (WSIFConstants.CONTEXT_OPERATION_STYLE_WRAPPED.equals(style)) { 670 wrappedInContext = true; 671 } else { 672 wrappedInContext = false; 673 } 674 return wrappedInContext; 675 } 676 677 public String deep() { 678 String buff = ""; 679 try { 680 buff = new String (this.toString() + "\n"); 681 buff += "iface: " + iface; 682 buff += " def: " + Trc.brief(def); 683 buff += " serviceNS: " + serviceNS; 684 buff += " serviceName: " + serviceName; 685 buff += " portTypeNS: " + portTypeNS; 686 buff += " portTypeName: " + portTypeName; 687 buff += " typeMap: " + typeMap; 688 buff += "\nsimpleTypeReg: " + simpleTypeReg; 689 buff += "\nportType: " + Trc.brief(portType); 690 buff += " wsifport: " + wsifport; 691 692 } catch (Exception e) { 695 Trc.exceptionInTrace(e); 696 } 697 return buff; 698 } 699 } 700 | Popular Tags |