KickJava   Java API By Example, From Geeks To Geeks.

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


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.enums.Style;
59 import org.jboss.axis.enums.Use;
60 import org.jboss.axis.utils.JavaUtils;
61 import org.jboss.axis.utils.Messages;
62 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
63 import org.jboss.axis.wsdl.symbolTable.CollectionTE;
64 import org.jboss.axis.wsdl.symbolTable.Element;
65 import org.jboss.axis.wsdl.symbolTable.FaultInfo;
66 import org.jboss.axis.wsdl.symbolTable.MimeInfo;
67 import org.jboss.axis.wsdl.symbolTable.Parameter;
68 import org.jboss.axis.wsdl.symbolTable.Parameters;
69 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
70 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
71
72 import javax.wsdl.Binding;
73 import javax.wsdl.BindingOperation;
74 import javax.wsdl.Fault;
75 import javax.wsdl.Message;
76 import javax.wsdl.Operation;
77 import javax.wsdl.OperationType;
78 import javax.wsdl.Part;
79 import javax.wsdl.PortType;
80 import javax.wsdl.extensions.UnknownExtensibilityElement;
81 import javax.wsdl.extensions.soap.SOAPBinding;
82 import javax.wsdl.extensions.soap.SOAPOperation;
83 import javax.xml.namespace.QName JavaDoc;
84 import java.io.IOException JavaDoc;
85 import java.io.PrintWriter JavaDoc;
86 import java.util.ArrayList JavaDoc;
87 import java.util.Collection JavaDoc;
88 import java.util.HashMap JavaDoc;
89 import java.util.HashSet JavaDoc;
90 import java.util.Iterator JavaDoc;
91 import java.util.List JavaDoc;
92 import java.util.Map JavaDoc;
93 import java.util.Vector JavaDoc;
94
95 /**
96  * This is Wsdl2java's stub writer. It writes the <BindingName>Stub.java
97  * file which contains the <bindingName>Stub class.
98  */

99 public class JavaStubWriter extends JavaClassWriter
100 {
101    private BindingEntry bEntry;
102    private Binding binding;
103    private SymbolTable symbolTable;
104
105    static String JavaDoc[] modeStrings = new String JavaDoc[]{"",
106                                               "org.jboss.axis.description.ParameterDesc.IN",
107                                               "org.jboss.axis.description.ParameterDesc.OUT",
108                                               "org.jboss.axis.description.ParameterDesc.INOUT"};
109    static HashMap JavaDoc styles = new HashMap JavaDoc();
110    static HashMap JavaDoc uses = new HashMap JavaDoc();
111
112    static
113    {
114       styles.put(Style.DOCUMENT, "org.jboss.axis.enums.Style.DOCUMENT");
115       styles.put(Style.RPC, "org.jboss.axis.enums.Style.RPC");
116       styles.put(Style.MESSAGE, "org.jboss.axis.enums.Style.MESSAGE");
117       styles.put(Style.WRAPPED, "org.jboss.axis.enums.Style.WRAPPED");
118       uses.put(Use.ENCODED, "org.jboss.axis.enums.Use.ENCODED");
119       uses.put(Use.LITERAL, "org.jboss.axis.enums.Use.LITERAL");
120    }
121
122    /**
123     * Constructor.
124     */

125    protected JavaStubWriter(Emitter emitter,
126                             BindingEntry bEntry,
127                             SymbolTable symbolTable)
128    {
129       super(emitter, bEntry.getName() + "Stub", "stub");
130       this.bEntry = bEntry;
131       this.binding = bEntry.getBinding();
132       this.symbolTable = symbolTable;
133    } // ctor
134

135    /**
136     * Returns "extends org.jboss.axis.client.Stub ".
137     */

138    protected String JavaDoc getExtendsText()
139    {
140       return "extends org.jboss.axis.client.Stub ";
141    } // getExtendsText
142

143    /**
144     * Returns "implements <SEI> ".
145     */

146    protected String JavaDoc getImplementsText()
147    {
148       return "implements " + bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME) + " ";
149    } // getImplementsText
150

151    /**
152     * Write the body of the binding's stub file.
153     */

