KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > toJava > JavaServiceImplWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36 import java.util.Vector JavaDoc;
37
38 /**
39  * This is Wsdl2java's service implementation writer.
40  * It writes the <serviceName>Locator.java file.
41  */

42 public class JavaServiceImplWriter extends JavaClassWriter {
43
44     /** Field sEntry */
45     private ServiceEntry sEntry;
46
47     /** Field symbolTable */
48     private SymbolTable symbolTable;
49
50     /**
51      * Constructor.
52      *
53      * @param emitter
54      * @param sEntry
55      * @param symbolTable
56      */

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     } // ctor
65

66     /**
67      * Returns "extends org.apache.axis.client.Service ".
68      *
69      * @return
70      */

71     protected String JavaDoc getExtendsText() {
72         return "extends org.apache.axis.client.Service ";
73     } // getExtendsText
74

75     /**
76      * Returns "implements <serviceInterface>".
77      *
78      * @return
79      */

80     protected String JavaDoc getImplementsText() {
81         return "implements " + sEntry.getName() + ' ';
82     } // getImplementsText
83

84     /**
85      * Write the body of the service file.
86      *
87      * @param pw
88      * @throws IOException
89      */

90     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
91
92         Service service = sEntry.getService();
93
94         // output comments
95
writeComment(pw, service.getDocumentationElement(), false);
96
97         // Used to construct the getPort(Class) method.
98
Vector JavaDoc getPortIfaces = new Vector JavaDoc();
99         Vector JavaDoc getPortStubClasses = new Vector JavaDoc();
100         Vector JavaDoc getPortPortNames = new Vector JavaDoc();
101         Vector JavaDoc getPortPortXmlNames = new Vector JavaDoc();
102         boolean printGetPortNotice = false;
103
104         // get ports
105
Map JavaDoc portMap = service.getPorts();
106         Iterator JavaDoc portIterator = portMap.values().iterator();
107
108         //Write the constructor for <servicename>Locator
109
writeConstructors(pw);
110         
111         // write a get method for each of the ports with a SOAP binding
112
while (portIterator.hasNext()) {
113             Port p = (Port) portIterator.next();
114             Binding binding = p.getBinding();
115
116             if (binding == null) {
117                 throw new IOException JavaDoc(Messages.getMessage("emitFailNoBinding01",
118                         new String JavaDoc[]{
119                             p.getName()}));
120             }
121
122             BindingEntry bEntry =
123                     symbolTable.getBindingEntry(binding.getQName());
124
125             if (bEntry == null) {
126                 throw new IOException JavaDoc(
127                         Messages.getMessage(
128                                 "emitFailNoBindingEntry01",
129                                 new String JavaDoc[]{binding.getQName().toString()}));
130             }
131
132             PortTypeEntry ptEntry =
133                     symbolTable.getPortTypeEntry(binding.getPortType().getQName());
134
135             if (ptEntry == null) {
136                 throw new IOException JavaDoc(
137                         Messages.getMessage(
138                                 "emitFailNoPortType01",
139                                 new String JavaDoc[]{
140                                     binding.getPortType().getQName().toString()}));
141             }
142
143             // If this isn't an SOAP binding, skip it
144
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
145                 continue;
146             }
147
148             // JSR 101 indicates that the name of the port used
149
// in the java code is the name of the wsdl:port. It
150
// does not indicate what should occur if the
151
// wsdl:port name is not a java identifier. The
152
// TCK depends on the case-sensitivity being preserved,
153
// and the interop tests have port names that are not
154
// valid java identifiers. Thus the following code.
155

