1 19 20 package org.netbeans.modules.websvc.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.websvc.dev.dd.gen.wscreation.Bean; 26 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices; 27 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription; 28 import org.netbeans.modules.j2ee.dd.api.webservices.ServiceImplBean; 29 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent; 30 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport; 31 import java.net.URI ; 32 import org.openide.filesystems.FileLock; 33 import java.io.OutputStream ; 34 import org.netbeans.modules.websvc.dev.dd.gen.Configuration; 35 import org.netbeans.modules.websvc.dev.dd.gen.InterfaceType; 36 import org.openide.filesystems.FileSystem; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.Repository; 39 import org.netbeans.modules.web.spi.webmodule.WebModuleImplementation; 40 import org.openide.nodes.Node; 41 import org.netbeans.api.project.ant.AntArtifact; 42 import org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef; 43 import org.netbeans.modules.j2ee.dd.api.common.EjbRef; 44 import org.netbeans.modules.j2ee.api.ejbjar.EjbReference; 45 import org.netbeans.modules.j2ee.api.ejbjar.EnterpriseReferenceContainer; 46 import org.netbeans.spi.project.ant.AntArtifactProvider; 47 import org.netbeans.modules.websvc.dev.dd.gen.WsdlType; 48 import java.util.Iterator ; 49 import javax.xml.parsers.ParserConfigurationException ; 50 import javax.xml.parsers.SAXParser ; 51 import javax.xml.parsers.SAXParserFactory ; 52 import javax.xml.transform.stream.StreamSource ; 53 import org.netbeans.modules.websvc.wsdl.PortInformationHandler; 54 import org.xml.sax.SAXException ; 55 import java.io.InputStream ; 56 import java.util.List ; 57 import java.util.Set ; 58 59 import org.openide.ErrorManager; 60 import org.openide.NotifyDescriptor; 61 import org.openide.DialogDisplayer; 62 import org.openide.util.NbBundle; 63 64 public class WebServiceGenerator { 65 private WSGenerationUtil wsgenUtil = new WSGenerationUtil(); 66 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"; 72 private String implBeanClass = ""; 73 private String intfClass = ""; 74 private String targetNS = null; 75 private String soapBinding = ""; 76 private String portTypeName = null; 77 public static final String WEBSERVICES_DD = "webservices"; private WebServicesSupport wsSupport; 79 private String wsName; 80 private FileObject pkg; 81 private Project project; 82 private List importedSchemaList; 83 private boolean changeWsName; 86 private String [] wscompileFeatures; 87 88 public WebServiceGenerator(WebServicesSupport wsSupport, String wsName, FileObject pkg, Project project) { 89 this.wsSupport = wsSupport; 90 this.wsName = wsName; 91 this.pkg = pkg; 92 this.project = project; 93 } 94 95 public WebServiceGenerator(FileObject pkg, Project project){ 96 this.pkg = pkg; 97 this.project = project; 98 } 99 100 public void generateWebService() throws IOException { 101 generateWebService(null); 102 } 103 104 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, FileObject originalFolder, String wsdlName, StreamSource source) throws IOException { 105 return wsgenUtil.generateWSDL(template, wsName, soapBinding, portTypeName, folder, originalFolder, wsdlName, source); 106 } 107 108 public FileObject generateWSDL(String template, String wsName, String soapBinding, String portTypeName, FileObject folder, String wsdlName, StreamSource source) throws IOException { 109 return wsgenUtil.generateWSDL(template, wsName, soapBinding, portTypeName, folder, wsdlName, source); 110 } 111 112 118 public String parseWSDL(InputStream wsdlInputStream) throws IOException , NoWSPortDefinedException { 119 121 String changedWsName=null; 122 123 PortInformationHandler handler = new PortInformationHandler(); 124 125 try { 126 SAXParserFactory factory = SAXParserFactory.newInstance(); 127 factory.setNamespaceAware(true); 128 SAXParser saxParser = factory.newSAXParser(); 129 saxParser.parse(wsdlInputStream, handler); 130 } 131 catch(ParserConfigurationException ex) { 132 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 133 String mes = NbBundle.getMessage(WebServiceGenerator.class, "ERR_WsdlParseFailure"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 135 DialogDisplayer.getDefault().notify(desc); 136 return changedWsName; 137 } 138 catch(SAXException ex) { 139 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 140 String mes = NbBundle.getMessage(WebServiceGenerator.class, "ERR_WsdlParseFailure"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 142 DialogDisplayer.getDefault().notify(desc); 143 return changedWsName; 144 } 145 146 List entirePortList = handler.getEntirePortList(); 147 if (entirePortList.isEmpty()) { 148 throw new NoWSPortDefinedException(); 149 } 150 Iterator iterator = entirePortList.iterator(); 151 String firstPortType = null; 152 while(iterator.hasNext()) { 153 PortInformationHandler.PortInfo portInfo = (PortInformationHandler.PortInfo)iterator.next(); 154 if(firstPortType == null){ 155 firstPortType = portInfo.getPortType(); 157 } 158 if(portInfo.getBindingType() != null && 160 portInfo.getBindingType().equals("http://schemas.xmlsoap.org/wsdl/soap")) { portTypeName = portInfo.getPortType(); 162 soapBinding = portInfo.getBinding(); 163 break; 164 } 165 } 166 if(portTypeName == null) { 168 portTypeName = firstPortType; 169 } 170 if (portTypeName == null) { 171 throw new NoWSPortDefinedException(); 172 } 173 intfClass = WSGenerationUtil.getSelectedPackageName(pkg, project) + "." + normalizePortTypeName(portTypeName); 176 implBeanClass = intfClass + "_Impl"; targetNS = handler.getTargetNamespace(); 178 importedSchemaList = handler.getImportedSchemas(); 179 if (changeWsName) changedWsName=wsName+"_Service"; Set features = handler.getWscompileFeatures(); 181 wscompileFeatures=new String [features.size()]; 182 features.toArray(wscompileFeatures); 183 return changedWsName; 184 } 185 186 191 private String normalizePortTypeName(String portTypeName) { 192 if (portTypeName == null) { 193 return "unknown"; } 195 String first = portTypeName.substring(0,1); 196 String result = first.toUpperCase() + ((portTypeName.length() > 1) ? portTypeName.substring(1) : ""); 197 if (result.equals(wsName)) { 198 changeWsName=true; 199 } 200 return result; 201 202 } 203 204 public void generateMessageHandler(String handlerName) throws IOException { 205 String pkgName = wsgenUtil.getSelectedPackageName(pkg, project); 206 Bean b = wsgenUtil.getDefaultBean(); 207 b.setCommentDataWsName(handlerName); 208 b.setClassname(true); 209 b.setClassnameName(handlerName); 210 if(pkgName != null) { 211 b.setClassnamePackage(pkgName); 212 } 213 String handlerClass = wsgenUtil.getFullClassName(pkgName, 214 wsgenUtil.generateClass(HANDLER_TEMPLATE, b, pkg, true)); 215 } 216 217 public void generateWebService( Node[] nodes) throws IOException { 218 String pkgName = wsgenUtil.getSelectedPackageName(pkg, project); 219 220 Bean b = wsgenUtil.getDefaultBean(); 221 b.setCommentDataWsName(wsName); 222 b.setClassname(true); 223 b.setDelegateData(""); 224 225 b.setClassnameName(wsgenUtil.getBeanClassName(wsName)); 226 227 if (pkgName != null) { 228 b.setClassnamePackage(pkgName); 229 } 230 231 if (project.getLookup().lookup(WebModuleImplementation.class) != null) { 233 implBeanClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(WEBSERVICE_TEMPLATE, b, pkg, true)); 234 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 235 intfClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(INTERFACE_TEMPLATE, b, pkg, false)); 236 if (implBeanClass!=null) { 237 } 251 } else { 252 try { 253 implBeanClass = wsSupport.generateImplementationBean(wsName, pkg, project, null); 254 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 255 intfClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(INTERFACE_TEMPLATE, b, pkg, false)); 256 } catch (java.io.IOException e) { 257 throw new RuntimeException (e.getMessage(), e); 259 } 260 if (implBeanClass!=null) { 261 } 275 } 276 } 277 278 public void generateWebServiceJavaEE5(Node[] nodes) throws IOException { 279 String pkgName = wsgenUtil.getSelectedPackageName(pkg, project); 280 281 Bean b = wsgenUtil.getDefaultBean(); 282 b.setCommentDataWsName(wsName); 283 b.setClassname(true); 284 b.setDelegateData(""); 285 286 b.setClassnameName(wsgenUtil.getBeanClassName(wsName)); 287 288 if (pkgName != null) { 289 b.setClassnamePackage(pkgName); 290 } 291 292 if (project.getLookup().lookup(WebModuleImplementation.class) != null) { 294 implBeanClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(WEBSERVICEJAVAEE5_TEMPLATE, b, pkg, true)); 295 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 296 if (implBeanClass!=null) { 297 boolean rollback = true; 298 } 315 } else { 316 try { 317 implBeanClass = wsSupport.generateImplementationBean(wsName, pkg, project, null); 318 b.setClassnameName(wsgenUtil.getSEIName(wsName)); 319 } catch (java.io.IOException e) { 320 throw new RuntimeException (e.getMessage(), e); 322 } 323 if (implBeanClass!=null) { 324 } 344 } 345 } 346 347 348 public void addReferences(String beanClassName, Node[] nodes) { 349 for(int i = 0; i < nodes.length; i++) { 350 Node node = nodes[i]; 351 EjbReference ref = (EjbReference)node.getCookie(EjbReference.class); 352 if(ref != null) { 353 EnterpriseReferenceContainer erc = (EnterpriseReferenceContainer)project.getLookup() 354 .lookup(EnterpriseReferenceContainer.class); 355 if(ref.supportsRemoteInvocation()) { 356 EjbRef ejbRef = ref.createRef(); 357 if(ejbRef.getEjbRefType().equals("Session")) { try { 359 } 362 catch(Exception e) { 363 throw new RuntimeException (e.getMessage()); 364 } 365 } 366 } 367 if(!ref.supportsRemoteInvocation() && 368 ref.supportsLocalInvocation()) { 369 EjbLocalRef ejbLocalRef = ref.createLocalRef(); 370 if(ejbLocalRef.getEjbRefType().equals("Session")) { try { 372 } 375 catch(Exception e) { 376 throw new RuntimeException (e.getMessage()); 377 } 378 } 379 } 380 } 381 else { 383 } 417 } 418 } 419 486 private static String varFromName(final String name) { 487 if(name.length() > 0) { 488 StringBuffer buf = new StringBuffer (name); 489 490 if(Character.isUpperCase(buf.charAt(0))) { 493 buf.setCharAt(0, Character.toLowerCase(buf.charAt(0))); 494 } else { 495 buf.insert(0, '_'); 496 } 497 498 return buf.toString(); 499 } else { 500 return "unknown"; } 502 } 503 504 private String getAntArtifactType(Project project){ 506 AntArtifactProvider antArtifactProvider = (AntArtifactProvider)project.getLookup().lookup(AntArtifactProvider.class); 507 AntArtifact[] artifacts = antArtifactProvider.getBuildArtifacts(); 508 return artifacts[0].getType(); 509 } 510 511 521 public String getServantClassName() { 522 return implBeanClass; 523 } 524 525 public String getSEIClassName() { 526 return intfClass; 527 } 528 529 public String getSEIBaseName() { 530 return wsgenUtil.getBaseName(intfClass); 531 } 532 533 public String getSoapBinding(){ 534 return soapBinding; 535 } 536 537 public String getPortTypeName(){ 538 return portTypeName; 539 } 540 541 public List getImportedSchemas() { 542 return importedSchemaList; 543 } 544 545 public String [] getWscompileFeatures() { 546 return wscompileFeatures; 547 } 548 549 public void addWebServiceEntry(String seiClassName, String portTypeName, URI targetNS ) 550 throws java.io.IOException { 551 if(wsSupport.getWebservicesDD() == null) { 553 try { 554 final FileObject wsxmlTemplate = Repository.getDefault().getDefaultFileSystem(). 555 findResource("org-netbeans-modules-websvc-dev/webservices.xml"); final FileObject wsddFolder = wsSupport.getWsDDFolder(); 557 FileSystem fs = wsddFolder.getFileSystem(); 558 fs.runAtomicAction(new FileSystem.AtomicAction() { 559 public void run() throws IOException { 560 FileUtil.copyFile(wsxmlTemplate, wsddFolder, WEBSERVICES_DD); 561 } 562 }); 563 } catch(IOException ioe) { 564 565 throw new RuntimeException (ioe.getMessage()); 566 } 567 } 568 org.netbeans.modules.j2ee.dd.api.webservices.DDProvider wsDDProvider = 570 org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault(); 571 572 Webservices webServices = wsDDProvider.getDDRoot(wsSupport.getWebservicesDD()); 573 if(webServices != null){ 574 try{ 575 WebserviceDescription wsDescription = 576 (WebserviceDescription)webServices.createBean("WebserviceDescription"); wsDescription.setWebserviceDescriptionName(wsName); 578 ServiceImplBean serviceImplBean = 579 (ServiceImplBean)webServices.createBean("ServiceImplBean"); PortComponent portComponent = 581 (PortComponent)webServices.createBean("PortComponent"); portComponent.setPortComponentName(wsName); 583 org.netbeans.modules.schema2beans.QName wsdlPortQName = 584 new org.netbeans.modules.schema2beans.QName(targetNS.toString(), ((portTypeName == null) ? wsgenUtil.getBaseName(seiClassName) + "Port" : portTypeName + "Port"), "wsdl-port_ns"); portComponent.setWsdlPort(wsdlPortQName); 588 portComponent.setServiceEndpointInterface 589 (seiClassName); 590 wsSupport.addServiceImplLinkEntry(serviceImplBean, wsName); 592 String wsDDFolder = wsSupport.getArchiveDDFolderName(); 593 wsDescription.setWsdlFile( wsDDFolder + "/wsdl/" + wsName + ".wsdl"); wsDescription.setJaxrpcMappingFile(wsDDFolder +"/" + wsName + "-mapping.xml"); portComponent.setServiceImplBean(serviceImplBean); 596 wsDescription.addPortComponent(portComponent); 597 webServices.addWebserviceDescription(wsDescription); 598 webServices.write(wsSupport.getWebservicesDD()); 599 }catch(ClassNotFoundException e){ 600 throw new RuntimeException (e.getMessage()); 601 } 602 } 603 } 604 605 public URI getTargetNS() throws java.net.URISyntaxException { 606 if(targetNS != null) { 607 return new URI (targetNS); 608 } 609 return getDefaultTargetNS(wsName); 610 } 611 612 613 public URI getDefaultTargetNS(String wsName) throws java.net.URISyntaxException { 614 return new URI ("urn:" + wsName + "/wsdl"); 615 } 616 617 public URI getDefaultTypeNS(String wsName) throws java.net.URISyntaxException { 618 return new URI ("urn:" + wsName + "/types"); 619 } 620 621 public FileObject generateConfigFile(URI wsdlLocation)throws java.io.IOException { 623 FileObject configFile = pkg.createData(wsName + "-config", "xml"); Configuration configuration = new Configuration(); 625 WsdlType wsdl = configuration.newWsdlType(); 626 wsdl.setLocation(wsdlLocation); 627 wsdl.setPackageName(wsgenUtil.getSelectedPackageName(pkg, project)); 628 configuration.setWsdl(wsdl); 629 630 FileLock lock = null; 631 OutputStream out = null; 632 try{ 633 lock = configFile.lock(); 634 out = configFile.getOutputStream(lock); 635 configuration.write(out, "UTF-8"); } 637 catch(IOException ioe){ 638 throw new RuntimeException (ioe.getMessage()); 639 } 640 finally { 641 if(lock != null) 642 lock.releaseLock(); 643 if(out != null) 644 out.close(); 645 } 646 return configFile; 647 } 648 649 650 public FileObject generateConfigFile(String seiClassName, String servantClassName, URI targetNS, URI typeNS) throws java.io.IOException { 652 FileObject configFile = pkg.createData(wsName + "-config", "xml"); 654 Configuration configuration = new Configuration(); 655 org.netbeans.modules.websvc.dev.dd.gen.ServiceType service = 656 new org.netbeans.modules.websvc.dev.dd.gen.ServiceType(); 657 service.setName(wsName); 658 service.setTargetNamespace(targetNS); 659 service.setTypeNamespace(typeNS); 660 service.setPackageName(wsgenUtil.getSelectedPackageName(pkg, project)); 661 InterfaceType interf = new InterfaceType(); 662 interf.setName(seiClassName); 663 interf.setServantName(servantClassName); 664 service.setInterface(new InterfaceType[] {interf}); 665 configuration.setService(service); 666 667 FileLock lock = null; 668 OutputStream out = null; 669 try{ 670 lock = configFile.lock(); 671 out = configFile.getOutputStream(lock); 672 configuration.write(out, "UTF-8"); } 674 catch(IOException ioe){ 675 throw new RuntimeException (ioe.getMessage()); 676 } 677 finally { 678 if(lock != null) 679 lock.releaseLock(); 680 if(out != null) 681 out.close(); 682 } 683 return configFile; 684 } 685 686 public static final String WSCOMPILE_CLASSPATH = "wscompile.classpath"; 688 } 689 690 691 | Popular Tags |