KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
58 import org.jboss.axis.utils.Messages;
59 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
60 import org.jboss.axis.wsdl.symbolTable.FaultInfo;
61 import org.jboss.axis.wsdl.symbolTable.Parameter;
62 import org.jboss.axis.wsdl.symbolTable.Parameters;
63 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
64
65 import javax.wsdl.Binding;
66 import javax.wsdl.BindingInput;
67 import javax.wsdl.BindingOperation;
68 import javax.wsdl.BindingOutput;
69 import javax.wsdl.Operation;
70 import javax.wsdl.OperationType;
71 import javax.wsdl.extensions.ExtensibilityElement;
72 import javax.wsdl.extensions.UnknownExtensibilityElement;
73 import javax.wsdl.extensions.soap.SOAPBody;
74 import javax.wsdl.extensions.soap.SOAPOperation;
75 import javax.xml.namespace.QName JavaDoc;
76 import java.io.IOException JavaDoc;
77 import java.io.PrintWriter JavaDoc;
78 import java.util.ArrayList JavaDoc;
79 import java.util.Iterator JavaDoc;
80 import java.util.List JavaDoc;
81
82 /**
83  * This is Wsdl2java's skeleton writer. It writes the <BindingName>Skeleton.java
84  * file which contains the <bindingName>Skeleton class.
85  */

86 public class JavaSkelWriter extends JavaClassWriter
87 {
88    private BindingEntry bEntry;
89    private Binding binding;
90    private SymbolTable symbolTable;
91
92    /**
93     * Constructor.
94     */

95    protected JavaSkelWriter(Emitter emitter,
96                             BindingEntry bEntry,
97                             SymbolTable symbolTable)
98    {
99       super(emitter, bEntry.getName() + "Skeleton", "skeleton");
100       this.bEntry = bEntry;
101       this.binding = bEntry.getBinding();
102       this.symbolTable = symbolTable;
103    } // ctor
104

105    /**
106     * Returns "implements <SEI>, org.jboss.axis.wsdl.Skeleton ".
107     */

108    protected String JavaDoc getImplementsText()
109    {
110       return "implements " + bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME)
111               + ", org.jboss.axis.wsdl.Skeleton ";
112    } // getImplementsText
113

114    /**
115     * Write the body of the binding's stub file.
116     */