154    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
155    {
156       PortType portType = binding.getPortType();
157
158       HashSet JavaDoc types = getTypesInPortType(portType);
159       boolean hasMIME = Utils.hasMIME(bEntry);
160       if (types.size() > 0 || hasMIME)
161       {
162          pw.println(" private java.util.Vector cachedSerClasses = new java.util.Vector();");
163          pw.println(" private java.util.Vector cachedSerQNames = new java.util.Vector();");
164          pw.println(" private java.util.Vector cachedSerFactories = new java.util.Vector();");
165          pw.println(" private java.util.Vector cachedDeserFactories = new java.util.Vector();");
166       }
167       pw.println();
168
169       pw.println(" static org.jboss.axis.description.OperationDesc [] _operations;");
170       pw.println();
171       writeOperationMap(pw);
172       pw.println();
173
174       pw.println(" public " + className + "() throws org.jboss.axis.AxisFault {");
175       pw.println(" this(null);");
176       pw.println(" }");
177       pw.println();
178
179       pw.println(" public " + className + "(java.net.URL endpointURL, javax.xml.rpc.Service service) throws org.jboss.axis.AxisFault {");
180       pw.println(" this(service);");
181       pw.println(" super.cachedEndpoint = endpointURL;");
182       pw.println(" }");
183       pw.println();
184
185       pw.println(" public " + className + "(javax.xml.rpc.Service service) throws org.jboss.axis.AxisFault {");
186       pw.println(" if (service == null) {");
187       pw.println(" super.service = new org.jboss.axis.client.Service();");
188       pw.println(" } else {");
189       pw.println(" super.service = service;");
190       pw.println(" }");
191
192       // keep track of how many type mappings we write out
193
int typeMappingCount = 0;
194       if (types.size() > 0)
195       {
196          Iterator JavaDoc it = types.iterator();
197          while (it.hasNext())
198          {
199             TypeEntry type = (TypeEntry)it.next();
200             // Note this same check is repeated in JavaDeployWriter.
201

202             // 1) Don't register types that are base (primitive) types.
203
// If the baseType != null && getRefType() != null this
204
// is a simpleType that must be registered.
205
// 2) Don't register the special types for collections
206
// (indexed properties) or elements
207
// 3) Don't register types that are not referenced
208
// or only referenced in a literal context.
209
if ((type.getBaseType() != null && type.getRefType() == null) ||
210                     type instanceof CollectionTE ||
211                     type instanceof Element ||
212                     !type.isReferenced() ||
213                     type.isOnlyLiteralReferenced())
214             {
215                continue;
216             }
217
218             // Write out serializer declarations
219
if (typeMappingCount == 0)
220             {
221                writeSerializationDecls(pw, hasMIME, binding.getQName().getNamespaceURI());
222             }
223
224             // write the type mapping for this type
225
writeSerializationInit(pw, type);
226
227             // increase the number of type mappings count
228
typeMappingCount++;
229          }
230       }
231       // We need to write out the MIME mapping, even if we don't have
232
// any type mappings
233
if (typeMappingCount == 0 && hasMIME)
234       {
235          writeSerializationDecls(pw, hasMIME, binding.getQName().getNamespaceURI());
236          typeMappingCount++;
237       }
238
239       pw.println(" }");
240       pw.println();
241       pw.println(" private org.jboss.axis.client.Call createCall() throws java.rmi.RemoteException {");
242       pw.println(" try {");
243       pw.println(" org.jboss.axis.client.Call _call =");
244       pw.println(" (org.jboss.axis.client.Call) super.service.createCall();");
245       pw.println(" if (super.maintainSessionSet) {");
246       pw.println(" _call.setMaintainSession(super.maintainSession);");
247       pw.println(" }");
248       pw.println(" if (super.cachedUsername != null) {");
249       pw.println(" _call.setUsername(super.cachedUsername);");
250
251       pw.println(" }");
252       pw.println(" if (super.cachedPassword != null) {");
253       pw.println(" _call.setPassword(super.cachedPassword);");
254       pw.println(" }");
255       pw.println(" if (super.cachedEndpoint != null) {");
256       pw.println(" _call.setTargetEndpointAddress(super.cachedEndpoint);");
257       pw.println(" }");
258       pw.println(" if (super.cachedTimeout != null) {");
259       pw.println(" _call.setTimeout(super.cachedTimeout);");
260       pw.println(" }");
261       pw.println(" if (super.cachedPortName != null) {");
262       pw.println(" _call.setPortName(super.cachedPortName);");
263       pw.println(" }");
264       pw.println(" java.util.Enumeration keys = super.cachedProperties.keys();");
265       pw.println(" while (keys.hasMoreElements()) {");
266       pw.println(" java.lang.String key = (java.lang.String) keys.nextElement();");
267       pw.println(" _call.setProperty(key, super.cachedProperties.get(key));");
268       pw.println(" }");
269       if (typeMappingCount > 0)
270       {
271          pw.println(" // " + Messages.getMessage("typeMap00"));
272          pw.println(" // " + Messages.getMessage("typeMap01"));
273          pw.println(" // " + Messages.getMessage("typeMap02"));
274          pw.println(" // " + Messages.getMessage("typeMap03"));
275          pw.println(" // " + Messages.getMessage("typeMap04"));
276          pw.println(" synchronized (this) {");
277          pw.println(" if (firstCall()) {");
278
279          // Hack alert - we need to establish the encoding style before we register type mappings due
280
// to the fact that TypeMappings key off of encoding style
281
pw.println(" // "
282                  + Messages.getMessage("mustSetStyle"));
283          if (bEntry.hasLiteral())
284          {
285             pw.println(" _call.setEncodingStyle(null);");
286          }
287          else
288          {
289             Iterator JavaDoc iterator = bEntry.getBinding().getExtensibilityElements().iterator();
290             while (iterator.hasNext())
291             {
292                Object JavaDoc obj = iterator.next();
293                if (obj instanceof SOAPBinding)
294                {
295                   pw.println(" _call.setSOAPVersion(org.jboss.axis.soap.SOAPConstants.SOAP11_CONSTANTS);");
296                   pw.println(" _call.setEncodingStyle(org.jboss.axis.Constants.URI_SOAP11_ENC);");
297                }
298                else if (obj instanceof UnknownExtensibilityElement)
299                {
300                   //TODO: After WSDL4J supports soap12, change this code
301
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
302                   QName JavaDoc name = unkElement.getElementType();
303                   if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
304                           name.getLocalPart().equals("binding"))
305                   {
306                      pw.println(" _call.setSOAPVersion(org.jboss.axis.soap.SOAPConstants.SOAP12_CONSTANTS);");
307                      pw.println(" _call.setEncodingStyle(org.jboss.axis.Constants.URI_SOAP12_ENC);");
308                   }
309                }
310             }
311          }
312
313          pw.println(" for (int i = 0; i < cachedSerFactories.size(); ++i) {");
314          pw.println(" java.lang.Class cls = (java.lang.Class) cachedSerClasses.get(i);");
315          pw.println(" javax.xml.namespace.QName qName =");
316          pw.println(" (javax.xml.namespace.QName) cachedSerQNames.get(i);");
317          pw.println(" java.lang.Class sf = (java.lang.Class)");
318          pw.println(" cachedSerFactories.get(i);");
319          pw.println(" java.lang.Class df = (java.lang.Class)");
320          pw.println(" cachedDeserFactories.get(i);");
321          pw.println(" _call.registerTypeMapping(cls, qName, sf, df, false);");
322          pw.println(" }");
323          pw.println(" }");
324          pw.println(" }");
325       }
326       pw.println(" return _call;");
327       pw.println(" }");
328       pw.println(" catch (java.lang.Throwable t) {");
329       pw.println(" throw new org.jboss.axis.AxisFault(\""
330               + Messages.getMessage("badCall01") + "\", t);");
331       pw.println(" }");
332       pw.println(" }");
333       pw.println();
334
335       List JavaDoc operations = binding.getBindingOperations();
336       for (int i = 0; i < operations.size(); ++i)
337       {
338          BindingOperation operation = (BindingOperation)operations.get(i);
339          Parameters parameters =
340                  bEntry.getParameters(operation.getOperation());
341
342          // Get the soapAction from the <soap:operation>
343
String JavaDoc soapAction = "";
344          String JavaDoc opStyle = null;
345          Iterator JavaDoc operationExtensibilityIterator = operation.getExtensibilityElements().iterator();
346          for (; operationExtensibilityIterator.hasNext();)
347          {
348             Object JavaDoc obj = operationExtensibilityIterator.next();
349             if (obj instanceof SOAPOperation)
350             {
351                soapAction = ((SOAPOperation)obj).getSoapActionURI();
352                opStyle = ((SOAPOperation)obj).getStyle();
353                break;
354             }
355             else if (obj instanceof UnknownExtensibilityElement)
356             {
357                //TODO: After WSDL4J supports soap12, change this code
358
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
359                QName JavaDoc name = unkElement.getElementType();
360                if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
361                        name.getLocalPart().equals("operation"))
362                {
363                   if (unkElement.getElement().getAttribute("soapAction") != null)
364                   {
365                      soapAction = unkElement.getElement().getAttribute("soapAction");
366                   }
367                   opStyle = unkElement.getElement().getAttribute("style");
368                }
369             }
370          }
371          Operation ptOperation = operation.getOperation();
372          OperationType type = ptOperation.getStyle();
373
374          // These operation types are not supported. The signature
375
// will be a string stating that fact.
376
if (type == OperationType.NOTIFICATION
377                  || type == OperationType.SOLICIT_RESPONSE)
378          {
379             pw.println(parameters.signature);
380             pw.println();
381          }
382          else
383          {
384             writeOperation(pw, operation, parameters, soapAction, opStyle,
385                     type == OperationType.ONE_WAY, i);
386          }
387       }
388    } // writeFileBody
389

