1 19 20 package org.netbeans.modules.websvc.jaxrpc.dev.wizard; 21 22 import org.openide.filesystems.FileObject; 23 import org.netbeans.api.project.Project; 24 import java.io.IOException ; 25 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices; 26 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription; 27 import org.netbeans.modules.j2ee.dd.api.webservices.ServiceImplBean; 28 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent; 29 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport; 30 import java.net.URI ; 31 import org.openide.filesystems.FileLock; 32 import java.io.OutputStream ; 33 import org.openide.filesystems.FileSystem; 34 import org.openide.filesystems.FileUtil; 35 import org.openide.filesystems.Repository; 36 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation; 37 import org.openide.nodes.Node; 38 import org.netbeans.api.project.ant.AntArtifact; 39 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 40 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 41 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference; 42 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 43 import org.netbeans.spi.project.ant.AntArtifactProvider; 44 import java.util.Iterator ; 45 import javax.xml.parsers.ParserConfigurationException ; 46 import javax.xml.parsers.SAXParser ; 47 import javax.xml.parsers.SAXParserFactory ; 48 import javax.xml.transform.stream.StreamSource ; 49 import org.xml.sax.SAXException ; 50 import java.io.InputStream ; 51 import java.util.List ; 52 import java.util.Set ; 53 import org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.Configuration; 54 import org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.InterfaceType; 55 import org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.WsdlType; 56 import org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.wscreation.Bean; 57 import org.netbeans.modules.websvc.wsdl.config.PortInformationHandler; 58 import org.openide.ErrorManager; 59 import org.openide.NotifyDescriptor; 60 import org.openide.DialogDisplayer; 61 import org.openide.util.NbBundle; 62 63 public class WebServiceGenerator { 64 private WSGenerationUtil wsgenUtil = new WSGenerationUtil(); 65 private static final String WEBSERVICE_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE+"WSImplBean.xml"; private static final String WEBSERVICEJAVAEE5_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE+"WSImplBeanJavaEE5.xml"; private static final String INTERFACE_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE+"WSInterface.xml"; private static final String HANDLER_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE+"MessageHandler.xml"; public static final String WSDL_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE+"WSDL.xml"; 71 private String implBeanClass = ""; 72 private String intfClass = ""; 73 private String targetNS = null; 74 private String soapBinding = ""; 75 private String portTypeName = null; 76 public static final String WEBSERVICES_DD = "webservices"; private WebServicesSupport wsSupport; 78 private String wsName; 79 private FileObject pkg; 80 private Project project; 81 private List importedSchemaList; 82 private boolean changeWsName; 85 private String [] wscompileFeatures; 86 87 public WebServiceGenerator(WebServicesSupport wsSupport, String wsName, FileObject pkg, Project project) { 88 this.wsSupport = wsSupport; 89 this.wsName = wsName; 90 this.pkg = pkg; 91 this.project = project; 92 } 93 94 public WebServiceGenerator(FileObject pkg, Project project){ 95 this.pkg = pkg; 96 this.project = project; 97 } 98 99 public void generateWebService() throws IOException { 100 generateWebService(null); 101 } 102 103 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, FileObject originalFolder, String wsdlName, StreamSource source) throws IOException { 104 return wsgenUtil.generateWSDL(template, wsName, soapBinding, portTypeName, folder, originalFolder, wsdlName, source); 105 } 106 107 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, String wsdlName, StreamSource source) throws IOException { 108 return wsgenUtil.generateWSDL(template, wsName, soapBinding, portTypeName, folder, wsdlName, source); 109 } 110 111 117 public String parseWSDL(InputStream wsdlInputStream) throws IOException , NoWSPortDefinedException { 118 120 String changedWsName=null; 121 122 PortInformationHandler handler = new PortInformationHandler(); 123 124 try { 125 SAXParserFactory factory = SAXParserFactory.newInstance(); 126 factory.setNamespaceAware(true); 127 SAXParser saxParser = factory.newSAXParser(); 128 saxParser.parse(wsdlInputStream, handler); 129 } 130 catch(ParserConfigurationException ex) { 131 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 132 String mes = NbBundle.getMessage(WebServiceGenerator.class, "ERR_WsdlParseFailure"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 134 DialogDisplayer.getDefault().notify(desc); 135 return changedWsName; 136 } 137 catch(SAXException ex) { 138 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 139 String mes = NbBundle.getMessage(WebServiceGenerator.class, "ERR_WsdlParseFailure"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 141 DialogDisplayer.getDefault().notify(desc); 142 return changedWsName; 143 } 144 145 List entirePortList = handler.getEntirePortList(); 146 if (entirePortList.isEmpty()) { 147 throw new NoWSPortDefinedException(); 148 } 149 Iterator iterator = entirePortList.iterator(); 150 String firstPortType = null; 151 while(iterator.hasNext()) { 152 PortInformationHandler.PortInfo portInfo = (PortInformationHandler.PortInfo)iterator.next(); 153 if(firstPortType == null){ 154 firstPortType = portInfo.getPortType(); 156 } 157 if(portInfo.getBindingType() != null && 159 portInfo.getBindingType().equals("http://schemas.xmlsoap.org/wsdl/soap")) { portTypeName = portInfo.getPortType(); 161 soapBinding = portInfo.getBinding(); 162 break; 163 } 164 } 165 if(portTypeName == null) { 167 portTypeName = firstPortType; 168 } 169 if (portTypeName == null) { 170 throw new NoWSPortDefinedException(); 171 } 172 intfClass = WSGenerationUtil.getSelectedPackageName(pkg, project) + "." + normalizePortTypeName(portTypeName); 175 implBeanClass = intfClass + "_Impl"; targetNS = handler.getTargetNamespace(); 177 importedSchemaList = handler.getImportedSchemas(); 178 if (changeWsName) changedWsName=wsName+"_Service"; Set features = handler.getWscompileFeatures(); 180 wscompileFeatures=new String [features.size()]; 181 features.toArray(wscompileFeatures); 182 return changedWsName; 183 } 184 185 190 private String normalizePortTypeName(String portTypeName) { 191 if (portTypeName == null) { 192 return "unknown"; } 194 String first = portTypeName.substring(0,1); 195 String result = first.toUpperCase() + ((portTypeName.length() > 1) ? portTypeName.substring(1) : ""); 196 if (result.equals(wsName)) { 197 changeWsName=true; 198 } 199 return result; 200 201 } 202 203 public void generateMessageHandler(String handlerName) throws IOException { 204 String pkgName = wsgenUtil.getSelectedPackageName(pkg, project); 205 Bean b = wsgenUtil.getDefaultBean(); 206 b.setCommentDataWsName(handlerName); 207 b.setClassname(true); 208 b.setClassnameName(handlerName); 209 if(pkgName != null) { 210 b.setClassnamePackage(pkgName); 211 } 212 String handlerClass = wsgenUtil.getFullClassName(pkgName, 213 wsgenUtil.generateClass(HANDLER_TEMPLATE, b, pkg, true)); 214 } 215 216 public void generateWebService( Node[] nodes) throws IOException { 217 String pkgName = wsgenUtil.getSelectedPackageName(pkg, project); 218 219 Bean b = wsgenUtil.getDefaultBean(); 220 b.setCommentDataWsName(wsName); 221 b.setClassname(true); 222 b.setDelegateData(""); 223 224 b.setClassnameName(wsgenUtil.getBeanClassName(wsName)); 225 226 if (pkgName != null) { 227 b.setClassnamePackage(pkgName); 228 } 229 230 if (project.getLookup().lookup(WebModuleImplementation.class) != null) { 232 implBeanClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(WEBSERVICE_TEMPLATE, b, pkg, true)); 233 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 234 intfClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(INTERFACE_TEMPLATE, b, pkg, false)); 235 if (implBeanClass!=null) { 236 } 250 } else { 251 try { 252 implBeanClass = wsSupport.generateImplementationBean(wsName, pkg, project, null); 253 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 254 intfClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(INTERFACE_TEMPLATE, b, pkg, false)); 255 } catch (java.io.IOException e) { 256 throw new RuntimeException (e.getMessage(), e); 258 } 259 if (implBeanClass!=null) { 260 } 274 } 275 } 276 277 278 public void addReferences(String beanClassName, Node[] nodes) { 279 for(int i = 0; i < nodes.length; i++) { 280 Node node = nodes[i]; 281 EjbReference ref = (EjbReference)node.getCookie(EjbReference.class); 282 if(ref != null) { 283 EnterpriseReferenceContainer erc = (EnterpriseReferenceContainer)project.getLookup() 284 .lookup(EnterpriseReferenceContainer.class); 285 if(ref.supportsRemoteInvocation()) { 286 EjbRef ejbRef = ref.createRef(); 287 if(ejbRef.getEjbRefType().equals("Session")) { try { 289 } 292 catch(Exception e) { 293 throw new RuntimeException (e.getMessage()); 294 } 295 } 296 } 297 if(!ref.supportsRemoteInvocation() && 298 ref.supportsLocalInvocation()) { 299 EjbLocalRef ejbLocalRef = ref.createLocalRef(); 300 if(ejbLocalRef.getEjbRefType().equals("Session")) { try { 302 } 305 catch(Exception e) { 306 throw new RuntimeException (e.getMessage()); 307 } 308 } 309 } 310 } 311 else { 313 } 347 } 348 } 349 416 private static String varFromName(final String name) { 417 if(name.length() > 0) { 418 StringBuffer buf = new StringBuffer (name); 419 420 if(Character.isUpperCase(buf.charAt(0))) { 423 buf.setCharAt(0, Character.toLowerCase(buf.charAt(0))); 424 } else { 425 buf.insert(0, '_'); 426 } 427 428 return buf.toString(); 429 } else { 430 return "unknown"; } 432 } 433 434 private String getAntArtifactType(Project project){ 436 AntArtifactProvider antArtifactProvider = (AntArtifactProvider)project.getLookup().lookup(AntArtifactProvider.class); 437 AntArtifact[] artifacts = antArtifactProvider.getBuildArtifacts(); 438 return artifacts[0].getType(); 439 } 440 441 451 public String getServantClassName() { 452 return implBeanClass; 453 } 454 455 public String getSEIClassName() { 456 return intfClass; 457 } 458 459 public String getSEIBaseName() { 460 return wsgenUtil.getBaseName(intfClass); 461 } 462 463 public String getSoapBinding(){ 464 return soapBinding; 465 } 466 467 public String getPortTypeName(){ 468 return portTypeName; 469 } 470 471 public List getImportedSchemas() { 472 return importedSchemaList; 473 } 474 475 public String [] getWscompileFeatures() { 476 return wscompileFeatures; 477 } 478 479 public void addWebServiceEntry(String seiClassName, String portTypeName, URI targetNS ) 480 throws java.io.IOException { 481 if(wsSupport.getWebservicesDD() == null) { 483 try { 484 final FileObject wsxmlTemplate = Repository.getDefault().getDefaultFileSystem(). 485 findResource("org-netbeans-modules-websvc-jaxrpc/webservices.xml"); System.out.println("wsxmlTemplate = " + wsxmlTemplate); 487 final FileObject wsddFolder = wsSupport.getWsDDFolder(); 488 FileSystem fs = wsddFolder.getFileSystem(); 489 fs.runAtomicAction(new FileSystem.AtomicAction() { 490 public void run() throws IOException { 491 FileUtil.copyFile(wsxmlTemplate, wsddFolder, WEBSERVICES_DD); 492 } 493 }); 494 } catch(IOException ioe) { 495 496 throw new RuntimeException (ioe.getMessage()); 497 } 498 } 499 org.netbeans.modules.j2ee.dd.api.webservices.DDProvider wsDDProvider = 501 org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault(); 502 503 Webservices webServices = wsDDProvider.getDDRoot(wsSupport.getWebservicesDD()); 504 if(webServices != null){ 505 try{ 506 WebserviceDescription wsDescription = 507 (WebserviceDescription)webServices.createBean("WebserviceDescription"); wsDescription.setWebserviceDescriptionName(wsName); 509 ServiceImplBean serviceImplBean = 510 (ServiceImplBean)webServices.createBean("ServiceImplBean"); PortComponent portComponent = 512 (PortComponent)webServices.createBean("PortComponent"); portComponent.setPortComponentName(wsName); 514 org.netbeans.modules.schema2beans.QName wsdlPortQName = 515 new org.netbeans.modules.schema2beans.QName(targetNS.toString(), ((portTypeName == null) ? wsgenUtil.getBaseName(seiClassName) + "Port" : portTypeName + "Port"), "wsdl-port_ns"); portComponent.setWsdlPort(wsdlPortQName); 519 portComponent.setServiceEndpointInterface 520 (seiClassName); 521 wsSupport.addServiceImplLinkEntry(serviceImplBean, wsName); 523 String wsDDFolder = wsSupport.getArchiveDDFolderName(); 524 wsDescription.setWsdlFile( wsDDFolder + "/wsdl/" + wsName + ".wsdl"); wsDescription.setJaxrpcMappingFile(wsDDFolder +"/" + wsName + "-mapping.xml"); portComponent.setServiceImplBean(serviceImplBean); 527 wsDescription.addPortComponent(portComponent); 528 webServices.addWebserviceDescription(wsDescription); 529 webServices.write(wsSupport.getWebservicesDD()); 530 }catch(ClassNotFoundException e){ 531 throw new RuntimeException (e.getMessage()); 532 } 533 } 534 } 535 536 public URI getTargetNS() throws java.net.URISyntaxException { 537 if(targetNS != null) { 538 return new URI (targetNS); 539 } 540 return getDefaultTargetNS(wsName); 541 } 542 543 544 public URI getDefaultTargetNS(String wsName) throws java.net.URISyntaxException { 545 return new URI ("urn:" + wsName + "/wsdl"); 546 } 547 548 public URI getDefaultTypeNS(String wsName) throws java.net.URISyntaxException { 549 return new URI ("urn:" + wsName + "/types"); 550 } 551 552 public FileObject generateConfigFile(URI wsdlLocation)throws java.io.IOException { 554 FileObject configFile = pkg.createData(wsName + "-config", "xml"); Configuration configuration = new Configuration(); 556 WsdlType wsdl = configuration.newWsdlType(); 557 wsdl.setLocation(wsdlLocation); 558 wsdl.setPackageName(wsgenUtil.getSelectedPackageName(pkg, project)); 559 configuration.setWsdl(wsdl); 560 561 FileLock lock = null; 562 OutputStream out = null; 563 try{ 564 lock = configFile.lock(); 565 out = configFile.getOutputStream(lock); 566 configuration.write(out, "UTF-8"); } 568 catch(IOException ioe){ 569 throw new RuntimeException (ioe.getMessage()); 570 } 571 finally { 572 if(lock != null) 573 lock.releaseLock(); 574 if(out != null) 575 out.close(); 576 } 577 return configFile; 578 } 579 580 581 public FileObject generateConfigFile(String seiClassName, String servantClassName, URI targetNS, URI typeNS) throws java.io.IOException { 583 FileObject configFile = pkg.createData(wsName + "-config", "xml"); 585 Configuration configuration = new Configuration(); 586 org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.ServiceType service = 587 new org.netbeans.modules.websvc.jaxrpc.dev.dd.gen.ServiceType(); 588 service.setName(wsName); 589 service.setTargetNamespace(targetNS); 590 service.setTypeNamespace(typeNS); 591 service.setPackageName(wsgenUtil.getSelectedPackageName(pkg, project)); 592 InterfaceType interf = new InterfaceType(); 593 interf.setName(seiClassName); 594 interf.setServantName(servantClassName); 595 service.setInterface(new InterfaceType[] {interf}); 596 configuration.setService(service); 597 598 FileLock lock = null; 599 OutputStream out = null; 600 try{ 601 lock = configFile.lock(); 602 out = configFile.getOutputStream(lock); 603 configuration.write(out, "UTF-8"); } 605 catch(IOException ioe){ 606 throw new RuntimeException (ioe.getMessage()); 607 } 608 finally { 609 if(lock != null) 610 lock.releaseLock(); 611 if(out != null) 612 out.close(); 613 } 614 return configFile; 615 } 616 617 public static final String WSCOMPILE_CLASSPATH = "wscompile.classpath"; 619 } 620 621 622 | Popular Tags |