KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxws > spi > ProjectJAXWSSupport


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.websvc.jaxws.spi;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.List JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34 import org.apache.tools.ant.module.api.support.ActionUtils;
35 import org.netbeans.api.project.Project;
36 import org.netbeans.modules.websvc.api.jaxws.project.GeneratedFilesHelper;
37 import org.netbeans.modules.websvc.api.jaxws.project.WSUtils;
38 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
39 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
40 import org.netbeans.modules.websvc.api.jaxws.project.config.ServiceAlreadyExistsExeption;
41 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperGenerator;
42 import org.netbeans.modules.websvc.jaxws.api.WsdlWrapperHandler;
43 import org.netbeans.spi.project.support.ant.AntProjectHelper;
44 import org.openide.ErrorManager;
45 import org.openide.execution.ExecutorTask;
46 import org.openide.filesystems.FileLock;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileStateInvalidException;
49 import org.openide.filesystems.FileSystem.AtomicAction;
50 import org.openide.filesystems.FileUtil;
51 import org.xml.sax.SAXException JavaDoc;
52
53 /** Abstract class that implements most of JAXWSSupportImpl methods
54  * Useful to implement instead of JAXWSSupportImpl
55  *
56  * @author mkuchtiak
57  * Created on February 7, 2006, 11:09 AM
58  */

