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.utils.JavaUtils;
19  import org.apache.axis.utils.Messages;
20  import org.apache.axis.utils.WSDLUtils;
21  import org.apache.axis.wsdl.symbolTable.BindingEntry;
22  import org.apache.axis.wsdl.symbolTable.PortTypeEntry;
23  import org.apache.axis.wsdl.symbolTable.ServiceEntry;
24  import org.apache.axis.wsdl.symbolTable.SymbolTable;
25  import org.apache.axis.wsdl.toJava.Utils;
26  
27  import javax.wsdl.Binding;
28  import javax.wsdl.Port;
29  import javax.wsdl.Service;
30  import javax.xml.namespace.QName;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  import java.util.Iterator;
36  import java.util.Map;
37  import java.util.Vector;
38  
39  /***
40   * This is Wsdl2java's service implementation writer.
41   * It writes the <serviceName>Locator.java file.
42   *
43   * @author Ias (iasandcb@tmax.co.kr)
44   * @deprecated no more used by J2eeGeneratorFactory
45   */
46  public class J2eeServiceImplWriter extends J2eeClassWriter {
47      private ServiceEntry sEntry;
48      private SymbolTable symbolTable;
49  
50      /***
51       * Constructor.
52       */
53      protected J2eeServiceImplWriter(J2eeEmitter emitter,
54                                      ServiceEntry sEntry,
55                                      SymbolTable symbolTable) {
56  //        super(emitter, sEntry.getName() + "Locator", "service");
57          super(emitter, sEntry.getName() + "_Impl", "service");
58          this.sEntry = sEntry;
59          this.symbolTable = symbolTable;
60      } // ctor
61  
62      /***
63       * Returns "extends org.apache.axis.client.Service ".
64       */
65      protected String getExtendsText() {
66          return "extends org.apache.axis.client.Service ";
67      } // getExtendsText
68  
69      /***
70       * Returns "implements <serviceInterface>".
71       */
72      protected String getImplementsText() {
73          return "implements " + jaxRpcMapper.getServiceInterfaceName(sEntry) + ' ';
74      } // getImplementsText
75  
76      /***
77       * Write the body of the service file.
78       */
79      protected void writeFileBody(PrintWriter pw) throws IOException {
80          Service service = sEntry.getService();
81          // output comments
82          writeComment(pw, service.getDocumentationElement(), false);
83  
84          // Used to construct the getPort(Class) method.
85          Vector getPortIfaces = new Vector();
86          Vector getPortStubClasses = new Vector();
87          Vector getPortPortNames = new Vector();
88          boolean printGetPortNotice = false;
89  
90          // get ports
91          Map portMap = service.getPorts();
92          Iterator portIterator = portMap.values().iterator();
93  
94          // write a get method for each of the ports with a SOAP binding
95          while (portIterator.hasNext()) {
96              Port p = (Port) portIterator.next();
97              Binding binding = p.getBinding();
98              if (binding == null) {
99                  throw new IOException(Messages.getMessage("emitFailNoBinding01",
100                         new String[]{p.getName()}));
101             }
102             BindingEntry bEntry =
103                     symbolTable.getBindingEntry(binding.getQName());
104             if (bEntry == null) {
105                 throw new IOException(Messages.getMessage("emitFailNoBindingEntry01",
106                         new String[]{binding.getQName().toString()}));
107             }
108             PortTypeEntry ptEntry = symbolTable.getPortTypeEntry(binding.getPortType().getQName());
109             if (ptEntry == null) {
110                 throw new IOException(Messages.getMessage("emitFailNoPortType01",
111                         new String[]{binding.getPortType().getQName().toString()}));
112             }
113 
114             // If this isn't an SOAP binding, skip it
115             if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
116                 continue;
117             }
118 
119             // JSR 101 indicates that the name of the port used
120             // in the java code is the name of the wsdl:port.  It
121             // does not indicate what should occur if the
122             // wsdl:port name is not a java identifier.  The
123             // TCK depends on the case-sensitivity being preserved,
124             // and the interop tests have port names that are not
125             // valid java identifiers.  Thus the following code.
126             String portName = p.getName();
127             if (!JavaUtils.isJavaId(portName)) {
128                 portName = Utils.xmlNameToJavaClass(portName);
129             }
130             String stubClass = bEntry.getName() + "Stub";
131             String bindingType = (String) bEntry.getDynamicVar(J2eeBindingWriter.INTERFACE_NAME);
132 
133             // getPort(Class) must return a stub for an interface.  Collect all
134             // the port interfaces so the getPort(Class) method can be constructed.
135             if (getPortIfaces.contains(bindingType)) {
136                 printGetPortNotice = true;
137             }
138             getPortIfaces.add(bindingType);
139             getPortStubClasses.add(stubClass);
140             getPortPortNames.add(portName);
141 
142             // Get endpoint address and validate it
143             String address = WSDLUtils.getAddressFromPort(p);
144             if (address == null) {
145                 // now what?
146                 throw new IOException(Messages.getMessage("emitFail02",
147                         portName, className));
148             }
149             try {
150                 new URL(address);
151             } catch (MalformedURLException e) {
152                 throw new IOException(Messages.getMessage("emitFail03",
153                         new String[]{portName, className, address}));
154             }
155             writeAddressInfo(pw, portName, address, p);
156             String wsddServiceName = portName + "WSDDServiceName";
157             writeWSDDServiceNameInfo(pw, wsddServiceName, portName);
158             writeGetPortName(pw, bindingType, portName);
159             writeGetPortNameURL(pw, bindingType, portName, stubClass,
160                     wsddServiceName);
161         }
162         writeGetPortClass(pw, getPortIfaces, getPortStubClasses,
163                 getPortPortNames, printGetPortNotice);
164         writeGetPortQNameClass(pw, getPortPortNames);
165         writeGetServiceName(pw, sEntry.getQName());
166         writeGetPorts(pw, getPortPortNames);
167     } // writeFileBody
168 
169     /***
170      * Write the private address field for this port and the public getter for it.
171      */
172     protected void writeAddressInfo(PrintWriter pw, String portName,
173                                     String address, Port p) {
174         // Write the private address field for this port
175         pw.println();
176         pw.println("    // " + Messages.getMessage("getProxy00", portName));
177         writeComment(pw, p.getDocumentationElement(), true);
178         pw.println("    private final java.lang.String " + portName + "_address = \"" + address + "\";");
179 
180         // Write the public address getter for this field
181         pw.println();
182         pw.println("    public java.lang.String get" + portName + "Address() {");
183         pw.println("        return " + portName + "_address;");
184         pw.println("    }");
185         pw.println();
186     } // writeAddressInfo
187 
188     /***
189      * Write the private WSDD service name field and the public accessors for it.
190      */
191     protected void writeWSDDServiceNameInfo(PrintWriter pw,
192                                             String wsddServiceName, String portName) {
193         // Write the private WSDD service name field
194         pw.println("    // " + Messages.getMessage("wsddServiceName00"));
195         pw.println("    private java.lang.String " + wsddServiceName + " = \"" + portName + "\";");
196         pw.println();
197 
198         // Write the public accessors for the WSDD service name
199         pw.println("    public java.lang.String get" + wsddServiceName + "() {");
200         pw.println("        return " + wsddServiceName + ";");
201         pw.println("    }");
202         pw.println();
203         pw.println("    public void set" + wsddServiceName + "(java.lang.String name) {");
204         pw.println("        " + wsddServiceName + " = name;");
205         pw.println("    }");
206         pw.println();
207     } // writeWSDDServiceNameInfo
208 
209     /***
210      * Write the get<portName>() method.
211      */
212     protected void writeGetPortName(PrintWriter pw, String bindingType,
213                                     String portName) {
214         pw.println("    public " + bindingType + " get" + portName + "() throws " + javax.xml.rpc.ServiceException.class.getName() + " {");
215         pw.println("       java.net.URL endpoint;");
216         pw.println("        try {");
217         pw.println("            endpoint = new java.net.URL(" + portName + "_address);");
218         pw.println("        }");
219         pw.println("        catch (java.net.MalformedURLException e) {");
220         pw.println("            throw new javax.xml.rpc.ServiceException(e);");
221         pw.println("        }");
222         pw.println("        return get" + portName + "(endpoint);");
223         pw.println("    }");
224         pw.println();
225     } // writeGetPortName
226 
227     /***
228      * Write the get<portName>(URL) method.
229      */
230     protected void writeGetPortNameURL(PrintWriter pw, String bindingType,
231                                        String portName, String stubClass, String wsddServiceName) {
232         pw.println("    public " + bindingType + " get" + portName + "(java.net.URL portAddress) throws " + javax.xml.rpc.ServiceException.class.getName() + " {");
233         pw.println("        try {");
234         pw.println("            " + stubClass + " _stub = new " + stubClass + "(portAddress, this);");
235         pw.println("            _stub.setPortName(get" + wsddServiceName + "());");
236         pw.println("            return _stub;");
237         pw.println("        }");
238         pw.println("        catch (org.apache.axis.AxisFault e) {");
239         pw.println("            return null;");
240         pw.println("        }");
241         pw.println("    }");
242         pw.println();
243     } // writeGetPortNameURL
244 
245     /***
246      * Write the getPort(Class serviceInterfaceWriter) method.
247      */
248     protected void writeGetPortClass(PrintWriter pw, Vector getPortIfaces,
249                                      Vector getPortStubClasses, Vector getPortPortNames,
250                                      boolean printGetPortNotice) {
251         pw.println("    /**");
252         pw.println("     * " + Messages.getMessage("getPortDoc00"));
253         pw.println("     * " + Messages.getMessage("getPortDoc01"));
254         pw.println("     * " + Messages.getMessage("getPortDoc02"));
255         if (printGetPortNotice) {
256             pw.println("     * " + Messages.getMessage("getPortDoc03"));
257             pw.println("     * " + Messages.getMessage("getPortDoc04"));
258         }
259         pw.println("     */");
260         pw.println("    public java.rmi.Remote getPort(Class serviceEndpointInterface) throws " + javax.xml.rpc.ServiceException.class.getName() + " {");
261         if (getPortIfaces.size() == 0) {
262             pw.println("        throw new " + javax.xml.rpc.ServiceException.class.getName() + "(\""
263                     + Messages.getMessage("noStub") + "  \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));");
264         } else {
265             pw.println("        try {");
266             for (int i = 0; i < getPortIfaces.size(); ++i) {
267                 String iface = (String) getPortIfaces.get(i);
268                 String stubClass = (String) getPortStubClasses.get(i);
269                 String portName = (String) getPortPortNames.get(i);
270                 pw.println("            if (" + iface + ".class.isAssignableFrom(serviceEndpointInterface)) {");
271                 pw.println("                " + stubClass + " _stub = new " + stubClass + "(new java.net.URL(" + portName + "_address), this);");
272                 pw.println("                _stub.setPortName(get" + portName + "WSDDServiceName());");
273                 pw.println("                return _stub;");
274                 pw.println("            }");
275             }
276             pw.println("        }");
277             pw.println("        catch (java.lang.Throwable t) {");
278             pw.println("            throw new " + javax.xml.rpc.ServiceException.class.getName() + "(t);");
279             pw.println("        }");
280             pw.println("        throw new " + javax.xml.rpc.ServiceException.class.getName() + "(\""
281                     + Messages.getMessage("noStub") + "  \" + (serviceEndpointInterface == null ? \"null\" : serviceEndpointInterface.getName()));");
282         }
283         pw.println("    }");
284         pw.println();
285     } // writeGetPortClass
286 
287     /***
288      * Write the getPort(QName portName, Class serviceInterfaceWriter) method.
289      */
290     protected void writeGetPortQNameClass(PrintWriter pw,
291                                           Vector getPortPortNames) {
292         pw.println("    /**");
293         pw.println("     * " + Messages.getMessage("getPortDoc00"));
294         pw.println("     * " + Messages.getMessage("getPortDoc01"));
295         pw.println("     * " + Messages.getMessage("getPortDoc02"));
296         pw.println("     */");
297         pw.println("    public java.rmi.Remote getPort(javax.xml.namespace.QName portName, Class serviceEndpointInterface) throws " + javax.xml.rpc.ServiceException.class.getName() + " {");
298         pw.println("        if (portName == null) {");
299         pw.println("            return getPort(serviceEndpointInterface);");
300         pw.println("        }");
301         pw.println("        String inputPortName = portName.getLocalPart();");
302         pw.print("        ");
303         for (int i = 0; i < getPortPortNames.size(); ++i) {
304             String portName = (String) getPortPortNames.get(i);
305             pw.println("if (\"" + portName + "\".equals(inputPortName)) {");
306             pw.println("            return get" + portName + "();");
307             pw.println("        }");
308             pw.print("        else ");
309         }
310         pw.println(" {");
311         /*
312         pw.println("            java.rmi.Remote _stub = getPort(serviceEndpointInterface);");
313         pw.println("            ((org.apache.axis.client.Stub) _stub).setPortName(portName);");
314         pw.println("            return _stub;");
315         */
316         pw.println("        throw new " + javax.xml.rpc.ServiceException.class.getName() + "(\""
317                 + "Invalid QName" + "\");");
318         pw.println("        }");
319         pw.println("    }");
320         pw.println();
321     } // writeGetPortQNameClass
322 
323     /***
324      * Write the getServiceName method.
325      */
326     protected void writeGetServiceName(PrintWriter pw, QName qname) {
327         pw.println("    public javax.xml.namespace.QName getServiceName() {");
328         pw.println("        return " + Utils.getNewQName(qname) + ";");
329         pw.println("    }");
330         pw.println();
331     } // writeGetServiceName
332 
333     /***
334      * Write the getPorts method.
335      */
336     protected void writeGetPorts(PrintWriter pw, Vector portNames) {
337         pw.println("    private java.util.HashSet ports = null;");
338         pw.println();
339         pw.println("    public java.util.Iterator getPorts() {");
340         pw.println("        if (ports == null) {");
341         pw.println("            ports = new java.util.HashSet();");
342         for (int i = 0; i < portNames.size(); ++i) {
343             pw.println("            ports.add(new javax.xml.namespace.QName(\"" +
344                     portNames.get(i) + "\"));");
345         }
346         pw.println("        }");
347         pw.println("        return ports.iterator();");
348         pw.println("    }");
349         pw.println();
350     } // writeGetPorts
351 
352 }