KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

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 JavaDoc;
69 import java.io.IOException JavaDoc;
70 import java.io.PrintWriter JavaDoc;
71 import java.net.MalformedURLException JavaDoc;
72 import java.net.URL JavaDoc;
73 import java.util.Iterator JavaDoc;
74 import java.util.Map JavaDoc;
75 import java.util.Vector JavaDoc;
76
77 /**
78  * This is Wsdl2java's service implementation writer.
79  * It writes the <serviceName>Locator.java file.
80  */

81 public class JavaServiceImplWriter extends JavaClassWriter
82 {
83    private ServiceEntry sEntry;
84    private SymbolTable symbolTable;
85
86    /**
87     * Constructor.
88     */

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

98    /**
99     * Returns "extends org.jboss.axis.client.Service ".
100     */

101    protected String JavaDoc getExtendsText()
102    {
103       return "extends org.jboss.axis.client.Service ";
104    } // getExtendsText
105

106    /**
107     * Returns "implements <serviceInterface>".
108     */

109    protected String JavaDoc getImplementsText()
110    {
111       return "implements " + sEntry.getName() + ' ';
112    } // getImplementsText
113

114    /**
115     * Write the body of the service file.
116     */

117    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
118    {
119       Service service = sEntry.getService();
120       // output comments
121
writeComment(pw, service.getDocumentationElement());
122
123       // Used to construct the getPort(Class) method.
124
Vector JavaDoc getPortIfaces = new Vector JavaDoc();
125       Vector JavaDoc getPortStubClasses = new Vector JavaDoc();
126       Vector JavaDoc getPortPortNames = new Vector JavaDoc();
127       boolean printGetPortNotice = false;
128
129       // get ports
130
Map JavaDoc portMap = service.getPorts();
131       Iterator JavaDoc portIterator = portMap.values().iterator();
132
133       // write a get method for each of the ports with a SOAP binding
134
while (portIterator.hasNext())
135       {
136          Port p = (Port)portIterator.next();
137          Binding binding = p.getBinding();
138          if (binding == null)
139          {
140             throw new IOException JavaDoc(Messages.getMessage("emitFailNoBinding01",
141                     new String JavaDoc[]{p.getName()}));
142          }
143
144          BindingEntry bEntry =
145                  symbolTable.getBindingEntry(binding.getQName());
146          if (bEntry == null)
147          {
148             throw new IOException JavaDoc(Messages.getMessage("emitFailNoBindingEntry01",
149                     new String JavaDoc[]{binding.getQName().toString()}));
150          }
151
152          PortTypeEntry ptEntry = symbolTable.getPortTypeEntry(binding.getPortType().getQName());
153          if (ptEntry == null)
154          {
155             throw new IOException JavaDoc(Messages.getMessage("emitFailNoPortType01",
156                     new String JavaDoc[]{binding.getPortType().getQName().toString()}));
157          }
158
159          // If this isn't an SOAP binding, skip it
160
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP)
161          {
162             continue;
163          }
164
165          // JSR 101 indicates that the name of the port used
166
// in the java code is the name of the wsdl:port. It
167
// does not indicate what should occur if the
168
// wsdl:port name is not a java identifier. The
169
// TCK depends on the case-sensitivity being preserved,
170
// and the interop tests have port names that are not
171
// valid java identifiers. Thus the following code.
172
String JavaDoc portName = p.getName();
173          if (!JavaUtils.isJavaId(portName))
174          {
175             portName = Utils.xmlNameToJavaClass(portName);
176          }
177          String JavaDoc stubClass = bEntry.getName() + "Stub";
178
179          String JavaDoc bindingType = (String JavaDoc)bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
180
181          // getPort(Class) must return a stub for an interface. Collect all
182
// the port interfaces so the getPort(Class) method can be constructed.
183
if (getPortIfaces.contains(bindingType))
184          {
185             printGetPortNotice = true;
186          }
187          getPortIfaces.add(bindingType);
188          getPortStubClasses.add(stubClass);
189          getPortPortNames.add(portName);
190
191          // Get endpoint address and validate it
192
String JavaDoc address = WSDLUtils.getAddressFromPort(p);
193          if (address == null)
194          {
195             // now what?
196
throw new IOException JavaDoc(Messages.getMessage("emitFail02",
197                     portName, className));
198          }
199          try
200          {
201             new URL JavaDoc(address);
202          }
203          catch (MalformedURLException JavaDoc e)
204          {
205             throw new IOException JavaDoc(Messages.getMessage("emitFail03",
206                     new String JavaDoc[]{portName, className, address}));
207          }
208          writeAddressInfo(pw, portName, address, p);
209          String JavaDoc 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    } // writeFileBody
221

222    /**
223     * Write the private address field for this port and the public getter for it.
224     */

225    protected void writeAddressInfo(PrintWriter JavaDoc pw, String JavaDoc portName,
226                                    String JavaDoc address, Port p)
227    {
228       // Write the private address field for this port
229
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       // Write the public address getter for this field
235
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    } // writeAddressInfo
242

243    /**
244     * Write the private WSDD service name field and the public accessors for it.
245     */

246    protected void writeWSDDServiceNameInfo(PrintWriter JavaDoc pw,
247                                            String JavaDoc wsddServiceName, String JavaDoc portName)
248    {
249       // Write the private WSDD service name field
250
pw.println(" // " + Messages.getMessage("wsddServiceName00"));
251       pw.println(" private java.lang.String " + wsddServiceName + " = \"" + portName + "\";");
252       pw.println();
253
254       // Write the public accessors for the WSDD service name
255
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    } // writeWSDDServiceNameInfo
264

265    /**
266     * Write the get<portName>() method.
267     */

268    protected void writeGetPortName(PrintWriter JavaDoc pw, String JavaDoc bindingType,
269                                    String JavaDoc portName)
270    {
271       pw.println(" public " + bindingType + " get" + portName + "() throws " + javax.xml.rpc.ServiceException JavaDoc.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    } // writeGetPortName
283

284    /**
285     * Write the get<portName>(URL) method.
286     */

287    protected void writeGetPortNameURL(PrintWriter JavaDoc pw, String JavaDoc bindingType,
288                                       String JavaDoc portName, String JavaDoc stubClass, String JavaDoc wsddServiceName)
289    {
290       pw.println(" public " + bindingType + " get" + portName + "(java.net.URL portAddress) throws " + javax.xml.rpc.ServiceException JavaDoc.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    } // writeGetPortNameURL
302

303    /**
304     * Write the getPort(Class serviceInterfaceWriter) method.
305     */

306    protected void writeGetPortClass(PrintWriter JavaDoc pw, Vector JavaDoc getPortIfaces,
307                                     Vector JavaDoc getPortStubClasses, Vector JavaDoc 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 JavaDoc.class.getName() + " {");
321       if (getPortIfaces.size() == 0)
322       {
323          pw.println(" throw new " + javax.xml.rpc.ServiceException JavaDoc.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 JavaDoc iface = (String JavaDoc)getPortIfaces.get(i);
332             String JavaDoc stubClass = (String JavaDoc)getPortStubClasses.get(i);
333             String JavaDoc portName = (String JavaDoc)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 JavaDoc.class.getName() + "(t);");
343          pw.println(" }");
344          pw.println(" throw new " + javax.xml.rpc.ServiceException JavaDoc.class.getName() + "(\""
345                  + Messages.getMessage("noStub") + " \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));");
346       }
347       pw.println(" }");
348       pw.println();
349
350    } // writeGetPortClass
351

352    /**
353     * Write the getPort(QName portName, Class serviceInterfaceWriter) method.
354     */

355    protected void writeGetPortQNameClass(PrintWriter JavaDoc pw,
356                                          Vector JavaDoc 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 JavaDoc.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 JavaDoc portName = (String JavaDoc)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    } // writeGetPortQNameClass
385

386    /**
387     * Write the getServiceName method.
388     */

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

397    /**
398     * Write the getPorts method.
399     */

400    protected void writeGetPorts(PrintWriter JavaDoc pw, Vector JavaDoc 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    } // writeGetPorts
417

418 } // class JavaServiceImplWriter
419
Popular Tags