1 19 20 package org.netbeans.modules.web.project.jaxws; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import org.netbeans.api.java.classpath.ClassPath; 25 import org.netbeans.api.java.project.JavaProjectConstants; 26 import org.netbeans.api.project.Project; 27 import org.netbeans.api.project.ProjectUtils; 28 import org.netbeans.api.project.SourceGroup; 29 import org.netbeans.api.project.libraries.Library; 30 import org.netbeans.api.project.libraries.LibraryManager; 31 import org.netbeans.api.project.ui.OpenProjects; 32 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException; 33 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 34 import org.netbeans.modules.j2ee.dd.api.web.Listener; 35 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 36 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping; 37 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 38 import org.netbeans.modules.web.api.webmodule.WebModule; 39 import org.netbeans.modules.web.project.ProjectWebModule; 40 import org.netbeans.modules.web.project.WebProject; 41 import org.netbeans.modules.web.project.WebProjectWebServicesSupport; 42 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils; 43 import org.netbeans.modules.websvc.api.jaxws.project.config.Endpoint; 44 import org.netbeans.modules.websvc.api.jaxws.project.config.Endpoints; 45 import org.netbeans.modules.websvc.api.jaxws.project.config.EndpointsProvider; 46 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 47 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 48 import org.netbeans.modules.websvc.jaxws.spi.ProjectJAXWSSupport; 49 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 50 import org.netbeans.spi.project.support.ant.AntProjectHelper; 51 import org.openide.DialogDisplayer; 52 import org.openide.ErrorManager; 53 import org.openide.NotifyDescriptor; 54 import org.openide.filesystems.FileLock; 55 import org.openide.filesystems.FileObject; 56 import org.openide.util.NbBundle; 57 58 62 public class WebProjectJAXWSSupport extends ProjectJAXWSSupport { 63 private WebProject project; 64 private final static String servletClassName = "com.sun.xml.ws.transport.http.servlet.WSServlet"; private final static String servletListener = "com.sun.xml.ws.transport.http.servlet.WSServletContextListener"; 68 69 public WebProjectJAXWSSupport(WebProject project, AntProjectHelper antProjectHelper) { 70 super(project,antProjectHelper); 71 this.project = project; 72 } 73 74 public FileObject getWsdlFolder(boolean create) throws java.io.IOException { 75 WebModule webModule = WebModule.getWebModule(project.getProjectDirectory()); 76 if (webModule!=null) { 77 FileObject webInfFo = webModule.getWebInf(); 78 if (webInfFo!=null) { 79 FileObject wsdlFo = webInfFo.getFileObject("wsdl"); if (wsdlFo!=null) return wsdlFo; 81 else if (create) { 82 return webInfFo.createFolder("wsdl"); } 84 } 85 } 86 return null; 87 } 88 89 93 public String getWsdlLocation(String serviceName) { 94 String localWsdl = serviceName+".wsdl"; JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 96 if (jaxWsModel!=null) { 97 Service service = jaxWsModel.findServiceByName(serviceName); 98 if (service!=null) { 99 String localWsdlFile = service.getLocalWsdlFile(); 100 if (localWsdlFile!=null) localWsdl=localWsdlFile; 101 } 102 } 103 String prefix = "WEB-INF/wsdl/"; return prefix+serviceName+"/"+localWsdl; } 106 107 112 private void addServiceEntriesToDD(String serviceName) { 113 String servletName = serviceName; 115 116 WebApp webApp = getWebApp(); 117 if(webApp != null){ 118 Servlet servlet = null; 119 Listener listener = null; 120 try{ 121 servlet = (Servlet)webApp.addBean("Servlet", new String []{"ServletName","ServletClass"}, 122 new Object []{servletName,servletClassName}, "ServletName"); 123 servlet.setLoadOnStartup(new java.math.BigInteger ("1")); 124 ServletMapping servletMapping = (ServletMapping) 125 webApp.addBean("ServletMapping", new String []{"ServletName","UrlPattern"}, 126 new Object []{servletName, "/" + serviceName}, "ServletName"); 127 128 if(!webAppHasListener(webApp, servletListener)){ 129 listener = (Listener)webApp.addBean("Listener", new String []{"ListenerClass"}, 130 new Object []{servletListener}, "ListenerClass"); 131 } 132 webApp.write(getDeploymentDescriptor()); 134 } catch (ClassNotFoundException exc) { 135 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage()); 136 } catch (NameAlreadyUsedException exc) { 137 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage()); 138 } catch (IOException exc) { 139 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage()); 140 } 141 } 142 } 143 144 150 private void removeServiceEntriesFromDD(String serviceName) { 151 boolean changed = false; 152 WebApp webApp = getWebApp(); 153 Servlet[] servlets = webApp.getServlet(); 155 for(int i = 0; i < servlets.length; i++){ 156 Servlet servlet = servlets[i]; 157 if(servlet.getServletName().equals(serviceName)){ 158 webApp.removeServlet(servlet); 159 changed = true; 160 break; 161 } 162 } 163 ServletMapping[] mappings = webApp.getServletMapping(); 165 for(int i = 0; i < mappings.length; i++){ 166 ServletMapping mapping = mappings[i]; 167 if(mapping.getServletName().equals(serviceName)){ 168 webApp.removeServletMapping(mapping); 169 changed = true; 170 break; 171 } 172 } 173 174 boolean hasMoreWebServices = false; 177 Servlet[] remainingServlets = webApp.getServlet(); 178 for(int i = 0; i < remainingServlets.length; i++){ 179 if(remainingServlets[i].getServletClass().equals(servletClassName)){ 180 hasMoreWebServices = true; 181 break; 182 } 183 } 184 if(!hasMoreWebServices){ 185 Listener[] listeners = webApp.getListener(); 186 for(int i = 0; i < listeners.length; i++){ 187 Listener listener = listeners[i]; 188 if(listener.getListenerClass().equals(servletListener)){ 189 webApp.removeListener(listener); 190 changed = true; 191 break; 192 } 193 } 194 } 195 if(changed){ 196 try{ 197 webApp.write(getDeploymentDescriptor()); 198 } catch(IOException e){ 199 ErrorManager.getDefault().notify(e); 200 } 201 } 202 } 203 204 private boolean webAppHasListener(WebApp webApp, String listenerClass){ 205 Listener[] listeners = webApp.getListener(); 206 for(int i = 0; i < listeners.length; i++){ 207 Listener listener = listeners[i]; 208 if(listenerClass.equals(listener.getListenerClass())){ 209 return true; 210 } 211 } 212 return false; 213 } 214 217 public FileObject getDeploymentDescriptorFolder() { 218 return this.getWebInf(); 219 } 220 221 private WebApp getWebApp() { 222 try { 223 FileObject deploymentDescriptor = getDeploymentDescriptor(); 224 if(deploymentDescriptor != null) { 225 return DDProvider.getDefault().getDDRoot(deploymentDescriptor); 226 } 227 } catch (java.io.IOException e) { 228 org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage()); 229 } 230 return null; 231 } 232 233 private boolean isProjectOpened() { 234 if (OpenProjects.getDefault() == null) 237 return true; 238 239 Project[] projects = OpenProjects.getDefault().getOpenProjects(); 240 for (int i = 0; i < projects.length; i++) { 241 if (projects[i].equals(project)) 242 return true; 243 } 244 return false; 245 } 246 247 public FileObject getDeploymentDescriptor() { 248 FileObject webInfFo = getWebInf(); 249 if (webInfFo==null) { 250 if (isProjectOpened()) { 251 DialogDisplayer.getDefault().notify( 252 new NotifyDescriptor.Message(NbBundle.getMessage(WebProjectWebServicesSupport.class,"MSG_WebInfCorrupted"), NotifyDescriptor.ERROR_MESSAGE)); 254 } 255 return null; 256 } 257 return getWebInf().getFileObject(ProjectWebModule.FILE_DD); 258 } 259 260 public FileObject getWebInf() { 261 WebModule webModule = WebModule.getWebModule(project.getProjectDirectory()); 262 if(webModule != null){ 263 return webModule.getWebInf(); 264 } 265 return null; 266 } 267 268 protected void addJaxwsArtifacts(Project project, String wsName, String serviceImpl) throws Exception { 269 270 SourceGroup[] sgs = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 272 ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE); 273 FileObject wsimportFO = classPath.findResource("com/sun/tools/ws/ant/WsImport.class"); 275 if (wsimportFO == null) { 276 ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class); 278 Library jaxws20_ext = LibraryManager.getDefault().getLibrary("jaxws20"); if ((pce!=null) && (jaxws20_ext != null)) { 280 try{ 281 pce.addLibrary(jaxws20_ext); 282 }catch(IOException e){ 283 throw new Exception ("Unable to add JAXWS 2.0 library"); 284 } 285 } else { 286 throw new Exception ("Unable to add JAXWS 2.0 Library. " + 287 "ProjectClassPathExtender or library not found"); 288 } 289 } 290 291 addServiceEntriesToDD(wsName); 293 294 FileObject ddFolder = getDeploymentDescriptorFolder(); 296 if(ddFolder != null){ 297 FileObject sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml"); 298 if(sunjaxwsFile == null){ 299 WSUtils.generateSunJaxwsFile(ddFolder); 300 } 301 302 sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml"); 303 Endpoints endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile); 304 Endpoint endpoint = endpoints.newEndpoint(); 305 endpoint.setEndpointName(wsName); 306 endpoint.setImplementation(serviceImpl); 307 endpoint.setUrlPattern("/" + wsName); 308 endpoints.addEnpoint(endpoint); 309 FileLock lock = null; 310 OutputStream os = null; 311 try{ 312 lock = sunjaxwsFile.lock(); 313 os = sunjaxwsFile.getOutputStream(lock); 314 endpoints.write(os); 315 }finally{ 316 if(lock != null) 317 lock.releaseLock(); 318 319 if(os != null) 320 os.close(); 321 } 322 }else{ 323 String mes = NbBundle.getMessage(WebProjectJAXWSSupport.class, "MSG_CannotFindWEB-INF"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 325 DialogDisplayer.getDefault().notify(desc); 326 } 327 } 328 329 331 protected FileObject getXmlArtifactsRoot() { 332 return project.getWebModule().getConfDir(); 333 } 334 335 341 public void serviceFromJavaRemoved(String serviceName) { 342 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 343 Boolean isJsr109 = jaxWsModel.getJsr109(); 344 if(isJsr109!=null && !isJsr109.booleanValue()){ 345 try{ 346 removeNonJsr109Entries(serviceName); 347 }catch(IOException e){ 348 ErrorManager.getDefault().notify(e); 349 } 350 } 351 352 } 353 354 358 public void removeNonJsr109Entries(String serviceName) throws IOException { 359 removeServiceEntriesFromDD(serviceName); 361 FileObject ddFolder = getDeploymentDescriptorFolder(); 363 if(ddFolder != null){ 364 FileObject sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml"); 365 if(sunjaxwsFile != null){ 366 FileLock lock = null; 367 JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class); 369 if(jaxWsModel.getServices().length == 0){ 370 try{ 371 lock = sunjaxwsFile.lock(); 372 sunjaxwsFile.delete(lock); 373 } finally{ 374 if(lock != null){ 375 lock.releaseLock(); 376 } 377 } 378 } else{ 379 Endpoints endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile); 381 Endpoint endpoint = endpoints.findEndpointByName(serviceName); 382 if(endpoint != null){ 383 endpoints.removeEndpoint(endpoint); 384 OutputStream os = null; 385 try{ 386 lock = sunjaxwsFile.lock(); 387 os = sunjaxwsFile.getOutputStream(lock); 388 endpoints.write(os); 389 }finally{ 390 if(lock != null){ 391 lock.releaseLock(); 392 } 393 if(os != null){ 394 os.close(); 395 } 396 } 397 } 398 } 399 } 400 }else{ 401 String mes = NbBundle.getMessage(WebProjectJAXWSSupport.class, "MSG_CannotFindDDDirectory"); NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); 403 DialogDisplayer.getDefault().notify(desc); 404 } 405 406 } 408 409 public String addService(String name, String serviceImpl, String wsdlUrl, String serviceName, String portName, String packageName, boolean isJsr109) { 410 FileObject fo = project.findJaxWsFileObject(); 412 if (fo==null) { 413 try { 414 project.createJaxWsFileObject(); 415 } catch (IOException ex) { 416 ErrorManager.getDefault().notify(ex); 417 } 418 } 419 return super.addService(name, serviceImpl, wsdlUrl, serviceName, portName, packageName, isJsr109); 420 } 421 422 public void addService(String serviceName, String serviceImpl, boolean isJsr109) { 423 FileObject fo = project.findJaxWsFileObject(); 425 if (fo==null) { 426 try { 427 project.createJaxWsFileObject(); 428 } catch (IOException ex) { 429 ErrorManager.getDefault().notify(ex); 430 } 431 } 432 super.addService(serviceName, serviceImpl, isJsr109); 433 } 434 435 } 436 | Popular Tags |