1 57 58 package org.apache.wsif.providers.java; 59 60 import java.lang.reflect.Constructor; 61 import java.lang.reflect.InvocationTargetException; 62 import java.lang.reflect.Method; 63 import java.util.ArrayList; 64 import java.util.Enumeration; 65 import java.util.HashMap; 66 import java.util.Iterator; 67 import java.util.List; 68 import java.util.Map; 69 import java.util.Vector; 70 71 import javax.wsdl.BindingFault; 72 import javax.wsdl.BindingOperation; 73 import javax.wsdl.BindingOutput; 74 import javax.wsdl.Fault; 75 import javax.wsdl.Operation; 76 import javax.wsdl.OperationType; 77 import javax.wsdl.Part; 78 import javax.wsdl.Port; 79 import javax.xml.namespace.QName; 80 81 import org.apache.wsif.WSIFException; 82 import org.apache.wsif.WSIFMessage; 83 import org.apache.wsif.WSIFOperation; 84 import org.apache.wsif.WSIFPort; 85 import org.apache.wsif.base.WSIFDefaultOperation; 86 import org.apache.wsif.logging.MessageLogger; 87 import org.apache.wsif.logging.Trc; 88 import org.apache.wsif.providers.ProviderUtils; 89 import org.apache.wsif.wsdl.extensions.java.JavaOperation; 90 91 98 public class WSIFOperation_Java 99 extends WSIFDefaultOperation 100 implements WSIFOperation { 101 102 private static final long serialVersionUID = 1L; 103 104 protected javax.wsdl.Port fieldPortModel; 105 protected WSIFPort_Java fieldPort; 106 protected javax.wsdl.BindingOperation fieldBindingOperationModel; 107 protected JavaOperation fieldJavaOperationModel; 108 109 protected String[] fieldInParameterNames = null; 110 protected String[] fieldOutParameterNames = null; 111 protected Map fieldFaultMessageInfos = null; 112 protected Method[] fieldMethods = null; 113 protected Constructor[] fieldConstructors = null; 114 protected String fieldOutputMessageName = null; 115 protected boolean fieldIsStatic = false; 116 protected boolean fieldIsConstructor = false; 117 protected Map fieldTypeMaps = null; 118 119 private class FaultMessageInfo { 120 String fieldMessageName; 121 String fieldPartName; 122 String fieldFormatType; 123 125 FaultMessageInfo(String messageName, String partName, String formatType) { 126 fieldMessageName = messageName; 127 fieldPartName = partName; 128 fieldFormatType = formatType; 129 } 130 } 131 132 public WSIFOperation_Java( 133 javax.wsdl.Port portModel, 134 BindingOperation bindingOperationModel, 135 WSIFPort_Java port, 136 Map typeMaps) 137 throws WSIFException { 138 139 Trc.entry(this, portModel, bindingOperationModel, port, typeMaps); 140 141 fieldPortModel = portModel; 142 fieldBindingOperationModel = bindingOperationModel; 143 fieldPort = port; 144 fieldTypeMaps = typeMaps; 145 Method[] allMethods = fieldPort.getObjectReference().getClass().getMethods(); 146 147 try { 148 fieldJavaOperationModel = 149 (JavaOperation) fieldBindingOperationModel.getExtensibilityElements().get(0); 150 } catch (Exception e) { 151 Trc.exception(e); 152 throw new WSIFException( 153 "Unable to resolve Java binding for operation '" 154 + bindingOperationModel.getName() 155 + "'"); 156 } 157 158 String methodType = fieldJavaOperationModel.getMethodType(); 160 if (methodType != null) { 161 if (methodType.equals("static")) { 162 fieldIsStatic = true; 163 } else if (methodType.equals("constructor")) { 164 fieldIsConstructor = true; 165 } else { 166 } 168 } 169 170 fieldConstructors = getConstructors(); 171 fieldMethods = getMethods(allMethods); 172 173 if (Trc.ON) 174 Trc.exit(deep()); 175 } 176 177 private WSIFOperation_Java( 178 Port p, 179 WSIFPort_Java pj, 180 BindingOperation bo, 181 JavaOperation jo, 182 String[] inPNames, 183 String[] outPNames, 184 Map faultMsgInfos, 185 Method[] m, 186 Constructor[] c, 187 String outMName, 188 boolean isSttc, 189 boolean isCnstr, 190 Map tMap) { 191 Trc.entry( 192 this, 193 p, 194 pj, 195 bo, 196 jo, 197 inPNames, 198 outPNames, 199 faultMsgInfos, 200 m, 201 c, 202 outMName, 203 new Boolean(isSttc), 204 new Boolean(isCnstr), 205 tMap); 206 207 fieldPortModel = p; 208 fieldPort = pj; 209 fieldBindingOperationModel = bo; 210 fieldJavaOperationModel = jo; 211 fieldInParameterNames = inPNames; 212 fieldOutParameterNames = outPNames; 213 fieldFaultMessageInfos = faultMsgInfos; 214 fieldMethods = m; 215 fieldConstructors = c; 216 fieldOutputMessageName = outMName; 217 fieldIsStatic = isSttc; 218 fieldIsConstructor = isCnstr; 219 fieldTypeMaps = tMap; 220 221 if (Trc.ON) 222 Trc.exit(deep()); 223 } 224 225 229 public WSIFOperation_Java copy() throws WSIFException { 230 Trc.entry(this); 231 WSIFOperation_Java woj = 232 new WSIFOperation_Java( 233 fieldPortModel, 234 fieldPort, 235 fieldBindingOperationModel, 236 fieldJavaOperationModel, 237 fieldInParameterNames, 238 fieldOutParameterNames, 239 fieldFaultMessageInfos, 240 fieldMethods, 241 fieldConstructors, 242 fieldOutputMessageName, 243 fieldIsStatic, 244 fieldIsConstructor, 245 fieldTypeMaps); 246 Trc.exit(woj); 247 return woj; 248 } 249 250 protected static Class getClassForName(String classname) throws WSIFException { 251 Trc.entry(null,classname); 252 253 Class cls = null; 254 255 if (classname == null) { 256 throw new WSIFException("Error in getClassForName(): No class name specified!"); 257 } 258 259 try { 260 if (classname.lastIndexOf('.') == -1) { 261 if (classname.equals("char")) { 263 cls = char.class; 264 } else if (classname.equals("boolean")) { 265 cls = boolean.class; 266 } else if (classname.equals("byte")) { 267 cls = byte.class; 268 } else if (classname.equals("short")) { 269 cls = short.class; 270 } else if (classname.equals("int")) { 271 cls = int.class; 272 } else if (classname.equals("long")) { 273 cls = long.class; 274 } else if (classname.equals("float")) { 275 cls = float.class; 276 } else if (classname.equals("double")) { 277 cls = double.class; 278 } else { 279 cls = 281 Class.forName(classname, true, Thread.currentThread().getContextClassLoader()); 282 } 283 } else { 284 cls = 285 Class.forName(classname, true, Thread.currentThread().getContextClassLoader()); 286 } 287 } catch (ClassNotFoundException ex) { 288 Trc.exception(ex); 289 throw new WSIFException("Could not instantiate class '" + classname + "'", ex); 290 } 291 Trc.exit(cls); 292 return cls; 293 } 294 295 protected Constructor[] getConstructors() throws WSIFException { 296 Trc.entry(this); 297 Constructor[] candidates; 298 Constructor[] constructors = 300 fieldPort.getObjectReference().getClass().getConstructors(); 301 Object[] args = getMethodArgumentClasses(); 302 Vector possibles = new Vector(); 303 for (int i = 0; i < constructors.length; i++) { 304 Class[] params = constructors[i].getParameterTypes(); 305 if (params.length != args.length) 306 continue; 307 308 boolean match = true; 309 for (int j = 0; j < params.length; j++) { 310 Object obj = args[j]; 311 if (obj instanceof Vector) { 312 Vector vec = (Vector) obj; 313 boolean found = false; 314 for (int p = 0; p < vec.size(); p++) { 315 Class cl = (Class) vec.get(p); 316 if (cl.getName().equals(params[j].getName())) { 317 found = true; 318 break; 319 } else if (params[j].isAssignableFrom(cl)) { 320 found = true; 321 break; 322 } 323 } 324 if (!found) { 325 match = false; 326 break; 327 } 328 } else { 329 if (!(((Class) obj).getName().equals(params[j].getName())) 330 && !(params[j].isAssignableFrom((Class) obj))) { 331 match = false; 332 break; 333 } 334 } 335 } 336 if (match) { 337 possibles.addElement(constructors[i]); 338 } 339 } 340 candidates = new Constructor[possibles.size()]; 341 for (int k = 0; k < candidates.length; k++) { 342 candidates[k] = (Constructor) possibles.get(k); 343 } 344 Trc.exit(candidates); 345 return candidates; 346 } 347 348 protected Map getFaultMessageInfos() throws WSIFException { 349 Trc.entry(this); 350 Operation operation = null; 352 try { 353 operation = getOperation(); 354 } catch (Exception e) { 355 Trc.exception(e); 356 throw new WSIFException("Failed to get Operation", e); 357 } 358 359 if (fieldFaultMessageInfos == null) { 360 fieldFaultMessageInfos = new HashMap(); 361 } 362 363 BindingFault bindingFaultModel = null; 364 Map bindingFaultModels = fieldBindingOperationModel.getBindingFaults(); 365 List parts = null; 366 Iterator modelsIterator = bindingFaultModels.values().iterator(); 367 368 while (modelsIterator.hasNext()) { 369 bindingFaultModel = (BindingFault) modelsIterator.next(); 370 String name = bindingFaultModel.getName(); 371 if (name == null) { 372 throw new WSIFException("Fault name not found in binding"); 373 } 374 375 Map map = operation.getFault(name).getMessage().getParts(); 376 if (map.size() >= 1) { 377 Part part = (Part) map.values().iterator().next(); 378 QName partType = part.getTypeName(); 379 if (partType == null) partType = part.getElementName(); 380 Object formatType = fieldTypeMaps.get(partType); 381 if (formatType == null) { 382 throw new WSIFException( 383 "formatType for typeName '" + part.getName() + "' not found in document"); 384 } 385 386 if (formatType instanceof Vector) { 387 Vector types = (Vector) formatType; 388 Enumeration enum = types.elements(); 389 while (enum.hasMoreElements()) { 390 String type = (String) enum.nextElement(); 391 fieldFaultMessageInfos.put( 393 type, 394 new FaultMessageInfo(name, part.getName(), type)); 395 } 396 } else { 397 String type = (String) formatType; 398 fieldFaultMessageInfos.put( 400 type, 401 new FaultMessageInfo(name, part.getName(), type)); 402 } 403 } 404 } 405 Trc.exit(fieldFaultMessageInfos); 406 return fieldFaultMessageInfos; 407 } 408 409 protected Method[] getMethods(Method[] allMethods) throws WSIFException { 410 Trc.entry(this,allMethods); 411 Method[] candidates; 412 try { 413 if (!fieldIsConstructor) { 414 Object[] args = getMethodArgumentClasses(); 416 Object retClass = getMethodReturnClass(); 417 Vector possibles = new Vector(); 418 for (int i = 0; i < allMethods.length; i++) { 419 if (!allMethods[i] 420 .getName() 421 .equals(fieldJavaOperationModel.getMethodName())) 422 continue; 423 Class[] params = allMethods[i].getParameterTypes(); 424 if (params.length != args.length) 425 continue; 426 Class retType = allMethods[i].getReturnType(); 427 if (retClass != null && retClass instanceof Vector) { 428 Vector vec = (Vector) retClass; 429 boolean found = false; 430 for (int p = 0; p < vec.size(); p++) { 431 Class cl = (Class) vec.get(p); 432 if (cl.getName().equals(retType.getName())) { 433 found = true; 434 break; 435 } else if (cl.isAssignableFrom(retType)) { 436 found = true; 437 break; 438 } 439 } 440 if (!found) 441 continue; 442 } else { 443 if (retType != null && retClass != null) { 444 if (!(((Class) retClass).getName().equals(retType.getName())) 445 && !(((Class) retClass).isAssignableFrom(retType))) 446 continue; 447 } 448 } 449 450 boolean match = true; 451 for (int j = 0; j < params.length; j++) { 452 Object obj = args[j]; 453 if (obj instanceof Vector) { 454 Vector vec = (Vector) obj; 455 boolean found = false; 456 for (int p = 0; p < vec.size(); p++) { 457 Class cl = (Class) vec.get(p); 458 if (cl.getName().equals(params[j].getName())) { 459 found = true; 460 break; 461 } else if (params[j].isAssignableFrom(cl)) { 462 found = true; 463 break; 464 } 465 } 466 if (!found) { 467 match = false; 468 break; 469 } 470 } else { 471 if (!(((Class) obj).getName().equals(params[j].getName())) 472 && !(params[j].isAssignableFrom((Class) obj))) { 473 match = false; 474 break; 475 } 476 } 477 } 478 if (match) { 479 possibles.addElement(allMethods[i]); 480 } 481 } 482 candidates = new Method[possibles.size()]; 483 for (int k = 0; k < candidates.length; k++) { 484 candidates[k] = (Method) possibles.get(k); 485 } 486 Trc.exit(candidates); 487 return candidates; 488 } 489 Method[] m = new Method[0]; 490 Trc.exit(m); 491 return m; 492 } catch (WSIFException ex) { 493 Trc.exception(ex); 494 throw ex; 495 } catch (Exception ex) { 496 Trc.exception(ex); 497 throw new WSIFException( 498 "Error while resolving meta information of method " 499 + fieldJavaOperationModel.getMethodName() 500 + " : The meta information is not consistent.", 501 ex); 502 } 503 } 504 505 protected Operation getOperation() throws Exception { 506 Trc.entry(this); 507 Operation operation = null; 508 509 String inputName = null; 512 if (fieldBindingOperationModel.getBindingInput()!=null) 513 inputName = fieldBindingOperationModel.getBindingInput().getName(); 514 515 String outputName = null; 516 if (fieldBindingOperationModel.getBindingOutput()!=null) 517 outputName = fieldBindingOperationModel.getBindingOutput().getName(); 518 519 operation = 522 this.fieldPortModel.getBinding().getPortType().getOperation( 523 this.fieldBindingOperationModel.getName(), 524 inputName, 525 outputName); 526 Trc.exit(operation); 527 return operation; 528 } 529 530 protected Object getMethodReturnClass() throws WSIFException { 531 Trc.entry(this); 532 Object methodReturnClass = null; 533 try { 534 String returnPartString = fieldJavaOperationModel.getReturnPart(); 535 if (returnPartString != null) { 536 Part returnPart = 539 getOperation().getOutput().getMessage().getPart(returnPartString); 540 541 if (returnPart != null) { 543 QName partType = returnPart.getTypeName(); 544 if (partType == null) partType = returnPart.getElementName(); 545 Object obj = this.fieldTypeMaps.get(partType); 546 if (obj == null) 547 throw new WSIFException( 548 "Could not map type " 549 + partType 550 + " to a java type. Part name was " 551 + returnPart.getName() == null 552 ? "<null>" : returnPart.getName()); 553 554 if (obj instanceof Vector) { 555 Vector v = (Vector) obj; 556 Vector argv = new Vector(); 557 Enumeration enum = v.elements(); 558 while (enum.hasMoreElements()) { 559 String cls = (String) enum.nextElement(); 560 argv.addElement(getClassForName(cls)); 561 } 562 methodReturnClass = argv; 563 } else { 564 methodReturnClass = 565 getClassForName((String) obj); 566 } 567 } else { 568 throw new Exception( 571 "returnPart '" + returnPartString + "' was not in the output message"); 572 } 573 } 574 } catch (Exception ex) { 576 Trc.exception(ex); 577 throw new WSIFException( 578 "Error while determining return class of method " 579 + fieldJavaOperationModel.getMethodName() 580 + " : The meta information is not consistent.", 581 ex); 582 } 583 Trc.exit(methodReturnClass); 584 return methodReturnClass; 585 } 586 587 protected Object[] getMethodArgumentClasses() throws WSIFException { 588 Trc.entry(this); 589 Object[] methodArgClasses = null; 590 try { 591 592 Operation operation = getOperation(); 593 594 601 602 List parameterOrder = null; 604 605 parameterOrder = fieldJavaOperationModel.getParameterOrder(); 606 607 if (parameterOrder == null) { 608 parameterOrder = operation.getParameterOrdering(); 609 } 610 611 618 if (parameterOrder == null) { 619 List partList = operation.getInput().getMessage().getOrderedParts(null); 620 parameterOrder = new Vector(); 621 Iterator partListIterator = partList.iterator(); 622 while (partListIterator.hasNext()) { 623 Part part = (Part) partListIterator.next(); 624 parameterOrder.add(part.getName()); 625 } 626 } 627 628 647 648 ArrayList argNames = new ArrayList(); 649 ArrayList argTypes = new ArrayList(); 650 651 Iterator parameterIterator = parameterOrder.iterator(); 652 while (parameterIterator.hasNext()) { 653 String param = (String) parameterIterator.next(); 654 Part part = (Part) operation.getInput().getMessage().getPart(param); 655 if (part == null) { 656 part = (Part) operation.getOutput().getMessage().getPart(param); 657 } 658 if (part == null) 659 throw new Exception( 660 "Part '" 661 + param 662 + "' from parameterOrder not found in input or output message"); 663 argNames.add((String) part.getName()); 664 665 QName partType = part.getTypeName();; 667 if (partType == null) partType = part.getElementName(); 668 Object obj = this.fieldTypeMaps.get(partType); 669 if (obj == null) 670 throw new WSIFException( 671 "Could not map type " 672 + partType 673 + " to a java type. Part name was " 674 + part.getName() == null ? "<null>" : part.getName()); 675 676 if (obj instanceof Vector) { 677 Vector v = (Vector) obj; 678 Vector argv = new Vector(); 679 Enumeration enum = v.elements(); 680 while (enum.hasMoreElements()) { 681 String cls = (String) enum.nextElement(); 682 argv.addElement(getClassForName(cls)); 683 } 684 argTypes.add(argv); 685 } else { 686 argTypes.add(getClassForName((String)obj)); 687 } 688 689 } 690 691 methodArgClasses = argTypes.toArray(); 692 fieldInParameterNames = new String[argNames.size()]; 693 for (int i = 0; i < argNames.size(); i++) { 694 fieldInParameterNames[i] = (String) argNames.get(i); 695 } 696 697 if (operation.getStyle().equals(OperationType.REQUEST_RESPONSE)) { 699 argNames = new ArrayList(); 700 String returnPart = fieldJavaOperationModel.getReturnPart(); 702 Iterator outputPartsIterator = 703 operation.getOutput().getMessage().getOrderedParts(null).iterator(); 704 while (outputPartsIterator.hasNext()) { 705 Part part = (Part) outputPartsIterator.next(); 706 String partName = part.getName(); 707 if (partName != null && returnPart != null && partName.equals(returnPart)) { 708 argNames.add(0, partName); 710 } else { 711 argNames.add((String) part.getName()); 712 } 713 } 714 715 fieldOutParameterNames = new String[argNames.size()]; 717 for (int i = 0; i < argNames.size(); i++) { 718 fieldOutParameterNames[i] = (String) argNames.get(i); 719 } 720 } else { 721 fieldOutParameterNames = new String[0]; 722 } 723 } catch (Exception ex) { 724 Trc.exception(ex); 725 throw new WSIFException( 726 "Error while determining signature of method " 727 + fieldJavaOperationModel.getMethodName() 728 + " : The meta information is not consistent.", 729 ex); 730 } 731 Trc.exit(methodArgClasses); 732 return methodArgClasses; 733 } 734 735 protected Object[] getCompatibleArguments(Class[] parmTypes, Object[] args) { 739 Trc.entry(this,parmTypes,args); 740 if (args == null || parmTypes == null) { 746 Object[] compatibleArgs = new Object[0]; 747 return compatibleArgs; 748 } 749 750 Object[] compatibleArgs = new Object[args.length]; 751 for (int i = 0; i < parmTypes.length; i++) { 752 if (args[i] == null) { 754 compatibleArgs[i] = ProviderUtils.getDefaultObject(parmTypes[i]); 755 continue; 756 } 757 Object convertedArg = getCompatibleObject(parmTypes[i], args[i]); 759 if (convertedArg == null) { 760 return null; 762 } else { 763 compatibleArgs[i] = convertedArg; 764 } 765 766 } 767 Trc.exit(compatibleArgs); 768 return compatibleArgs; 769 } 770 771 protected Object getCompatibleReturn(Method method, Object returnObj) { 772 Trc.entry(this,method,returnObj); 773 Object o = null; 774 Class rt = method.getReturnType(); 775 Class ct = null; 776 int dims = 0; 777 if (rt.isArray()) { 778 ct = rt.getComponentType(); 779 dims++; 780 while (ct.isArray()) { 781 ct = ct.getComponentType(); 782 dims++; 783 } 784 } 785 if (returnObj instanceof java.lang.Character) { 786 o = getCompatibleObject(java.lang.String.class, returnObj); 787 } else if (ct != null && (ct.equals(java.lang.Character.class) || ct.equals(char.class))) { 788 String stringArrayClassName = "[Ljava.lang.String;"; 789 for (int d=1; d<dims; d++) { 790 stringArrayClassName = "["+stringArrayClassName; 791 } 792 try { 793 Class stringArrayClass = Class.forName(stringArrayClassName, true, Thread.currentThread().getContextClassLoader()); 794 o = getCompatibleObject(stringArrayClass, returnObj); 795 } catch(ClassNotFoundException cnf) { 796 Trc.ignoredException(cnf); 797 o = returnObj; 798 } 799 } else { 800 o = returnObj; 801 } 802 Trc.exit(o); 803 return o; 804 } 805 806 protected Object getCompatibleObject(Class cls, Object obj) { 816 Trc.entry(this,cls,obj); 817 818 if (cls.getName().equals(obj.getClass().getName())) return obj; 819 820 if ((cls.equals(java.lang.Character.class) || cls.equals(char.class)) 822 && obj.getClass().equals(java.lang.String.class)) { 823 Character charArg = ProviderUtils.stringToCharacter((String) obj); 824 if (charArg == null) { 825 Trc.exit(null); 827 return null; 828 } 829 Trc.exit(charArg); 830 return charArg; 831 } 832 833 if (cls.isArray() && obj.getClass().isArray()) { 835 Class cct = cls.getComponentType(); 836 Class objct = obj.getClass().getComponentType(); 837 while (cct.isArray()) { 838 cct = cct.getComponentType(); 839 } 840 while (objct.isArray()) { 841 objct = objct.getComponentType(); 842 } 843 if (objct.equals(java.lang.String.class) && cct.equals(char.class)) { 844 try { 845 Object charArray = ProviderUtils.stringArrayToCharArray(obj); 846 Trc.exit(charArray); 847 return charArray; 848 } catch (Exception e) { 849 Trc.ignoredException(e); 850 Trc.exit(null); 851 return null; 852 } 853 } else if (objct.equals(java.lang.String.class) && cct.equals(java.lang.Character.class)) { 854 try { 855 Object charArray = ProviderUtils.stringArrayToCharacterArray(obj); 856 Trc.exit(charArray); 857 return charArray; 858 } catch (Exception e) { 859 Trc.ignoredException(e); 860 Trc.exit(null); 861 return null; 862 } 863 } else if (objct.equals(java.lang.Character.class) && cct.equals(java.lang.String.class)) { 864 try { 865 Object charArray = ProviderUtils.characterArrayToStringArray(obj); 866 Trc.exit(charArray); 867 return charArray; 868 } catch (Exception e) { 869 Trc.ignoredException(e); 870 Trc.exit(null); 871 return null; 872 } 873 } else if (objct.equals(char.class) && cct.equals(java.lang.String.class)) { 874 try { 875 Object charArray = ProviderUtils.charArrayToStringArray(obj); 876 Trc.exit(charArray); 877 return charArray; 878 } catch (Exception e) { 879 Trc.ignoredException(e); 880 Trc.exit(null); 881 return null; 882 } 883 } 884 } 885 886 if (cls.equals(java.lang.String.class) 887 && obj.getClass().equals(java.lang.Character.class)) { 888 Trc.exit(obj.toString()); 889 return (obj.toString()); 890 } 891 Trc.exit(obj); 892 return obj; 893 } 894 895 protected String getOutputMessageName() throws WSIFException { 896 Trc.entry(this); 897 if (fieldOutputMessageName == null) { 898 BindingOutput bindingOutputModel = 899 fieldBindingOperationModel.getBindingOutput(); 900 if (bindingOutputModel != null) { 901 fieldOutputMessageName = bindingOutputModel.getName(); 902 } 903 } 904 Trc.exit(fieldOutputMessageName); 905 return fieldOutputMessageName; 906 } 907 908 public WSIFPort getWSIFPort() { 909 Trc.entry(this); 910 Trc.exit(fieldPort); 911 return fieldPort; 912 } 913 914 public boolean executeRequestResponseOperation( 915 WSIFMessage input, 916 WSIFMessage output, 917 WSIFMessage fault) 918 throws WSIFException { 919 920 Trc.entry(this, input, output, fault); 921 close(); 922 923 boolean operationSucceeded = true; 924 boolean usedOutputParam = false; 925 Method chosenMethod = null; 926 927 try { 928 Object result = null; 929 if (fieldIsConstructor) { 931 if (fieldConstructors.length <= 0) 932 throw new WSIFException( 933 "No constructor found that match the parts specified"); 934 } else { 935 if (fieldMethods.length <= 0) 936 throw new WSIFException("No method named '" 937 + fieldJavaOperationModel.getMethodName() 938 + "' found that match the parts specified"); 939 } 940 941 Object[] arguments = null; 942 Object part = null; 943 if ((fieldInParameterNames != null) && (fieldInParameterNames.length > 0)) { 944 arguments = new Object[fieldInParameterNames.length]; 945 for (int i = 0; i < fieldInParameterNames.length; i++) { 946 try { 947 part = input.getObjectPart(fieldInParameterNames[i]); 948 arguments[i] = part; 949 } catch (WSIFException e) { 950 Trc.exception(e); 951 arguments[i] = null; 952 if (fieldOutParameterNames.length > 0) { 953 String outParameterName = null; 954 for (int j = 0; j < fieldOutParameterNames.length; j++) { 955 outParameterName = 956 fieldOutParameterNames[j]; 957 if ((outParameterName != null) 958 && (outParameterName 959 .equals(fieldInParameterNames[i]))) { 960 arguments[i] = 961 (fieldMethods[0].getParameterTypes()[i]) 962 .newInstance(); 963 usedOutputParam = true; 964 } 965 } 966 } 967 } 968 } 969 } 970 971 boolean invokedOK = false; 972 if (fieldIsConstructor) { 973 for (int a = 0; a < fieldConstructors.length; a++) { 974 try { 975 Object[] compatibleArguments = 977 getCompatibleArguments(fieldConstructors[a].getParameterTypes(), arguments); 978 if (compatibleArguments == null) 980 break; 981 983 Trc.event( 984 this, 985 "Invoking constructor ", 986 fieldConstructors[a], 987 " with arguments ", 988 compatibleArguments); 989 990 result = 991 fieldConstructors[a].newInstance( 992 compatibleArguments); 993 994 Trc.event( 995 this, 996 "Returned from constructor, result is ", 997 result); 998 999 fieldPort.setObjectReference(result); 1000 invokedOK = true; 1001 break; 1002 } catch (IllegalArgumentException ia) { 1003 Trc.ignoredException(ia); 1004 } 1006 } 1007 if (!invokedOK) 1008 throw new WSIFException("Failed to call constructor for object in Java operation"); 1009 } else { 1011 if (fieldIsStatic) { 1012 for (int a = 0; a < fieldMethods.length; a++) { 1013 if (usedOutputParam) { 1014 for (int i = 0; i < fieldInParameterNames.length; i++) { 1015 String outParameterName = null; 1016 for (int j = 0; j < fieldOutParameterNames.length; j++) { 1017 outParameterName = fieldOutParameterNames[j]; 1018 if ((outParameterName != null) 1019 && (outParameterName.equals(fieldInParameterNames[i]))) { 1020 arguments[i] = (fieldMethods[a].getParameterTypes()[i]).newInstance(); 1021 } 1022 } 1023 } 1024 } 1025 try { 1026 Object[] compatibleArguments = 1028 getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments); 1029 if (compatibleArguments == null) 1031 break; 1032 1034 Trc.event( 1035 this, 1036 "Invoking method ", 1037 fieldMethods[a], 1038 " with arguments ", 1039 compatibleArguments); 1040 1041 result = 1042 fieldMethods[a].invoke( 1043 null, 1044 compatibleArguments); 1045 1046 Trc.event( 1047 this, 1048 "Returned from method, result is ", 1049 result); 1050 1051 chosenMethod = fieldMethods[a]; 1052 invokedOK = true; 1053 break; 1054 } catch (IllegalArgumentException ia) { 1055 Trc.ignoredException(ia); 1056 } 1058 } 1059 if (!invokedOK) 1060 throw new WSIFException( 1061 "Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'"); 1062 } else { 1063 for (int a = 0; a < fieldMethods.length; a++) { 1064 if (usedOutputParam) { 1065 for (int i = 0; i < fieldInParameterNames.length; i++) { 1066 String outParameterName = null; 1067 for (int j = 0; j < fieldOutParameterNames.length; j++) { 1068 outParameterName = fieldOutParameterNames[j]; 1069 if ((outParameterName != null) 1070 && (outParameterName.equals(fieldInParameterNames[i]))) { 1071 arguments[i] = (fieldMethods[a].getParameterTypes()[i]).newInstance(); 1072 } 1073 } 1074 } 1075 } 1076 try { 1077 Object[] compatibleArguments = 1079 getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments); 1080 if (compatibleArguments == null) 1082 break; 1083 1085 Object objRef = fieldPort.getObjectReference(); 1086 Trc.event( 1087 this, 1088 "Invoking object ", 1089 objRef, 1090 " method ", 1091 fieldMethods[a], 1092 " with arguments ", 1093 compatibleArguments); 1094 1095 result = 1096 fieldMethods[a].invoke( 1097 objRef, 1098 compatibleArguments); 1099 1100 Trc.event( 1101 this, 1102 "Returned from method, result is ", 1103 result); 1104 1105 chosenMethod = fieldMethods[a]; 1106 invokedOK = true; 1107 break; 1108 } catch (IllegalArgumentException ia) { 1109 Trc.ignoredException(ia); 1110 } 1112 } 1113 if (!invokedOK) 1114 throw new WSIFException( 1115 "Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'"); 1116 } 1117 1118 String outParameterName = null; 1119 if (fieldOutParameterNames != null && fieldOutParameterNames.length > 0) { 1120 output.setName(getOutputMessageName()); 1121 outParameterName = (String) fieldOutParameterNames[0]; 1122 if (outParameterName != null) { 1123 output.setObjectPart( 1124 outParameterName, 1125 getCompatibleReturn(chosenMethod, result)); 1126 } 1128 1129 if (arguments != null) { 1130 for (int i = 1; i < fieldOutParameterNames.length; i++) { 1131 outParameterName = fieldOutParameterNames[i]; 1132 if (outParameterName != null) { 1133 try { 1134 for (int r = 0; r < fieldInParameterNames.length; r++) { 1135 if (outParameterName.equals(fieldInParameterNames[r])) { 1136 output.setObjectPart(outParameterName, arguments[r]); 1137 break; 1138 } 1139 } 1140 } catch (WSIFException e) { 1141 Trc.ignoredException(e); 1142 } 1144 } 1145 } 1146 } 1147 } 1148 } 1149 } catch (InvocationTargetException ex) { 1150 Trc.exception(ex); 1151 Throwable invocationFault = ex.getTargetException(); 1152 String className = invocationFault.getClass().getName(); 1153 Map faultMessageInfos = getFaultMessageInfos(); 1154 FaultMessageInfo faultMessageInfo = 1155 (FaultMessageInfo) faultMessageInfos.get(className); 1156 if ((faultMessageInfo != null) 1157 && (faultMessageInfo.fieldPartName != null)) { Object faultPart = invocationFault; 1159 fault.setObjectPart(faultMessageInfo.fieldPartName, faultPart); 1161 fault.setName(faultMessageInfo.fieldMessageName); 1162 if (faultMessageInfo.fieldMessageName != null) { 1163 Fault wsdlFault = fieldBindingOperationModel.getOperation().getFault(faultMessageInfo.fieldMessageName); 1164 if (wsdlFault != null) { 1165 fault.setMessageDefinition(wsdlFault.getMessage()); 1166 } 1167 } 1168 operationSucceeded = false; 1169 } else { 1170 Class invocationFaultClass = invocationFault.getClass(); 1172 Class tempClass = null; 1173 Iterator it = faultMessageInfos.values().iterator(); 1174 boolean found = false; 1175 while (it.hasNext()) { 1176 faultMessageInfo = (FaultMessageInfo) it.next(); 1177 try { 1178 tempClass = 1179 Class.forName( 1180 faultMessageInfo.fieldFormatType, 1181 true, 1182 Thread.currentThread().getContextClassLoader()); 1183 if (tempClass.isAssignableFrom(invocationFaultClass)) { 1184 found = true; 1185 Object faultPart = invocationFault; 1186 fault.setObjectPart(faultMessageInfo.fieldPartName, faultPart); 1188 fault.setName(faultMessageInfo.fieldMessageName); 1189 if (faultMessageInfo.fieldMessageName != null) { 1190 Fault wsdlFault = fieldBindingOperationModel.getOperation().getFault(faultMessageInfo.fieldMessageName); 1191 if (wsdlFault != null) { 1192 fault.setMessageDefinition(wsdlFault.getMessage()); 1193 } 1194 } 1195 operationSucceeded = false; 1196 } 1197 } catch (Exception exc) { Trc.ignoredException(exc); 1199 } 1200 } 1201 if (!found) { 1202 throw new WSIFException("Operation failed!", invocationFault); 1203 } 1204 } 1205 } catch (Exception ex) { 1206 Trc.exception(ex); 1207 1208 MessageLogger.log( 1210 "WSIF.0005E", 1211 "Java", 1212 fieldJavaOperationModel.getMethodName()); 1213 1214 throw new WSIFException( 1215 this 1216 + " : Could not invoke '" 1217 + fieldJavaOperationModel.getMethodName() 1218 + "'", 1219 ex); 1220 } 1221 1222 Trc.exit(operationSucceeded); 1223 return operationSucceeded; 1224 } 1225 1226 public void executeInputOnlyOperation(WSIFMessage input) throws WSIFException { 1227 1228 Trc.entry(this, input); 1229 close(); 1230 1231 try { 1232 Object result = null; 1233 if (fieldIsConstructor) { 1235 if (fieldConstructors.length <= 0) 1236 throw new WSIFException("No constructors found that match the parts specified"); 1237 } else { 1238 if (fieldMethods.length <= 0) 1239 throw new WSIFException("No methods named '" 1240 + fieldJavaOperationModel.getMethodName() 1241 + "' found that match the parts specified"); 1242 } 1243 1244 Object[] arguments = null; 1245 Object part = null; 1246 if ((fieldInParameterNames != null) && (fieldInParameterNames.length > 0)) { 1247 arguments = new Object[fieldInParameterNames.length]; 1248 for (int i = 0; i < fieldInParameterNames.length; i++) { 1249 try { 1250 part = input.getObjectPart(fieldInParameterNames[i]); 1251 arguments[i] = part; 1252 } catch (WSIFException e) { 1253 Trc.exception(e); 1254 arguments[i] = null; 1255 } 1256 } 1257 } 1258 1259 boolean invokedOK = false; 1260 1261 if (fieldIsConstructor) { 1262 for (int a = 0; a < fieldConstructors.length; a++) { 1263 try { 1264 Object[] compatibleArguments = 1266 getCompatibleArguments(fieldConstructors[a].getParameterTypes(), arguments); 1267 if (compatibleArguments == null) 1269 break; 1270 1272 Trc.event( 1273 this, 1274 "Invoking constructor ", 1275 fieldConstructors[a], 1276 " with arguments ", 1277 compatibleArguments); 1278 1279 result = 1280 fieldConstructors[a].newInstance( 1281 compatibleArguments); 1282 1283 Trc.event( 1284 this, 1285 "Returned from constructor, result is ", 1286 result); 1287 1288 fieldPort.setObjectReference(result); 1289 invokedOK = true; 1290 break; 1291 } catch (IllegalArgumentException ia) { 1292 Trc.ignoredException(ia); 1293 } 1295 } 1296 if (!invokedOK) 1297 throw new WSIFException("Failed to call constructor for object in Java operation"); 1298 } else { 1299 if (fieldIsStatic) { 1300 for (int a = 0; a < fieldMethods.length; a++) { 1301 try { 1302 Object[] compatibleArguments = 1304 getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments); 1305 if (compatibleArguments == null) 1307 break; 1308 1310 Trc.event( 1311 this, 1312 "Invoking method ", 1313 fieldMethods[a], 1314 " with arguments ", 1315 compatibleArguments); 1316 1317 result = 1318 fieldMethods[a].invoke( 1319 null, 1320 compatibleArguments); 1321 1322 Trc.event( 1323 this, 1324 "Returned from method, result is ", 1325 result); 1326 1327 invokedOK = true; 1328 break; 1329 } catch (IllegalArgumentException ia) { 1330 Trc.ignoredException(ia); 1331 } 1333 } 1334 if (!invokedOK) 1335 throw new WSIFException( 1336 "Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'"); 1337 } else { 1338 for (int a = 0; a < fieldMethods.length; a++) { 1339 try { 1340 Object[] compatibleArguments = 1342 getCompatibleArguments(fieldMethods[a].getParameterTypes(), arguments); 1343 if (compatibleArguments == null) 1345 break; 1346 1348 Object objRef = fieldPort.getObjectReference(); 1349 Trc.event( 1350 this, 1351 "Invoking object ", 1352 objRef, 1353 " method ", 1354 fieldMethods[a], 1355 " with arguments ", 1356 compatibleArguments); 1357 1358 result = 1359 fieldMethods[a].invoke( 1360 objRef, 1361 compatibleArguments); 1362 1363 Trc.event( 1364 this, 1365 "Returned from method, result is ", 1366 result); 1367 1368 invokedOK = true; 1369 break; 1370 } catch (IllegalArgumentException ia) { 1371 Trc.ignoredException(ia); 1372 } 1374 } 1375 if (!invokedOK) 1376 throw new WSIFException( 1377 "Failed to invoke method '" + fieldJavaOperationModel.getMethodName() + "'"); 1378 } 1379 } 1380 } catch (InvocationTargetException ex) { 1381 Trc.exception(ex); 1382 1383 MessageLogger.log( 1385 "WSIF.0005E", 1386 "Java", 1387 fieldJavaOperationModel.getMethodName()); 1388 1389 throw new WSIFException( 1390 this 1391 + " : Invocation of '" 1392 + fieldJavaOperationModel.getMethodName() 1393 + "' failed.", 1394 ex); 1395 } catch (Exception ex) { 1396 Trc.exception(ex); 1397 1398 MessageLogger.log( 1400 "WSIF.0005E", 1401 "Java", 1402 fieldJavaOperationModel.getMethodName()); 1403 1404 throw new WSIFException( 1405 this 1406 + " : Could not invoke '" 1407 + fieldJavaOperationModel.getMethodName() 1408 + "'", 1409 ex); 1410 } 1411 1412 Trc.exit(); 1413 } 1414 1415 public String deep() { 1416 String buff = ""; 1417 try { 1418 buff = new String(super.toString() + ":\n"); 1419 buff += "portModel:" + Trc.brief(fieldPortModel); 1420 buff += " wsifPort_Java:" + fieldPort; 1421 buff += " bindingOperationModel:" + Trc.brief(fieldBindingOperationModel); 1422 buff += " JavaOperation:" + fieldJavaOperationModel; 1423 1424 buff += Trc.brief("inParameterNames", fieldInParameterNames); 1425 buff += Trc.brief("outParameterNames", fieldOutParameterNames); 1426 1427 if (fieldFaultMessageInfos == null) { 1428 buff += " faultMessageInfos:null"; 1429 } else { 1430 Iterator it = fieldFaultMessageInfos.keySet().iterator(); 1431 int i = 0; 1432 while (it.hasNext()) { 1433 String key = (String) it.next(); 1434 buff += " faultMessageInfos[" 1435 + i 1436 + "]:" 1437 + key 1438 + " " 1439 + fieldFaultMessageInfos.get(key); 1440 i++; 1441 } 1442 } 1443 1444 buff += Trc.brief("methods", fieldMethods); 1445 buff += Trc.brief("constructors", fieldConstructors); 1446 1447 buff += " outputMessageName:" + fieldOutputMessageName; 1448 buff += " isStatic:" + fieldIsStatic; 1449 buff += " isConstructor:" + fieldIsConstructor; 1450 1451 if (fieldTypeMaps == null) { 1452 buff += " faultTypeMaps:null"; 1453 } else { 1454 Iterator it = fieldTypeMaps.keySet().iterator(); 1455 int i = 0; 1456 while (it.hasNext()) { 1457 QName key = (QName) it.next(); 1458 buff += " typeMaps[" + i + "]:" + key + " " + fieldTypeMaps.get(key); 1459 i++; 1460 } 1461 } 1462 } catch (Exception e) { 1463 Trc.exceptionInTrace(e); 1464 } 1465 1466 return buff; 1467 } 1468} | Popular Tags |