KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
19 import org.apache.axis.utils.Messages;
20 import org.apache.axis.wsdl.symbolTable.BindingEntry;
21 import org.apache.axis.wsdl.symbolTable.FaultInfo;
22 import org.apache.axis.wsdl.symbolTable.Parameter;
23 import org.apache.axis.wsdl.symbolTable.Parameters;
24 import org.apache.axis.wsdl.symbolTable.SymbolTable;
25
26 import javax.wsdl.Binding;
27 import javax.wsdl.BindingInput;
28 import javax.wsdl.BindingOperation;
29 import javax.wsdl.BindingOutput;
30 import javax.wsdl.Operation;
31 import javax.wsdl.OperationType;
32 import javax.wsdl.extensions.UnknownExtensibilityElement;
33 import javax.wsdl.extensions.soap.SOAPBody;
34 import javax.wsdl.extensions.soap.SOAPOperation;
35 import javax.xml.namespace.QName JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.util.ArrayList JavaDoc;
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 /**
43  * This is Wsdl2java's skeleton writer. It writes the <BindingName>Skeleton.java
44  * file which contains the <bindingName>Skeleton class.
45  */

46 public class JavaSkelWriter extends JavaClassWriter {
47
48     /** Field bEntry */
49     private BindingEntry bEntry;
50
51     /** Field binding */
52     private Binding binding;
53
54     /** Field symbolTable */
55     private SymbolTable symbolTable;
56
57     /**
58      * Constructor.
59      *
60      * @param emitter
61      * @param bEntry
62      * @param symbolTable
63      */

64     protected JavaSkelWriter(Emitter emitter, BindingEntry bEntry,
65                              SymbolTable symbolTable) {
66
67         super(emitter, bEntry.getName() + "Skeleton", "skeleton");
68
69         this.bEntry = bEntry;
70         this.binding = bEntry.getBinding();
71         this.symbolTable = symbolTable;
72     } // ctor
73

74     /**
75      * Returns "implements <SEI>, org.apache.axis.wsdl.Skeleton ".
76      *
77      * @return
78      */

79     protected String JavaDoc getImplementsText() {
80
81         return "implements "
82                 + bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME)
83                 + ", org.apache.axis.wsdl.Skeleton ";
84     } // getImplementsText
85

86     /**
87      * Write the body of the binding's stub file.
88      *
89      * @param pw
90      * @throws IOException
91      */

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

465     /**
466      * Write the skeleton code for the given operation.
467      *
468      * @param pw
469      * @param operation
470      * @param parms
471      * @param soapAction
472      * @param namespace
473      */

474     protected void writeOperation(PrintWriter JavaDoc pw, BindingOperation operation,
475                                 Parameters parms, String JavaDoc soapAction,
476                                 String JavaDoc namespace) {
477
478         writeComment(pw, operation.getDocumentationElement(), true);
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
// Call the real implementation
488
if (parms.returnParam == null) {
489             pw.print(" ");
490         } else {
491             pw.print(" " + Utils.getParameterTypeName(parms.returnParam)
492                     + " ret = ");
493         }
494
495         String JavaDoc call = "impl." + Utils.xmlNameToJava(operation.getName())
496                 + "(";
497         boolean needComma = false;
498
499         for (int i = 0; i < parms.list.size(); ++i) {
500             if (needComma) {
501                 call = call + ", ";
502             } else {
503                 needComma = true;
504             }
505
506             Parameter p = (Parameter) parms.list.get(i);
507
508             call = call + Utils.xmlNameToJava(p.getName());
509         }
510
511         call = call + ")";
512
513         pw.println(call + ";");
514
515         if (parms.returnParam != null) {
516             pw.println(" return ret;");
517         }
518
519         pw.println(" }");
520         pw.println();
521     } // writeSkeletonOperation
522
} // class JavaSkelWriter
523
Popular Tags