156             // java port <--> wsdl port mapping
157
String JavaDoc portXmlName = p.getName();
158             String JavaDoc portName = (String JavaDoc) 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 JavaDoc stubClass = bEntry.getName() + "Stub";
168             String JavaDoc bindingType =
169                     (String JavaDoc) bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
170
171             // getPort(Class) must return a stub for an interface. Collect all
172
// the port interfaces so the getPort(Class) method can be constructed.
173
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             // Get endpoint address and validate it
183
String JavaDoc address = WSDLUtils.getAddressFromPort(p);
184
185             if (address == null) {
186
187                 // now what?
188
throw new IOException JavaDoc(Messages.getMessage("emitFail02",
189                         portName, className));
190             }
191
192             try {
193                 new java.net.URL JavaDoc(address);
194             } catch (MalformedURLException JavaDoc e) {
195
196                 // this exception may be due to an unrecognized protocol
197
// so try to instantiate the protocol handler directly
198
// and use that to create the URL
199
java.net.URL JavaDoc url = null;
200                 java.net.URLStreamHandler JavaDoc handler = null;
201                 String JavaDoc 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 JavaDoc protocol = address.substring(0,
209                                 protIndex);
210                         StringTokenizer JavaDoc st =
211                                 new StringTokenizer JavaDoc(handlerPkgs, "|");
212
213                         while (st.hasMoreTokens()) {
214                             String JavaDoc pkg = st.nextToken();
215                             String JavaDoc handlerClass = pkg + "." + protocol
216                                     + ".Handler";
217
218                             try {
219                                 Class JavaDoc c = Class.forName(handlerClass);
220
221                                 handler =
222                                         (java.net.URLStreamHandler JavaDoc) c.newInstance();
223                                 url = new java.net.URL JavaDoc(null, address,
224                                         handler);
225
226                                 break;
227                             } catch (Exception JavaDoc e2) {
228                                 url = null;
229                             }
230                         }
231                     }
232                 }
233
234                 if (url == null) {
235                     if (emitter.isAllowInvalidURL()) {
236                         // Unchecked URL mode
237
System.err.println(Messages.getMessage("emitWarnInvalidURL01", new String JavaDoc[] {portName, className, address}));
238                     } else {
239                         // Checked URL mode :
240
// URL invalid -> Exception
241
throw new IOException JavaDoc(Messages.getMessage("emitFail03",
242                                 new String JavaDoc[]{
243                                     portName,
244                                     className,
245                                     address}));
246                     }
247                 }
248             }
249
250             writeAddressInfo(pw, portName, address, p);
251
252             String JavaDoc 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     } // writeFileBody
268

269     /**
270      * write Constructors
271      *
272      * @param pw
273      */

274     protected void writeConstructors(PrintWriter JavaDoc pw) {
275         //Write the default constructor
276
pw.println();
277         pw.println(" public " + Utils.getJavaLocalName(sEntry.getName())
278                 + "Locator() {");
279         pw.println(" }");
280         pw.println();
281         
282         //Write a constructor that accepts an Engine Configuration
283
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         //Write a constructor that accepts String wsdl location + Service QName
290
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 JavaDoc.class.getName() + " {");
294         pw.println(" super(wsdlLoc, sName);");
295         pw.println(" }");
296     }
297
298     /**
299      * Write the private address field for this port and the public getter for it.
300      *
301      * @param pw
302      * @param portName
303      * @param address
304      * @param p
305      */

306     protected void writeAddressInfo(PrintWriter JavaDoc pw, String JavaDoc portName,
307                                     String JavaDoc address, Port p) {
308
309         // Write the private address field for this port
310
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         // Write the public address getter for this field
317
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     } // writeAddressInfo
324

325     /**
326      * Write the private WSDD service name field and the public accessors for it.
327      *
328      * @param pw
329      * @param wsddServiceName
330      * @param portName
331      */

332     protected void writeWSDDServiceNameInfo(PrintWriter JavaDoc pw,
333                                             String JavaDoc wsddServiceName,
334                                             String JavaDoc portName, String JavaDoc portXmlName) {
335
336         // Write the private WSDD service name field
337
pw.println(" // " + Messages.getMessage("wsddServiceName00"));
338         pw.println(" private java.lang.String " + wsddServiceName + " = \""
339                 + portXmlName + "\";");
340         pw.println();
341
342         // Write the public accessors for the WSDD service name
343
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     } // writeWSDDServiceNameInfo
354

355     /**
356      * Write the get<portName>() method.
357      *
358      * @param pw
359      * @param bindingType
360      * @param portName
361      */

