1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.utils.JavaUtils; 19 import org.apache.axis.utils.Messages; 20 import org.apache.axis.utils.WSDLUtils; 21 import org.apache.axis.wsdl.symbolTable.BindingEntry; 22 import org.apache.axis.wsdl.symbolTable.PortTypeEntry; 23 import org.apache.axis.wsdl.symbolTable.ServiceEntry; 24 import org.apache.axis.wsdl.symbolTable.SymbolTable; 25 26 import javax.wsdl.Binding; 27 import javax.wsdl.Port; 28 import javax.wsdl.Service; 29 import javax.xml.namespace.QName ; 30 import java.io.IOException ; 31 import java.io.PrintWriter ; 32 import java.net.MalformedURLException ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.StringTokenizer ; 36 import java.util.Vector ; 37 38 42 public class JavaServiceImplWriter extends JavaClassWriter { 43 44 45 private ServiceEntry sEntry; 46 47 48 private SymbolTable symbolTable; 49 50 57 protected JavaServiceImplWriter(Emitter emitter, ServiceEntry sEntry, 58 SymbolTable symbolTable) { 59 60 super(emitter, sEntry.getName() + "Locator", "service"); 61 62 this.sEntry = sEntry; 63 this.symbolTable = symbolTable; 64 } 66 71 protected String getExtendsText() { 72 return "extends org.apache.axis.client.Service "; 73 } 75 80 protected String getImplementsText() { 81 return "implements " + sEntry.getName() + ' '; 82 } 84 90 protected void writeFileBody(PrintWriter pw) throws IOException { 91 92 Service service = sEntry.getService(); 93 94 writeComment(pw, service.getDocumentationElement(), false); 96 97 Vector getPortIfaces = new Vector (); 99 Vector getPortStubClasses = new Vector (); 100 Vector getPortPortNames = new Vector (); 101 Vector getPortPortXmlNames = new Vector (); 102 boolean printGetPortNotice = false; 103 104 Map portMap = service.getPorts(); 106 Iterator portIterator = portMap.values().iterator(); 107 108 writeConstructors(pw); 110 111 while (portIterator.hasNext()) { 113 Port p = (Port) portIterator.next(); 114 Binding binding = p.getBinding(); 115 116 if (binding == null) { 117 throw new IOException (Messages.getMessage("emitFailNoBinding01", 118 new String []{ 119 p.getName()})); 120 } 121 122 BindingEntry bEntry = 123 symbolTable.getBindingEntry(binding.getQName()); 124 125 if (bEntry == null) { 126 throw new IOException ( 127 Messages.getMessage( 128 "emitFailNoBindingEntry01", 129 new String []{binding.getQName().toString()})); 130 } 131 132 PortTypeEntry ptEntry = 133 symbolTable.getPortTypeEntry(binding.getPortType().getQName()); 134 135 if (ptEntry == null) { 136 throw new IOException ( 137 Messages.getMessage( 138 "emitFailNoPortType01", 139 new String []{ 140 binding.getPortType().getQName().toString()})); 141 } 142 143 if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) { 145 continue; 146 } 147 148 156 String portXmlName = p.getName(); 158 String portName = (String ) bEntry.getDynamicVar(JavaServiceWriter.PORT_NAME + ":" + p.getName()); 159 if (portName == null) { 160 portName = p.getName(); 161 } 162 163 if (!JavaUtils.isJavaId(portName)) { 164 portName = Utils.xmlNameToJavaClass(portName); 165 } 166 167 String stubClass = bEntry.getName() + "Stub"; 168 String bindingType = 169 (String ) bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME); 170 171 if (getPortIfaces.contains(bindingType)) { 174 printGetPortNotice = true; 175 } 176 177 getPortIfaces.add(bindingType); 178 getPortPortXmlNames.add(portXmlName); 179 getPortStubClasses.add(stubClass); 180 getPortPortNames.add(portName); 181 182 String address = WSDLUtils.getAddressFromPort(p); 184 185 if (address == null) { 186 187 throw new IOException (Messages.getMessage("emitFail02", 189 portName, className)); 190 } 191 192 try { 193 new java.net.URL (address); 194 } catch (MalformedURLException e) { 195 196 java.net.URL url = null; 200 java.net.URLStreamHandler handler = null; 201 String handlerPkgs = 202 System.getProperty("java.protocol.handler.pkgs"); 203 204 if (handlerPkgs != null) { 205 int protIndex = address.indexOf(":"); 206 207 if (protIndex > 0) { 208 String protocol = address.substring(0, 209 protIndex); 210 StringTokenizer st = 211 new StringTokenizer (handlerPkgs, "|"); 212 213 while (st.hasMoreTokens()) { 214 String pkg = st.nextToken(); 215 String handlerClass = pkg + "." + protocol 216 + ".Handler"; 217 218 try { 219 Class c = Class.forName(handlerClass); 220 221 handler = 222 (java.net.URLStreamHandler ) c.newInstance(); 223 url = new java.net.URL (null, address, 224 handler); 225 226 break; 227 } catch (Exception e2) { 228 url = null; 229 } 230 } 231 } 232 } 233 234 if (url == null) { 235 if (emitter.isAllowInvalidURL()) { 236 System.err.println(Messages.getMessage("emitWarnInvalidURL01", new String [] {portName, className, address})); 238 } else { 239 throw new IOException (Messages.getMessage("emitFail03", 242 new String []{ 243 portName, 244 className, 245 address})); 246 } 247 } 248 } 249 250 writeAddressInfo(pw, portName, address, p); 251 252 String wsddServiceName = portName + "WSDDServiceName"; 253 254 writeWSDDServiceNameInfo(pw, wsddServiceName, portName, portXmlName); 255 writeGetPortName(pw, bindingType, portName); 256 writeGetPortNameURL(pw, bindingType, portName, stubClass, 257 wsddServiceName); 258 writeSetPortEndpointAddress(pw, portName); 259 } 260 261 writeGetPortClass(pw, getPortIfaces, getPortStubClasses, 262 getPortPortNames, printGetPortNotice); 263 writeGetPortQNameClass(pw, getPortPortNames, getPortPortXmlNames); 264 writeGetServiceName(pw, sEntry.getQName()); 265 writeGetPorts(pw, sEntry.getQName().getNamespaceURI(), getPortPortXmlNames); 266 writeSetEndpointAddress(pw, getPortPortNames); 267 } 269 274 protected void writeConstructors(PrintWriter pw) { 275 pw.println(); 277 pw.println(" public " + Utils.getJavaLocalName(sEntry.getName()) 278 + "Locator() {"); 279 pw.println(" }"); 280 pw.println(); 281 282 pw.println(); 284 pw.println(" public " + Utils.getJavaLocalName(sEntry.getName()) 285 + "Locator(org.apache.axis.EngineConfiguration config) {"); 286 pw.println(" super(config);"); 287 pw.println(" }"); 288 289 pw.println(); 291 pw.println(" public " + Utils.getJavaLocalName(sEntry.getName()) 292 + "Locator(java.lang.String wsdlLoc, javax.xml.namespace.QName sName) " 293 + "throws " + javax.xml.rpc.ServiceException .class.getName() + " {"); 294 pw.println(" super(wsdlLoc, sName);"); 295 pw.println(" }"); 296 } 297 298 306 protected void writeAddressInfo(PrintWriter pw, String portName, 307 String address, Port p) { 308 309 pw.println(); 311 pw.println(" // " + Messages.getMessage("getProxy00", portName)); 312 writeComment(pw, p.getDocumentationElement(), true); 313 pw.println(" private java.lang.String " + portName + "_address = \"" 314 + address + "\";"); 315 316 pw.println(); 318 pw.println(" public java.lang.String get" + portName 319 + "Address() {"); 320 pw.println(" return " + portName + "_address;"); 321 pw.println(" }"); 322 pw.println(); 323 } 325 332 protected void writeWSDDServiceNameInfo(PrintWriter pw, 333 String wsddServiceName, 334 String portName, String portXmlName) { 335 336 pw.println(" // " + Messages.getMessage("wsddServiceName00")); 338 pw.println(" private java.lang.String " + wsddServiceName + " = \"" 339 + portXmlName + "\";"); 340 pw.println(); 341 342 pw.println(" public java.lang.String get" + wsddServiceName 344 + "() {"); 345 pw.println(" return " + wsddServiceName + ";"); 346 pw.println(" }"); 347 pw.println(); 348 pw.println(" public void set" + wsddServiceName 349 + "(java.lang.String name) {"); 350 pw.println(" " + wsddServiceName + " = name;"); 351 pw.println(" }"); 352 pw.println(); 353 } 355 362 protected void writeGetPortName(PrintWriter pw, String bindingType, 363 String portName) { 364 365 pw.println(" public " + bindingType + " get" + portName 366 + "() throws " 367 + javax.xml.rpc.ServiceException .class.getName() + " {"); 368 pw.println(" java.net.URL endpoint;"); 369 pw.println(" try {"); 370 pw.println(" endpoint = new java.net.URL(" + portName 371 + "_address);"); 372 pw.println(" }"); 373 pw.println(" catch (java.net.MalformedURLException e) {"); 374 pw.println(" throw new javax.xml.rpc.ServiceException(e);"); 375 pw.println(" }"); 376 pw.println(" return get" + portName + "(endpoint);"); 377 pw.println(" }"); 378 pw.println(); 379 } 381 390 protected void writeGetPortNameURL(PrintWriter pw, String bindingType, 391 String portName, String stubClass, 392 String wsddServiceName) { 393 394 pw.println(" public " + bindingType + " get" + portName 395 + "(java.net.URL portAddress) throws " 396 + javax.xml.rpc.ServiceException .class.getName() + " {"); 397 pw.println(" try {"); 398 pw.println(" " + stubClass + " _stub = new " + stubClass 399 + "(portAddress, this);"); 400 pw.println(" _stub.setPortName(get" + wsddServiceName 401 + "());"); 402 pw.println(" return _stub;"); 403 pw.println(" }"); 404 pw.println(" catch (org.apache.axis.AxisFault e) {"); 405 pw.println(" return null;"); 406 pw.println(" }"); 407 pw.println(" }"); 408 pw.println(); 409 } 411 417 protected void writeSetPortEndpointAddress(PrintWriter pw, 418 String portName) { 419 420 pw.println(" public void set" + portName 421 + "EndpointAddress(java.lang.String address) {"); 422 pw.println(" " + portName + "_address = address;"); 423 pw.println(" }"); 424 pw.println(); 425 } 427 436 protected void writeGetPortClass(PrintWriter pw, Vector getPortIfaces, 437 Vector getPortStubClasses, 438 Vector getPortPortNames, 439 boolean printGetPortNotice) { 440 441 pw.println(" /**"); 442 pw.println(" * " + Messages.getMessage("getPortDoc00")); 443 pw.println(" * " + Messages.getMessage("getPortDoc01")); 444 pw.println(" * " + Messages.getMessage("getPortDoc02")); 445 446 if (printGetPortNotice) { 447 pw.println(" * " + Messages.getMessage("getPortDoc03")); 448 pw.println(" * " + Messages.getMessage("getPortDoc04")); 449 } 450 451 pw.println(" */"); 452 pw.println( 453 " public java.rmi.Remote getPort(Class serviceEndpointInterface) throws " 454 + javax.xml.rpc.ServiceException .class.getName() + " {"); 455 456 if (getPortIfaces.size() == 0) { 457 pw.println( 458 " throw new " 459 + javax.xml.rpc.ServiceException .class.getName() + "(\"" 460 + Messages.getMessage("noStub") 461 + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));"); 462 } else { 463 pw.println(" try {"); 464 465 for (int i = 0; i < getPortIfaces.size(); ++i) { 466 String iface = (String ) getPortIfaces.get(i); 467 String stubClass = (String ) getPortStubClasses.get(i); 468 String portName = (String ) getPortPortNames.get(i); 469 470 pw.println( 471 " if (" + iface 472 + ".class.isAssignableFrom(serviceEndpointInterface)) {"); 473 pw.println(" " + stubClass + " _stub = new " 474 + stubClass + "(new java.net.URL(" + portName 475 + "_address), this);"); 476 pw.println(" _stub.setPortName(get" + portName 477 + "WSDDServiceName());"); 478 pw.println(" return _stub;"); 479 pw.println(" }"); 480 } 481 482 pw.println(" }"); 483 pw.println(" catch (java.lang.Throwable t) {"); 484 pw.println(" throw new " 485 + javax.xml.rpc.ServiceException .class.getName() 486 + "(t);"); 487 pw.println(" }"); 488 pw.println( 489 " throw new " 490 + javax.xml.rpc.ServiceException .class.getName() + "(\"" 491 + Messages.getMessage("noStub") 492 + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));"); 493 } 494 495 pw.println(" }"); 496 pw.println(); 497 } 499 505 protected void writeGetPortQNameClass(PrintWriter pw, 506 Vector getPortPortNames, 507 Vector getPortPortXmlNames) { 508 509 pw.println(" /**"); 510 pw.println(" * " + Messages.getMessage("getPortDoc00")); 511 pw.println(" * " + Messages.getMessage("getPortDoc01")); 512 pw.println(" * " + Messages.getMessage("getPortDoc02")); 513 pw.println(" */"); 514 pw.println( 515 " public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws " 516 + javax.xml.rpc.ServiceException .class.getName() + " {"); 517 pw.println(" if (portName == null) {"); 518 pw.println(" return getPort(serviceEndpointInterface);"); 519 pw.println(" }"); 520 pw.println(" java.lang.String inputPortName = portName.getLocalPart();"); 521 pw.print(" "); 522 523 for (int i = 0; i < getPortPortNames.size(); ++i) { 524 String portName = (String ) getPortPortNames.get(i); 525 String portXmlName = (String ) getPortPortXmlNames.get(i); 526 527 pw.println("if (\"" + portXmlName + "\".equals(inputPortName)) {"); 528 pw.println(" return get" + portName + "();"); 529 pw.println(" }"); 530 pw.print(" else "); 531 } 532 533 pw.println(" {"); 534 pw.println( 535 " java.rmi.Remote _stub = getPort(serviceEndpointInterface);"); 536 pw.println( 537 " ((org.apache.axis.client.Stub) _stub).setPortName(portName);"); 538 pw.println(" return _stub;"); 539 pw.println(" }"); 540 pw.println(" }"); 541 pw.println(); 542 } 544 550 protected void writeGetServiceName(PrintWriter pw, QName qname) { 551 552 pw.println(" public javax.xml.namespace.QName getServiceName() {"); 553 pw.println(" return " + Utils.getNewQName(qname) + ";"); 554 pw.println(" }"); 555 pw.println(); 556 } 558 564 protected void writeGetPorts(PrintWriter pw, String namespaceURI, Vector portNames) { 565 566 pw.println(" private java.util.HashSet ports = null;"); 567 pw.println(); 568 pw.println(" public java.util.Iterator getPorts() {"); 569 pw.println(" if (ports == null) {"); 570 pw.println(" ports = new java.util.HashSet();"); 571 572 for (int i = 0; i < portNames.size(); ++i) { 573 pw.println(" ports.add(new javax.xml.namespace.QName(\"" + 574 namespaceURI + "\", \"" + portNames.get(i) + "\"));"); 575 } 576 577 pw.println(" }"); 578 pw.println(" return ports.iterator();"); 579 pw.println(" }"); 580 pw.println(); 581 } 583 590 protected void writeSetEndpointAddress(PrintWriter pw, Vector portNames) { 591 592 if (portNames.isEmpty()) { 593 return; 594 } 595 596 pw.println(" /**"); 598 pw.println(" * " + Messages.getMessage("setEndpointDoc00")); 599 pw.println(" */"); 600 pw.println( 601 " public void setEndpointAddress(java.lang.String portName, java.lang.String address) throws " 602 + javax.xml.rpc.ServiceException .class.getName() + " {"); 603 604 pw.println(" "); 605 for (Iterator p = portNames.iterator(); p.hasNext();) { 606 String name = (String ) p.next(); 607 608 pw.println("if (\"" + name + "\".equals(portName)) {"); 609 pw.println(" set" + name + "EndpointAddress(address);"); 610 pw.println(" }"); 611 pw.println(" else "); 612 } 613 614 pw.println("{ // Unknown Port Name"); 615 pw.println(" throw new " 616 + javax.xml.rpc.ServiceException .class.getName() + "(\" " 617 + Messages.getMessage("unknownPortName") 618 + "\" + portName);"); 619 pw.println(" }"); 620 pw.println(" }"); 621 pw.println(); 622 623 pw.println(" /**"); 625 pw.println(" * " + Messages.getMessage("setEndpointDoc00")); 626 pw.println(" */"); 627 pw.println( 628 " public void setEndpointAddress(javax.xml.namespace.QName portName, java.lang.String address) throws " 629 + javax.xml.rpc.ServiceException .class.getName() + " {"); 630 pw.println( 631 " setEndpointAddress(portName.getLocalPart(), address);"); 632 pw.println(" }"); 633 pw.println(); 634 } } | Popular Tags |