1 19 20 package org.netbeans.modules.websvc.registry.wsdl; 21 22 import org.openide.ErrorManager; 23 import org.netbeans.modules.websvc.registry.jaxrpc.Wsdl2Java; 24 import org.netbeans.modules.websvc.registry.model.WebServiceData; 25 import org.netbeans.modules.websvc.registry.nodes.WebServicesPortNode; 26 import org.netbeans.modules.websvc.registry.util.Util; 27 import com.sun.xml.rpc.processor.ProcessorOptions; 28 import com.sun.xml.rpc.processor.config.Configuration; 29 import com.sun.xml.rpc.processor.config.WSDLModelInfo; 30 import com.sun.xml.rpc.processor.model.Operation; 31 import com.sun.xml.rpc.processor.model.Port; 32 import com.sun.xml.rpc.processor.model.java.JavaMethod; 33 import com.sun.xml.rpc.processor.model.java.JavaParameter; 34 import com.sun.xml.rpc.processor.modeler.ModelerException; 35 import com.sun.xml.rpc.processor.modeler.wsdl.WSDLModelerBase; 36 import com.sun.xml.rpc.processor.util.ClientProcessorEnvironment; 37 import com.sun.xml.rpc.spi.model.JavaInterface; 38 import com.sun.xml.rpc.spi.model.Model; 39 import com.sun.xml.rpc.spi.model.Service; 40 import com.sun.xml.rpc.util.JAXRPCClassFactory; 41 import java.io.ByteArrayOutputStream ; 42 import java.io.File ; 43 import java.io.IOException ; 44 import java.io.StringWriter ; 45 import java.net.URL ; 46 import java.util.HashSet ; 47 import java.util.Iterator ; 48 import java.util.Properties ; 49 import java.util.Set ; 50 import org.openide.util.NbBundle; 51 52 53 54 55 56 57 61 public class WSDLInfo { 62 63 File errorFile; 64 private String program ="wscompile"; 65 private String outputDir; 66 private String nonClassOutputDir; 67 private String sourceOutputDir; 68 private URL wsdlUrl; 69 private String packageName=""; 70 private String errorMessage; 71 private boolean hasOperations; 72 73 77 private static final String WSDL_DOCUMENTATION = "com.sun.xml.rpc.processor.modeler.wsdl.documentation"; 78 79 public static final String SERVICE_DISPLAYNAME_TOKEN = "%%DISPLAY_NAME%%"; 80 public static final String TOKEN_SEPARATOR = "@"; 81 public static final String SIG_SEPARATOR = "|"; 82 public static final String RETURN_SEPARATOR = "!"; 83 public static final String PARAM_SEPARATOR = ":"; 84 85 private Model wsdlModel; 86 Wsdl2Java wsdl2Java; 87 88 boolean removeGeneratedFiles = true; 89 90 public WSDLInfo() { 91 try{ 92 errorFile = File.createTempFile("wscompile","error"); 93 }catch(IOException exc){ 94 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, exc); 95 } 96 } 97 98 public boolean hasOperations() { 99 return hasOperations; 100 } 101 102 public void setWsdlUrl(URL url){ 103 wsdlUrl = url; 104 } 105 106 public URL getWsdlUrl(){ 107 return wsdlUrl; 108 } 109 110 public void setPackageName(String pkgName){ 111 packageName = pkgName; 112 } 113 114 public String getPackageName(){ 115 if(packageName == null) { 116 packageName = Wsdl2Java.DEFAULT_TARGET_PACKAGE; 117 } 118 return packageName; 119 } 120 121 public String getErrorMessage(){ 122 StringWriter errorWriter = new StringWriter (); 123 StringBuffer strBuffer = new StringBuffer (); 124 errorWriter.write("<B>" + NbBundle.getMessage(this.getClass(), "WSDLINFO_ERROR_PARSING") + " </B>"); 125 errorWriter.write("<BR><B><I>" + errorMessage + "</I></B>"); 126 return errorWriter.toString(); 127 } 128 129 public void setOutputDirectory(String dir){ 130 outputDir = dir; 131 } 132 133 public String getOutputDirectory(){ 134 if (outputDir == null){ 135 File outDir = new File (System.getProperty("user.home")); 136 return outDir.getAbsolutePath(); 137 } 138 return outputDir; 139 } 140 141 public void setRemoveGeneratedFiles(boolean remove){ 142 removeGeneratedFiles = remove; 143 } 144 145 public boolean create() { 146 156 157 160 161 WSDLModelInfo modelInfo = new WSDLModelInfo(); 162 ClientProcessorEnvironment env = new ClientProcessorEnvironment(new ByteArrayOutputStream (), null, null); 163 164 Configuration config = new Configuration(env); 165 166 modelInfo = new WSDLModelInfo(); 167 168 config.setModelInfo(modelInfo); 169 modelInfo.setParent(config); 170 171 172 modelInfo.setLocation(getWsdlUrl().toExternalForm()); 173 Properties options = new Properties (); 174 options.put(ProcessorOptions.VALIDATE_WSDL_PROPERTY, "true"); 175 options.put(ProcessorOptions.SEARCH_SCHEMA_FOR_SUBTYPES, "true"); 176 options.put(ProcessorOptions.USE_WSI_BASIC_PROFILE, "false"); 177 178 179 WSDLModelerBase modeler = JAXRPCClassFactory.newInstance().createWSDLModeler(modelInfo, options); 180 181 184 Model tmpModel = null; 185 try { 186 tmpModel = modeler.buildModel(); 187 } catch(Exception me) { 188 191 errorMessage = " Exception = " + me.getLocalizedMessage();; 192 return false; 193 } 194 195 this.setWSDLModel(tmpModel); 196 197 return true; 198 } 199 200 private void setWSDLModel(Model inModel) { 201 wsdlModel = inModel; 202 } 203 204 public Set getWebServices(){ 205 Set webServices = new HashSet (); 206 Model model = getWSDLModel(); 207 if(null != model) { 208 for (Iterator services = model.getServices(); services.hasNext(); ) { 209 WebServiceData wsData = new WebServiceData() ; 210 wsData.setURL(getWsdlUrl().toString()); 211 Service service = (Service) services.next(); 212 213 String serviceName = service.getJavaIntf().getName(); 214 wsData.setName(serviceName); 215 serviceName = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length()); 216 String hotsName = getWsdlUrl().getHost(); 217 String displayName = serviceName; 218 wsData.setDisplayName(displayName); 219 220 Iterator ports = service.getPorts(); 221 while (ports.hasNext()) { 222 Port port = (Port)ports.next(); 223 227 port.setProperty(WebServiceData.PORT_PROPERTY_NAME, port.getName().getLocalPart()); 228 wsData.addPort(port); 229 wsData.setWebServiceAddress(port.getAddress()); 230 String portName = port.getJavaInterface().getRealName(); 231 portName = portName.substring(portName.lastIndexOf('.') + 1, portName.length()); 232 wsData.setProxy(portName); 233 242 } 243 webServices.add(wsData); 244 } 245 } 246 247 return webServices; 248 } 249 250 private Model getWSDLModel() { 251 return wsdlModel; 252 } 253 258 public String getServiceInfo(String inServiceName) { 259 StringWriter strWriter = new StringWriter (); 260 261 262 Model model = getWSDLModel(); 263 if(null != model) { 264 Iterator services = model.getServices(); 265 for (int ii=0; services.hasNext();ii++ ) { 266 Service service = (Service) services.next(); 267 String serviceName = service.getJavaIntf().getName(); 268 if(serviceName.equals(inServiceName)) { 269 serviceName = serviceName.substring(serviceName.lastIndexOf('.') + 1, serviceName.length()); 270 strWriter.write("<a name=\"TOPOFPAGE\"> </a>"); 271 strWriter.write("<FORM NAME=\"service_form." + serviceName + "\">"); 273 strWriter.write("<TABLE><TR>"); 274 strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "SERVICE_WSDL_NAME") + "</B></TD>"); 275 strWriter.write("<TD VALIGN=TOP>" + serviceName + "</TD>"); 276 strWriter.write("</TR><TR>"); 277 strWriter.write("<TD VALIGN=TOP><LABEL FOR=\"service_name." + serviceName +"\" ><B>" 278 + NbBundle.getMessage(this.getClass(), "SERVICE_DISPLAY_NAME") + "</B></LABEL></TD>"); 279 strWriter.write("<TD VALIGN=TOP><INPUT TYPE=TEXT ID=\"service_name." + serviceName + "\" NAME=\"service_name." 280 + serviceName + "\" VALUE=\"" + 281 this.SERVICE_DISPLAYNAME_TOKEN + 282 "\" STYLE=\"background: #ade7ff\"></TD>"); 283 strWriter.write("</TR><TR>"); 284 strWriter.write("<TD VALIGN=TOP><LABEL FOR=\"package_name." + serviceName +"\" ><B>" 285 + NbBundle.getMessage(this.getClass(), "CLIENT_PACKAGE_NAME") + "</B></LABEL></TD>"); 286 strWriter.write("<TD VALIGN=TOP><INPUT TYPE=TEXT SIZE=30 ID=\"package_name." + serviceName 287 + "\" NAME=\"package_name." + serviceName + "\" VALUE=\"" + 288 Wsdl2Java.DEFAULT_TARGET_PACKAGE + "." + serviceName.toLowerCase() + 289 "\" STYLE=\"background: #ade7ff\"></TD>"); 290 strWriter.write("</TR>"); 291 String serviceDocumentationContent = (String )service.getProperty(this.WSDL_DOCUMENTATION); 292 293 if(null != serviceDocumentationContent) { 294 strWriter.write("<TR>"); 295 strWriter.write("<TD VALIGN=TOP><LABEL><B>" + NbBundle.getMessage(this.getClass(), "SERVICE_DOCUMENTATION") + "</B></LABEL></TD>"); 296 strWriter.write("<TD VALIGN=TOP>" + serviceDocumentationContent + "</TD>"); 297 strWriter.write("</TR>"); 298 } 299 Iterator ports = service.getPorts(); 300 if(!ports.hasNext()) { 301 strWriter.write("</TABLE>"); 302 } 303 while (ports.hasNext()) { 304 Port port = (Port)ports.next(); 305 JavaInterface javaInterface = port.getJavaInterface(); 306 String proxy = javaInterface.getName(); 308 proxy = proxy.substring(proxy.lastIndexOf('.') + 1, proxy.length()); 309 strWriter.write("<TR>"); 311 strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "WEB_SERVICE_PORTNNAME") + "</B></TD>"); 312 strWriter.write("<TD VALIGN=TOP>" +port.getName().toString() +"</TD>"); 313 strWriter.write("</TR>"); 314 strWriter.write("<TR>"); 315 strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "WEB_SERVICE_ADDRESS") + "</B></TD>"); 316 strWriter.write("<TD VALIGN=TOP>" +port.getAddress() +"</TD>"); 317 strWriter.write("</TR>"); 318 Iterator operations = port.getOperations(); 319 strWriter.write("<TR>"); 320 strWriter.write("<TD VALIGN=TOP><B>" + NbBundle.getMessage(this.getClass(), "METHODS") + "</B></TD>"); 321 strWriter.write("</TR>"); 322 int methodCount=0; 323 while (operations.hasNext()) { 324 hasOperations=true; 325 Operation operation = (Operation) operations.next(); 326 JavaMethod method = operation.getJavaMethod(); 327 strWriter.write("<TR>"); 328 strWriter.write("<TD VALIGN=TOP>"); 329 337 String signatureString = method.getReturnType().getRealName() + RETURN_SEPARATOR; 338 Iterator paramameters = method.getParameters(); 339 while (paramameters.hasNext()) { 340 JavaParameter currentParam = (JavaParameter)paramameters.next(); 341 signatureString += currentParam.getType().getRealName() + PARAM_SEPARATOR; 342 } 343 344 strWriter.write("<A HREF=\"" + "test" + TOKEN_SEPARATOR + 345 serviceName + TOKEN_SEPARATOR + 346 port.getName().getLocalPart() + TOKEN_SEPARATOR + 347 method.getName() + TOKEN_SEPARATOR + 348 SIG_SEPARATOR + 349 signatureString + 350 "\">" + NbBundle.getMessage(this.getClass(), "TEST_METHOD")+ "</A>"); 351 strWriter.write("</TD>"); 352 strWriter.write("<TD VALIGN=TOP>"); 354 strWriter.write("<I>" + method.getReturnType().getRealName() + "</I> "); 356 strWriter.write("<B>" + method.getName() + "</B>("); 357 Iterator params = method.getParameters(); 358 while (params.hasNext()) { 359 strWriter.write("<I>"); 360 JavaParameter param = (JavaParameter)params.next(); 361 String parameterType = Util.getParameterType(port,param); 362 strWriter.write(parameterType); 363 if(params.hasNext()) strWriter.write(", "); 364 strWriter.write("</I>"); 365 } 366 strWriter.write(")"); 367 Iterator exceptions = method.getExceptionsList().iterator(); 368 while (exceptions.hasNext()) { 369 374 381 Object currentException = exceptions.next(); 382 if(null != currentException && 383 currentException instanceof String && 384 ((String )currentException).length() > 0) { 385 strWriter.write("<I>   throws   "); 386 strWriter.write((String )currentException); 387 if(params.hasNext()) strWriter.write(",  "); 388 strWriter.write("</I>"); 389 } 390 } 391 String operationDocumentationContent = (String )operation.getProperty(this.WSDL_DOCUMENTATION); 393 394 if(null != operationDocumentationContent) { 395 strWriter.write("<BR>" + operationDocumentationContent); 397 } 398 strWriter.write("</TD>"); 400 strWriter.write("</TR>"); 401 } 402 } 403 strWriter.write("</TABLE>"); 404 strWriter.write("</FORM>"); 405 if(services.hasNext()) { 406 strWriter.write("<HR>"); 407 } 408 411 break; 412 } 413 414 415 } 416 } else { 417 strWriter.write("<BR><B>" + NbBundle.getMessage(this.getClass(), "WSDLINFO_ERROR_PARSING") +"</B><BR> "); 418 419 } 420 421 return strWriter.toString(); 422 } 423 424 public static void main(String [] args){ 425 System.setProperty("http.proxyHost", "webcache.sfbay.sun.com"); 426 System.setProperty("http.proxyPort", "8080"); 427 428 WSDLInfo wsdlInfo = new WSDLInfo(); 429 File wsdlFile = new File ("D:\\realtimeservice.wsdl"); 430 431 try{ 432 wsdlInfo.setWsdlUrl(wsdlFile.toURL()); 433 }catch (Exception exc){ 435 exc.printStackTrace(); 436 System.exit(-1); 437 } 438 439 if(wsdlInfo.create()) { 440 wsdlInfo.getServiceInfo("RealTimeService"); 441 }else{ 442 System.out.println(wsdlInfo.getErrorMessage()); 443 } 444 } 445 } 446 | Popular Tags |