59 public abstract class ProjectJAXWSSupport implements JAXWSSupportImpl {
60     private Project project;
61     private AntProjectHelper antProjectHelper;
62     
63     /** Creates a new instance of JAXWSSupport */
64     public ProjectJAXWSSupport(Project project, AntProjectHelper antProjectHelper) {
65         this.project = project;
66         this.antProjectHelper = antProjectHelper;
67     }
68     
69     public void removeService(String JavaDoc serviceName) {
70         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
71         if (jaxWsModel!=null) {
72             Service service = jaxWsModel.findServiceByName(serviceName);
73             if (service!=null) {
74                 // remove the service element as well as the implementation class
75
jaxWsModel.removeService(serviceName);
76                 writeJaxWsModel(jaxWsModel);
77                 WSUtils.removeImplClass(project,service.getImplementationClass());
78             }
79         }
80     }
81     /**
82      * Notification when Service (created from java) is removed from jax-ws.xml
83      * (JAXWSSupport needs to react when @WebService annotation is removed
84      * or when impl.class is removed (manually from project)
85      * Default implementation does nothing.
86      */

87     public void serviceFromJavaRemoved(String JavaDoc serviceName) {}
88     
89     public boolean isFromWSDL(String JavaDoc serviceName) {
90         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
91         Service service = jaxWsModel.findServiceByName(serviceName);
92         if (service!=null && service.getWsdlUrl()!=null) return true;
93         else return false;
94     }
95     
96     /**
97      * Returns the name of the implementation class
98      * given the service (ide) name
99      */

100     public String JavaDoc getServiceImpl(String JavaDoc serviceName) {
101         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
102         if (jaxWsModel!=null) {
103             Service service = jaxWsModel.findServiceByName(serviceName);
104             return service==null?null:service.getImplementationClass();
105         }
106         return null;
107     }
108     
109     public AntProjectHelper getAntProjectHelper() {
110         return antProjectHelper;
111     }
112     
113     public void addService(String JavaDoc serviceName, String JavaDoc serviceImpl, boolean isJsr109) {
114         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
115         if (jaxWsModel!=null) {
116             Boolean JavaDoc value = jaxWsModel.getJsr109();
117             if((value == null || Boolean.TRUE.equals(value)) && !isJsr109){
118                 jaxWsModel.setJsr109(Boolean.FALSE);
119                 writeJaxWsModel(jaxWsModel);
120             } else if (Boolean.FALSE.equals(value) && isJsr109) {
121                 jaxWsModel.setJsr109(Boolean.TRUE);
122                 writeJaxWsModel(jaxWsModel);
123             }
124         }
125         
126         if(!isJsr109 ){
127             try{
128                 addJaxwsArtifacts(project, serviceName, serviceImpl);
129             } catch(Exception JavaDoc e){
130                 ErrorManager.getDefault().notify(e); //TODO handle this
131
}
132         }
133     }
134     
135     protected abstract void addJaxwsArtifacts(Project project, String JavaDoc wsName,
136             String JavaDoc serviceImpl) throws Exception JavaDoc;
137     
138     /*
139      * Add web service to jax-ws.xml
140      * intended for web services from wsdl
141      * @return returns the unique IDE service name
142      */

143     
144     public String JavaDoc addService(String JavaDoc name, String JavaDoc serviceImpl, String JavaDoc wsdlUrl, String JavaDoc serviceName,
145             String JavaDoc portName, String JavaDoc packageName, boolean isJsr109) {
146         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
147         if (jaxWsModel!=null) {
148             String JavaDoc finalServiceName = WSUtils.findProperServiceName(name, jaxWsModel);
149             boolean serviceAdded=false;
150             
151             // HACK to enable filesystems to fire events when new folder will be created
152
// need to ask for children recursively
153
List JavaDoc<FileObject> subfolders = null;
154             FileObject serviceArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/service"); //NOI18N
155
if (serviceArtifactsFolder!=null) {
156                 Enumeration JavaDoc en = serviceArtifactsFolder.getChildren(true);
157                 subfolders = new ArrayList JavaDoc<FileObject>();
158                 while (en.hasMoreElements()) {
159                     FileObject ch = (FileObject) en.nextElement();
160                     if (ch.isFolder()) subfolders.add(ch);
161                 }
162             }
163             
164             FileObject localWsdl=null;
165             try {
166                 // download resources to xml-resources
167
FileObject xmlResorcesFo = getLocalWsdlFolderForService(finalServiceName,true);
168                 localWsdl = WSUtils.retrieveResource(
169                         xmlResorcesFo,
170                         new URI JavaDoc(wsdlUrl));
171                 if (localWsdl!=null) {
172                     
173                     WsdlWrapperHandler handler = null;
174                     try {
175                         handler = WsdlWrapperGenerator.parse(localWsdl.getURL().toExternalForm());
176                     } catch (ParserConfigurationException JavaDoc ex) {
177                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
178                     } catch (SAXException JavaDoc ex) {
179                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
180                     } catch (IOException JavaDoc ex) {
181                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
182                     }
183                     if (!handler.isServiceElement()) {
184                         StreamSource JavaDoc source = new StreamSource JavaDoc(localWsdl.getURL().toExternalForm());
185                         try {
186                             File JavaDoc wrapperWsdlFile = new File JavaDoc(FileUtil.toFile(localWsdl.getParent()), WsdlWrapperGenerator.getWrapperName(localWsdl.getURL())); //NOI18N
187

188                             if(!wrapperWsdlFile.exists()) {
189                                 try {
190                                     wrapperWsdlFile.createNewFile();
191                                 } catch(IOException JavaDoc ex) {
192                                     ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex);
193                                 }
194                             }
195                             if (wrapperWsdlFile.exists()) {
196                                 WsdlWrapperGenerator.generateWrapperWSDLContent(wrapperWsdlFile, source, handler.getTargetNsPrefix(),localWsdl.getNameExt());
197                                 localWsdl=FileUtil.toFileObject(wrapperWsdlFile);
198                             }
199                         } catch (IOException JavaDoc ex) {
200                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex);
201                         }
202                     }
203
204                     Boolean JavaDoc value = jaxWsModel.getJsr109();
205                     if((value == null || Boolean.TRUE.equals(value)) && !isJsr109){
206                         jaxWsModel.setJsr109(Boolean.FALSE);
207                     } else if (Boolean.FALSE.equals(value) && isJsr109) {
208                         jaxWsModel.setJsr109(Boolean.TRUE);
209                     }
210                     Service service=null;
211                     try {
212                         service = jaxWsModel.addService(finalServiceName, serviceImpl, wsdlUrl, serviceName, portName, packageName);
213                     } catch (ServiceAlreadyExistsExeption ex) {
214                         //this shouldn't happen
215
}
216                     String JavaDoc localWsdlUrl = FileUtil.getRelativePath(xmlResorcesFo, localWsdl);
217                     service.setLocalWsdlFile(localWsdlUrl);
218                     FileObject catalog = getCatalogFileObject();
219                     if (catalog!=null) service.setCatalogFile(CATALOG_FILE);
220                     writeJaxWsModel(jaxWsModel);
221                     serviceAdded=true;
222                     // copy resources to WEB-INF/wsdl/${serviceName}
223
FileObject wsdlFolder = getWsdlFolderForService(finalServiceName);
224                     WSUtils.copyFiles(xmlResorcesFo, wsdlFolder);
225                 }
226                 
227             } catch (URISyntaxException JavaDoc ex) {
228                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex);
229             } catch (UnknownHostException JavaDoc ex) {
230                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex);
231             } catch (IOException JavaDoc ex) {
232                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,ex);
233             }
234             
235             if (serviceAdded) {
236                 if(!isJsr109 ){
237                     try{
238                         addJaxwsArtifacts(project, serviceName, serviceImpl);
239                     } catch(Exception JavaDoc e){
240                         ErrorManager.getDefault().notify(e); //TODO handle this
241
}
242                 }
243                 FileObject buildImplFo = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
244                 try {
245                     ExecutorTask wsimportTask =
246                             ActionUtils.runTarget(buildImplFo,new String JavaDoc[]{"wsimport-service-"+finalServiceName},null); //NOI18N
247
wsimportTask.waitFinished();
248                     
249                     // refresh service artifacts directory due to code copletion
250
if (serviceArtifactsFolder==null)
251                         serviceArtifactsFolder = project.getProjectDirectory().getFileObject("build/generated/wsimport/service/"+packageName.replace('.','/'));
252                     if (serviceArtifactsFolder!=null) {
253                         serviceArtifactsFolder.refresh();
254                         // refresh also all sub-folders
255
if (subfolders!=null) {
256                             for (int i=0;i<subfolders.size();i++) {
257                                 ((FileObject) subfolders.get(i)).refresh();
258                             }
259                         }
260                     }
261                 } catch (IOException JavaDoc ex) {
262                     ErrorManager.getDefault().log(ex.getLocalizedMessage());
263                 } catch (IllegalArgumentException JavaDoc ex) {
264                     ErrorManager.getDefault().log(ex.getLocalizedMessage());
265                 }
266                 return finalServiceName;
267             }
268         }
269         return null;
270     }
271     
272     /**
273      * Returns the list of web services in the project
274      */

