1 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 ; 32 import java.io.Writer ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.Set ; 36 37 41 public class WrapperClientWriter extends java.io.PrintWriter { 42 43 private String serviceName; 44 private String serviceVariable; 45 46 private String className; 47 private String superClassName; 48 private Set interfaces = new HashSet (); 49 private String packageName; 50 private Set imports = new HashSet (); 51 private Set ports = new HashSet (); 52 53 private Set constructorStatements = new HashSet (); 54 55 int indent = 0; 56 57 58 public WrapperClientWriter(Writer writer){ 59 super(writer); 60 } 61 62 public void setContainedClassInfo(String serviceName){ 63 this.serviceName = serviceName; 64 serviceVariable = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length()); 65 serviceVariable = serviceVariable.toLowerCase() + "1"; 66 } 67 68 69 public void setPackage(String pkgName){ 70 packageName = pkgName; 71 } 72 73 public void addImport(String importLine){ 74 imports.add(importLine); 75 } 76 77 78 public void setName(String name){ 79 className = name; 80 } 81 82 83 public void setSuperClass(String superClass){ 84 superClassName = superClass; 85 } 86 87 88 public void addInterface(String interfaceName){ 89 interfaces.add(interfaceName); 90 } 91 92 public void addConstructorStatements(String statement){ 93 constructorStatements.add(statement); 94 } 95 96 public void addPort(Port inPort){ 97 ports.add(inPort); 98 } 99 100 public void writeClass(){ 101 104 println("package " + packageName + ";"); 105 println(); 106 107 108 111 if (!imports.isEmpty()) { 112 Iterator 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 126 print("public class " + className); 127 if(superClassName != null) print("extends " + superClassName + " "); 128 if (!interfaces.isEmpty()) { 129 println("implements "); 130 Iterator iter = interfaces.iterator(); 131 while(iter.hasNext()) { 132 print((String )iter.next()); 133 if(iter.hasNext()) print(","); 134 } 135 } 136 println(" {"); 137 println(); 138 139 142 143 146 println(" private " + serviceName + " " + serviceVariable + " = " + "new " + serviceName + "_Impl();"); 147 148 151 if(!ports.isEmpty()) { 152 Iterator portIterator = ports.iterator(); 153 Port currentPort = null; 154 while(portIterator.hasNext()) { 155 currentPort = (Port)portIterator.next(); 156 159 String portImplName = Util.getProperPortName(currentPort.getName().getLocalPart()); 160 String portInterfaceName = currentPort.getJavaInterface().getName(); 161 164 portInterfaceName = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length()); 165 170 String portInterfaceVariable = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length()); 171 String portInterfacePrefix = portInterfaceVariable.toLowerCase() ; 172 portInterfaceVariable = portInterfaceVariable.toLowerCase() + "1"; 173 176 String modifiedPortName = Util.upperCaseFirstChar(portInterfaceName); 177 178 179 println(" private " + portInterfaceName + " " + portInterfaceVariable + ";"); addConstructorStatements(portInterfaceVariable + " = " + serviceVariable + ".get" + portImplName +"()"); 181 println(" private Stub " + portInterfacePrefix + "Stub;"); addConstructorStatements(portInterfacePrefix + "Stub = (Stub)" + portInterfaceVariable); 183 } 184 185 println(); 186 println(" public " + className + "() throws ServiceException {"); 188 if (!constructorStatements.isEmpty()) { 189 Iterator iter = constructorStatements.iterator(); 190 while(iter.hasNext()) { 191 println(" " + (String )iter.next() + ";"); 192 } 193 } 194 println(" }"); 195 println(); 196 197 printOperations(ports); 198 println("}"); 199 200 } 201 } 202 203 private void printOperations(Set inPorts) { 204 Iterator portIterator = inPorts.iterator(); 205 Port currentPort = null; 206 while(portIterator.hasNext()) { 207 currentPort = (Port)portIterator.next(); 208 211 String lowercasePortName = Util.getProperPortName(currentPort.getName().getLocalPart()).toLowerCase(); 212 215 String portInterfaceName = currentPort.getJavaInterface().getName(); 216 219 portInterfaceName = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length()); 220 224 String portInterfaceVariable = portInterfaceName.substring(portInterfaceName.lastIndexOf('.') + 1, portInterfaceName.length()); 225 String portInterfacePrefix = portInterfaceVariable.toLowerCase() ; 226 portInterfaceVariable = portInterfaceVariable.toLowerCase() + "1"; 227 Iterator 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 modifiedMethodName = Util.upperCaseFirstChar(method.getName()); 236 println(); 237 print(" public " + method.getReturnType().getRealName() + " "); 238 print(lowercasePortName + modifiedMethodName + "("); 239 Iterator params = method.getParameters(); 240 String parameterType = ""; 241 242 while (params.hasNext()) { 243 JavaParameter param = (JavaParameter)params.next(); 244 252 253 parameterType = Util.getParameterType(currentPort,param); 254 257 258 print(parameterType + " " + param.getName()); 259 if(params.hasNext()) write(", "); 260 } 261 print(") "); 262 Iterator exceptions = method.getExceptions(); 263 268 269 273 print(" throws "); 274 275 while (exceptions.hasNext()) { 276 284 289 Object currentException = exceptions.next(); 290 if(null != currentException && 291 currentException instanceof String && 292 ((String )currentException).length() > 0) { 293 print((String )currentException); 294 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 print(param.getName()); 314 if(params.hasNext()) write(", "); 316 } 317 println(");"); 318 319 320 println(" }"); 321 } 322 323 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 methodStatement = new HashSet (); 344 public Method(JavaMethod method){ 345 } 346 347 public void addStatement(String statement){ 348 methodStatement.add(statement); 349 } 350 351 public Set getStatements(){ 352 return methodStatement; 353 } 354 } 355 356 359 public static void main(String [] args) { 360 } 361 362 } 363 | Popular Tags |