390    private void writeOperationMap(PrintWriter JavaDoc pw)
391    {
392       List JavaDoc operations = binding.getBindingOperations();
393       pw.println(" static {");
394       pw.println(" _operations = new org.jboss.axis.description.OperationDesc[" +
395               operations.size() + "];");
396       pw.println(" org.jboss.axis.description.OperationDesc oper;");
397       for (int i = 0; i < operations.size(); ++i)
398       {
399          BindingOperation operation = (BindingOperation)operations.get(i);
400          Parameters parameters =
401                  bEntry.getParameters(operation.getOperation());
402
403          // Get the soapAction from the <soap:operation>
404
String JavaDoc opStyle = null;
405          Iterator JavaDoc operationExtensibilityIterator = operation.getExtensibilityElements().iterator();
406          for (; operationExtensibilityIterator.hasNext();)
407          {
408             Object JavaDoc obj = operationExtensibilityIterator.next();
409             if (obj instanceof SOAPOperation)
410             {
411                opStyle = ((SOAPOperation)obj).getStyle();
412                break;
413             }
414             else if (obj instanceof UnknownExtensibilityElement)
415             {
416                //TODO: After WSDL4J supports soap12, change this code
417
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
418                QName JavaDoc name = unkElement.getElementType();
419                if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
420                        name.getLocalPart().equals("operation"))
421                {
422                   opStyle = unkElement.getElement().getAttribute("style");
423                }
424             }
425          }
426          Operation ptOperation = operation.getOperation();
427          OperationType type = ptOperation.getStyle();
428
429          // These operation types are not supported. The signature
430
// will be a string stating that fact.
431
if (type == OperationType.NOTIFICATION
432                  || type == OperationType.SOLICIT_RESPONSE)
433          {
434             pw.println(parameters.signature);
435             pw.println();
436          }
437
438          String JavaDoc operName = operation.getName();
439          String JavaDoc indent = " ";
440          pw.println(indent + "oper = new org.jboss.axis.description.OperationDesc();");
441          pw.println(indent + "oper.setName(\"" + operName + "\");");
442
443          // loop over paramters and set up in/out params
444
for (int j = 0; j < parameters.list.size(); ++j)
445          {
446             Parameter p = (Parameter)parameters.list.get(j);
447
448             // Get the QName representing the parameter type
449
QName JavaDoc paramType = Utils.getXSIType(p);
450
451             // Set the javaType to the name of the type
452
String JavaDoc javaType = null;
453             if (p.getMIMEInfo() != null)
454             {
455                MimeInfo mimeInfo = p.getMIMEInfo();
456                javaType = JavaUtils.mimeToJava(mimeInfo.getType()) + mimeInfo.getDimensions() + ".class, ";
457             }
458             else
459             {
460                javaType = p.getType().getName();
461                if (javaType != null)
462                {
463                   javaType += ".class, ";
464                }
465                else
466                {
467                   javaType = "null, ";
468                }
469             }
470
471             // Get the text representing newing a QName for the name and type
472
String JavaDoc paramNameText = Utils.getNewQName(p.getQName());
473             String JavaDoc paramTypeText = Utils.getNewQName(paramType);
474
475             // Generate the addParameter call with the
476
// name qname, typeQName, optional javaType, and mode
477
boolean isInHeader = p.isInHeader();
478             boolean isOutHeader = p.isOutHeader();
479             pw.println(" oper.addParameter(" + paramNameText
480                     + ", " + paramTypeText + ", "
481                     + javaType + modeStrings[p.getMode()]
482                     + ", " + isInHeader + ", " + isOutHeader + ");");
483          }
484          // set output type
485
if (parameters.returnParam != null)
486          {
487
488             // Get the QName for the return Type
489
QName JavaDoc returnType = Utils.getXSIType(parameters.returnParam);
490
491             // Get the javaType
492
String JavaDoc javaType = null;
493             if (parameters.returnParam.getMIMEInfo() != null)
494             {
495                MimeInfo mimeInfo = parameters.returnParam.getMIMEInfo();
496                javaType = JavaUtils.mimeToJava(mimeInfo.getType()) + mimeInfo.getDimensions();
497             }
498             else
499             {
500                javaType = parameters.returnParam.getType().getName();
501             }
502             if (javaType == null)
503             {
504                javaType = "";
505             }
506             else
507             {
508                javaType = javaType + ".class";
509             }
510             pw.println(" oper.setReturnType(" +
511
512                     Utils.getNewQName(returnType) + ");");
513             pw.println(" oper.setReturnClass("
514                     + javaType + ");");
515             QName JavaDoc returnQName = parameters.returnParam.getQName();
516             if (returnQName != null)
517             {
518                pw.println(" oper.setReturnQName(" +
519                        Utils.getNewQName(returnQName) + ");");
520             }
521             if (parameters.returnParam.isOutHeader())
522             {
523                pw.println(" oper.setReturnHeader(true);");
524             }
525          }
526          else
527          {
528             pw.println(" oper.setReturnType(org.jboss.axis.encoding.XMLType.AXIS_VOID);");
529          }
530
531          boolean hasMIME = Utils.hasMIME(bEntry, operation);
532          Style style = Style.getStyle(opStyle, bEntry.getBindingStyle());
533          Use use = bEntry.getInputBodyType(operation.getOperation());
534          if (style == Style.DOCUMENT && symbolTable.isWrapped())
535          {
536             style = Style.WRAPPED;
537          }
538
539          if (!hasMIME)
540          {
541             pw.println(" oper.setStyle(" + styles.get(style) + ");");
542             pw.println(" oper.setUse(" + uses.get(use) + ");");
543          }
544
545          // Register fault/exception information for this operation
546
writeFaultInfo(pw, operation);
547
548          pw.println(indent + "_operations[" + i + "] = oper;");
549          pw.println("");
550       }
551       pw.println(" }");
552    }
553
554    /**
555     * This method returns a set of all the TypeEntry in a given PortType.
556     * The elements of the returned HashSet are Types.
557     */