275     public List JavaDoc<Service> getServices() {
276         JaxWsModel jaxWsModel = (JaxWsModel)project.getLookup().lookup(JaxWsModel.class);
277         if (jaxWsModel!=null) {
278             Service[] services = jaxWsModel.getServices();
279             if (services!=null) {
280                 List JavaDoc<Service> list = new ArrayList JavaDoc<Service>();
281                 for (int i=0;i<services.length;i++) {
282                     list.add(services[i]);
283                 }
284                 return list;
285             }
286         }
287         return new ArrayList JavaDoc<Service>();
288     }
289
290     private void writeJaxWsModel(final JaxWsModel jaxWsModel) {
291         try {
292             final FileObject jaxWsFo = project.getProjectDirectory().getFileObject("nbproject/jax-ws.xml"); //NOI18N
293
jaxWsFo.getFileSystem().runAtomicAction(new AtomicAction() {
294                 public void run() {
295                     FileLock lock=null;
296                     OutputStream JavaDoc os=null;
297                     try {
298                         lock = jaxWsFo.lock();
299                         os = jaxWsFo.getOutputStream(lock);
300                         jaxWsModel.write(os);
301                         os.close();
302                     } catch (java.io.IOException JavaDoc ex) {
303                         ErrorManager.getDefault().notify(ex);
304                     } finally {
305                         if (os!=null) {
306                             try {
307                                 os.close();
308                             } catch (IOException JavaDoc ex) {}
309                         }
310                         if (lock!=null) lock.releaseLock();
311                     }
312                 }
313             });
314         } catch (IOException JavaDoc ex) {
315             ErrorManager.getDefault().notify(ex);
316         }
317     }
318     
319     /**
320      * return folder for local wsdl artifacts
321      */

