1 19 20 package org.netbeans.modules.websvc.core.dev.wizard; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import org.netbeans.api.java.classpath.ClassPath; 28 import org.netbeans.api.java.project.JavaProjectConstants; 29 import org.netbeans.api.progress.ProgressHandle; 30 import org.netbeans.api.progress.ProgressHandleFactory; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.api.project.SourceGroup; 34 import org.netbeans.api.project.libraries.Library; 35 import org.netbeans.api.project.libraries.LibraryManager; 36 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 37 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 38 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel; 39 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener; 40 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler; 41 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort; 42 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService; 43 import org.netbeans.modules.websvc.core.ServiceCreator; 44 import org.netbeans.modules.websvc.core.JaxWsUtils; 45 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport; 46 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 47 import org.netbeans.spi.project.ui.templates.support.Templates; 48 import org.openide.DialogDisplayer; 49 import org.openide.ErrorManager; 50 import org.openide.NotifyDescriptor; 51 import org.openide.WizardDescriptor; 52 import org.openide.cookies.EditorCookie; 53 import org.openide.filesystems.FileObject; 54 import org.openide.filesystems.FileUtil; 55 import org.openide.loaders.DataFolder; 56 import org.openide.loaders.DataObject; 57 import org.openide.util.NbBundle; 58 import org.openide.util.RequestProcessor; 59 60 64 public class JaxWsServiceCreator implements ServiceCreator { 65 private ProjectInfo projectInfo; 66 private WizardDescriptor wiz; 67 private boolean addJaxWsLib; 68 private int serviceType; 69 private int projectType; 70 73 public JaxWsServiceCreator(ProjectInfo projectInfo, WizardDescriptor wiz, boolean addJaxWsLib) { 74 this.projectInfo = projectInfo; 75 this.wiz = wiz; 76 this.addJaxWsLib=addJaxWsLib; 77 } 78 79 public void createService() throws IOException { 80 serviceType = ((Integer ) wiz.getProperty(WizardProperties.WEB_SERVICE_TYPE)).intValue(); 81 projectType = projectInfo.getProjectType(); 82 83 final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(JaxWsServiceCreator.class, "TXT_WebServiceGeneration")); handle.start(100); 86 87 Runnable r = new Runnable () { 88 public void run() { 89 try { 90 generateWebService(handle); 91 } catch (Exception e) { 92 handle.finish(); 94 String message = e.getLocalizedMessage(); 95 if(message != null) { 96 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 97 NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE); 98 DialogDisplayer.getDefault().notify(nd); 99 } else { 100 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 101 } 102 } 103 } 104 }; 105 RequestProcessor.getDefault().post(r); 106 } 107 108 public void createServiceFromWsdl() throws IOException { 109 110 112 final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(JaxWsServiceCreator.class, "TXT_WebServiceGeneration")); 114 Runnable r = new Runnable () { 115 public void run() { 116 try { 117 handle.start(); 121 generateWsFromWsdl15(handle); 122 } catch (Exception e) { 127 handle.finish(); 129 String message = e.getLocalizedMessage(); 130 if(message != null) { 131 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 132 NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE); 133 DialogDisplayer.getDefault().notify(nd); 134 } else { 135 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 136 } 137 } 138 } 139 }; 140 RequestProcessor.getDefault().post(r); 141 } 142 143 144 private void generateWebService(ProgressHandle handle) throws Exception { 146 147 FileObject pkg = Templates.getTargetFolder(wiz); 148 String wsName = Templates.getTargetName(wiz); 149 150 151 if (serviceType == WizardProperties.FROM_SCRATCH) { 152 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(projectInfo.getProject().getProjectDirectory()); 158 wsName = getUniqueJaxwsName(jaxWsSupport, wsName); 159 handle.progress(NbBundle.getMessage(JaxWsServiceCreator.class, "MSG_GEN_WS"), 50); if (addJaxWsLib) addJaxws20Library(projectInfo.getProject()); 162 generateJaxWSImplFromTemplate(pkg, wsName, projectType); 163 handle.finish(); 164 return; 165 } 220 } 294 295 private FileObject generateJaxWSImplFromTemplate(FileObject pkg, String wsName, int projectType) throws Exception { 296 DataFolder df = DataFolder.findFolder(pkg); 297 FileObject template = Templates.getTemplate(wiz); 298 299 if (projectType==ProjectInfo.EJB_PROJECT_TYPE) { FileObject templateParent = template.getParent(); 301 template = templateParent.getFileObject("EjbWebService","java"); } 303 DataObject dTemplate = DataObject.find(template); 304 DataObject dobj = dTemplate.createFromTemplate(df, wsName); 305 FileObject createdFile = dobj.getPrimaryFile(); 306 307 308 JaxWsModel jaxWsModel = projectInfo.getProject().getLookup().lookup(JaxWsModel.class); 309 if ( jaxWsModel!= null) { 310 ClassPath classPath = ClassPath.getClassPath(createdFile, ClassPath.SOURCE); 311 String serviceImplPath = classPath.getResourceName(createdFile, '.', false); 312 jaxWsModel.addService(wsName, serviceImplPath); 313 jaxWsModel.write(); 314 } 315 openFileInEditor(dobj); 316 return createdFile; 317 } 318 319 private String getUniqueJaxwsName(JAXWSSupport jaxWsSupport, String origName){ 320 List <Service> webServices = jaxWsSupport.getServices(); 321 List <String > serviceNames = new ArrayList <String >(webServices.size()); 322 for(Service service : webServices){ 323 serviceNames.add(service.getName()); 324 } 325 return uniqueWSName(origName, serviceNames); 326 } 327 328 private String uniqueWSName(final String origName, List <String > names ){ 329 int uniquifier = 0; 330 String truename = origName; 331 while(names.contains(truename)){ 332 truename = origName + String.valueOf(++uniquifier); 333 } 334 return truename; 335 } 336 337 private void openFileInEditor(DataObject dobj){ 338 final EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class); 339 RequestProcessor.getDefault().post(new Runnable (){ 340 public void run(){ 341 ec.open(); 342 } 343 }, 1000); 344 } 345 346 private void addJaxws20Library(Project project) throws Exception { 347 348 SourceGroup[] sgs = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 350 ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE); 351 FileObject wsimportFO = classPath.findResource("com/sun/tools/ws/ant/WsImport.class"); if (wsimportFO != null) { 353 return; 354 } 355 356 ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class); 357 Library jaxws20_ext = LibraryManager.getDefault().getLibrary("jaxws20"); if (pce != null && jaxws20_ext != null) { 359 try{ 360 pce.addLibrary(jaxws20_ext); 361 } catch(IOException e){ 362 throw new Exception ("Unable to add JAXWS 2.0 Library. " + e.getMessage()); 363 } 364 } else{ 365 throw new Exception ("Unable to add JAXWS 2.0 Library. " + 366 "ProjectClassPathExtender or library not found"); 367 } 368 } 369 private void generateWsFromWsdl15(final ProgressHandle handle) throws Exception { 370 String wsdlFilePath = (String ) wiz.getProperty(WizardProperties.WSDL_FILE_PATH); 371 File normalizedWsdlFilePath = FileUtil.normalizeFile(new File (wsdlFilePath)); 372 final URL wsdlURL = normalizedWsdlFilePath.toURI().toURL(); 374 final WsdlService service = (WsdlService) wiz.getProperty(WizardProperties.WSDL_SERVICE); 375 if (service==null) { 376 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(projectInfo.getProject().getProjectDirectory()); 377 FileObject targetFolder = Templates.getTargetFolder(wiz); 378 String targetName = Templates.getTargetName(wiz); 379 WsdlServiceHandler handler = (WsdlServiceHandler)wiz.getProperty(WizardProperties.WSDL_SERVICE_HANDLER); 380 JaxWsUtils.generateJaxWsArtifacts(projectInfo.getProject(),targetFolder,targetName,wsdlURL,handler.getServiceName(),handler.getPortName()); 381 WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER); 382 if (wsdlModeler!=null && wsdlModeler.getCreationException()!=null) { 383 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message( 384 NbBundle.getMessage(JaxWsServiceCreator.class,"TXT_CannotGenerateArtifacts", 385 wsdlModeler.getCreationException().getLocalizedMessage()), 386 NotifyDescriptor.ERROR_MESSAGE) 387 ); 388 } 389 handle.finish(); 390 } else { 391 final WsdlPort port = (WsdlPort) wiz.getProperty(WizardProperties.WSDL_PORT); 392 WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER); 394 wsdlModeler.generateWsdlModel(new WsdlModelListener() { 396 397 public void modelCreated(WsdlModel model) { 398 WsdlService service1 = model.getServiceByName(service.getName()); 399 WsdlPort port1 = service1.getPortByName(port.getName()); 400 401 port1.setSOAPVersion(port.getSOAPVersion()); 402 FileObject targetFolder = Templates.getTargetFolder(wiz); 403 String targetName = Templates.getTargetName(wiz); 404 405 try { 406 JaxWsUtils.generateJaxWsImplementationClass(projectInfo.getProject(), 407 targetFolder, 408 targetName, 409 wsdlURL, 410 service1, port1); 411 handle.finish(); 412 } 413 catch (Exception ex) { 414 handle.finish(); 415 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, 416 ex); 417 } 418 } 419 }); 420 } 421 } 422 } 423 | Popular Tags |