558    private HashSet JavaDoc getTypesInPortType(PortType portType)
559    {
560       HashSet JavaDoc types = new HashSet JavaDoc();
561       HashSet JavaDoc firstPassTypes = new HashSet JavaDoc();
562
563       // Get all the types from all the operations
564
List JavaDoc operations = portType.getOperations();
565
566       for (int i = 0; i < operations.size(); ++i)
567       {
568          Operation op = (Operation)operations.get(i);
569          firstPassTypes.addAll(getTypesInOperation(op));
570       }
571
572       // Add all the types nested and derived from the types
573
// in the first pass.
574
Iterator JavaDoc i = firstPassTypes.iterator();
575       while (i.hasNext())
576       {
577          TypeEntry type = (TypeEntry)i.next();
578          if (!types.contains(type))
579          {
580             types.add(type);
581             types.addAll(Utils.getNestedTypes(type, symbolTable, true));
582          }
583       }
584       return types;
585    } // getTypesInPortType
586

587    /**
588     * This method returns a set of all the TypeEntry in a given Operation.
589     * The elements of the returned HashSet are TypeEntry.
590     */

591    private HashSet JavaDoc getTypesInOperation(Operation operation)
592    {
593       HashSet JavaDoc types = new HashSet JavaDoc();
594       Vector JavaDoc v = new Vector JavaDoc();
595
596       Parameters params = bEntry.getParameters(operation);
597
598       // Loop over parameter types for this operation
599
for (int i = 0; i < params.list.size(); i++)
600       {
601          Parameter p = (Parameter)params.list.get(i);
602          v.add(p.getType());
603       }
604
605       // Add the return type
606
if (params.returnParam != null)
607          v.add(params.returnParam.getType());
608
609       // Collect all the types in faults
610
Map JavaDoc faults = operation.getFaults();
611
612       if (faults != null)
613       {
614          Iterator JavaDoc i = faults.values().iterator();
615
616          while (i.hasNext())
617          {
618             Fault f = (Fault)i.next();
619             partTypes(v,
620                     f.getMessage().getOrderedParts(null));
621          }
622       }
623       // Put all these types into a set. This operation eliminates all duplicates.
624
for (int i = 0; i < v.size(); i++)
625          types.add(v.get(i));
626
627       return types;
628    } // getTypesInOperation
629