322     public FileObject getLocalWsdlFolderForService(String JavaDoc serviceName, boolean createFolder) {
323         return getArtifactsFolder(serviceName, createFolder, true);
324     }
325     /**
326      * return folder for local wsdl bindings
327      */

328     public FileObject getBindingsFolderForService(String JavaDoc serviceName, boolean createFolder) {
329         return getArtifactsFolder(serviceName, createFolder, false);
330     }
331     
332     private FileObject getArtifactsFolder(String JavaDoc serviceName, boolean createFolder, boolean forWsdl) {
333         String JavaDoc folderName = forWsdl?"wsdl":"bindings"; //NOI18N
334
FileObject root = getXmlArtifactsRoot();
335         if (root==null) {
336             assert !createFolder : "Cannot create XML artifacts folder"; //NOI18N
337
return null;
338         }
339         FileObject wsdlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER+"/"+SERVICES_LOCAL_FOLDER+"/"+serviceName+"/"+folderName); //NOI18N
340
if (wsdlLocalFolder==null && createFolder) {
341             try {
342                 FileObject xmlLocalFolder = root.getFileObject(XML_RESOURCES_FOLDER);
343                 if (xmlLocalFolder==null) xmlLocalFolder = root.createFolder(XML_RESOURCES_FOLDER);
344                 FileObject servicesLocalFolder = xmlLocalFolder.getFileObject(SERVICES_LOCAL_FOLDER);
345                 if (servicesLocalFolder==null) servicesLocalFolder = xmlLocalFolder.createFolder(SERVICES_LOCAL_FOLDER);
346                 FileObject serviceLocalFolder = servicesLocalFolder.getFileObject(serviceName);
347                 if (serviceLocalFolder==null) serviceLocalFolder = servicesLocalFolder.createFolder(serviceName);
348                 wsdlLocalFolder=serviceLocalFolder.getFileObject(folderName);
349                 if (wsdlLocalFolder==null) wsdlLocalFolder = serviceLocalFolder.createFolder(folderName);
350             } catch (IOException JavaDoc ex) {
351                 return null;
352             }
353         }
354         return wsdlLocalFolder;
355     }
356     
357     /** return root folder for xml artifacts
358      */

359     protected FileObject getXmlArtifactsRoot() {
360         return project.getProjectDirectory();
361     }
362     
363     private FileObject getCatalogFileObject() {
364         return project.getProjectDirectory().getFileObject(CATALOG_FILE);
365     }
366     
367     public URL JavaDoc getCatalog() {
368         try {
369             FileObject catalog = getCatalogFileObject();
370             return catalog==null?null:catalog.getURL();
371         } catch (FileStateInvalidException ex) {
372             return null;
373         }
374         
375     }
376     
377     private FileObject getWsdlFolderForService(String JavaDoc name) throws IOException JavaDoc {
378         FileObject globalWsdlFolder = getWsdlFolder(true);
379         FileObject oldWsdlFolder = globalWsdlFolder.getFileObject(name);
380         if (oldWsdlFolder!=null) {
381             FileLock lock = oldWsdlFolder.lock();
382             try {
383                 oldWsdlFolder.delete(lock);
384             } finally {
385                 lock.releaseLock();
386             }
387         }
388         return globalWsdlFolder.createFolder(name);
389     }
390     
391     public abstract FileObject getWsdlFolder(boolean create) throws java.io.IOException JavaDoc;
392     
393     /** Get wsdlLocation information
394      * Useful for web service from wsdl
395      * @param name service "display" name
396      */

397     public abstract String JavaDoc getWsdlLocation(String JavaDoc serviceName);
398 }
399
Popular Tags