1 55 package org.jboss.axis.wsdl.toJava; 56 57 import org.jboss.axis.utils.JavaUtils; 58 import org.jboss.axis.utils.Messages; 59 import org.jboss.axis.utils.WSDLUtils; 60 import org.jboss.axis.wsdl.symbolTable.BindingEntry; 61 import org.jboss.axis.wsdl.symbolTable.PortTypeEntry; 62 import org.jboss.axis.wsdl.symbolTable.ServiceEntry; 63 import org.jboss.axis.wsdl.symbolTable.SymbolTable; 64 65 import javax.wsdl.Binding; 66 import javax.wsdl.Port; 67 import javax.wsdl.Service; 68 import javax.xml.namespace.QName ; 69 import java.io.IOException ; 70 import java.io.PrintWriter ; 71 import java.net.MalformedURLException ; 72 import java.net.URL ; 73 import java.util.Iterator ; 74 import java.util.Map ; 75 import java.util.Vector ; 76 77 81 public class JavaServiceImplWriter extends JavaClassWriter 82 { 83 private ServiceEntry sEntry; 84 private SymbolTable symbolTable; 85 86 89 protected JavaServiceImplWriter(Emitter emitter, 90 ServiceEntry sEntry, 91 SymbolTable symbolTable) 92 { 93 super(emitter, sEntry.getName() + "Locator", "service"); 94 this.sEntry = sEntry; 95 this.symbolTable = symbolTable; 96 } 98 101 protected String getExtendsText() 102 { 103 return "extends org.jboss.axis.client.Service "; 104 } 106 109 protected String getImplementsText() 110 { 111 return "implements " + sEntry.getName() + ' '; 112 } 114 117 protected void writeFileBody(PrintWriter pw) throws IOException 118 { 119 Service service = sEntry.getService(); 120 writeComment(pw, service.getDocumentationElement()); 122 123 Vector getPortIfaces = new Vector (); 125 Vector getPortStubClasses = new Vector (); 126 Vector getPortPortNames = new Vector (); 127 boolean printGetPortNotice = false; 128 129 Map portMap = service.getPorts(); 131 Iterator portIterator = portMap.values().iterator(); 132 133 while (portIterator.hasNext()) 135 { 136 Port p = (Port)portIterator.next(); 137 Binding binding = p.getBinding(); 138 if (binding == null) 139 { 140 throw new IOException (Messages.getMessage("emitFailNoBinding01", 141 new String []{p.getName()})); 142 } 143 144 BindingEntry bEntry = 145 symbolTable.getBindingEntry(binding.getQName()); 146 if (bEntry == null) 147 { 148 throw new IOException (Messages.getMessage("emitFailNoBindingEntry01", 149 new String []{binding.getQName().toString()})); 150 } 151 152 PortTypeEntry ptEntry = symbolTable.getPortTypeEntry(binding.getPortType().getQName()); 153 if (ptEntry == null) 154 { 155 throw new IOException (Messages.getMessage("emitFailNoPortType01", 156 new String []{binding.getPortType().getQName().toString()})); 157 } 158 159 if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) 161 { 162 continue; 163 } 164 165 String portName = p.getName(); 173 if (!JavaUtils.isJavaId(portName)) 174 { 175 portName = Utils.xmlNameToJavaClass(portName); 176 } 177 String stubClass = bEntry.getName() + "Stub"; 178 179 String bindingType = (String )bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME); 180 181 if (getPortIfaces.contains(bindingType)) 184 { 185 printGetPortNotice = true; 186 } 187 getPortIfaces.add(bindingType); 188 getPortStubClasses.add(stubClass); 189 getPortPortNames.add(portName); 190 191 String address = WSDLUtils.getAddressFromPort(p); 193 if (address == null) 194 { 195 throw new IOException (Messages.getMessage("emitFail02", 197 portName, className)); 198 } 199 try 200 { 201 new URL (address); 202 } 203 catch (MalformedURLException e) 204 { 205 throw new IOException (Messages.getMessage("emitFail03", 206 new String []{portName, className, address})); 207 } 208 writeAddressInfo(pw, portName, address, p); 209 String wsddServiceName = portName + "WSDDServiceName"; 210 writeWSDDServiceNameInfo(pw, wsddServiceName, portName); 211 writeGetPortName(pw, bindingType, portName); 212 writeGetPortNameURL(pw, bindingType, portName, stubClass, 213 wsddServiceName); 214 } 215 writeGetPortClass(pw, getPortIfaces, getPortStubClasses, 216 getPortPortNames, printGetPortNotice); 217 writeGetPortQNameClass(pw, getPortPortNames); 218 writeGetServiceName(pw, sEntry.getQName()); 219 writeGetPorts(pw, getPortPortNames); 220 } 222 225 protected void writeAddressInfo(PrintWriter pw, String portName, 226 String address, Port p) 227 { 228 pw.println(); 230 pw.println(" // " + Messages.getMessage("getProxy00", portName)); 231 writeComment(pw, p.getDocumentationElement()); 232 pw.println(" private final java.lang.String " + portName + "_address = \"" + address + "\";"); 233 234 pw.println(); 236 pw.println(" public java.lang.String get" + portName + "Address() {"); 237 pw.println(" return " + portName + "_address;"); 238 pw.println(" }"); 239 pw.println(); 240 241 } 243 246 protected void writeWSDDServiceNameInfo(PrintWriter pw, 247 String wsddServiceName, String portName) 248 { 249 pw.println(" // " + Messages.getMessage("wsddServiceName00")); 251 pw.println(" private java.lang.String " + wsddServiceName + " = \"" + portName + "\";"); 252 pw.println(); 253 254 pw.println(" public java.lang.String get" + wsddServiceName + "() {"); 256 pw.println(" return " + wsddServiceName + ";"); 257 pw.println(" }"); 258 pw.println(); 259 pw.println(" public void set" + wsddServiceName + "(java.lang.String name) {"); 260 pw.println(" " + wsddServiceName + " = name;"); 261 pw.println(" }"); 262 pw.println(); 263 } 265 268 protected void writeGetPortName(PrintWriter pw, String bindingType, 269 String portName) 270 { 271 pw.println(" public " + bindingType + " get" + portName + "() throws " + javax.xml.rpc.ServiceException .class.getName() + " {"); 272 pw.println(" java.net.URL endpoint;"); 273 pw.println(" try {"); 274 pw.println(" endpoint = new java.net.URL(" + portName + "_address);"); 275 pw.println(" }"); 276 pw.println(" catch (java.net.MalformedURLException e) {"); 277 pw.println(" throw new javax.xml.rpc.ServiceException(e);"); 278 pw.println(" }"); 279 pw.println(" return get" + portName + "(endpoint);"); 280 pw.println(" }"); 281 pw.println(); 282 } 284 287 protected void writeGetPortNameURL(PrintWriter pw, String bindingType, 288 String portName, String stubClass, String wsddServiceName) 289 { 290 pw.println(" public " + bindingType + " get" + portName + "(java.net.URL portAddress) throws " + javax.xml.rpc.ServiceException .class.getName() + " {"); 291 pw.println(" try {"); 292 pw.println(" " + stubClass + " _stub = new " + stubClass + "(portAddress, this);"); 293 pw.println(" _stub.setPortName(get" + wsddServiceName + "());"); 294 pw.println(" return _stub;"); 295 pw.println(" }"); 296 pw.println(" catch (org.jboss.axis.AxisFault e) {"); 297 pw.println(" return null;"); 298 pw.println(" }"); 299 pw.println(" }"); 300 pw.println(); 301 } 303 306 protected void writeGetPortClass(PrintWriter pw, Vector getPortIfaces, 307 Vector getPortStubClasses, Vector getPortPortNames, 308 boolean printGetPortNotice) 309 { 310 pw.println(" /**"); 311 pw.println(" * " + Messages.getMessage("getPortDoc00")); 312 pw.println(" * " + Messages.getMessage("getPortDoc01")); 313 pw.println(" * " + Messages.getMessage("getPortDoc02")); 314 if (printGetPortNotice) 315 { 316 pw.println(" * " + Messages.getMessage("getPortDoc03")); 317 pw.println(" * " + Messages.getMessage("getPortDoc04")); 318 } 319 pw.println(" */"); 320 pw.println(" public java.rmi.Remote getPort(Class serviceEndpointInterface) throws " + javax.xml.rpc.ServiceException .class.getName() + " {"); 321 if (getPortIfaces.size() == 0) 322 { 323 pw.println(" throw new " + javax.xml.rpc.ServiceException .class.getName() + "(\"" 324 + Messages.getMessage("noStub") + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));"); 325 } 326 else 327 { 328 pw.println(" try {"); 329 for (int i = 0; i < getPortIfaces.size(); ++i) 330 { 331 String iface = (String )getPortIfaces.get(i); 332 String stubClass = (String )getPortStubClasses.get(i); 333 String portName = (String )getPortPortNames.get(i); 334 pw.println(" if (" + iface + ".class.isAssignableFrom(serviceEndpointInterface)) {"); 335 pw.println(" " + stubClass + " _stub = new " + stubClass + "(new java.net.URL(" + portName + "_address), this);"); 336 pw.println(" _stub.setPortName(get" + portName + "WSDDServiceName());"); 337 pw.println(" return _stub;"); 338 pw.println(" }"); 339 } 340 pw.println(" }"); 341 pw.println(" catch (java.lang.Throwable t) {"); 342 pw.println(" throw new " + javax.xml.rpc.ServiceException .class.getName() + "(t);"); 343 pw.println(" }"); 344 pw.println(" throw new " + javax.xml.rpc.ServiceException .class.getName() + "(\"" 345 + Messages.getMessage("noStub") + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));"); 346 } 347 pw.println(" }"); 348 pw.println(); 349 350 } 352 355 protected void writeGetPortQNameClass(PrintWriter pw, 356 Vector getPortPortNames) 357 { 358 pw.println(" /**"); 359 pw.println(" * " + Messages.getMessage("getPortDoc00")); 360 pw.println(" * " + Messages.getMessage("getPortDoc01")); 361 pw.println(" * " + Messages.getMessage("getPortDoc02")); 362 pw.println(" */"); 363 pw.println(" public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws " + javax.xml.rpc.ServiceException .class.getName() + " {"); 364 pw.println(" if (portName == null) {"); 365 pw.println(" return getPort(serviceEndpointInterface);"); 366 pw.println(" }"); 367 pw.println(" String inputPortName = portName.getLocalPart();"); 368 pw.print(" "); 369 for (int i = 0; i < getPortPortNames.size(); ++i) 370 { 371 String portName = (String )getPortPortNames.get(i); 372 pw.println("if (\"" + portName + "\".equals(inputPortName)) {"); 373 pw.println(" return get" + portName + "();"); 374 pw.println(" }"); 375 pw.print(" else "); 376 } 377 pw.println(" {"); 378 pw.println(" java.rmi.Remote _stub = getPort(serviceEndpointInterface);"); 379 pw.println(" ((org.jboss.axis.client.Stub) _stub).setPortName(portName);"); 380 pw.println(" return _stub;"); 381 pw.println(" }"); 382 pw.println(" }"); 383 pw.println(); 384 } 386 389 protected void writeGetServiceName(PrintWriter pw, QName qname) 390 { 391 pw.println(" public javax.xml.namespace.QName getServiceName() {"); 392 pw.println(" return " + Utils.getNewQName(qname) + ";"); 393 pw.println(" }"); 394 pw.println(); 395 } 397 400 protected void writeGetPorts(PrintWriter pw, Vector portNames) 401 { 402 pw.println(" private java.util.HashSet ports = null;"); 403 pw.println(); 404 pw.println(" public java.util.Iterator getPorts() {"); 405 pw.println(" if (ports == null) {"); 406 pw.println(" ports = new java.util.HashSet();"); 407 for (int i = 0; i < portNames.size(); ++i) 408 { 409 pw.println(" ports.add(new javax.xml.namespace.QName(\"" + 410 portNames.get(i) + "\"));"); 411 } 412 pw.println(" }"); 413 pw.println(" return ports.iterator();"); 414 pw.println(" }"); 415 pw.println(); 416 } 418 } | Popular Tags |