630    /**
631     * This method returns a vector of TypeEntry for the parts.
632     */

633    private void partTypes(Vector JavaDoc v, Collection JavaDoc parts)
634    {
635       Iterator JavaDoc i = parts.iterator();
636
637       while (i.hasNext())
638       {
639          Part part = (Part)i.next();
640
641          QName JavaDoc qType = part.getTypeName();
642          if (qType != null)
643          {
644             v.add(symbolTable.getType(qType));
645          }
646          else
647          {
648             qType = part.getElementName();
649             if (qType != null)
650             {
651                v.add(symbolTable.getElement(qType));
652             }
653          }
654       } // while
655

656    } // partTypes
657

658    /**
659     * This function writes the regsiterFaultInfo API calls
660     */

661    private void writeFaultInfo(PrintWriter JavaDoc pw, BindingOperation bindOp)
662    {
663       Map JavaDoc faultMap = bEntry.getFaults();
664       // Get the list of faults for this operation
665
ArrayList JavaDoc faults = (ArrayList JavaDoc)faultMap.get(bindOp);
666
667       // check for no faults
668
if (faults == null)
669       {
670          return;
671       }
672       // For each fault, register its information
673
for (Iterator JavaDoc faultIt = faults.iterator(); faultIt.hasNext();)
674       {
675          FaultInfo info = (FaultInfo)faultIt.next();
676          QName JavaDoc qname = info.getQName();
677          Message message = info.getMessage();
678
679          // if no parts in fault, skip it!
680
if (qname == null)
681          {
682             continue;
683          }
684
685          // Get the Exception class name
686
String JavaDoc className = Utils.getFullExceptionName(message, symbolTable);
687
688          // output the registration API call
689
pw.println(" oper.addFault(new org.jboss.axis.description.FaultDesc(");
690          pw.println(" " + Utils.getNewQName(qname) + ",");
691          pw.println(" \"" + className + "\",");
692          pw.println(" " + Utils.getNewQName(info.getXMLType()) + ", ");
693          pw.println(" " + Utils.isFaultComplex(message, symbolTable));
694          pw.println(" ));");
695       }
696    }
697
698    /**
699     * In the stub constructor, write the serializer code for the complex types.
700     */

