KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > jaxws > WebProjectJAXWSSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.project.jaxws;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
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 /**
59  *
60  * @author mkuchtiak
61  */

62 public class WebProjectJAXWSSupport extends ProjectJAXWSSupport /*implements JAXWSSupportImpl*/ {
63     private WebProject project;
64     //servlet name and servlet listener for non-JSR 109 web services
65
private final static String JavaDoc servletClassName = "com.sun.xml.ws.transport.http.servlet.WSServlet"; //NOI18N
66
private final static String JavaDoc servletListener = "com.sun.xml.ws.transport.http.servlet.WSServletContextListener"; //NOI18N
67

68     /** Creates a new instance of JAXWSSupport */
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 JavaDoc {
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"); //NOI18N
80
if (wsdlFo!=null) return wsdlFo;
81                 else if (create) {
82                     return webInfFo.createFolder("wsdl"); //NOI18N
83
}
84             }
85         }
86         return null;
87     }
88     
89     /** Get wsdlLocation information
90      * Useful for web service from wsdl
91      * @param name service "display" name
92      */

93     public String JavaDoc getWsdlLocation(String JavaDoc serviceName) {
94         String JavaDoc localWsdl = serviceName+".wsdl"; //NOI18N
95
JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
96         if (jaxWsModel!=null) {
97             Service service = jaxWsModel.findServiceByName(serviceName);
98             if (service!=null) {
99                 String JavaDoc localWsdlFile = service.getLocalWsdlFile();
100                 if (localWsdlFile!=null) localWsdl=localWsdlFile;
101             }
102         }
103         String JavaDoc prefix = "WEB-INF/wsdl/"; //NOI18N
104
return prefix+serviceName+"/"+localWsdl; //NOI18N
105
}
106     
107     /**
108      * This is to support non-JSR 109 containers. In this case, a regular jaxws web service
109      * is created and the deployment descriptor is updated with the jaxws-ri servlet and
110      * listener.
111      */

112     private void addServiceEntriesToDD(String JavaDoc serviceName) {
113         //add servlet entry to web.xml
114
String JavaDoc 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 JavaDoc[]{"ServletName","ServletClass"},
122                         new Object JavaDoc[]{servletName,servletClassName}, "ServletName");
123                 servlet.setLoadOnStartup(new java.math.BigInteger JavaDoc("1"));
124                 ServletMapping servletMapping = (ServletMapping)
125                 webApp.addBean("ServletMapping", new String JavaDoc[]{"ServletName","UrlPattern"},
126                         new Object JavaDoc[]{servletName, "/" + serviceName}, "ServletName");
127                 
128                 if(!webAppHasListener(webApp, servletListener)){
129                     listener = (Listener)webApp.addBean("Listener", new String JavaDoc[]{"ListenerClass"},
130                             new Object JavaDoc[]{servletListener}, "ListenerClass");
131                 }
132                 // This also saves server specific configuration, if necessary.
133
webApp.write(getDeploymentDescriptor());
134             } catch (ClassNotFoundException JavaDoc exc) {
135                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage());
136             } catch (NameAlreadyUsedException exc) {
137                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage());
138             } catch (IOException JavaDoc exc) {
139                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, exc.getLocalizedMessage());
140             }
141         }
142     }
143     
144     /**
145      * Remove the web.xml entries for the non-JSR 109 web service.
146      *
147      * @param serviceName Name of the web service to be removed
148      * @param serviceNames List of names of other non-JSR 109 web services in the project
149      */

