KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > core > dev > wizard > JaxWsServiceCreator


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.core.dev.wizard;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
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 /**
61  *
62  * @author Radko, Milan Kuchtiak
63  */

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     /**
71      * Creates a new instance of WebServiceClientCreator
72      */

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 JavaDoc {
80         serviceType = ((Integer JavaDoc) wiz.getProperty(WizardProperties.WEB_SERVICE_TYPE)).intValue();
81         projectType = projectInfo.getProjectType();
82         
83         // Use Progress API to display generator messages.
84
final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(JaxWsServiceCreator.class, "TXT_WebServiceGeneration")); //NOI18N
85
handle.start(100);
86         
87         Runnable JavaDoc r = new Runnable JavaDoc() {
88             public void run() {
89                 try {
90                     generateWebService(handle);
91                 } catch (Exception JavaDoc e) {
92                     //finish progress bar
93
handle.finish();
94                     String JavaDoc 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 JavaDoc {
109         
110         //initProjectInfo(project);
111

112         final ProgressHandle handle = ProgressHandleFactory.createHandle( NbBundle.getMessage(JaxWsServiceCreator.class, "TXT_WebServiceGeneration")); //NOI18N
113

114         Runnable JavaDoc r = new Runnable JavaDoc() {
115             public void run() {
116                 try {
117 // if (Util.isJavaEE5orHigher(project) ||
118
// (!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported)
119
// || jwsdpSupported) {
120
handle.start();
121                         generateWsFromWsdl15(handle);
122 // } else {
123
// handle.start(100);
124
// generateWsFromWsdl14(handle);
125
// }
126
} catch (Exception JavaDoc e) {
127                     //finish progress bar
128
handle.finish();
129                     String JavaDoc 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     //TODO it should be refactored to prevent duplicate code but it is more readable now during development
145
private void generateWebService(ProgressHandle handle) throws Exception JavaDoc {
146         
147         FileObject pkg = Templates.getTargetFolder(wiz);
148         String JavaDoc wsName = Templates.getTargetName(wiz);
149         
150         
151         if (serviceType == WizardProperties.FROM_SCRATCH) {
152 // if ((projectType == ProjectInfo.JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
153
// ((Util.isJavaEE5orHigher(project) &&
154
// (projectType == WEB_PROJECT_TYPE || projectType == EJB_PROJECT_TYPE))) ||
155
// (jwsdpSupported)
156
// ) {
157
JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(projectInfo.getProject().getProjectDirectory());
158                 wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
159                 handle.progress(NbBundle.getMessage(JaxWsServiceCreator.class, "MSG_GEN_WS"), 50); //NOI18N
160
//add the JAXWS 2.0 library, if not already added
161
if (addJaxWsLib) addJaxws20Library(projectInfo.getProject());
162                 generateJaxWSImplFromTemplate(pkg, wsName, projectType);
163                 handle.finish();
164                 return;
165 // } else if (!Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE ||projectType == EJB_PROJECT_TYPE) || (wsitSupported)) { //NOI18N
166
// if ((!jsr109Supported && projectType == WEB_PROJECT_TYPE && !jsr109oldSupported) || (wsitSupported)) { //if the platform is not jsr 109
167
//
168
// JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
169
// wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
170
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_WS"), 50); //NOI18N
171
// //add the JAXWS 2.0 library, if not already added
172
// addJaxws20Library(project);
173
// generateJaxWSImplFromTemplate(pkg);
174
//
175
// handle.finish();
176
//
177
// } else { //it's not J2ee 1.5 but it is JSR 109
178
// WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
179
// assert wsSupport != null;
180
// wsName = getUniqueJaxrpcName(wsSupport, wsName);
181
// WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
182
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
183
// generator.generateWebService();
184
//
185
// URI targetNS = null;
186
// URI typeNS = null;
187
// try {
188
// targetNS = generator.getTargetNS();
189
// typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
190
// } catch(java.net.URISyntaxException e) {
191
// String mes = NbBundle.getMessage(WebServiceCreator.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
192
// throw new Exception(mes);
193
// }
194
// //Create config file
195
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
196
// String servantClassName = generator.getServantClassName();
197
// String seiClassName = generator.getSEIClassName();
198
// FileObject configFile = null;
199
// configFile = generator.generateConfigFile(seiClassName, servantClassName, targetNS, typeNS);
200
// handle.progress(70);
201
//
202
// //Add web service entries to the project's property file, project file
203
// wsSupport.addServiceImpl(wsName, configFile, false);
204
// handle.progress(90);
205
//
206
// //Add web service entries to the module's DD
207
// wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
208
//
209
// //Add webservice entry in webservices.xml
210
// handle.progress(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
211
// String portTypeName = null;
212
// generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
213
//
214
// handle.finish();
215
//
216
// return;
217
// }
218
// }
219
}
220 // if (serviceType == WizardProperties.ENCAPSULATE_SESSION_BEAN) {
221
// if ((projectType == JSE_PROJECT_TYPE && Util.isSourceLevel16orHigher(project)) ||
222
// (Util.isJavaEE5orHigher(project) && (projectType == WEB_PROJECT_TYPE
223
// ||projectType == EJB_PROJECT_TYPE)) //NOI18N
224
// ) {
225
//
226
// JAXWSSupport jaxWsSupport = JAXWSSupport.getJAXWSSupport(project.getProjectDirectory());
227
// wsName = getUniqueJaxwsName(jaxWsSupport, wsName);
228
// // PENDING : the part below need to be simplified
229
// // (the SEI class doesn't need to be generated)
230
// WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
231
// assert wsSupport != null;
232
// WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
233
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
234
// Node[] nodes = (Node[]) wiz.getProperty(WizardProperties.DELEGATE_TO_SESSION_BEAN);
235
// generator.generateWebServiceJavaEE5(nodes);
236
//
237
// handle.progress(70);
238
//
239
// String servantClassName = generator.getServantClassName();
240
// generator.addReferences(servantClassName, nodes);
241
//
242
//
243
// jaxWsSupport.addService(wsName,servantClassName, jsr109Supported);
244
//
245
// handle.finish();
246
//
247
// return;
248
// } else if (projectType == WEB_PROJECT_TYPE ||projectType == EJB_PROJECT_TYPE) {
249
// WebServicesSupport wsSupport = WebServicesSupport.getWebServicesSupport(pkg);
250
// assert wsSupport != null;
251
// wsName = getUniqueJaxrpcName(wsSupport, wsName);
252
// WebServiceGenerator generator = new WebServiceGenerator(wsSupport, wsName, pkg, project);
253
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_GEN_SEI_AND_IMPL"), 50); //NOI18N
254
// Node[] nodes = (Node[]) wiz.getProperty(WizardProperties.DELEGATE_TO_SESSION_BEAN);
255
// generator.generateWebService(nodes);
256
//
257
// URI targetNS = null;
258
// URI typeNS = null;
259
// try {
260
// targetNS = generator.getTargetNS();
261
// typeNS = generator.getDefaultTypeNS(wsName); //Need to get from user
262
// } catch(java.net.URISyntaxException e) {
263
// String mes = NbBundle.getMessage(WebServiceCreator.class, "MSG_INVALID_URL_SYNTAX"); //NOI18N
264
// throw new Exception(mes);
265
// }
266
// //Create config file
267
// handle.progress(NbBundle.getMessage(WebServiceCreator.class, "MSG_CREATING_WSCOMPILE_ARTIFACTS")); //NOI18N
268
// String servantClassName = generator.getServantClassName();
269
// String seiClassName = generator.getSEIClassName();
270
// FileObject configFile = null;
271
// configFile = generator.generateConfigFile(seiClassName, servantClassName, targetNS, typeNS);
272
// handle.progress(70);
273
//
274
// //Add web service entries to the project's property file, project file
275
// wsSupport.addServiceImpl(wsName, configFile, false);
276
// handle.progress(90);
277
//
278
// //Add web service entries to the module's DD
279
// wsSupport.addServiceEntriesToDD(wsName, seiClassName, servantClassName);
280
//
281
// //Add webservice entry in webservices.xml
282
// handle.progress(NbBundle.getMessage(NewWebServiceWizardIterator.class, "MSG_ADDING_DD_ENTRIES")); //NOI18N
283
// String portTypeName = null;
284
// generator.addWebServiceEntry(seiClassName, portTypeName, targetNS);
285
//
286
// generator.addReferences(servantClassName, nodes);
287
//
288
// handle.finish();
289
// }
290
//
291
// return;
292
// }
293
}
294     
295     private FileObject generateJaxWSImplFromTemplate(FileObject pkg, String JavaDoc wsName, int projectType) throws Exception JavaDoc {
296         DataFolder df = DataFolder.findFolder(pkg);
297         FileObject template = Templates.getTemplate(wiz);
298         
299         if (projectType==ProjectInfo.EJB_PROJECT_TYPE) { //EJB Web Service
300
FileObject templateParent = template.getParent();
301             template = templateParent.getFileObject("EjbWebService","java"); //NOI18N
302
}
303         DataObject dTemplate = DataObject.find(template);
304         DataObject dobj = dTemplate.createFromTemplate(df, wsName);
305         FileObject createdFile = dobj.getPrimaryFile();
306         
307         /* don't rely on annotation listener to notify the service creation */
308         JaxWsModel jaxWsModel = projectInfo.getProject().getLookup().lookup(JaxWsModel.class);
309         if ( jaxWsModel!= null) {
310             ClassPath classPath = ClassPath.getClassPath(createdFile, ClassPath.SOURCE);
311             String JavaDoc 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 JavaDoc getUniqueJaxwsName(JAXWSSupport jaxWsSupport, String JavaDoc origName){
320         List JavaDoc<Service> webServices = jaxWsSupport.getServices();
321         List JavaDoc<String JavaDoc> serviceNames = new ArrayList JavaDoc<String JavaDoc>(webServices.size());
322         for(Service service : webServices){
323             serviceNames.add(service.getName());
324         }
325         return uniqueWSName(origName, serviceNames);
326     }
327     
328     private String JavaDoc uniqueWSName(final String JavaDoc origName, List JavaDoc<String JavaDoc> names ){
329         int uniquifier = 0;
330         String JavaDoc 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 JavaDoc(){
340             public void run(){
341                 ec.open();
342             }
343         }, 1000);
344     }
345     
346     private void addJaxws20Library(Project project) throws Exception JavaDoc {
347         
348         // check if the wsimport class is already present - this means we don't need to add the library
349
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"); // NOI18N
352
if (wsimportFO != null) {
353             return;
354         }
355         
356         ProjectClassPathExtender pce = (ProjectClassPathExtender)project.getLookup().lookup(ProjectClassPathExtender.class);
357         Library jaxws20_ext = LibraryManager.getDefault().getLibrary("jaxws20"); //NOI18N
358
if (pce != null && jaxws20_ext != null) {
359             try{
360                 pce.addLibrary(jaxws20_ext);
361             } catch(IOException JavaDoc e){
362                 throw new Exception JavaDoc("Unable to add JAXWS 2.0 Library. " + e.getMessage());
363             }
364         } else{
365             throw new Exception JavaDoc("Unable to add JAXWS 2.0 Library. " +
366                     "ProjectClassPathExtender or library not found");
367         }
368     }
369     private void generateWsFromWsdl15(final ProgressHandle handle) throws Exception JavaDoc {
370         String JavaDoc wsdlFilePath = (String JavaDoc) wiz.getProperty(WizardProperties.WSDL_FILE_PATH);
371         File JavaDoc normalizedWsdlFilePath = FileUtil.normalizeFile(new File JavaDoc(wsdlFilePath));
372         //convert to URI first to take care of spaces
373
final URL JavaDoc 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 JavaDoc 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             //String portJavaName = port.getJavaName();
393
WsdlModeler wsdlModeler = (WsdlModeler) wiz.getProperty(WizardProperties.WSDL_MODELER);
394             // don't set the packageName for modeler (use the default one generated from target Namespace)
395
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 JavaDoc 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 JavaDoc ex) {
414                         handle.finish();
415                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION,
416                                                          ex);
417                     }
418                 }
419             });
420         }
421     }
422 }
423
Popular Tags