117    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
118    {
119       String JavaDoc portTypeName = (String JavaDoc)bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
120       String JavaDoc implType = portTypeName + " impl";
121
122       // Declare private impl and skeleton base delegates
123
pw.println(" private " + implType + ";");
124       pw.println(" private static java.util.Map _myOperations = new java.util.Hashtable();");
125       pw.println(" private static java.util.Collection _myOperationsList = new java.util.ArrayList();");
126       pw.println();
127       pw.println(" /**");
128       pw.println(" * Returns List of OperationDesc objects with this name");
129       pw.println(" */");
130       pw.println(" public static java.util.List getOperationDescByName(java.lang.String methodName) {");
131       pw.println(" return (java.util.List)_myOperations.get(methodName);");
132       pw.println(" }");
133       pw.println();
134       pw.println(" /**");
135       pw.println(" * Returns Collection of OperationDescs");
136       pw.println(" */");
137       pw.println(" public static java.util.Collection getOperationDescs() {");
138       pw.println(" return _myOperationsList;");
139       pw.println(" }");
140       pw.println();
141
142       // Initialize operation parameter names
143
pw.println(" static {");
144       pw.println(" org.jboss.axis.description.OperationDesc _oper;");
145       pw.println(" org.jboss.axis.description.FaultDesc _fault;");
146       pw.println(" org.jboss.axis.description.ParameterDesc [] _params;");
147       List JavaDoc operations = binding.getBindingOperations();
148       for (int i = 0; i < operations.size(); ++i)
149       {
150          BindingOperation bindingOper = (BindingOperation)operations.get(i);
151          Operation operation = bindingOper.getOperation();
152          OperationType type = operation.getStyle();
153          // These operation types are not supported. The signature
154
// will be a string stating that fact.
155
if (type == OperationType.NOTIFICATION
156                  || type == OperationType.SOLICIT_RESPONSE)
157          {
158             continue;
159          }
160
161          Parameters parameters =
162                  bEntry.getParameters(bindingOper.getOperation());
163
164          if (parameters != null)
165          {
166             // The invoked java name of the bindingOper is stored.
167
String JavaDoc opName = bindingOper.getOperation().getName();
168             String JavaDoc javaOpName = Utils.xmlNameToJava(opName);
169             pw.println(" _params = new org.jboss.axis.description.ParameterDesc [] {");
170
171             for (int j = 0; j < parameters.list.size(); j++)
172             {
173                Parameter p = (Parameter)parameters.list.get(j);
174                String JavaDoc modeStr;
175                switch (p.getMode())
176                {
177                   case Parameter.IN:
178                      modeStr = "org.jboss.axis.description.ParameterDesc.IN";
179                      break;
180                   case Parameter.OUT:
181                      modeStr = "org.jboss.axis.description.ParameterDesc.OUT";
182                      break;
183                   case Parameter.INOUT:
184                      modeStr = "org.jboss.axis.description.ParameterDesc.INOUT";
185                      break;
186                   default:
187                      throw new IOException JavaDoc(Messages.getMessage("badParmMode00",
188                              (new Byte JavaDoc(p.getMode())).toString()));
189                }
190
191                // Get the QNames representing the parameter name and type
192
QName JavaDoc paramName = p.getQName();
193                QName JavaDoc paramType = Utils.getXSIType(p);
194
195                // Is this parameter a header?
196
String JavaDoc inHeader = p.isInHeader() ? "true" : "false";
197                String JavaDoc outHeader = p.isOutHeader() ? "true" : "false";
198                pw.println(" " +
199                        "new org.jboss.axis.description.ParameterDesc(" +
200                        Utils.getNewQName(paramName) +
201                        ", " + modeStr +
202                        ", " + Utils.getNewQName(paramType) +
203                        ", " + Utils.getParameterTypeName(p) + ".class" +
204                        ", " + inHeader +
205                        ", " + outHeader + "), ");
206             }
207
208             pw.println(" };");
209
210             // Get the return name QName and type
211
QName JavaDoc retName = null;
212             QName JavaDoc retType = null;
213             if (parameters.returnParam != null)
214             {
215                retName = parameters.returnParam.getQName();
216                retType = Utils.getXSIType(parameters.returnParam);
217             }
218
219             String JavaDoc returnStr;
220             if (retName != null)
221             {
222                returnStr = Utils.getNewQName(retName);
223             }
224             else
225             {
226                returnStr = "null";
227             }
228             pw.println(" _oper = new org.jboss.axis.description.OperationDesc(\"" +
229                     javaOpName + "\", _params, " + returnStr + ");");
230
231             if (retType != null)
232             {
233                pw.println(" _oper.setReturnType(" +
234                        Utils.getNewQName(retType) + ");");
235                if (parameters.returnParam != null &&
236                        parameters.returnParam.isOutHeader())
237                {
238                   pw.println(" _oper.setReturnHeader(true);");
239                }
240             }
241
242             // If we need to know the QName (if we have a namespace or
243
// the actual method name doesn't match the XML we expect),
244
// record it in the OperationDesc
245
QName JavaDoc elementQName =
246                     Utils.getOperationQName(bindingOper, bEntry, symbolTable);
247             if (elementQName != null)
248             {
249                pw.println(" _oper.setElementQName(" +
250                        Utils.getNewQName(elementQName) + ");");
251             }
252
253             // Find the SOAPAction.
254
List JavaDoc elems = bindingOper.getExtensibilityElements();
255             Iterator JavaDoc it = elems.iterator();
256             boolean found = false;
257             while (!found && it.hasNext())
258             {
259                ExtensibilityElement elem = (ExtensibilityElement)it.next();
260                if (elem instanceof SOAPOperation)
261                {
262                   SOAPOperation soapOp = (SOAPOperation)elem;
263                   String JavaDoc action = soapOp.getSoapActionURI();
264                   if (action != null)
265                   {
266                      pw.println(" _oper.setSoapAction(\"" + action + "\");");
267                      found = true;
268                   }
269                }
270                else if (elem instanceof UnknownExtensibilityElement)
271                {
272                   //TODO: After WSDL4J supports soap12, change this code
273
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)elem;
274                   QName JavaDoc name = unkElement.getElementType();
275                   if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
276                           name.getLocalPart().equals("operation"))
277                   {
278                      String JavaDoc action = unkElement.getElement().getAttribute("soapAction");
279                      if (action != null)
280                      {
281                         pw.println(" _oper.setSoapAction(\"" + action + "\");");
282                         found = true;
283                      }
284                   }
285                }
286             }
287
288             pw.println(" _myOperationsList.add(_oper);");
289             pw.println(" if (_myOperations.get(\"" + javaOpName + "\") == null) {");
290             pw.println(" _myOperations.put(\"" + javaOpName + "\", new java.util.ArrayList());");
291             pw.println(" }");
292             pw.println(" ((java.util.List)_myOperations.get(\"" + javaOpName + "\")).add(_oper);");
293          }
294
295          // Now generate FaultDesc
296
if (bEntry.getFaults() != null)
297          {
298             ArrayList JavaDoc faults = (ArrayList JavaDoc)bEntry.getFaults().get(bindingOper);
299             if (faults != null)
300             {
301
302                // Operation was not created if there were no parameters
303
if (parameters == null)
304                {
305                   String JavaDoc opName = bindingOper.getOperation().getName();
306                   String JavaDoc javaOpName = Utils.xmlNameToJava(opName);
307                   pw.println(" _oper = " +
308                           "new org.jboss.axis.description.OperationDesc();");
309                   pw.println(" _oper.setName(\"" +
310                           javaOpName + "\");");
311                }
312                // Create FaultDesc items for each fault
313
Iterator JavaDoc it = faults.iterator();
314                while (it.hasNext())
315                {
316                   FaultInfo faultInfo = (FaultInfo)it.next();
317                   QName JavaDoc faultQName = faultInfo.getQName();
318                   QName JavaDoc faultXMLType = faultInfo.getXMLType();
319                   String JavaDoc faultName = faultInfo.getName();
320                   String JavaDoc className =
321                           Utils.getFullExceptionName(faultInfo.getMessage(), symbolTable);
322                   pw.println(" _fault = " +
323                           "new org.jboss.axis.description.FaultDesc();");
324                   if (faultName != null)
325                   {
326                      pw.println(" _fault.setName(\"" +
327                              faultName + "\");");
328                   }
329                   if (faultQName != null)
330                   {
331                      pw.println(" _fault.setQName(" +
332                              Utils.getNewQName(faultQName) + ");");
333                   }
334                   if (className != null)
335                   {
336                      pw.println(" _fault.setClassName(\"" +
337                              className + "\");");
338                   }
339                   if (faultXMLType != null)
340                   {
341                      pw.println(" _fault.setXmlType(" +
342                              Utils.getNewQName(faultXMLType) + ");");
343                   }
344                   pw.println(" _oper.addFault(_fault);");
345                }
346             }
347          }
348       }
349       pw.println(" }");
350       pw.println();
351
352       // Skeleton constructors
353
pw.println(" public " + className + "() {");
354       pw.println(" this.impl = new " + bEntry.getName() + "Impl();");
355       pw.println(" }");
356       pw.println();
357       pw.println(" public " + className + "(" + implType + ") {");
358       pw.println(" this.impl = impl;");
359       pw.println(" }");
360
361       // Now write each of the operation methods
362
for (int i = 0; i < operations.size(); ++i)
363       {
364          BindingOperation operation = (BindingOperation)operations.get(i);
365          Parameters parameters =
366                  bEntry.getParameters(operation.getOperation());
367
368          // Get the soapAction from the <soap:operation>
369
String JavaDoc soapAction = "";
370          Iterator JavaDoc operationExtensibilityIterator = operation.getExtensibilityElements().iterator();
371          for (; operationExtensibilityIterator.hasNext();)
372          {
373             Object JavaDoc obj = operationExtensibilityIterator.next();
374             if (obj instanceof SOAPOperation)
375             {
376                soapAction = ((SOAPOperation)obj).getSoapActionURI();
377                break;
378             }
379             else if (obj instanceof UnknownExtensibilityElement)
380             {
381                //TODO: After WSDL4J supports soap12, change this code
382
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
383                QName JavaDoc name = unkElement.getElementType();
384                if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
385                        name.getLocalPart().equals("operation"))
386                {
387                   if (unkElement.getElement().getAttribute("soapAction") != null)
388                   {
389                      soapAction = unkElement.getElement().getAttribute("soapAction");
390                   }
391                }
392             }
393          }
394          // Get the namespace for the operation from the <soap:body>
395
// RJB: is this the right thing to do?
396
String JavaDoc namespace = "";
397          Iterator JavaDoc bindingMsgIterator = null;
398          BindingInput input = operation.getBindingInput();
399          BindingOutput output;
400          if (input != null)
401          {
402             bindingMsgIterator =
403                     input.getExtensibilityElements().iterator();
404          }
405          else
406          {
407             output = operation.getBindingOutput();
408             if (output != null)
409             {
410                bindingMsgIterator =
411                        output.getExtensibilityElements().iterator();
412             }
413          }
414          if (bindingMsgIterator != null)
415          {
416             for (; bindingMsgIterator.hasNext();)
417             {
418                Object JavaDoc obj = bindingMsgIterator.next();
419                if (obj instanceof SOAPBody)
420                {
421                   namespace = ((SOAPBody)obj).getNamespaceURI();
422                   if (namespace == null)
423                   {
424                      namespace = symbolTable.getDefinition().getTargetNamespace();
425                   }
426                   if (namespace == null)
427                      namespace = "";
428                   break;
429                }
430                else if (obj instanceof UnknownExtensibilityElement)
431                {
432                   //TODO: After WSDL4J supports soap12, change this code
433
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
434                   QName JavaDoc name = unkElement.getElementType();
435                   if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
436                           name.getLocalPart().equals("body"))
437                   {
438                      namespace = unkElement.getElement().getAttribute("namespace");
439                      if (namespace == null)
440                      {
441                         namespace = symbolTable.getDefinition().getTargetNamespace();
442                      }
443                      if (namespace == null)
444                         namespace = "";
445                      break;
446                   }
447                }
448             }
449          }
450          Operation ptOperation = operation.getOperation();
451          OperationType type = ptOperation.getStyle();
452
453          // These operation types are not supported. The signature
454
// will be a string stating that fact.
455
if (type == OperationType.NOTIFICATION
456                  || type == OperationType.SOLICIT_RESPONSE)
457          {
458             pw.println(parameters.signature);
459             pw.println();
460          }
461          else
462          {
463             writeOperation(pw,
464                     operation, parameters, soapAction, namespace);
465          }
466       }
467    } // writeFileBody
468

