KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > dev > wizard > WebServiceCreator


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.dev.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.text.DateFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32 import org.apache.tools.ant.module.api.support.ActionUtils;
33 import org.netbeans.api.java.classpath.ClassPath;
34 import org.netbeans.api.java.project.JavaProjectConstants;
35 import org.netbeans.api.progress.ProgressHandle;
36 import org.netbeans.api.progress.ProgressHandleFactory;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.ProjectUtils;
39 import org.netbeans.api.project.SourceGroup;
40 import org.netbeans.api.project.libraries.Library;
41 import org.netbeans.api.project.libraries.LibraryManager;
42 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
43 import org.netbeans.modules.j2ee.common.Util;
44 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
45 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
46 import org.netbeans.modules.web.api.webmodule.WebModule;
47 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel;
48 import org.netbeans.modules.websvc.api.jaxws.project.config.Service;
49 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModelListener;
50 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModeler;
51 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
52 import org.netbeans.modules.websvc.jaxws.api.JAXWSSupport;
53 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlModel;
54 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
55 import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlService;
56 import org.netbeans.modules.websvc.api.webservices.WsCompileEditorSupport;
57 import org.netbeans.modules.websvc.core.jaxws.JaxWsUtils;
58 import org.netbeans.modules.websvc.dev.dd.gen.wscreation.Bean;
59 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender;
60 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
61 import org.netbeans.spi.project.ui.templates.support.Templates;
62 import org.openide.DialogDisplayer;
63 import org.openide.ErrorManager;
64 import org.openide.NotifyDescriptor;
65 import org.openide.WizardDescriptor;
66 import org.openide.cookies.EditorCookie;
67 import org.openide.execution.ExecutorTask;
68 import org.openide.filesystems.FileLock;
69 import org.openide.filesystems.FileObject;
70 import org.openide.filesystems.FileUtil;
71 import org.openide.loaders.DataFolder;
72 import org.openide.loaders.DataObject;
73 import org.openide.nodes.Node;
74 import org.openide.util.NbBundle;
75 import org.openide.util.RequestProcessor;
76
77 /**
78  *
79  * @author radko
80  */

81 public class WebServiceCreator {
82     
83     private Project project;
84     private String JavaDoc wsName;
85     private int serviceType;
86     private WizardDescriptor wiz;
87     private int projectType;
88     
89     protected static final int JSE_PROJECT_TYPE = 0;
90     protected static final int WEB_PROJECT_TYPE = 1;
91     protected static final int EJB_PROJECT_TYPE = 2;
92     
93     private boolean jwsdpSupported = false;
94     private boolean jsr109Supported = false;
95     private boolean jsr109oldSupported = false;
96     
97     /** Creates a new instance of WebServiceCreator */
98     
99     public WebServiceCreator(Project project, WizardDescriptor wiz) {
100         this.project = project;
101         this.wiz = wiz;
102     }
103     
104     public WebServiceCreator(Project project) {
105         this.project = project;
106     }
107     
108     public void create() {
109
110         serviceType = ((Integer JavaDoc) wiz.getProperty(WizardProperties.WEB_SERVICE_TYPE)).intValue();
111         initProjectInfo(project);
112         
113         // Use Progress API to display generator messages.
114
final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(WebServiceCreator.class, "TXT_WebServiceGeneration")); //NOI18N
115
handle.start(100);
116         
117         Runnable JavaDoc r = new Runnable JavaDoc() {
118             public void run() {
119                 try {
120                     generateWebService(handle);
121                 } catch (Exception JavaDoc e) {
122                     //finish progress bar
123
handle.finish();
124                     String JavaDoc message = e.getLocalizedMessage();
125                     if(message != null) {
126                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
127                         NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE);
128                         DialogDisplayer.getDefault().notify(nd);
129                     } else {
130                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
131                     }
132                 }
133             }
134         };
135         RequestProcessor.getDefault().post(r);
136     }
137     
138     public void createFromWSDL() {
139         
140         initProjectInfo(project);
141         
142         final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(WebServiceCreator.class, "TXT_WebServiceGeneration")); //NOI18N
143

