1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.Constants; 19 import org.apache.axis.deployment.wsdd.WSDDConstants; 20 import org.apache.axis.description.OperationDesc; 21 import org.apache.axis.description.ServiceDesc; 22 import org.apache.axis.components.logger.LogFactory; 23 import org.apache.axis.constants.Scope; 24 import org.apache.axis.constants.Style; 25 import org.apache.axis.constants.Use; 26 import org.apache.axis.utils.JavaUtils; 27 import org.apache.axis.utils.Messages; 28 import org.apache.axis.wsdl.symbolTable.BindingEntry; 29 import org.apache.axis.wsdl.symbolTable.FaultInfo; 30 import org.apache.axis.wsdl.symbolTable.Parameter; 31 import org.apache.axis.wsdl.symbolTable.Parameters; 32 import org.apache.axis.wsdl.symbolTable.SchemaUtils; 33 import org.apache.axis.wsdl.symbolTable.SymbolTable; 34 import org.apache.axis.wsdl.symbolTable.TypeEntry; 35 import org.apache.commons.logging.Log; 36 37 import javax.wsdl.Binding; 38 import javax.wsdl.BindingOperation; 39 import javax.wsdl.Definition; 40 import javax.wsdl.Operation; 41 import javax.wsdl.OperationType; 42 import javax.wsdl.Port; 43 import javax.wsdl.Service; 44 import javax.wsdl.extensions.UnknownExtensibilityElement; 45 import javax.wsdl.extensions.soap.SOAPBinding; 46 import javax.xml.namespace.QName ; 47 import java.io.File ; 48 import java.io.FileOutputStream ; 49 import java.io.IOException ; 50 import java.io.OutputStreamWriter ; 51 import java.io.PrintWriter ; 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.HashMap ; 55 import java.util.HashSet ; 56 import java.util.Iterator ; 57 import java.util.Map ; 58 import java.util.Vector ; 59 60 63 public class JavaDeployWriter extends JavaWriter { 64 65 protected static Log log = LogFactory.getLog(JavaDeployWriter.class.getName()); 66 67 68 protected Definition definition; 69 70 71 protected SymbolTable symbolTable; 72 73 74 protected Emitter emitter; 75 76 77 Use use = Use.DEFAULT; 78 79 86 public JavaDeployWriter(Emitter emitter, Definition definition, 87 SymbolTable symbolTable) { 88 89 super(emitter, "deploy"); 90 91 this.emitter = emitter; 92 this.definition = definition; 93 this.symbolTable = symbolTable; 94 } 96 102 public void generate() throws IOException { 103 104 if (emitter.isServerSide()) { 105 super.generate(); 106 } 107 } 109 115 protected String getFileName() { 116 117 String dir = 118 emitter.getNamespaces().getAsDir(definition.getTargetNamespace()); 119 120 return dir + "deploy.wsdd"; 121 } 123 129 protected void writeFileHeader(PrintWriter pw) throws IOException { 130 131 pw.println(Messages.getMessage("deploy00")); 132 pw.println(Messages.getMessage("deploy02")); 133 pw.println(Messages.getMessage("deploy03")); 134 pw.println(Messages.getMessage("deploy05")); 135 pw.println(Messages.getMessage("deploy06")); 136 pw.println(Messages.getMessage("deploy07")); 137 pw.println(Messages.getMessage("deploy09")); 138 pw.println(); 139 pw.println("<deployment"); 140 pw.println(" xmlns=\"" + WSDDConstants.URI_WSDD + "\""); 141 pw.println(" xmlns:" + WSDDConstants.NS_PREFIX_WSDD_JAVA + "=\"" 142 + WSDDConstants.URI_WSDD_JAVA + "\">"); 143 } 145 151 protected void writeFileBody(PrintWriter pw) throws IOException { 152 writeDeployServices(pw); 153 pw.println("</deployment>"); 154 } 156 162 protected void writeDeployServices(PrintWriter pw) throws IOException { 163 164 Map serviceMap = definition.getServices(); 166 167 for (Iterator mapIterator = serviceMap.values().iterator(); 168 mapIterator.hasNext();) { 169 Service myService = (Service) mapIterator.next(); 170 171 pw.println(); 172 pw.println( 173 " <!-- " 174 + Messages.getMessage( 175 "wsdlService00", myService.getQName().getLocalPart()) + " -->"); 176 pw.println(); 177 178 for (Iterator portIterator = myService.getPorts().values().iterator(); 179 portIterator.hasNext();) { 180 Port myPort = (Port) portIterator.next(); 181 BindingEntry bEntry = 182 symbolTable.getBindingEntry(myPort.getBinding().getQName()); 183 184 if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) { 186 continue; 187 } 188 189 writeDeployPort(pw, myPort, myService, bEntry); 190 } 191 } 192 } 194 204 protected void writeDeployTypes( 205 PrintWriter pw, Binding binding, boolean hasLiteral, boolean hasMIME, Use use) 206 throws IOException { 207 208 pw.println(); 209 210 if (hasMIME) { 211 QName bQName = binding.getQName(); 212 213 writeTypeMapping( 214 pw, bQName.getNamespaceURI(), "DataHandler", 215 "javax.activation.DataHandler", 216 "org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory", 217 "org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory", 218 use.getEncoding()); 219 } 220 221 Map types = symbolTable.getTypeIndex(); 222 Collection typeCollection = types.values(); 223 for (Iterator i = typeCollection.iterator(); i.hasNext(); ) { 224 TypeEntry type = (TypeEntry) i.next(); 225 226 boolean process = true; 228 229 if (!Utils.shouldEmit(type)) { 232 process = false; 233 } 234 235 if (process) { 236 String namespaceURI = type.getQName().getNamespaceURI(); 237 String localPart = type.getQName().getLocalPart(); 238 String javaType = type.getName(); 239 String serializerFactory; 240 String deserializerFactory; 241 String encodingStyle = ""; 242 QName innerType = null; 243 244 if (!hasLiteral) { 245 encodingStyle = use.getEncoding(); 246 } 247 248 if (javaType.endsWith("[]")) { 249 if (SchemaUtils.isListWithItemType(type.getNode())) { 250 serializerFactory = 251 "org.apache.axis.encoding.ser.SimpleListSerializerFactory"; 252 deserializerFactory = 253 "org.apache.axis.encoding.ser.SimpleListDeserializerFactory"; 254 } else { 255 serializerFactory = 256 "org.apache.axis.encoding.ser.ArraySerializerFactory"; 257 deserializerFactory = 258 "org.apache.axis.encoding.ser.ArrayDeserializerFactory"; 259 innerType = type.getComponentType(); 260 } 261 } else if ((type.getNode() != null) && (Utils.getEnumerationBaseAndValues( 262 type.getNode(), symbolTable) != null)) { 263 serializerFactory = 264 "org.apache.axis.encoding.ser.EnumSerializerFactory"; 265 deserializerFactory = 266 "org.apache.axis.encoding.ser.EnumDeserializerFactory"; 267 } else if (type.isSimpleType()) { 268 serializerFactory = 269 "org.apache.axis.encoding.ser.SimpleSerializerFactory"; 270 deserializerFactory = 271 "org.apache.axis.encoding.ser.SimpleDeserializerFactory"; 272 } else if (type.getBaseType() != null) { 273 serializerFactory = 274 "org.apache.axis.encoding.ser.SimpleSerializerFactory"; 275 deserializerFactory = 276 "org.apache.axis.encoding.ser.SimpleDeserializerFactory"; 277 } else { 278 serializerFactory = 279 "org.apache.axis.encoding.ser.BeanSerializerFactory"; 280 deserializerFactory = 281 "org.apache.axis.encoding.ser.BeanDeserializerFactory"; 282 } 283 284 if (innerType == null) { 285 writeTypeMapping(pw, namespaceURI, localPart, javaType, 287 serializerFactory, deserializerFactory, 288 encodingStyle); 289 } else { 290 writeArrayTypeMapping(pw, namespaceURI, localPart, javaType, 292 encodingStyle, innerType); 293 } 294 } 295 } 296 } 298 310 protected void writeArrayTypeMapping( 311 PrintWriter pw, String namespaceURI, String localPart, String javaType, String encodingStyle, QName innerType) 312 throws IOException { 313 314 pw.println(" <arrayMapping"); 315 pw.println(" xmlns:ns=\"" + namespaceURI + "\""); 316 pw.println(" qname=\"ns:" + localPart + '"'); 317 pw.println(" type=\"java:" + javaType + '"'); 318 pw.println(" innerType=\"" + Utils.genQNameAttributeString(innerType, "cmp-ns") + '"'); 319 pw.println(" encodingStyle=\"" + encodingStyle + "\""); 320 pw.println(" />"); 321 } 322 323 335 protected void writeTypeMapping( 336 PrintWriter pw, String namespaceURI, String localPart, String javaType, String serializerFactory, String deserializerFactory, String encodingStyle) 337 throws IOException { 338 339 pw.println(" <typeMapping"); 340 pw.println(" xmlns:ns=\"" + namespaceURI + "\""); 341 pw.println(" qname=\"ns:" + localPart + '"'); 342 pw.println(" type=\"java:" + javaType + '"'); 343 pw.println(" serializer=\"" + serializerFactory + "\""); 344 pw.println(" deserializer=\"" + deserializerFactory + "\""); 345 pw.println(" encodingStyle=\"" + encodingStyle + "\""); 346 pw.println(" />"); 347 } 348 349 358 protected void writeDeployPort( 359 PrintWriter pw, Port port, Service service, BindingEntry bEntry) 360 throws IOException { 361 362 String serviceName = port.getName(); 363 boolean hasLiteral = bEntry.hasLiteral(); 364 boolean hasMIME = Utils.hasMIME(bEntry); 365 String prefix = WSDDConstants.NS_PREFIX_WSDD_JAVA; 366 String styleStr = ""; 367 Iterator iterator = 368 bEntry.getBinding().getExtensibilityElements().iterator(); 369 370 while (iterator.hasNext()) { 371 Object obj = iterator.next(); 372 373 if (obj instanceof SOAPBinding) { 374 use = Use.ENCODED; 375 } else if (obj instanceof UnknownExtensibilityElement) { 376 377 UnknownExtensibilityElement unkElement = 379 (UnknownExtensibilityElement) obj; 380 QName name = 381 unkElement.getElementType(); 382 383 if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) 384 && name.getLocalPart().equals("binding")) { 385 use = Use.ENCODED; 386 } 387 } 388 } 389 390 if (symbolTable.isWrapped()) { 391 styleStr = " style=\"" + Style.WRAPPED + "\""; 392 use = Use.LITERAL; 393 } else { 394 styleStr = " style=\"" + bEntry.getBindingStyle().getName() + "\""; 395 396 if (hasLiteral) { 397 use = Use.LITERAL; 398 } 399 } 400 401 String useStr = " use=\"" + use + "\""; 402 403 pw.println(" <service name=\"" + serviceName + "\" provider=\"" 404 + prefix + ":RPC" + "\"" + styleStr + useStr + ">"); 405 pw.println(" <parameter name=\"wsdlTargetNamespace\" value=\"" 406 + service.getQName().getNamespaceURI() + "\"/>"); 407 pw.println(" <parameter name=\"wsdlServiceElement\" value=\"" 408 + service.getQName().getLocalPart() + "\"/>"); 409 if (hasMIME) { 411 pw.println( 412 " <parameter name=\"sendMultiRefs\" value=\"false\"/>"); 413 } 414 ArrayList qualified = new ArrayList (); 415 ArrayList unqualified = new ArrayList (); 416 Map elementFormDefaults = symbolTable.getElementFormDefaults(); 417 for(Iterator it = elementFormDefaults.entrySet().iterator();it.hasNext();){ 418 Map.Entry entry = (Map.Entry ) it.next(); 419 if(entry.getValue().equals("qualified")){ 420 qualified.add(entry.getKey()); 421 } else { 422 unqualified.add(entry.getKey()); 423 } 424 } 425 if(qualified.size()>0){ 426 pw.print(" <parameter name=\"schemaQualified\" value=\""); 427 for(int i=0;i<qualified.size();i++){ 428 pw.print(qualified.get(i)); 429 if(i != qualified.size()-1){ 430 pw.print(','); 431 } 432 } 433 pw.println("\"/>"); 434 } 435 if(unqualified.size()>0){ 436 pw.print(" <parameter name=\"schemaUnqualified\" value=\""); 437 for(int i=0;i<unqualified.size();i++){ 438 pw.print(unqualified.get(i)); 439 if(i != unqualified.size()-1){ 440 pw.print(','); 441 } 442 } 443 pw.println("\"/>"); 444 } 445 pw.println(" <parameter name=\"wsdlServicePort\" value=\"" 446 + serviceName + "\"/>"); 447 448 writeDeployBinding(pw, bEntry); 449 writeDeployTypes(pw, bEntry.getBinding(), hasLiteral, hasMIME, use); 450 pw.println(" </service>"); 451 } 453 460 protected void writeDeployBinding(PrintWriter pw, BindingEntry bEntry) 461 throws IOException { 462 463 Binding binding = bEntry.getBinding(); 464 String className = bEntry.getName(); 465 466 if (emitter.isSkeletonWanted()) { 467 className += "Skeleton"; 468 } else 469 { 470 String customClassName = emitter.getImplementationClassName(); 471 if ( customClassName != null ) 472 className = customClassName; 473 else 474 className += "Impl"; 475 } 476 477 pw.println(" <parameter name=\"className\" value=\"" + className 478 + "\"/>"); 479 480 pw.println(" <parameter name=\"wsdlPortType\" value=\"" 481 + binding.getPortType().getQName().getLocalPart() + "\"/>"); 482 483 pw.println(" <parameter name=\"typeMappingVersion\" value=\"" 484 + emitter.getTypeMappingVersion() + "\"/>"); 485 486 HashSet allowedMethods = new HashSet (); 487 488 String namespaceURI = binding.getQName().getNamespaceURI(); 489 490 if (!emitter.isSkeletonWanted()) { 491 Iterator operationsIterator = 492 binding.getBindingOperations().iterator(); 493 494 for (; operationsIterator.hasNext();) { 495 BindingOperation bindingOper = 496 (BindingOperation) operationsIterator.next(); 497 Operation operation = bindingOper.getOperation(); 498 OperationType type = operation.getStyle(); 499 500 if ((OperationType.NOTIFICATION.equals(type)) 503 || (OperationType.SOLICIT_RESPONSE.equals(type))) { 504 continue; 505 } 506 String javaOperName = null; 507 508 ServiceDesc serviceDesc = emitter.getServiceDesc(); 509 if (emitter.isDeploy() && serviceDesc != null) { 510 OperationDesc[] operDescs = serviceDesc.getOperationsByQName(new QName (namespaceURI, operation.getName())); 512 if (operDescs.length == 0) { 513 log.warn("Can't find operation in the Java Class for WSDL binding operation : " + operation.getName()); 514 continue; 515 } 516 OperationDesc operDesc = operDescs[0]; 517 if (operDesc.getMethod() == null) { 518 log.warn("Can't find Java method for operation descriptor : " + operDesc.getName()); 519 continue; 520 } 521 522 javaOperName = operDesc.getMethod().getName(); 523 } else { 524 javaOperName = 525 JavaUtils.xmlNameToJava(operation.getName()); 526 } 527 528 allowedMethods.add(javaOperName); 529 530 Parameters params = 533 symbolTable.getOperationParameters(operation, "", bEntry); 534 535 if (params != null) { 536 540 QName elementQName = Utils.getOperationQName(bindingOper, 542 bEntry, symbolTable); 543 544 QName returnQName = null; 546 QName returnType = null; 547 548 if (params.returnParam != null) { 549 returnQName = params.returnParam.getQName(); 550 returnType = Utils.getXSIType(params.returnParam); 551 } 552 553 Map faultMap = bEntry.getFaults(); 555 ArrayList faults = null; 556 557 if (faultMap != null) { 558 faults = (ArrayList ) faultMap.get(bindingOper); 559 } 560 561 String SOAPAction = Utils.getOperationSOAPAction(bindingOper); 563 564 writeOperation(pw, javaOperName, elementQName, returnQName, 566 returnType, params, binding.getQName(), 567 faults, SOAPAction); 568 } 569 } 570 } 571 572 pw.print(" <parameter name=\"allowedMethods\" value=\""); 573 574 if (allowedMethods.isEmpty()) { 575 pw.println("*\"/>"); 576 } else { 577 boolean first = true; 578 579 for (Iterator i = allowedMethods.iterator(); i.hasNext();) { 580 String method = (String ) i.next(); 581 582 if (first) { 583 pw.print(method); 584 585 first = false; 586 } else { 587 pw.print(" " + method); 588 } 589 } 590 591 pw.println("\"/>"); 592 } 593 594 Scope scope = emitter.getScope(); 595 596 if (scope != null) { 597 pw.println(" <parameter name=\"scope\" value=\"" 598 + scope.getName() + "\"/>"); 599 } 600 } 602 614 protected void writeOperation(PrintWriter pw, String javaOperName, 615 QName elementQName, QName returnQName, 616 QName returnType, Parameters params, 617 QName bindingQName, ArrayList faults, 618 String SOAPAction) { 619 620 pw.print(" <operation name=\"" + javaOperName + "\""); 621 622 if (elementQName != null) { 623 pw.print(" qname=\"" 624 + Utils.genQNameAttributeString(elementQName, "operNS") 625 + "\""); 626 } 627 628 if (returnQName != null) { 629 pw.print(" returnQName=\"" 630 + Utils.genQNameAttributeStringWithLastLocalPart(returnQName, "retNS") 631 + "\""); 632 } 633 634 if (returnType != null) { 635 pw.print(" returnType=\"" 636 + Utils.genQNameAttributeString(returnType, "rtns") 637 + "\""); 638 } 639 640 Parameter retParam = params.returnParam; 641 if (retParam != null) { 642 TypeEntry type = retParam.getType(); 643 QName returnItemQName = Utils.getItemQName(type); 644 if (returnItemQName != null) { 645 pw.print(" returnItemQName=\""); 646 pw.print(Utils.genQNameAttributeString(returnItemQName, "tns")); 647 pw.print("\""); 648 } 649 QName returnItemType = Utils.getItemType(type); 650 if (returnItemType != null && use == Use.ENCODED) { 651 pw.print(" returnItemType=\""); 652 pw.print(Utils.genQNameAttributeString(returnItemType, "tns2")); 653 pw.print("\""); 654 } 655 } 656 657 if (SOAPAction != null) { 658 pw.print(" soapAction=\"" 659 + SOAPAction 660 + "\""); 661 } 662 663 if (!OperationType.REQUEST_RESPONSE.equals(params.mep)) { 664 String mepString = getMepString(params.mep); 665 if (mepString != null) { 666 pw.print(" mep=\"" 667 + mepString 668 + "\""); 669 } 670 } 671 672 if ((params.returnParam != null) && params.returnParam.isOutHeader()) { 673 pw.print(" returnHeader=\"true\""); 674 } 675 676 pw.println(" >"); 677 678 Vector paramList = params.list; 679 680 for (int i = 0; i < paramList.size(); i++) { 681 Parameter param = (Parameter) paramList.elementAt(i); 682 683 QName paramQName = param.getQName(); 685 QName paramType = Utils.getXSIType(param); 686 687 pw.print(" <parameter"); 688 689 if (paramQName == null) { 690 pw.print(" name=\"" + param.getName() + "\""); 691 } else { 692 pw.print(" qname=\"" 693 + Utils.genQNameAttributeStringWithLastLocalPart(paramQName, "pns") 694 + "\""); 695 } 696 697 pw.print(" type=\"" 698 + Utils.genQNameAttributeString(paramType, "tns") + "\""); 699 700 if (param.getMode() != Parameter.IN) { 702 pw.print(" mode=\"" + getModeString(param.getMode()) + "\""); 703 } 704 705 if (param.isInHeader()) { 707 pw.print(" inHeader=\"true\""); 708 } 709 710 if (param.isOutHeader()) { 711 pw.print(" outHeader=\"true\""); 712 } 713 714 QName itemQName = Utils.getItemQName(param.getType()); 715 if (itemQName != null) { 716 pw.print(" itemQName=\""); 717 pw.print(Utils.genQNameAttributeString(itemQName, "itns")); 718 pw.print("\""); 719 } 720 721 pw.println("/>"); 722 } 723 724 if (faults != null) { 725 for (Iterator iterator = faults.iterator(); iterator.hasNext();) { 726 FaultInfo faultInfo = (FaultInfo) iterator.next(); 727 QName faultQName = faultInfo.getQName(); 728 729 if (faultQName != null) { 730 String className = 731 Utils.getFullExceptionName(faultInfo.getMessage(), 732 symbolTable); 733 734 pw.print(" <fault"); 735 pw.print(" name=\"" + faultInfo.getName() + "\""); 736 pw.print(" qname=\"" 737 + Utils.genQNameAttributeString(faultQName, "fns") 738 + "\""); 739 pw.print(" class=\"" + className + "\""); 740 pw.print( 741 " type=\"" 742 + Utils.genQNameAttributeString( 743 faultInfo.getXMLType(), "tns") + "\""); 744 pw.println("/>"); 745 } 746 } 747 } 748 749 pw.println(" </operation>"); 750 } 751 752 758 public String getModeString(byte mode) { 759 760 if (mode == Parameter.IN) { 761 return "IN"; 762 } else if (mode == Parameter.INOUT) { 763 return "INOUT"; 764 } else { 765 return "OUT"; 766 } 767 } 768 769 776 protected PrintWriter getPrintWriter(String filename) throws IOException { 777 778 File file = new File (filename); 779 File parent = new File (file.getParent()); 780 781 parent.mkdirs(); 782 783 FileOutputStream out = new FileOutputStream (file); 784 OutputStreamWriter writer = new OutputStreamWriter (out, "UTF-8"); 785 786 return new PrintWriter (writer); 787 } 788 789 private static final Map mepStrings = new HashMap (); 790 static { 791 mepStrings.put(OperationType.REQUEST_RESPONSE.toString(), "request-response"); 792 mepStrings.put(OperationType.ONE_WAY.toString(), "oneway"); 793 } 794 795 String getMepString(OperationType mep) { 796 return (String )mepStrings.get(mep.toString()); 797 } 798 } | Popular Tags |