469    /**
470     * Write the skeleton code for the given operation.
471     */

472    private void writeOperation(PrintWriter JavaDoc pw,
473                                BindingOperation operation,
474                                Parameters parms,
475                                String JavaDoc soapAction,
476                                String JavaDoc namespace)
477    {
478       writeComment(pw, operation.getDocumentationElement());
479
480       // The skeleton used to have specialized operation signatures.
481
// now the same signature is used as the portType
482
pw.println(parms.signature);
483       pw.println(" {");
484
485       // Note: The holders are now instantiated by the runtime and passed
486
// in as parameters.
487

488       // Call the real implementation
489
if (parms.returnParam == null)
490       {
491          pw.print(" ");
492       }
493       else
494       {
495          pw.print(" " + Utils.getParameterTypeName(parms.returnParam) + " ret = ");
496       }
497       String JavaDoc call = "impl." + Utils.xmlNameToJava(operation.getName()) + "(";
498       boolean needComma = false;
499       for (int i = 0; i < parms.list.size(); ++i)
500       {
501          if (needComma)
502             call = call + ", ";
503          else
504             needComma = true;
505          Parameter p = (Parameter)parms.list.get(i);
506
507          call = call + Utils.xmlNameToJava(p.getName());
508       }
509       call = call + ")";
510       pw.println(call + ";");
511
512       if (parms.returnParam != null)
513       {
514          pw.println(" return ret;");
515       }
516       pw.println(" }");
517       pw.println();
518    } // writeSkeletonOperation
519

520 } // class JavaSkelWriter
521
Popular Tags