144         Runnable JavaDoc r = new Runnable JavaDoc() {
145             public void run() {
146                 try {
147                     if (Util.isJavaEE5orHigher(project) ||
148                             (!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported)
149                             || jwsdpSupported) {
150                         handle.start();
151                         generateWsFromWsdl15(handle);
152                     } else {
153                         handle.start(100);
154                         generateWsFromWsdl14(handle);
155                     }
156                 } catch (Exception JavaDoc e) {
157                     //finish progress bar
158
handle.finish();
159                     String JavaDoc message = e.getLocalizedMessage();
160                     if(message != null) {
161                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
162                         NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE);
163                         DialogDisplayer.getDefault().notify(nd);
164                     } else {
165                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
166                     }
167                 }
168             }
169         };
170         RequestProcessor.getDefault().post(r);
171     }
172     
173     private void initProjectInfo(Project project) {
174         JAXWSSupport wss = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
175         if (wss != null) {
176             Map JavaDoc properties = wss.getAntProjectHelper().getStandardPropertyEvaluator().getProperties();
177             String JavaDoc serverInstance = (String JavaDoc)properties.get("j2ee.server.instance"); //NOI18N
178
if (serverInstance != null) {
179                 J2eePlatform j2eePlatform = Deployment.getDefault().getJ2eePlatform(serverInstance);
180                 if (j2eePlatform != null) {
181                     jwsdpSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP);
182                     jsr109Supported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_JSR109);
183                     jsr109oldSupported = j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSCOMPILE);
184                 }
185             }
186         }
187         
188         WebModule wm = WebModule.getWebModule(project.getProjectDirectory());
189         EjbJar em = EjbJar.getEjbJar(project.getProjectDirectory());
190         if (em != null)
191             projectType = EJB_PROJECT_TYPE;
192         else if (wm != null)
193             projectType = WEB_PROJECT_TYPE;
194         else
195             projectType = JSE_PROJECT_TYPE;
196     }
197     
198     private FileObject generateJaxWSImplFromTemplate(FileObject pkg) throws Exception JavaDoc{
199         DataFolder df = DataFolder.findFolder(pkg);
200         FileObject template = Templates.getTemplate(wiz);
201         
202         if (projectType==EJB_PROJECT_TYPE) { //EJB Web Service
203
FileObject templateParent = template.getParent();
204             template = templateParent.getFileObject("EjbWebService","java"); //NOI18N
205
}
206         DataObject dTemplate = DataObject.find(template);
207         DataObject dobj = dTemplate.createFromTemplate(df, wsName);
208         FileObject createdFile = dobj.getPrimaryFile();
209         
210         /* don't rely on annotation listener to notify the service creation */
211         JaxWsModel jaxWsModel = project.getLookup().lookup(JaxWsModel.class);
212         if ( jaxWsModel!= null) {
213             ClassPath classPath = ClassPath.getClassPath(createdFile, ClassPath.SOURCE);
214             String JavaDoc serviceImplPath = classPath.getResourceName(createdFile, '.', false);
215             jaxWsModel.addService(wsName, serviceImplPath);
216             jaxWsModel.write();
217         }
218         
219         openFileInEditor(dobj);
220         return createdFile;
221     }
222     
223     private void openFileInEditor(DataObject dobj){
224         final EditorCookie ec = (EditorCookie)dobj.getCookie(EditorCookie.class);
225         RequestProcessor.getDefault().post(new Runnable JavaDoc(){
226             public void run(){
227                 ec.open();
228             }
229         }, 1000);
230     }
231     
232     private String JavaDoc uniqueWSName(final String JavaDoc origName, List JavaDoc<String JavaDoc> names ){
233         int uniquifier = 0;
234         String JavaDoc truename = origName;
235         while(names.contains(truename)){
236             truename = origName + String.valueOf(++uniquifier);
237         }
238         return truename;
239     }
240     
241     private String JavaDoc getUniqueJaxwsName(JAXWSSupport jaxWsSupport, String JavaDoc origName){
242         List JavaDoc<Service> webServices = jaxWsSupport.getServices();
243         List JavaDoc<String JavaDoc> serviceNames = new ArrayList JavaDoc<String JavaDoc>(webServices.size());
244         for(Service service : webServices){
245             serviceNames.add(service.getName());
246         }
247         return uniqueWSName(origName, serviceNames);
248     }
249     
250     private String JavaDoc getUniqueJaxrpcName(WebServicesSupport wsSupport, String JavaDoc origName){
251         List JavaDoc<WsCompileEditorSupport.ServiceSettings> webServices = wsSupport.getServices();
252         List JavaDoc<String JavaDoc> serviceNames = new ArrayList JavaDoc<String JavaDoc>(webServices.size());
253         for(WsCompileEditorSupport.ServiceSettings service: webServices){
254             serviceNames.add(service.getServiceName());
255         }
256         return uniqueWSName(origName, serviceNames);
257     }
258     
259     //TODO it should be refactored to prevent duplicate code but it is more readable now during development
260
private void generateWebService(ProgressHandle handle) throws Exception JavaDoc {
261         
262         FileObject pkg = Templates.getTargetFolder(wiz);
263         wsName = Templates.getTargetName(wiz);
264         
265         if (serviceType == WizardProperties.FROM_SCRATCH){
266             if ((projectType == JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
267                     ((Util.isJavaEE5orHigher(project) &&
268                     (projectType == WEB_PROJECT_TYPE || projectType == EJB_PROJECT_TYPE))) ||
269                     (jwsdpSupported)
270                     ) {
271                 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
272                 wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
273                 handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_WS"), 50); //NOI18N
274
FileObject createdFile = generateJaxWSImplFromTemplate(pkg);
275                 handle.finish();
276                 return;
277             } else if (!Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE ||projectType == EJB_PROJECT_TYPE)) { //NOI18N
278
if ((!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported)) { //if the platform is not jsr 109
279

280                     JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
281                     wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
282                     handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_WS"), 50); //NOI18N
283
//add the JAXWS 2.0 library, if not already added
284
addJaxws20Library(project);
285                     generateJaxWSImplFromTemplate(pkg);
286                     
287                     handle.finish();
288                     
289                 } else { //it's not J2ee 1.5 but it is JSR 109
290
WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
291                     assert wsSupport != null;
292                     wsName = getUniqueJaxrpcName(wsSupport, wsName);
293                     WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
294                     handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
295
generator.generateWebService();
296                     
297                     URI JavaDoc targetNS = null;
298                     URI JavaDoc typeNS = null;
299                     try {
300                         targetNS = generator.getTargetNS();
301                         typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
302
} catch(java.net.URISyntaxException JavaDoc e) {
303                         String JavaDoc mes = NbBundle.getMessage(WebServiceCreator.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
304
throw new Exception JavaDoc(mes);
305                     }
306                     //Create config file
307
handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
308
String JavaDoc servantClassName = generator.getServantClassName();
309                     String JavaDoc seiClassName = generator.getSEIClassName();
310                     FileObject configFile = null;
311                     configFile = generator.generateConfigFile(seiClassName, servantClassName, targetNS, typeNS);
312                     handle.progress(70);
313                     
314                     //Add web service entries to the project's property file, project file
315
wsSupport.addServiceImpl(wsName, configFile, false);
316                     handle.progress(90);
317                     
318                     //Add web service entries to the module's DD
319
wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
320                     
321                     //Add webservice entry in webservices.xml
322
handle.progress(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
323
String JavaDoc portTypeName = null;
324                     generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
325                     
326                     handle.finish();
327                     
328                     return;
329                 }
330             }
331         }
332         if (serviceType == WizardProperties.ENCAPSULATE_SESSION_BEAN) {
333             if ((projectType == JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
334                     (Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE
335                     ||projectType == EJB_PROJECT_TYPE)) //NOI18N
336
) {
337                 
338                 JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
339                 wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
340                 // PENDING : the part below need to be simplified
341
// (the SEI class doesn't need to be generated)
342
WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
343                 assert wsSupport != null;
344                 WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
345                 handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
346
Node[] nodes = (Node[]) wiz.getProperty(WizardProperties.DELEGATE_TO_SESSION_BEAN);
347                 generator.generateWebServiceJavaEE5(nodes);
348                 
349                 handle.progress(70);
350                 
351                 String JavaDoc servantClassName = generator.getServantClassName();
352                 generator.addReferences(servantClassName, nodes);
353                 
354                 
355                 jaxWsSupport.addService(wsName,servantClassName, jsr109Supported);
356                 
357                 handle.finish();
358                 
359                 return;
360             } else if (projectType == WEB_PROJECT_TYPE ||projectType == EJB_PROJECT_TYPE) {
361                 WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
362                 assert wsSupport != null;
363                 wsName = getUniqueJaxrpcName(wsSupport, wsName);
364                 WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
365                 handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
366
Node[] nodes = (Node[]) wiz.getProperty(WizardProperties.DELEGATE_TO_SESSION_BEAN);
367                 generator.generateWebService(nodes);
368                 
369                 URI JavaDoc targetNS = null;
370                 URI JavaDoc typeNS = null;
371                 try {
372                     targetNS = generator.getTargetNS();
373                     typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
374
} catch(java.net.URISyntaxException JavaDoc e) {
375                     String JavaDoc mes = NbBundle.getMessage(WebServiceCreator.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
376
throw new Exception JavaDoc(mes);
377                 }
378                 //Create config file
379
handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
380
String JavaDoc servantClassName = generator.getServantClassName();
381                 String JavaDoc seiClassName = generator.getSEIClassName();
382                 FileObject configFile = null;
383                 configFile = generator.generateConfigFile(seiClassName, servantClassName, targetNS, typeNS);
384                 handle.progress(70);
385                 
386                 //Add web service entries to the project's property file, project file
387
wsSupport.addServiceImpl(wsName, configFile, false);
388                 handle.progress(90);
389                 
390                 //Add web service entries to the module's DD
391
wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
392                 
393                 //Add webservice entry in webservices.xml
394
handle.progress(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
395
String JavaDoc portTypeName = null;
396                 generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
397                 
398                 generator.addReferences(servantClassName, nodes);
399                 
400                 handle.finish();
401             }
402             
403             return;
404         }
405     }
406     
407     private void addJaxws20Library(Project project) throws Exception JavaDoc {
408         
409         // check if the wsimport class is already present - this means we don't need to add the library
410
SourceGroup[] sgs = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
411         ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE);
412         FileObject wsimportFO = classPath.findResource("com/sun/tools/ws/ant/WsImport.class"); // NOI18N
413
if (wsimportFO != null) {
414             return;
415         }
416         
417         ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class);
418         Library jaxws20_ext = LibraryManager.getDefault().getLibrary("jaxws20"); //NOI18N
419
if (pce != null && jaxws20_ext != null) {
420             try{
421                 pce.addLibrary(jaxws20_ext);
422             } catch(IOException JavaDoc e){
423                 throw new Exception JavaDoc("Unable to add JAXWS 2.0 Library. " + e.getMessage());
424             }
425         } else{
426             throw new Exception JavaDoc("Unable to add JAXWS 2.0 Library. " +
427                     "ProjectClassPathExtender or library not found");
428         }
429     }
430     
431     private void generateWsFromWsdl14(ProgressHandle handle) throws Exception JavaDoc {
432         FileObject pkg = Templates.getTargetFolder(wiz);
433         wsName = Templates.getTargetName(wiz);
434         WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
435         assert wsSupport != null;
436         WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
437         
438         //coming from wsdl
439
FileObject wsDDFolder = wsSupport.getWsDDFolder();
440         //get wsdl folder, if none, create it
441
FileObject wsdlFolder = wsDDFolder.getFileObject("wsdl"); //NOI18N
442
if(wsdlFolder == null) {
443             wsdlFolder = wsDDFolder.createFolder("wsdl"); //NOI18N
444
}
445         
446         handle.progress(NbBundle.getMessage(WebServiceWizard.class, "MSG_PARSING_WSDL"), 30); //NOI18N
447
String JavaDoc wsdlFilePath = (String JavaDoc)wiz.getProperty(WizardProperties.WSDL_FILE_PATH);
448         File JavaDoc normalizedWsdlFilePath = FileUtil.normalizeFile(new File JavaDoc(wsdlFilePath));
449         final FileObject sourceWsdlFile = FileUtil.toFileObject(normalizedWsdlFilePath);
450         if(sourceWsdlFile == null) {
451             String JavaDoc mes = NbBundle.getMessage(WebServiceWizard.class, "MSG_CANNOT_GET_FILE_OBJECT", normalizedWsdlFilePath.getAbsolutePath()); //NOI18N
452
throw new IOException JavaDoc(mes);
453         }
454         List JavaDoc schemaFiles = WSGenerationUtil.getSchemaNames(sourceWsdlFile,true);
455         String JavaDoc changedWsName = null;
456         try {
457             changedWsName = generator.parseWSDL(sourceWsdlFile.getInputStream());
458         } catch (NoWSPortDefinedException exc) {
459             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "WSDL does not contain any defined ports"); //NOI18N
460
String JavaDoc mes = NbBundle.getMessage(WebServiceGenerator.class, "ERR_WsdlNoPortDefined"); // NOI18N
461
NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
462             DialogDisplayer.getDefault().notify(desc);
463             handle.finish();
464             return;
465         }
466         if (changedWsName==null) changedWsName = wsName;
467         handle.progress(NbBundle.getMessage(WebServiceWizard.class, "MSG_CREATING_NEW_WSDL"), 50); //NOI18N
468
FileObject wsdlFO = generator.generateWSDL(WebServiceGenerator.WSDL_TEMPLATE, changedWsName, generator.getSoapBinding(),
469                 generator.getPortTypeName(), wsdlFolder, sourceWsdlFile.getParent(), wsName, new StreamSource JavaDoc(sourceWsdlFile.getInputStream()));
470         
471         URI JavaDoc targetNS = null;
472         URI JavaDoc typeNS = null;
473         try {
474             targetNS = generator.getTargetNS();
475             typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
476
} catch(java.net.URISyntaxException JavaDoc e) {
477             String JavaDoc mes = NbBundle.getMessage(WebServiceWizard.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
478
throw new Exception JavaDoc(mes);
479         }
480         //Create config file
481
handle.progress(NbBundle.getMessage(WebServiceWizard.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
482
String JavaDoc servantClassName = generator.getServantClassName();
483         String JavaDoc seiClassName = generator.getSEIClassName();
484         FileObject configFile = null;
485         
486         File JavaDoc wsdlFile = FileUtil.toFile(wsdlFO);
487         URI JavaDoc wsdlURI = wsdlFile.toURI();
488         configFile = generator.generateConfigFile(wsdlURI);
489         
490         handle.progress(70);
491         
492         //Add web service entries to the project's property file, project file
493
wsSupport.addServiceImpl(wsName, configFile, true, generator.getWscompileFeatures());
494         //run the wscompile ant target
495
handle.progress(NbBundle.getMessage(WebServiceWizard.class, "MSG_RUNNING_WSCOMPILE_TARGET")); //NOI18N
496
String JavaDoc targetName = wsName + "_wscompile"; //NOI18N
497
ExecutorTask task = ActionUtils.runTarget(findBuildXml(), new String JavaDoc[]{targetName}, null);
498         task.waitFinished();
499         if(task.result() != 0) {
500             String JavaDoc mes = NbBundle.getMessage(WebServiceWizard.class, "MSG_WSCOMPILE_UNSUCCESSFUL"); //NOI18N
501
wsSupport.removeProjectEntries(wsName);
502             try {
503                 deleteFile(configFile);
504                 deleteFile(wsdlFO);
505             } catch(IOException JavaDoc e) {
506                 String JavaDoc message = NbBundle.getMessage(WebServiceWizard.class, "MSG_UNABLE_DELETE_FILES"); //NOI18N
507
NotifyDescriptor nd =
508                         new NotifyDescriptor.Message(message,
509                         NotifyDescriptor.ERROR_MESSAGE);
510                 DialogDisplayer.getDefault().notify(nd);
511                 //let this through
512
}
513             throw new Exception JavaDoc(mes);
514         }
515         handle.progress(90);
516         addHeaderComments(wsName, servantClassName, pkg);
517         wsSupport.addInfrastructure(servantClassName, pkg);
518         
519         //open the class in the editor
520
String JavaDoc implClassName = servantClassName.substring(servantClassName.lastIndexOf(".") + 1); //NOI18N
521
FileObject clz = pkg.getFileObject(implClassName, "java"); //NOI18N
522
DataObject dobj = DataObject.find(clz);
523         EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class);
524         ec.open();
525         
526         //Add web service entries to the module's DD
527
wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
528         
529         //Add webservice entry in webservices.xml
530
handle.progress(NbBundle.getMessage(WebServiceWizard.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
531
String JavaDoc portTypeName = null;
532         
533         portTypeName = generator.getPortTypeName();
534         
535         generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
536         
537         handle.finish();
538     }
539     
540     private void generateWsFromWsdl15(final ProgressHandle handle) throws Exception JavaDoc {
541         String JavaDoc wsdlFilePath = (String JavaDoc) wiz.getProperty(WizardProperties.WSDL_FILE_PATH);
542         File JavaDoc normalizedWsdlFilePath = FileUtil.normalizeFile(new File JavaDoc(wsdlFilePath));
543         //convert to URI first to take care of spaces
544
final URL JavaDoc wsdlURL = normalizedWsdlFilePath.toURI().toURL();
545         final WsdlService service = (WsdlService) wiz.getProperty(WizardProperties.WSDL_SERVICE);
546         if (service==null) {
547             JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
548             FileObject targetFolder = Templates.getTargetFolder(wiz);
549             String JavaDoc targetName = Templates.getTargetName(wiz);
550             WsdlServiceHandler handler = (WsdlServiceHandler)wiz.getProperty(WizardProperties.WSDL_SERVICE_HANDLER);
551             JaxWsUtils.generateJaxWsArtifacts(project,targetFolder,targetName,wsdlURL,handler.getServiceName(),handler.getPortName());
552             WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER);
553             if (wsdlModeler!=null && wsdlModeler.getCreationException()!=null) {
554                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(
555                             NbBundle.getMessage(WebServiceCreator.class,"TXT_CannotGenerateArtifacts",
556                                                 wsdlModeler.getCreationException().getLocalizedMessage()),
557                             NotifyDescriptor.ERROR_MESSAGE)
558                     );
559             }
560             handle.finish();
561         } else {
562             final WsdlPort port = (WsdlPort) wiz.getProperty(WizardProperties.WSDL_PORT);
563             //String portJavaName = port.getJavaName();
564
WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER);
565             // don't set the packageName for modeler (use the default one generated from target Namespace)
566
wsdlModeler.generateWsdlModel(new WsdlModelListener() {
567                 public void modelCreated(WsdlModel model) {
568                     WsdlService service1 = model.getServiceByName(service.getName());
569                     WsdlPort port1 = service1.getPortByName(port.getName());
570                     port1.setSOAPVersion(port.getSOAPVersion());
571                     FileObject targetFolder = Templates.getTargetFolder(wiz);
572                     String JavaDoc targetName = Templates.getTargetName(wiz);
573                     try {
574                         JaxWsUtils.generateJaxWsImplementationClass(project, targetFolder, targetName, wsdlURL, service1, port1);
575                         handle.finish();
576                     } catch (Exception JavaDoc ex) {
577                         handle.finish();
578                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
579                     }
580                 }
581             });
582         }
583     }
584     
585     private void addHeaderComments(String JavaDoc wsName, String JavaDoc servantClassName, FileObject pkg) {
586         String JavaDoc comment = NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_WS_CLASS_COMMENT", wsName); //NOI18N
587
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(comment + "\n"); //NOI18N
588
buffer.append(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_CREATED_COMMENT")+ " " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + "\n" ); //NOI18N
589
buffer.append("@author " + System.getProperty("user.name") ); //NOI18N
590
// Retouche
591
// JavaMetamodel.getManager().waitScanFinished();
592
// JavaClass clazz = JMIUtils.findClass(servantClassName);
593
// if (clazz!=null) {
594
// String javadoc = clazz.getJavadocText();
595
// clazz.setJavadocText(buffer.toString());
596
// }
597
}
598     
599     private void deleteFile(FileObject file)throws IOException JavaDoc {
600         FileLock lock = null;
601         try {
602             lock = file.lock();
603             file.delete(lock);
604         } finally {
605             if(lock != null) {
606                 lock.releaseLock();
607             }
608         }
609     }
610     
611     private FileObject findBuildXml() {
612         return project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
613     }
614     
615     public void createMessageHandler() throws IOException JavaDoc {
616         
617         initProjectInfo(project);
618         
619         //test JAX-WS library
620
SourceGroup[] sgs = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
621         ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE);
622         FileObject wsimportFO = classPath.findResource("com/sun/tools/ws/ant/WsImport.class"); // NOI18N
623

624         if ((projectType == JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
625                 (projectType == JSE_PROJECT_TYPE && Util.getSourceLevel(project).equals("1.5") && wsimportFO != null) || //NOI18N
626
(Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE || projectType == EJB_PROJECT_TYPE)) || //NOI18N
627
(jwsdpSupported) || (!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported)
628                 ) {
629             String JavaDoc handlerName = Templates.getTargetName(wiz);
630             FileObject pkg = Templates.getTargetFolder(wiz);
631             DataFolder df = DataFolder.findFolder(pkg);
632             FileObject template = Templates.getTemplate(wiz);
633             DataObject dTemplate = DataObject.find(template);
634             DataObject dobj = dTemplate.createFromTemplate(df, handlerName);
635             
636             //open in the editor
637
final EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class);
638             RequestProcessor.getDefault().post(new Runnable JavaDoc(){
639                 public void run(){
640                     ec.open();
641                 }
642             }, 1000);
643         } else {
644             WSGenerationUtil wsgenUtil = new WSGenerationUtil();
645             final String JavaDoc HANDLER_TEMPLATE = WSGenerationUtil.TEMPLATE_BASE + "MessageHandler.xml"; //NOI18N
646
FileObject pkg = Templates.getTargetFolder(wiz);
647             String JavaDoc handlerName = Templates.getTargetName(wiz);
648             String JavaDoc pkgName = wsgenUtil.getSelectedPackageName(pkg, project);
649             Bean b = wsgenUtil.getDefaultBean();
650             b.setCommentDataWsName(handlerName);
651             b.setClassname(true);
652             b.setClassnameName(handlerName);
653             if(pkgName != null) {
654                 b.setClassnamePackage(pkgName);
655             }
656             String JavaDoc handlerClass = wsgenUtil.getFullClassName(pkgName, wsgenUtil.generateClass(HANDLER_TEMPLATE, b, pkg, true));
657         }
658     }
659     
660     public void createLogicalHandler() throws IOException JavaDoc {
661         
662         initProjectInfo(project);
663         
664         //test JAX-WS library
665
SourceGroup[] sgs = ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
666         ClassPath classPath = ClassPath.getClassPath(sgs[0].getRootFolder(),ClassPath.COMPILE);
667         FileObject wsimportFO = classPath.findResource("com/sun/tools/ws/ant/WsImport.class"); // NOI18N
668

669         if ((projectType == JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
670                 (projectType == JSE_PROJECT_TYPE && Util.getSourceLevel(project).equals("1.5") && wsimportFO != null) || //NOI18N
671
(Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE || projectType == EJB_PROJECT_TYPE)) || //NOI18N
672
(jwsdpSupported) || (!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported)
673                 ) {
674             String JavaDoc handlerName = Templates.getTargetName(wiz);
675             FileObject pkg = Templates.getTargetFolder(wiz);
676             DataFolder df = DataFolder.findFolder(pkg);
677             FileObject template = Templates.getTemplate(wiz);
678             DataObject dTemplate = DataObject.find(template);
679             DataObject dobj = dTemplate.createFromTemplate(df, handlerName);
680             
681             //open in the editor
682
final EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class);
683             RequestProcessor.getDefault().post(new Runnable JavaDoc(){
684                 public void run(){
685                     ec.open();
686                 }
687             }, 1000);
688         }
689     }
690     
691     public int getProjectType() {
692         initProjectInfo(project);
693         return projectType;
694     }
695     
696 }
697
698
Popular Tags