KickJava   Java API By Example, From Geeks To Geeks.

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


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.utils.JavaUtils;
19 import org.apache.axis.utils.Messages;
20 import org.apache.axis.wsdl.symbolTable.BindingEntry;
21 import org.apache.axis.wsdl.symbolTable.PortTypeEntry;
22 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
23 import org.apache.axis.wsdl.symbolTable.SymbolTable;
24
25 import javax.wsdl.Binding;
26 import javax.wsdl.Port;
27 import javax.wsdl.Service;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 /**
34  * This is Wsdl2java's service writer. It writes the <serviceName>.java file.
35  */

36 public class JavaServiceIfaceWriter extends JavaClassWriter {
37
38     /** Field service */
39     private Service service;
40
41     /** Field symbolTable */
42     private SymbolTable symbolTable;
43
44     /**
45      * Constructor.
46      *
47      * @param emitter
48      * @param sEntry
49      * @param symbolTable
50      */

51     protected JavaServiceIfaceWriter(Emitter emitter, ServiceEntry sEntry,
52                                      SymbolTable symbolTable) {
53
54         super(emitter, sEntry.getName(), "service");
55
56         this.service = sEntry.getService();
57         this.symbolTable = symbolTable;
58     } // ctor
59

60     /**
61      * Returns "interface ".
62      *
63      * @return
64      */

65     protected String JavaDoc getClassText() {
66         return "interface ";
67     } // getClassString
68

69     /**
70      * Returns "extends javax.xml.rpc.Service ".
71      *
72      * @return
73      */

74     protected String JavaDoc getExtendsText() {
75         return "extends javax.xml.rpc.Service ";
76     } // getExtendsText
77

78     /**
79      * Write the body of the service file.
80      *
81      * @param pw
82      * @throws IOException
83      */

84     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
85
86         // output comments
87
writeComment(pw, service.getDocumentationElement(), false);
88
89         // get ports
90
Map JavaDoc portMap = service.getPorts();
91         Iterator JavaDoc portIterator = portMap.values().iterator();
92
93         // write a get method for each of the ports with a SOAP binding
94
while (portIterator.hasNext()) {
95             Port p = (Port) portIterator.next();
96             Binding binding = p.getBinding();
97
98             if (binding == null) {
99                 throw new IOException JavaDoc(Messages.getMessage("emitFailNoBinding01",
100                         new String JavaDoc[]{
101                             p.getName()}));
102             }
103
104             BindingEntry bEntry =
105                     symbolTable.getBindingEntry(binding.getQName());
106
107             if (bEntry == null) {
108                 throw new IOException JavaDoc(
109                         Messages.getMessage(
110                                 "emitFailNoBindingEntry01",
111                                 new String JavaDoc[]{binding.getQName().toString()}));
112             }
113
114             PortTypeEntry ptEntry =
115                     symbolTable.getPortTypeEntry(binding.getPortType().getQName());
116
117             if (ptEntry == null) {
118                 throw new IOException JavaDoc(
119                         Messages.getMessage(
120                                 "emitFailNoPortType01",
121                                 new String JavaDoc[]{
122                                     binding.getPortType().getQName().toString()}));
123             }
124
125             // If this isn't an SOAP binding, skip it
126
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
127                 continue;
128             }
129
130             // JSR 101 indicates that the name of the port used
131
// in the java code is the name of the wsdl:port. It
132
// does not indicate what should occur if the
133
// wsdl:port name is not a java identifier. The
134
// TCK depends on the case-sensitivity being preserved,
135
// and the interop tests have port names that are not
136
// valid java identifiers. Thus the following code.
137

138             // java port <--> wsdl port mapping
139
String JavaDoc portName = (String JavaDoc) bEntry.getDynamicVar(JavaServiceWriter.PORT_NAME + ":" + p.getName());
140             if (portName == null) {
141                 portName = p.getName();
142             }
143
144             if (!JavaUtils.isJavaId(portName)) {
145                 portName = Utils.xmlNameToJavaClass(portName);
146             }
147
148             // If there is not literal use, the interface name is the portType name.
149
// Otherwise it is the binding name.
150
String JavaDoc bindingType =
151                     (String JavaDoc) bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
152
153             // Write out the get<PortName> methods
154
pw.println(" public java.lang.String get" + portName
155                     + "Address();");
156             pw.println();
157             pw.println(" public " + bindingType + " get" + portName
158                     + "() throws "
159                     + javax.xml.rpc.ServiceException JavaDoc.class.getName() + ";");
160             pw.println();
161             pw.println(" public " + bindingType + " get" + portName
162                     + "(java.net.URL portAddress) throws "
163                     + javax.xml.rpc.ServiceException JavaDoc.class.getName() + ";");
164         }
165     } // writeFileBody
166
} // class JavaServiceIfaceWriter
167
Popular Tags