150     private void removeServiceEntriesFromDD(String JavaDoc serviceName) {
151         boolean changed = false;
152         WebApp webApp = getWebApp();
153         //first remove the servlet
154
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         //remove the servlet mapping
164
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         //determine if there are other web services in the project
175
//if none, remove the listener
176
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 JavaDoc e){
199                 ErrorManager.getDefault().notify(e);
200             }
201         }
202     }
203     
204     private boolean webAppHasListener(WebApp webApp, String JavaDoc 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     /**
215      * Returns the directory that contains the deployment descriptor in the project
216      */

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 JavaDoc e) {
228             org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
229         }
230         return null;
231     }
232     
233     private boolean isProjectOpened() {
234         // XXX workaround: OpenProjects.getDefault() can be null
235
// when called from ProjectOpenedHook.projectOpened() upon IDE startup
236
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"), // NOI18N
253
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 JavaDoc wsName, String JavaDoc serviceImpl) throws Exception JavaDoc {
269         
270         // check if the wsimport class is already present - this means we don't need to add the library
271
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"); // NOI18N
274

275         if (wsimportFO == null) {
276             //Add the jaxws20 library to the project to be packed with the archive
277
ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class);
278             Library jaxws20_ext = LibraryManager.getDefault().getLibrary("jaxws20"); //NOI18N
279
if ((pce!=null) && (jaxws20_ext != null)) {
280                 try{
281                     pce.addLibrary(jaxws20_ext);
282                 }catch(IOException JavaDoc e){
283                     throw new Exception JavaDoc("Unable to add JAXWS 2.0 library");
284                 }
285             } else {
286                 throw new Exception JavaDoc("Unable to add JAXWS 2.0 Library. " +
287                         "ProjectClassPathExtender or library not found");
288             }
289         }
290         
291         //Add the servlet and listener entry in web.xml
292
addServiceEntriesToDD(wsName);
293         
294         //create the sun-jaxws deployment descriptor
295
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 JavaDoc 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 JavaDoc mes = NbBundle.getMessage(WebProjectJAXWSSupport.class, "MSG_CannotFindWEB-INF"); // NOI18N
324
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
325             DialogDisplayer.getDefault().notify(desc);
326         }
327     }
328     
329     /** return root folder for xml artifacts
330      */

331     protected FileObject getXmlArtifactsRoot() {
332         return project.getWebModule().getConfDir();
333     }
334     
335     /**
336      * Notification when Service (created from java) is removed from jax-ws.xml
337      * (JAXWSSupport needs to react when @WebService annotation is removed
338      * or when impl.class is removed (manually from project)
339      * Default implementation does nothing.
340      */

341     public void serviceFromJavaRemoved(String JavaDoc serviceName) {
342         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
343         Boolean JavaDoc isJsr109 = jaxWsModel.getJsr109();
344         if(isJsr109!=null && !isJsr109.booleanValue()){
345             try{
346                 removeNonJsr109Entries(serviceName);
347             }catch(IOException JavaDoc e){
348                 ErrorManager.getDefault().notify(e);
349             }
350         }
351         
352     }
353     
354     /**
355      * Removes the servlet entry from web.xml and
356      * the endpoint entry from the sun-jaxws.xml file
357      */

358     public void removeNonJsr109Entries(String JavaDoc serviceName) throws IOException JavaDoc{
359         //delete web.xml entry
360
removeServiceEntriesFromDD(serviceName);
361         //delete entry in sun-jaxws.xml file.
362
FileObject ddFolder = getDeploymentDescriptorFolder();
363         if(ddFolder != null){
364             FileObject sunjaxwsFile = ddFolder.getFileObject("sun-jaxws.xml");
365             if(sunjaxwsFile != null){
366                 FileLock lock = null;
367                 //if there are no more services, delete the file
368
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                     //remove the entry from the sunjaxwsFile
380
Endpoints endpoints = EndpointsProvider.getDefault().getEndpoints(sunjaxwsFile);
381                     Endpoint endpoint = endpoints.findEndpointByName(serviceName);
382                     if(endpoint != null){
383                         endpoints.removeEndpoint(endpoint);
384                         OutputStream JavaDoc 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 JavaDoc mes = NbBundle.getMessage(WebProjectJAXWSSupport.class, "MSG_CannotFindDDDirectory"); // NOI18N
402
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
403             DialogDisplayer.getDefault().notify(desc);
404         }
405         
406         //TODO if no more web services, remove the jaxws20 library
407
}
408
409     public String JavaDoc addService(String JavaDoc name, String JavaDoc serviceImpl, String JavaDoc wsdlUrl, String JavaDoc serviceName, String JavaDoc portName, String JavaDoc packageName, boolean isJsr109) {
410         // create jax-ws.xml if necessary
411
FileObject fo = project.findJaxWsFileObject();
412         if (fo==null) {
413             try {
414                 project.createJaxWsFileObject();
415             } catch (IOException JavaDoc 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 JavaDoc serviceName, String JavaDoc serviceImpl, boolean isJsr109) {
423         // create jax-ws.xml if necessary
424
FileObject fo = project.findJaxWsFileObject();
425         if (fo==null) {
426             try {
427                 project.createJaxWsFileObject();
428             } catch (IOException JavaDoc ex) {
429                 ErrorManager.getDefault().notify(ex);
430             }
431         }
432         super.addService(serviceName, serviceImpl, isJsr109);
433     }
434     
435 }
436
Popular Tags