701
702    private void writeSerializationDecls(PrintWriter JavaDoc pw, boolean hasMIME,
703                                         String JavaDoc namespace)
704    {
705       pw.println(" java.lang.Class cls;");
706       pw.println(" javax.xml.namespace.QName qName;");
707       pw.println(" java.lang.Class beansf = org.jboss.axis.encoding.ser.BeanSerializerFactory.class;");
708       pw.println(" java.lang.Class beandf = org.jboss.axis.encoding.ser.BeanDeserializerFactory.class;");
709       pw.println(" java.lang.Class enumsf = org.jboss.axis.encoding.ser.EnumSerializerFactory.class;");
710       pw.println(" java.lang.Class enumdf = org.jboss.axis.encoding.ser.EnumDeserializerFactory.class;");
711       pw.println(" java.lang.Class arraysf = org.jboss.axis.encoding.ser.ArraySerializerFactory.class;");
712       pw.println(" java.lang.Class arraydf = org.jboss.axis.encoding.ser.ArrayDeserializerFactory.class;");
713       pw.println(" java.lang.Class simplesf = org.jboss.axis.encoding.ser.SimpleSerializerFactory.class;");
714       pw.println(" java.lang.Class simpledf = org.jboss.axis.encoding.ser.SimpleDeserializerFactory.class;");
715
716       if (hasMIME)
717       {
718          pw.println(" java.lang.Class mimesf = org.jboss.axis.encoding.ser.JAFDataHandlerSerializerFactory.class;");
719          pw.println(" java.lang.Class mimedf = org.jboss.axis.encoding.ser.JAFDataHandlerDeserializerFactory.class;");
720          pw.println();
721          QName JavaDoc qname = new QName JavaDoc(namespace, "DataHandler");
722
723          pw.println(" qName = new javax.xml.namespace.QName(\""
724                  + qname.getNamespaceURI() + "\", \"" + qname.getLocalPart()
725                  + "\");");
726          pw.println(" cachedSerQNames.add(qName);");
727          pw.println(" cls = javax.activation.DataHandler.class;");
728          pw.println(" cachedSerClasses.add(cls);");
729          pw.println(" cachedSerFactories.add(mimesf);");
730          pw.println(" cachedDeserFactories.add(mimedf);");
731          pw.println();
732       }
733    } // writeSerializationDecls
734

735    private void writeSerializationInit(PrintWriter JavaDoc pw, TypeEntry type)
736    {
737
738       QName JavaDoc qname = type.getQName();
739
740       pw.println(" qName = new javax.xml.namespace.QName(\""
741               + qname.getNamespaceURI() + "\", \"" + qname.getLocalPart()
742               + "\");");
743       pw.println(" cachedSerQNames.add(qName);");
744       pw.println(" cls = " + type.getName() + ".class;");
745       pw.println(" cachedSerClasses.add(cls);");
746       if (type.getName().endsWith("[]"))
747       {
748          pw.println(" cachedSerFactories.add(arraysf);");
749          pw.println(" cachedDeserFactories.add(arraydf);");
750       }
751       else if (type.getNode() != null &&
752               Utils.getEnumerationBaseAndValues(type.getNode(), symbolTable) != null)
753       {
754          pw.println(" cachedSerFactories.add(enumsf);");
755          pw.println(" cachedDeserFactories.add(enumdf);");
756       }
757       else if (type.isSimpleType())
758       {
759          pw.println(" cachedSerFactories.add(simplesf);");
760          pw.println(" cachedDeserFactories.add(simpledf);");
761       }
762       else if (type.getBaseType() != null)
763       {
764          // serializers are not required for types derived from base types
765
// java type to qname mapping is anyway established by default
766
// note that we have to add null to the serfactories vector to
767
// keep the order of other entries, this is not going to screw
768
// up because if type mapping returns null for a serialization
769
// factory, it is assumed to be not-defined and the delegate
770
// will be checked, the end delegate is DefaultTypeMappingImpl
771
// that'll get it right with the base type name
772
pw.println(" cachedSerFactories.add(null);");
773          pw.println(" cachedDeserFactories.add(simpledf);");
774       }
775       else
776       {
777          pw.println(" cachedSerFactories.add(beansf);");
778          pw.println(" cachedDeserFactories.add(beandf);");
779       }
780       pw.println();
781    } // writeSerializationInit
782

