KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > jaxrpc > WrapperClientWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.registry.jaxrpc;
21
22
23 import com.sun.xml.rpc.processor.model.java.JavaException;
24 import com.sun.xml.rpc.processor.model.java.JavaMethod;
25 import com.sun.xml.rpc.processor.model.java.JavaParameter;
26 import com.sun.xml.rpc.processor.model.Port;
27 import com.sun.xml.rpc.processor.model.Operation;
28
29 import org.netbeans.modules.websvc.registry.util.Util;
30
31 import java.io.PrintWriter JavaDoc;
32 import java.io.Writer JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * A simple writer to write the Java Source.
39  * @author Winston Prakash
40  */

41 public class WrapperClientWriter extends java.io.PrintWriter JavaDoc {
42     
43     private String JavaDoc serviceName;
44     private String JavaDoc serviceVariable;
45     
46     private String JavaDoc className;
47     private String JavaDoc superClassName;
48     private Set JavaDoc interfaces = new HashSet JavaDoc();
49     private String JavaDoc packageName;
50     private Set JavaDoc imports = new HashSet JavaDoc();
51     private Set JavaDoc ports = new HashSet JavaDoc();
52     
53     private Set JavaDoc constructorStatements = new HashSet JavaDoc();
54     
55     int indent = 0;
56     
57     /** Creates a new instance of JavaWriter */
58     public WrapperClientWriter(Writer JavaDoc writer){
59         super(writer);
60     }
61     
62     public void setContainedClassInfo(String JavaDoc serviceName){
63         this.serviceName = serviceName;
64         serviceVariable = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length());
65         serviceVariable = serviceVariable.toLowerCase() + "1";
66     }
67     
68     /** Set package name */
69     public void setPackage(String JavaDoc pkgName){
70         packageName = pkgName;
71     }
72     
73     public void addImport(String JavaDoc importLine){
74         imports.add(importLine);
75     }
76     
77     /** Set the name of the class */
78     public void setName(String JavaDoc name){
79         className = name;
80     }
81     
82     /** Set the name of the super class this class would extends */
83     public void setSuperClass(String JavaDoc superClass){
84         superClassName = superClass;
85     }
86     
87     /** Set the name of the interfaces this class would extends */
88     public void addInterface(String JavaDoc interfaceName){
89         interfaces.add(interfaceName);
90     }
91     
92     public void addConstructorStatements(String JavaDoc statement){
93         constructorStatements.add(statement);
94     }
95     
96     public void addPort(Port inPort){
97         ports.add(inPort);
98     }
99     
100     public void writeClass(){
101         /**
102          * Write the package statement
103          */

104         println("package " + packageName + ";");
105         println();
106         
107         
108         /**
109          * Write the imports statement.
110          */

111         if (!imports.isEmpty()) {
112             Iterator JavaDoc iter = imports.iterator();
113             while(iter.hasNext()) {
114                 println("import " + iter.next() + ";");
115             }
116             println();
117         }
118         println("import java.rmi.RemoteException;");
119         println("import javax.xml.rpc.ServiceException;");
120         println("import javax.xml.rpc.Stub;");
121         println();
122         
123         /**
124          * Write the class declaration
125          */

126         print("public class " + className);
127         if(superClassName != null) print("extends " + superClassName + " ");
128         if (!interfaces.isEmpty()) {
129             println("implements ");
130             Iterator JavaDoc iter = interfaces.iterator();
131             while(iter.hasNext()) {
132                 print((String JavaDoc)iter.next());
133                 if(iter.hasNext()) print(",");
134             }
135         }
136         println(" {");
137         println();
138         
139         /**
140          * write the class instance variables.
141          */

142         
143         /**
144          * write a variable for the service implementation.
145          */

146         println(" private " + serviceName + " " + serviceVariable + " = " + "new " + serviceName + "_Impl();");
147         
148         /**
149          * write a variable for each port in the serviee.
150          */

151         if(!ports.isEmpty()) {
152             Iterator JavaDoc portIterator = ports.iterator();
153             Port currentPort = null;
154             while(portIterator.hasNext()) {
155                 currentPort = (Port)portIterator.next();
156                 /**
157                  * get the Java class name for the port.
158                  */

159                String JavaDoc portImplName = Util.getProperPortName(currentPort.getName().getLocalPart());
160                String JavaDoc portInterfaceName = currentPort.getJavaInterface().getName();
161                 /**
162                  * Strip off the leading package qualification since we don't need it.
163                  */

164                 portInterfaceName = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length());
165                 /**
166                  * create the variable name for the port
167                  * NOTE: - When we write out the methods, we'll need to use the same convention of naming the port that
168                  * we do here.
169                  */

170                 String JavaDoc portInterfaceVariable = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length());
171                 String JavaDoc portInterfacePrefix = portInterfaceVariable.toLowerCase() ;
172                 portInterfaceVariable = portInterfaceVariable.toLowerCase() + "1";
173                 /**
174                  * Apparently, the compiletool uppercases the first letter of the port name to make it a proper getter so we need to do the same.
175                  */

176                 String JavaDoc modifiedPortName = Util.upperCaseFirstChar(portInterfaceName);
177                 
178                 
179                 println(" private " + portInterfaceName + " " + portInterfaceVariable + ";"); // " + portInterfaceVariable + " = " + serviceVariable + ".get" +portImplName +"();");
180
addConstructorStatements(portInterfaceVariable + " = " + serviceVariable + ".get" + portImplName +"()");
181                 println(" private Stub " + portInterfacePrefix + "Stub;"); // = (Stub)" + portInterfaceVariable + ";");
182
addConstructorStatements(portInterfacePrefix + "Stub = (Stub)" + portInterfaceVariable);
183             }
184             
185             println();
186             // Write the constructor
187
println(" public " + className + "() throws ServiceException {");
188             if (!constructorStatements.isEmpty()) {
189                 Iterator JavaDoc iter = constructorStatements.iterator();
190                 while(iter.hasNext()) {
191                     println(" " + (String JavaDoc)iter.next() + ";");
192                 }
193             }
194             println(" }");
195             println();
196             
197             printOperations(ports);
198             println("}");
199             
200         }
201     }
202     
203     private void printOperations(Set JavaDoc inPorts) {
204         Iterator JavaDoc portIterator = inPorts.iterator();
205         Port currentPort = null;
206         while(portIterator.hasNext()) {
207             currentPort = (Port)portIterator.next();
208             /**
209              * Get the name for the current port.
210              */

211             String JavaDoc lowercasePortName = Util.getProperPortName(currentPort.getName().getLocalPart()).toLowerCase();
212             /**
213              * get the Java class name for the port.
214              */

215             String JavaDoc portInterfaceName = currentPort.getJavaInterface().getName();
216             /**
217              * Strip off the leading package qualification since we don't need it.
218              */

219             portInterfaceName = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length());
220             /**
221              * create the variable name for the port
222              * NOTE: -This variable name needs to be the same one written out for the class instance varables.
223              */

224             String JavaDoc portInterfaceVariable = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length());
225             String JavaDoc portInterfacePrefix = portInterfaceVariable.toLowerCase() ;
226             portInterfaceVariable = portInterfaceVariable.toLowerCase() + "1";
227             Iterator JavaDoc operationsIterator = currentPort.getOperations();
228             Operation currentOperation = null;
229             while(operationsIterator.hasNext()) {
230                 currentOperation = (Operation)operationsIterator.next();
231                 if(null == currentOperation) {
232                     continue;
233                 }
234                 JavaMethod method = currentOperation.getJavaMethod();
235                 String JavaDoc modifiedMethodName = Util.upperCaseFirstChar(method.getName());
236                 println();
237                 print(" public " + method.getReturnType().getRealName() + " ");
238                 print(lowercasePortName + modifiedMethodName + "(");
239                 Iterator JavaDoc params = method.getParameters();
240                 String JavaDoc parameterType = "";
241                 
242                 while (params.hasNext()) {
243                     JavaParameter param = (JavaParameter)params.next();
244                     /**
245                      * Bug fix: 5059732
246                      * If the parameter is a "Holder" we need the holder type and not the JavaType. This is
247                      * typically the case when there is no return type and the parameter's meant to be mutable, pass-by-reference
248                      * type parameters. I took the code below directly from the JAX-RPC class:
249                      * "com.sun.xml.rpc.processor.generator.StubGenerator"
250                      * - David Botterill 6/8/2004
251                      */

252                     
253                     parameterType = Util.getParameterType(currentPort,param);
254                     /**
255                      * end of bug fix: 5059732
256                      */

257                     
258                     print(parameterType + " " + param.getName());
259                     if(params.hasNext()) write(", ");
260                 }
261                 print(") ");
262                 Iterator JavaDoc exceptions = method.getExceptions();
263                 /**
264                  * Bugid: 4970323 This area of code was not exercised before WSDLInfo was fixed. Hence this bug showed up as a result of
265                  * fixing a bug.
266                  *
267                  */

268                 
269                 /**
270                  * We need a "throws" at least for the RemoteException
271                  *
272                  */

273                 print(" throws ");
274                 
275                 while (exceptions.hasNext()) {
276                     /**
277                      * Bugid: 4970323 - The return type of an exceptions is a String not a JavaException. This
278                      * can only be know for sure by reading the current JavaException code since the "JavaMethod.getExceptions()"
279                      * method returns an Iterator and the javadoc says nothing of it being a String.
280                      *
281                      * This area of code was not exercised before WSDLInfo was fixed. Hence this bug showed up as a result of
282                      * fixing a bug.
283                      */

284                     /**
285                      * Make sure we don't get back a null or an empty String. Check to make sure
286                      * the Object is a string in case the API matures and JavaException turns back into a real object with
287                      * a "getName" method.
288                      */

289                     Object JavaDoc currentException = exceptions.next();
290                     if(null != currentException &&
291                     currentException instanceof String JavaDoc &&
292                     ((String JavaDoc)currentException).length() > 0) {
293                         print((String JavaDoc)currentException);
294                         /**
295                          * always print a "," because we have the RemoteException following.
296                          */

297                         print(", ");
298                     }
299                 }
300                 println(" RemoteException { ");
301                 
302                 if(!"void".equals(method.getReturnType().getRealName())){
303                     print(" return " + portInterfaceVariable + "." + method.getName()+ "(");
304                 }else{
305                     print(" " + portInterfaceVariable + "." + method.getName() + "(");
306                 }
307                 params = method.getParameters();
308                 while (params.hasNext()) {
309                     JavaParameter param = (JavaParameter)params.next();
310                     //if(param.isHolder()){
311
//print(param.getHolderName());
312
//}else {
313
print(param.getName());
314                     //}
315
if(params.hasNext()) write(", ");
316                 }
317                 println(");");
318                 
319                 
320                 println(" }");
321             }
322             
323             /**
324              * Now print out the methods for setting the Stub properties.
325              */

326             println("public void " + lowercasePortName + "SetUsername(String inUserName) {");
327             println(" " + portInterfacePrefix + "Stub._setProperty(Stub.USERNAME_PROPERTY, inUserName);");
328             println("}");
329             println();
330             println("public void " + lowercasePortName + "SetPassword(String inPassword) {");
331             println(" " + portInterfacePrefix + "Stub._setProperty(Stub.PASSWORD_PROPERTY, inPassword);");
332             println("}");
333             println();
334             println("public void " + lowercasePortName + "SetAddress(String inAddress) {");
335             println(" " + portInterfacePrefix + "Stub._setProperty(Stub.ENDPOINT_ADDRESS_PROPERTY, inAddress);");
336             println("}");
337             println();
338         }
339         
340     }
341     
342     public class Method {
343         Set JavaDoc methodStatement = new HashSet JavaDoc();
344         public Method(JavaMethod method){
345         }
346         
347         public void addStatement(String JavaDoc statement){
348             methodStatement.add(statement);
349         }
350         
351         public Set JavaDoc getStatements(){
352             return methodStatement;
353         }
354     }
355     
356     /**
357      * @param args the command line arguments
358      */

359     public static void main(String JavaDoc[] args) {
360     }
361     
362 }
363
Popular Tags