View Javadoc

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.geronimo.ews.jaxrpcmapping;
17  
18  import org.apache.axis.wsdl.symbolTable.BindingEntry;
19  import org.apache.axis.wsdl.symbolTable.Parameter;
20  import org.apache.axis.wsdl.symbolTable.Parameters;
21  import org.apache.axis.wsdl.symbolTable.SymbolTable;
22  import org.apache.axis.wsdl.symbolTable.TypeEntry;
23  import org.apache.axis.wsdl.toJava.JavaClassWriter;
24  import org.apache.axis.wsdl.toJava.Utils;
25  
26  import javax.wsdl.Binding;
27  import javax.wsdl.BindingOperation;
28  import javax.wsdl.Operation;
29  import javax.wsdl.OperationType;
30  import javax.xml.rpc.holders.BooleanHolder;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /***
37   * This is Wsdl2java's implementation template writer.  It writes the <BindingName>Impl.java
38   * file which contains the <bindingName>Impl class.
39   *
40   * @author Ias (iasandcb@tmax.co.kr)
41   */
42  public class J2eeImplWriter extends JavaClassWriter {
43      protected Binding binding;
44      protected SymbolTable symbolTable;
45      protected BindingEntry bEntry;
46  
47      /***
48       * Constructor.
49       */
50      protected J2eeImplWriter(J2eeEmitter emitter,
51                               BindingEntry bEntry,
52                               SymbolTable symbolTable) {
53          super(emitter, bEntry.getName() + "Impl", "templateImpl");
54          this.binding = bEntry.getBinding();
55          this.symbolTable = symbolTable;
56          this.bEntry = bEntry;
57      } // ctor
58  
59      /***
60       * Write the body of the binding's stub file.
61       */
62      protected void writeFileBody(PrintWriter pw) throws IOException {
63          List operations = binding.getBindingOperations();
64          for (int i = 0; i < operations.size(); ++i) {
65              BindingOperation operation = (BindingOperation) operations.get(i);
66              Operation ptOperation = operation.getOperation();
67              OperationType type = ptOperation.getStyle();
68              Parameters parameters =
69                      bEntry.getParameters(operation.getOperation());
70  
71              // These operation types are not supported.  The signature
72              // will be a string stating that fact.
73              if (type == OperationType.NOTIFICATION
74                      || type == OperationType.SOLICIT_RESPONSE) {
75                  pw.println(parameters.signature);
76                  pw.println();
77              } else {
78                  writeOperation(pw, parameters);
79              }
80          }
81      } // writeFileBody
82  
83      /***
84       * Returns the appropriate implements text
85       *
86       * @return " implements <classes>"
87       */
88      protected String getImplementsText() {
89          String portTypeName = (String) bEntry.getDynamicVar(J2eeBindingWriter.INTERFACE_NAME);
90          String implementsText = "implements " + portTypeName;
91          return implementsText;
92      }
93  
94      /***
95       * Write the implementation template for the given operation.
96       */
97      protected void writeOperation(PrintWriter pw, Parameters parms) throws IOException {
98          pw.println(parms.signature + " {");
99  
100         // Fill in any out parameter holders
101         Iterator iparam = parms.list.iterator();
102         while (iparam.hasNext()) {
103             Parameter param = (Parameter) iparam.next();
104             if (param.getMode() == Parameter.OUT) {
105                 // write a constructor for each of the parameters
106                 
107                 BooleanHolder bThrow = new BooleanHolder(false);
108                 String constructorString =
109                         Utils.getConstructorForParam(param, symbolTable, bThrow);
110                 if (bThrow.value) {
111                     pw.println("        try {");
112                 }
113                 pw.println("        " + Utils.xmlNameToJava(param.getName())
114                         + ".value = " + constructorString + ";");
115                 if (bThrow.value) {
116                     pw.println("        } catch (Exception e) {");
117                     pw.println("        }");
118                 }
119             }
120         }
121 
122         // Print the return statement
123         if (parms.returnParam != null) {
124             TypeEntry returnType = parms.returnParam.getType();
125             pw.print("        return ");
126             if (Utils.isPrimitiveType(returnType)) {
127                 String returnString = returnType.getName();
128                 if ("boolean".equals(returnString)) {
129                     pw.println("false;");
130                 } else if ("byte".equals(returnString)) {
131                     pw.println("(byte)-3;");
132                 } else if ("short".equals(returnString)) {
133                     pw.println("(short)-3;");
134                 } else {
135                     pw.println("-3;");
136                 }
137             } else {
138                 pw.println("null;");
139             }
140         }
141         pw.println("    }");
142         pw.println();
143     } // writeOperation
144 
145 }