783    /**
784     * Write the stub code for the given operation.
785     */

786    private void writeOperation(PrintWriter JavaDoc pw,
787                                BindingOperation operation,
788                                Parameters parms,
789                                String JavaDoc soapAction,
790                                String JavaDoc opStyle,
791                                boolean oneway,
792                                int opIndex)
793    {
794
795       writeComment(pw, operation.getDocumentationElement());
796
797       pw.println(parms.signature + " {");
798       pw.println(" if (super.cachedEndpoint == null) {");
799       pw.println(" throw new org.jboss.axis.NoEndPointException();");
800       pw.println(" }");
801       pw.println(" org.jboss.axis.client.Call _call = createCall();");
802
803       pw.println(" _call.setOperation(_operations[" + opIndex + "]);");
804
805       // SoapAction
806
if (soapAction != null)
807       {
808          pw.println(" _call.setUseSOAPAction(true);");
809          pw.println(" _call.setSOAPActionURI(\"" + soapAction + "\");");
810       }
811
812       boolean hasMIME = Utils.hasMIME(bEntry, operation);
813
814       // Encoding: literal or encoded use.
815
Use use = bEntry.getInputBodyType(operation.getOperation());
816       if (use == Use.LITERAL)
817       {
818          // Turn off encoding
819
pw.println(" _call.setEncodingStyle(null);");
820          // turn off XSI types
821
pw.println(" _call.setProperty(org.jboss.axis.client.Call.SEND_TYPE_ATTR, Boolean.FALSE);");
822       }
823       if (hasMIME || use == Use.LITERAL)
824       {
825          // If it is literal, turn off multirefs.
826
//
827
// If there are any MIME types, turn off multirefs.
828
// I don't know enough about the guts to know why
829
// attachments don't work with multirefs, but they don't.
830
pw.println(" _call.setProperty(org.jboss.axis.AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);");
831       }
832
833       Style style = Style.getStyle(opStyle, bEntry.getBindingStyle());
834       if (style == Style.DOCUMENT && symbolTable.isWrapped())
835       {
836          style = Style.WRAPPED;
837       }
838
839
840       Iterator JavaDoc iterator = bEntry.getBinding().getExtensibilityElements().iterator();
841       while (iterator.hasNext())
842       {
843          Object JavaDoc obj = iterator.next();
844          if (obj instanceof SOAPBinding)
845          {
846             pw.println(" _call.setSOAPVersion(org.jboss.axis.soap.SOAPConstants.SOAP11_CONSTANTS);");
847          }
848          else if (obj instanceof UnknownExtensibilityElement)
849          {
850             //TODO: After WSDL4J supports soap12, change this code
851
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement)obj;
852             QName JavaDoc name = unkElement.getElementType();
853             if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) &&
854                     name.getLocalPart().equals("binding"))
855             {
856                pw.println(" _call.setSOAPVersion(org.jboss.axis.soap.SOAPConstants.SOAP12_CONSTANTS);");
857             }
858          }
859       }
860
861       // Operation name
862
if (style == Style.WRAPPED)
863       {
864          // We need to make sure the operation name, which is what we
865
// wrap the elements in, matches the Qname of the parameter
866
// element.
867
Map JavaDoc partsMap = operation.getOperation().getInput().getMessage().getParts();
868          Part p = (Part)partsMap.values().iterator().next();
869          QName JavaDoc q = p.getElementName();
870          pw.println(" _call.setOperationName(" + Utils.getNewQName(q) + ");");
871       }
872       else
873       {
874          QName JavaDoc elementQName =
875                  Utils.getOperationQName(operation, bEntry, symbolTable);
876          if (elementQName != null)
877          {
878             pw.println(" _call.setOperationName(" +
879                     Utils.getNewQName(elementQName) + ");");
880          }
881       }
882       pw.println();
883
884       // Set the headers
885
pw.println(" setRequestHeaders(_call);");
886
887       // Set the attachments
888
pw.println(" setAttachments(_call);");
889
890       // Set DIME flag if needed
891
if (bEntry.isOperationDIME(operation.getOperation().getName()))
892       {
893          pw.println(" _call.setProperty(_call.ATTACHMENT_ENCAPSULATION_FORMAT, _call.ATTACHMENT_ENCAPSULATION_FORMAT_DIME);");
894       }
895
896       // Invoke the operation
897
if (oneway)
898       {
899          pw.print(" _call.invokeOneWay(");
900       }
901       else
902       {
903          pw.print(" java.lang.Object _resp = _call.invoke(");
904       }
905       pw.print("new java.lang.Object[] {");
906       writeParameters(pw, parms);
907       pw.println("});");
908       pw.println();
909
910       if (!oneway)
911       {
912          writeResponseHandling(pw, parms);
913       }
914       pw.println(" }");
915       pw.println();
916    } // writeOperation
917