362     protected void writeGetPortName(PrintWriter JavaDoc pw, String JavaDoc bindingType,
363                                     String JavaDoc portName) {
364
365         pw.println(" public " + bindingType + " get" + portName
366                 + "() throws "
367                 + javax.xml.rpc.ServiceException JavaDoc.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     } // writeGetPortName
380

381     /**
382      * Write the get<portName>(URL) method.
383      *
384      * @param pw
385      * @param bindingType
386      * @param portName
387      * @param stubClass
388      * @param wsddServiceName
389      */

390     protected void writeGetPortNameURL(PrintWriter JavaDoc pw, String JavaDoc bindingType,
391                                        String JavaDoc portName, String JavaDoc stubClass,
392                                        String JavaDoc wsddServiceName) {
393
394         pw.println(" public " + bindingType + " get" + portName
395                 + "(java.net.URL portAddress) throws "
396                 + javax.xml.rpc.ServiceException JavaDoc.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     } // writeGetPortNameURL
410

411     /**
412      * Write the set<portName>EndpointAddress(String) method.
413      *
414      * @param pw
415      * @param portName
416      */

417     protected void writeSetPortEndpointAddress(PrintWriter JavaDoc pw,
418                                                String JavaDoc 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     } // writeSetPortEndpointAddress
426

427     /**
428      * Write the getPort(Class serviceInterfaceWriter) method.
429      *
430      * @param pw
431      * @param getPortIfaces
432      * @param getPortStubClasses
433      * @param getPortPortNames
434      * @param printGetPortNotice
435      */

436     protected void writeGetPortClass(PrintWriter JavaDoc pw, Vector JavaDoc getPortIfaces,
437                                      Vector JavaDoc getPortStubClasses,
438                                      Vector JavaDoc 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 JavaDoc.class.getName() + " {");
455
456         if (getPortIfaces.size() == 0) {
457             pw.println(
458                     " throw new "
459                     + javax.xml.rpc.ServiceException JavaDoc.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 JavaDoc iface = (String JavaDoc) getPortIfaces.get(i);
467                 String JavaDoc stubClass = (String JavaDoc) getPortStubClasses.get(i);
468                 String JavaDoc portName = (String JavaDoc) 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 JavaDoc.class.getName()
486                     + "(t);");
487             pw.println(" }");
488             pw.println(
489                     " throw new "
490                     + javax.xml.rpc.ServiceException JavaDoc.class.getName() + "(\""
491                     + Messages.getMessage("noStub")
492                     + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));");
493         }
494
495         pw.println(" }");
496         pw.println();
497     } // writeGetPortClass
498

499     /**
500      * Write the getPort(QName portName, Class serviceInterfaceWriter) method.
501      *
502      * @param pw
503      * @param getPortPortNames
504      */

505     protected void writeGetPortQNameClass(PrintWriter JavaDoc pw,
506                                           Vector JavaDoc getPortPortNames,
507             Vector JavaDoc 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 JavaDoc.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 JavaDoc portName = (String JavaDoc) getPortPortNames.get(i);
525             String JavaDoc portXmlName = (String JavaDoc) 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     } // writeGetPortQNameClass
543

544     /**
545      * Write the getServiceName method.
546      *
547      * @param pw
548      * @param qname
549      */

550     protected void writeGetServiceName(PrintWriter JavaDoc pw, QName JavaDoc 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     } // writeGetServiceName
557

558     /**
559      * Write the getPorts method.
560      *
561      * @param pw
562      * @param portNames
563      */

564     protected void writeGetPorts(PrintWriter JavaDoc pw, String JavaDoc namespaceURI, Vector JavaDoc 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     } // writeGetPorts
582

583     /**
584      * Write the setEndpointAddress(String portName, String newAddress)
585      * and setEndpointAddress(QName portName, String newAddress) methods.
586      *
587      * @param pw
588      * @param portNames
589      */

590     protected void writeSetEndpointAddress(PrintWriter JavaDoc pw, Vector JavaDoc portNames) {
591
592         if (portNames.isEmpty()) {
593             return;
594         }
595
596         // String method
597
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 JavaDoc.class.getName() + " {");
603
604         pw.println(" ");
605         for (Iterator JavaDoc p = portNames.iterator(); p.hasNext();) {
606             String JavaDoc name = (String JavaDoc) 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 JavaDoc.class.getName() + "(\" "
617                 + Messages.getMessage("unknownPortName")
618                 + "\" + portName);");
619         pw.println(" }");
620         pw.println(" }");
621         pw.println();
622
623         // QName method
624
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 JavaDoc.class.getName() + " {");
630         pw.println(
631                 " setEndpointAddress(portName.getLocalPart(), address);");
632         pw.println(" }");
633         pw.println();
634     } // writeSetEndpointAddress
635
} // class JavaServiceImplWriter
636
Popular Tags