918    private void writeParameters(PrintWriter JavaDoc pw, Parameters parms)
919    {
920       // Write the input and inout parameter list
921
boolean needComma = false;
922       for (int i = 0; i < parms.list.size(); ++i)
923       {
924          Parameter p = (Parameter)parms.list.get(i);
925
926          if (p.getMode() != Parameter.OUT)
927          {
928             if (needComma)
929             {
930                pw.print(", ");
931             }
932             else
933             {
934                needComma = true;
935             }
936
937             String JavaDoc javifiedName = Utils.xmlNameToJava(p.getName());
938             if (p.getMode() != Parameter.IN)
939             {
940                javifiedName += ".value";
941             }
942             if (p.getMIMEInfo() == null)
943             {
944                javifiedName = Utils.wrapPrimitiveType(p.getType(), javifiedName);
945             }
946             pw.print(javifiedName);
947          }
948       }
949    } // writeParamters
950

951    private void writeResponseHandling(PrintWriter JavaDoc pw, Parameters parms)
952    {
953       pw.println(" if (_resp instanceof java.rmi.RemoteException) {");
954       pw.println(" throw (java.rmi.RemoteException)_resp;");
955       pw.println(" }");
956
957       int allOuts = parms.outputs + parms.inouts;
958       if (allOuts > 0)
959       {
960          pw.println(" else {");
961          pw.println(" extractAttachments(_call);");
962
963          if (allOuts == 1)
964          {
965             if (parms.returnParam != null)
966             {
967                writeOutputAssign(pw, "return ", parms.returnParam.getType(),
968                        parms.returnParam.getMIMEInfo(), "_resp");
969             }
970             else
971             {
972                // The resp object must go into a holder
973
int i = 0;
974                Parameter p = (Parameter)parms.list.get(i);
975
976                while (p.getMode() == Parameter.IN)
977                {
978                   p = (Parameter)parms.list.get(++i);
979                }
980                String JavaDoc javifiedName = Utils.xmlNameToJava(p.getName());
981                String JavaDoc qnameName = Utils.getNewQName(p.getQName());
982
983                pw.println(" java.util.Map _output;");
984                pw.println(" _output = _call.getOutputParams();");
985                writeOutputAssign(pw, javifiedName + ".value = ",
986                        p.getType(), p.getMIMEInfo(),
987                        "_output.get(" + qnameName + ")");
988             }
989          }
990          else
991          {
992             // There is more than 1 output. Get the outputs from getOutputParams.
993
pw.println(" java.util.Map _output;");
994             pw.println(" _output = _call.getOutputParams();");
995             for (int i = 0; i < parms.list.size(); ++i)
996             {
997                Parameter p = (Parameter)parms.list.get(i);
998                String JavaDoc javifiedName = Utils.xmlNameToJava(p.getName());
999                String JavaDoc qnameName = Utils.getNewQName(p.getQName());
1000               if (p.getMode() != Parameter.IN)
1001               {
1002                  writeOutputAssign(pw, javifiedName + ".value = ",
1003                          p.getType(), p.getMIMEInfo(),
1004                          "_output.get(" + qnameName + ")");
1005               }
1006            }
1007            if (parms.returnParam != null)
1008            {
1009               writeOutputAssign(pw, "return ", parms.returnParam.getType(),
1010                       parms.returnParam.getMIMEInfo(), "_resp");
1011            }
1012
1013         }
1014         pw.println(" }");
1015      }
1016      else
1017      {
1018         pw.println(" extractAttachments(_call);");
1019      }
1020   } // writeResponseHandling
1021

1022   /**
1023    * writeOutputAssign
1024    *
1025    * @param target (either "return" or "something ="
1026    * @param type (source TypeEntry)
1027    * @param source (source String)
1028    */

1029   private void writeOutputAssign(PrintWriter JavaDoc pw, String JavaDoc target,
1030                                  TypeEntry type, MimeInfo mimeInfo,
1031                                  String JavaDoc source)
1032   {
1033      if (type != null && type.getName() != null)
1034      {
1035         // Try casting the output to the expected output.
1036
// If that fails, use JavaUtils.convert()
1037
pw.println(" try {");
1038
1039         pw.println(" " + target +
1040                 Utils.getResponseString(type, mimeInfo, source));
1041
1042         pw.println(" } catch (java.lang.Exception _exception) {");
1043         pw.println(" " + target +
1044                 Utils.getResponseString(type, mimeInfo,
1045                         "org.jboss.axis.utils.JavaUtils.convert(" +
1046                 source + ", " + type.getName() + ".class)"));
1047         pw.println(" }");
1048      }
1049      else
1050      {
1051         pw.println(" " + target +
1052                 Utils.getResponseString(type, mimeInfo, source));
1053      }
1054   }
1055
1056} // class JavaStubWriter
1057
Popular Tags