KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarWebServicesSupport


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.j2ee.ejbjarproject;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.io.IOException JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.modules.j2ee.metadata.ClassPathSupport;
30 import org.netbeans.modules.websvc.api.client.WebServicesClientConstants;
31 import org.w3c.dom.Document JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34 import org.w3c.dom.NodeList JavaDoc;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.modules.websvc.spi.webservices.WebServicesSupportImpl;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.PropertyUtils;
39 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
40 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
41 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
42 import org.netbeans.modules.j2ee.dd.api.ejb.Session;
43 import org.netbeans.api.project.Project;
44 import org.netbeans.api.project.ProjectManager;
45 import org.netbeans.api.project.ProjectUtils;
46 import org.netbeans.api.project.SourceGroup;
47 import org.netbeans.api.project.Sources;
48 import org.openide.ErrorManager;
49 import org.openide.DialogDisplayer;
50 import org.openide.NotifyDescriptor;
51 import org.openide.filesystems.FileObject;
52 import org.openide.filesystems.FileUtil;
53 import org.openide.util.NbBundle;
54 import org.netbeans.modules.j2ee.dd.api.webservices.ServiceImplBean;
55 import org.netbeans.api.java.project.JavaProjectConstants;
56 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
57 import org.netbeans.modules.websvc.api.webservices.WsCompileEditorSupport;
58 import org.netbeans.modules.websvc.api.webservices.StubDescriptor;
59 import org.netbeans.spi.project.support.ant.ReferenceHelper;
60 import static org.netbeans.modules.websvc.spi.webservices.WebServicesConstants.*;
61 import org.netbeans.modules.j2ee.ejbjarproject.classpath.ClassPathProviderImpl;
62
63 /**
64  *
65  * @author rico
66  * Implementation of WebServicesSupportImpl
67  */

68 public class EjbJarWebServicesSupport implements WebServicesSupportImpl{
69     
70     private EjbJarProject project;
71     private AntProjectHelper helper;
72     private ReferenceHelper referenceHelper;
73     private ClassPath projectSourcesClassPath;
74     
75     /** Creates a new instance of EjbJarWebServicesSupport */
76     public EjbJarWebServicesSupport(EjbJarProject project, AntProjectHelper helper, ReferenceHelper referenceHelper) {
77         this.project = project;
78         this.helper = helper;
79         this.referenceHelper = referenceHelper;
80     }
81     
82     //implementation of WebServicesSupportImpl
83
public String JavaDoc generateImplementationBean(String JavaDoc wsName, FileObject pkg, Project project, String JavaDoc delegateData)throws java.io.IOException JavaDoc {
84         //TODO: RETOUCHE waiting for ejbcore
85
// SessionGenerator sessionGenerator = new SessionGenerator();
86
// return sessionGenerator.generateWebServiceImplBean(wsName, pkg, project, delegateData);
87
return null;
88     }
89     
90     public void addServiceImpl(String JavaDoc serviceName, FileObject configFile, boolean fromWSDL) {
91         this.addServiceImpl(serviceName, configFile, fromWSDL,null);
92     }
93     
94     public void addServiceImpl(String JavaDoc serviceName, FileObject configFile, boolean fromWSDL, String JavaDoc[] wscompileFeatures) {
95         
96         //Add properties to project.properties file
97
EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
98         String JavaDoc packageName = getPackageName(configFile);
99         ep.put(serviceName + CONFIG_PROP_SUFFIX, packageName +
100         (packageName.equals("") ? "" : "/") + configFile.getNameExt()); //NOI18N
101
ep.put(serviceName + MAPPING_PROP_SUFFIX, serviceName + MAPPING_FILE_SUFFIX); //NOI18N
102
// Add property for wscompile
103
String JavaDoc featurePropertyName = "wscompile.service." + serviceName + ".features"; // NOI18N
104
JAXRPCStubDescriptor stubDesc = null;
105         if (fromWSDL) {
106             if (wscompileFeatures!=null)
107                 stubDesc = new JAXRPCStubDescriptor(StubDescriptor.WSDL_SERVICE_STUB,
108                                 NbBundle.getMessage(EjbJarWebServicesSupport.class,"LBL_WSDLServiceStub"),
109                                 wscompileFeatures);
110             else stubDesc = wsdlServiceStub;
111         } else {
112             stubDesc = seiServiceStub;
113         }
114         String JavaDoc defaultFeatures = stubDesc.getDefaultFeaturesAsArgument();
115             ep.put(featurePropertyName, defaultFeatures);
116             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
117             
118             //Add web-services information in project.xml
119
Element JavaDoc data = helper.getPrimaryConfigurationData(true);
120             Document JavaDoc doc = data.getOwnerDocument();
121             NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICES); //NOI18N
122
Element JavaDoc webservices = null;
123             if(nodes.getLength() == 0){
124                 webservices = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICES); //NOI18N
125
NodeList JavaDoc insertBefore = data.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesClientConstants.WEB_SERVICE_CLIENTS);
126                 if (insertBefore.getLength() <= 0) {
127                     insertBefore = data.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "source-roots"); // NOI18N
128
assert insertBefore.getLength() == 1 : "Invalid project.xml file."; // NOI18N
129
}
130                 data.insertBefore(webservices, insertBefore.item(0));
131             }
132             else{
133                 webservices = (Element JavaDoc)nodes.item(0);
134             }
135             Element JavaDoc webservice = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE); //NOI18N
136
webservices.appendChild(webservice);
137             Element JavaDoc webserviceName = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_NAME); //NOI18N
138
webservice.appendChild(webserviceName);
139             webserviceName.appendChild(doc.createTextNode(serviceName));
140             if(fromWSDL) {
141                 Element JavaDoc fromWSDLElem = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "from-wsdl");
142                 webservice.appendChild(fromWSDLElem);
143             }
144             helper.putPrimaryConfigurationData(data, true);
145             
146             // Update wscompile related properties. boolean return indicates whether
147
// any changes were made.
148
updateWsCompileProperties(serviceName);
149             
150             try {
151                 ProjectManager.getDefault().saveProject(project);
152             }catch(java.io.IOException JavaDoc ioe){
153                 throw new RuntimeException JavaDoc(ioe.getMessage());
154             }
155     }
156     
157     public void addServiceEntriesToDD(String JavaDoc serviceName, String JavaDoc serviceEndpointInterface, String JavaDoc servantClassName) {
158         //add service endpoint entry to ejb-jar.xml
159
DDProvider provider = DDProvider.getDefault();
160         org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJarModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0];
161         org.netbeans.modules.j2ee.dd.api.ejb.EjbJar ejbJar = null;
162         try {
163             ejbJar = provider.getMergedDDRoot(ejbJarModule.getMetadataUnit());
164         }
165         catch(java.io.IOException JavaDoc e) {
166             //FIX-ME: handle this
167
throw new RuntimeException JavaDoc(e.getMessage());
168         }
169         
170         if (ejbJar == null) {
171             ErrorManager.getDefault().log(ErrorManager.USER,
172                     NbBundle.getMessage(EjbJarWebServicesSupport.class, "MSG_MissingMetadata"));
173             return;
174         }
175         
176         EjbJarProvider pwm = (EjbJarProvider) project.getLookup().lookup(EjbJarProvider.class);
177         pwm.getConfigSupport().ensureConfigurationReady();
178         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
179         Session s = null;
180         if(beans == null) {
181             beans = ejbJar.newEnterpriseBeans();
182             ejbJar.setEnterpriseBeans(beans);
183         }
184         s = beans.newSession();
185         s.setEjbName(serviceName);
186         s.setDisplayName(serviceName + "SB"); // NOI18N
187
s.setEjbClass(servantClassName);
188         try {
189             s.setServiceEndpoint(serviceEndpointInterface);
190         }
191         catch(org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException e) {
192             //FIX-ME: handle this
193
throw new RuntimeException JavaDoc(e.getMessage());
194         }
195         s.setSessionType("Stateless"); // NOI18N
196
s.setTransactionType("Container"); // NOI18N
197
beans.addSession(s);
198         try {
199             // This also saves server specific configuration, if necessary.
200
ejbJar.write(ejbJarModule.getDeploymentDescriptor());
201         }
202         catch(java.io.IOException JavaDoc e) {
203             //FIX-ME: handle this
204
throw new RuntimeException JavaDoc(e.getMessage());
205         }
206     }
207     
208     public void addServiceImplLinkEntry(ServiceImplBean serviceImplBean, String JavaDoc wsName) {
209         serviceImplBean.setEjbLink(wsName);
210     }
211     
212     /**
213      * Get the webservices.xml file object
214      */

215     public FileObject getWebservicesDD() {
216         FileObject metaInfFo = getMetaInf();
217         if (metaInfFo==null) {
218             return null;
219         }
220         return getMetaInf().getFileObject(WEBSERVICES_DD, "xml");
221     }
222     
223     /**
224      * Returns the directory that contains webservices.xml in the project
225      */

226     public FileObject getWsDDFolder() {
227         return getMetaInf();
228     }
229     
230     /**
231      * Returns the name of the directory that contains the webservices.xml in
232      * the archive
233      */

234     public String JavaDoc getArchiveDDFolderName() {
235         return "META-INF"; // NOI18N
236
}
237     
238     /**
239      * Returns the name of the implementation bean class
240      * given the ejb-link name
241      */

242     public String JavaDoc getImplementationBean(String JavaDoc linkName) {
243         EjbJar ejbJar = getEjbJar();
244         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
245         Session[] sessionBeans = beans.getSession();
246         for(int i = 0; i < sessionBeans.length; i++) {
247             Session sessionBean = sessionBeans[i];
248             if(sessionBean.getEjbName().equals(linkName)) {
249                 return sessionBean.getEjbClass();
250             }
251             
252         }
253         return null;
254     }
255     
256     public boolean isFromWSDL(String JavaDoc serviceName) {
257         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
258         NodeList JavaDoc nodes = data.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
259         WEB_SERVICES); //NOI18N
260
Element JavaDoc webservices = null;
261         Element JavaDoc wsNameNode = null;
262         if(nodes.getLength() == 1){
263             webservices = (Element JavaDoc)nodes.item(0);
264             NodeList JavaDoc wsNodes = webservices.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
265             WEB_SERVICE); //NOI18N
266
for(int j = 0; j < wsNodes.getLength(); j++) {
267                 Element JavaDoc wsNode = (Element JavaDoc)wsNodes.item(j);
268                 NodeList JavaDoc wsNameNodes = wsNode.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
269                 WEB_SERVICE_NAME); //NOI18N
270
if(wsNameNodes.getLength() == 1) {
271                     wsNameNode = (Element JavaDoc)wsNameNodes.item(0);
272                     NodeList JavaDoc nl = wsNameNode.getChildNodes();
273                     if(nl.getLength() == 1) {
274                         Node JavaDoc n = nl.item(0);
275                         if(n.getNodeType() == Node.TEXT_NODE) {
276                             if(serviceName.equals(n.getNodeValue())) {
277                                 NodeList JavaDoc fromWSDLNodes = wsNode.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
278                                 WebServicesConstants.WEB_SERVICE_FROM_WSDL); //NOI18N
279
if(fromWSDLNodes.getLength() == 1) {
280                                     return true;
281                                 }
282                             }
283                         }
284                     }
285                 }
286             }
287         }
288         return false;
289     }
290     
291     public void removeProjectEntries(String JavaDoc serviceName) {
292         boolean needsSave = false;
293         
294         //Remove entries in the project.properties file
295
//FIX-ME:we should move this to websvc
296
EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
297         String JavaDoc configProperty = serviceName + CONFIG_PROP_SUFFIX;
298         String JavaDoc mappingProperty = serviceName + MAPPING_PROP_SUFFIX;
299         if(ep.getProperty(configProperty) != null) {
300             ep.remove(configProperty);
301             needsSave = true;
302         }
303         if(ep.getProperty(mappingProperty) != null) {
304             ep.remove(mappingProperty);
305             needsSave = true;
306         }
307         String JavaDoc featureProperty = "wscompile.service." + serviceName + ".features"; // NOI18N
308
if(ep.getProperty(featureProperty) != null) {
309             ep.remove(featureProperty);
310             needsSave = true;
311         }
312         if(needsSave){
313             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
314         }
315         //Remove entry in the project.xml file (we should move this to websvc)
316
Element JavaDoc data = helper.getPrimaryConfigurationData(true);
317         NodeList JavaDoc nodes = data.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
318         WEB_SERVICES); //NOI18N
319
Element JavaDoc webservices = null;
320         Element JavaDoc wsNameNode = null;
321         if(nodes.getLength() == 1){
322             webservices = (Element JavaDoc)nodes.item(0);
323             NodeList JavaDoc wsNodes = webservices.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
324             WEB_SERVICE); //NOI18N
325
for(int j = 0; j < wsNodes.getLength(); j++) {
326                 Element JavaDoc wsNode = (Element JavaDoc)wsNodes.item(j);
327                 NodeList JavaDoc wsNameNodes = wsNode.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
328                 WEB_SERVICE_NAME); //NOI18N
329
if(wsNameNodes.getLength() == 1) {
330                     wsNameNode = (Element JavaDoc)wsNameNodes.item(0);
331                     NodeList JavaDoc nl = wsNameNode.getChildNodes();
332                     if(nl.getLength() == 1) {
333                         Node JavaDoc n = nl.item(0);
334                         if(n.getNodeType() == Node.TEXT_NODE) {
335                             if(serviceName.equals(n.getNodeValue())) {
336                                 webservices.removeChild(wsNode);
337                                 //if there are no more children, remove the web-services node
338
NodeList JavaDoc children = webservices.getChildNodes();
339                                 if(children.getLength() == 0) {
340                                     data.removeChild(webservices);
341                                 }
342                                 needsSave = true;
343                                 break;
344                             }
345                         }
346                     }
347                 }
348             }
349         }
350         if(needsSave) {
351             helper.putPrimaryConfigurationData(data, true);
352             try {
353                 ProjectManager.getDefault().saveProject(project);
354             } catch(java.io.IOException JavaDoc ex) {
355                 String JavaDoc mes = NbBundle.getMessage(this.getClass(), "MSG_ErrorSavingOnWSRemove") + serviceName // NOI18N
356
+ "'\r\n" + ex.getMessage(); // NOI18N
357
NotifyDescriptor desc = new NotifyDescriptor.
358
                Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
359                 DialogDisplayer.getDefault().notify(desc); }
360         }
361     }
362     
363     public void removeServiceEntry(String JavaDoc linkName) {
364         //remove ejb entry in ejb-jar.xml
365
org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejbJarModule = org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0];
366         EjbJar ejbJar = getEjbJar();
367         EnterpriseBeans beans = ejbJar.getEnterpriseBeans();
368         Session[] sessionBeans = beans.getSession();
369         for(int i = 0; i < sessionBeans.length; i++) {
370             Session sessionBean = sessionBeans[i];
371             if(sessionBean.getEjbName().equals(linkName)) {
372                 EjbJarProvider pwm = (EjbJarProvider) project.getLookup().lookup(EjbJarProvider.class);
373                 pwm.getConfigSupport().ensureConfigurationReady();
374                 beans.removeSession(sessionBean);
375                 break;
376             }
377         }
378         try {
379             ejbJar.write(ejbJarModule.getDeploymentDescriptor());
380         }
381         catch(java.io.IOException JavaDoc e) {
382             NotifyDescriptor ndd =
383             new NotifyDescriptor.Message(NbBundle.getMessage(this.getClass(), "MSG_Unable_WRITE_EJB_DD"), // NOI18N
384
NotifyDescriptor.ERROR_MESSAGE);
385             DialogDisplayer.getDefault().notify(ndd);
386         }
387     }
388     
389     public AntProjectHelper getAntProjectHelper() {
390         return helper;
391     }
392     
393     public ReferenceHelper getReferenceHelper(){
394         return referenceHelper;
395     }
396     
397     /** !PW This method is exposed in the service support API. Though it's
398      * implementation makes more sense here than anywhere else, perhaps this
399      * and the other project.xml/project.properties related methods in this
400      * object should be refactored into another object that this one delegates
401      * to. That way, this method would be directly available within the web or
402      * ejb module, as it is needed, and remain missing from the API (where it
403      * probably does not belong at this time.
404      */

405     private static final String JavaDoc [] WSCOMPILE_SEI_SERVICE_FEATURES = {
406         // "datahandleronly", // WSDL - portable
407
"documentliteral", // SEI ONLY - portable
408
"rpcliteral", // SEI ONLY - portable
409
// "explicitcontext", // WSDL - portable
410
// "infix:<name>", // difficult handle with current API
411
// "jaxbenumtype", // WSDL
412
// "nodatabinding", // WSDL - portable
413
"noencodedtypes",
414         "nomultirefs",
415         // "norpcstructures", // import only - portable
416
// "novalidation", // WSDL - portable
417
// "resolveidref", // WSDL
418
// "searchschema", // WSDL - portable
419
"serializeinterfaces",
420         "strict", // - portable
421
"useonewayoperations", // SEI ONLY - portable
422
// "wsi", // WSDL - portable
423
// "unwrap", // WSDL - portable
424
"donotoverride", // - portable
425
// "donotunwrap", // WSDL - portable
426
};
427     
428     private static final List JavaDoc allSeiServiceFeatures = Arrays.asList(WSCOMPILE_SEI_SERVICE_FEATURES);
429     
430     private static final String JavaDoc [] WSCOMPILE_KEY_SEI_SERVICE_FEATURES = {
431         "documentliteral",
432         "rpcliteral",
433         "strict",
434         "useonewayoperations",
435         "donotoverride"
436     };
437     
438     private static final List JavaDoc importantSeiServiceFeatures = Arrays.asList(WSCOMPILE_KEY_SEI_SERVICE_FEATURES);
439     
440     private static final String JavaDoc [] WSCOMPILE_WSDL_SERVICE_FEATURES = {
441         "datahandleronly", // WSDL - portable
442
// "documentliteral", // SEI ONLY - portable
443
// "rpcliteral", // SEI ONLY - portable
444
"explicitcontext", // WSDL - portable
445
// "infix:<name>", // difficult handle with current API
446
"jaxbenumtype", // WSDL
447
"nodatabinding", // WSDL - portable
448
"noencodedtypes",
449         "nomultirefs",
450         "norpcstructures", // import only - portable
451
"novalidation", // WSDL - portable
452
"resolveidref", // WSDL
453
"searchschema", // WSDL - portable
454
"serializeinterfaces",
455         "strict", // - portable
456
// "useonewayoperations", // SEI ONLY - portable
457
"wsi", // WSDL - portable
458
"unwrap", // WSDL - portable
459
"donotoverride", // - portable
460
"donotunwrap", // WSDL - portable
461
};
462     
463     private static final List JavaDoc allWsdlServiceFeatures = Arrays.asList(WSCOMPILE_WSDL_SERVICE_FEATURES);
464     
465     private static final String JavaDoc [] WSCOMPILE_KEY_WSDL_SERVICE_FEATURES = {
466         "datahandleronly",
467         "explicitcontext",
468         "nodatabinding",
469         "novalidation",
470         "searchschema",
471         "strict",
472         "wsi",
473         "unwrap",
474         "donotoverride",
475         "donotunwrap"
476     };
477     
478     private static final List JavaDoc importantWsdlServiceFeatures = Arrays.asList(WSCOMPILE_KEY_WSDL_SERVICE_FEATURES);
479     
480     public List JavaDoc/*WsCompileEditorSupport.ServiceSettings*/ getServices() {
481         List JavaDoc serviceList = new ArrayList JavaDoc();
482         
483         // Implementation from getServiceClients() -- FIXME
484
Element JavaDoc data = helper.getPrimaryConfigurationData(true);
485         NodeList JavaDoc nodes = data.getElementsByTagName(WebServicesConstants.WEB_SERVICES);
486         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
487         
488         if(nodes.getLength() != 0) {
489             Element JavaDoc serviceElements = (Element JavaDoc) nodes.item(0);
490             NodeList JavaDoc serviceNameList = serviceElements.getElementsByTagNameNS(
491             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesConstants.WEB_SERVICE_NAME);
492             for(int i = 0; i < serviceNameList.getLength(); i++ ) {
493                 Element JavaDoc serviceNameElement = (Element JavaDoc) serviceNameList.item(i);
494                 NodeList JavaDoc nl = serviceNameElement.getChildNodes();
495                 if(nl.getLength() == 1) {
496                     org.w3c.dom.Node JavaDoc n = nl.item(0);
497                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
498                         String JavaDoc serviceName = n.getNodeValue();
499                         String JavaDoc currentFeatures = projectProperties.getProperty("wscompile.service." + serviceName + ".features"); // NOI18N
500
StubDescriptor stubType = getServiceStubDescriptor(serviceNameElement.getParentNode());
501                         WsCompileEditorSupport.ServiceSettings settings;
502                         
503                         // !PW The logic for managing wscompile options needs refactoring badly.
504
if(seiServiceStub == stubType) {
505                             if(currentFeatures == null) {
506                                 // default for SEI generation
507
currentFeatures = seiServiceStub.getDefaultFeaturesAsArgument();
508                             }
509                             settings = new WsCompileEditorSupport.ServiceSettings(
510                             serviceName, stubType, currentFeatures, allSeiServiceFeatures, importantSeiServiceFeatures);
511                         } else {
512                             if(currentFeatures == null) {
513                                 // default for WSDL generation
514
currentFeatures = wsdlServiceStub.getDefaultFeaturesAsArgument();
515                             }
516                             settings = new WsCompileEditorSupport.ServiceSettings(
517                             serviceName, stubType, currentFeatures, allWsdlServiceFeatures, importantWsdlServiceFeatures);
518                         }
519                         serviceList.add(settings);
520                     } else {
521                         // !PW FIXME node is wrong type?! - log message or trace?
522
}
523                 } else {
524                     // !PW FIXME no name for this service entry - notify user
525
}
526             }
527         }
528         
529         return serviceList;
530     }
531     
532     private StubDescriptor getServiceStubDescriptor(org.w3c.dom.Node JavaDoc parentNode) {
533         StubDescriptor result = null;
534         
535         if(parentNode instanceof Element JavaDoc) {
536             Element JavaDoc parentElement = (Element JavaDoc) parentNode;
537             NodeList JavaDoc fromWsdlList = parentElement.getElementsByTagNameNS(
538             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesConstants.WEB_SERVICE_FROM_WSDL);
539             if(fromWsdlList.getLength() == 1) {
540                 result = wsdlServiceStub;
541             } else {
542                 result = seiServiceStub;
543             }
544         }
545         
546         return result;
547     }
548     
549     private boolean updateWsCompileProperties(String JavaDoc serviceName) {
550         /** Ensure wscompile.classpath and wscompile.tools.classpath are
551          * properly defined.
552          *
553          * wscompile.classpath goes in project properties and includes
554          * jaxrpc and qname right now.
555          *
556          * wscompile.tools.classpath is for tools.jar which is needed when
557          * running under the Sun JDK to invoke javac. It is placed in
558          * user.properties so that if we compute it incorrectly (say on a mac)
559          * the user can change it and we will not blow away the change.
560          * Hopefully we can do this better for release.
561          */

562         boolean globalPropertiesChanged = false;
563         
564         EditableProperties globalProperties = PropertyUtils.getGlobalProperties();
565         if(globalProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
566             globalProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar");
567             
568             try {
569                 PropertyUtils.putGlobalProperties(globalProperties);
570             } catch(java.io.IOException JavaDoc ex) {
571                 String JavaDoc mes = "Error saving global properties when adding wscompile.tools.classpath for service '" + serviceName + "'\r\n" + ex.getMessage();
572                 NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
573                 DialogDisplayer.getDefault().notify(desc);
574             }
575             
576             globalPropertiesChanged = true;
577         }
578         
579         boolean projectPropertiesChanged = false;
580         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
581         
582         { // Block that adjusts wscompile.client.classpath as necessary.
583
HashSet JavaDoc wscJars = new HashSet JavaDoc();
584             boolean newWscJars = false;
585             String JavaDoc wscClientClasspath = projectProperties.getProperty(WSCOMPILE_CLASSPATH);
586             if(wscClientClasspath != null) {
587                 String JavaDoc [] libs = PropertyUtils.tokenizePath(wscClientClasspath);
588                 for(int i = 0; i < libs.length; i++) {
589                     wscJars.add(libs[i]);
590                 }
591             }
592             
593             for(int i = 0; i < WSCOMPILE_JARS.length; i++) {
594                 if(!wscJars.contains(WSCOMPILE_JARS[i])) {
595                     wscJars.add(WSCOMPILE_JARS[i]);
596                     newWscJars = true;
597                 }
598             }
599             
600             if(newWscJars) {
601                 StringBuffer JavaDoc newClasspathBuf = new StringBuffer JavaDoc(256);
602                 for(Iterator JavaDoc iter = wscJars.iterator(); iter.hasNext(); ) {
603                     newClasspathBuf.append(iter.next().toString());
604                     if(iter.hasNext()) {
605                         newClasspathBuf.append(":");
606                     }
607                 }
608                 projectProperties.put(WSCOMPILE_CLASSPATH, newClasspathBuf.toString());
609                 projectPropertiesChanged = true;
610             }
611         }
612         
613         // set tools.jar property if not set
614
if(projectProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
615             projectProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar"); // NOI18N
616
projectPropertiesChanged = true;
617         }
618         
619         if(projectPropertiesChanged) {
620             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties);
621         }
622         return globalPropertiesChanged || projectPropertiesChanged;
623     }
624     
625     public void addInfrastructure(String JavaDoc implBeanClass, FileObject pkg){
626         //TODO: RETOUCHE webservices
627
// boolean rollbackFlag = true; // rollback the transaction by default
628
// JavaModel.getJavaRepository().beginTrans(true);
629
// try {
630
// JavaModel.setClassPath(pkg);
631
// JavaMetamodel.getManager().waitScanFinished();
632
// JavaClass clazz = Utils.findClass(implBeanClass);
633
//
634
// if (clazz == null) {
635
// ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
636
// "EjbJarWSSupport.addInfrastructure: Class not found: " + implBeanClass + " for package: " + pkg); //NOI18N
637
// return;
638
// }
639
//
640
// //remove java.rmi.Remote interface
641
// List interfaces = clazz.getInterfaceNames();
642
// for (Iterator it = interfaces.iterator(); it.hasNext();) {
643
// MultipartId interfaceId = (MultipartId) it.next();
644
// if (interfaceId.getElement().getName().equals("java.rmi.Remote")) {
645
// interfaces.remove(interfaceId);
646
// break;
647
// }
648
// }
649
//
650
// MultipartId id = JavaModelUtil.resolveImportsForClass(clazz, Utils.findClass("javax.ejb.SessionBean"));
651
// if (id!=null) interfaces.add(id);
652
//
653
// JavaModelPackage jmp = (JavaModelPackage) clazz.refImmediatePackage();
654
//
655
// //add javax.ejb.SessionContext field
656
// JavaClass sessionCtx = Utils.findClass("javax.ejb.SessionContext");
657
// Field field = jmp.getField().createField();
658
// field.setType(sessionCtx);
659
// field.setName("context");
660
// clazz.getContents().add(0,field);
661
//
662
// //add setSessionContext(javax.ejb.SessionContext aContext) method
663
// Method sessionCtxMethod = jmp.getMethod().createMethod();
664
// sessionCtxMethod.setName("setSessionContext");
665
// Parameter ctxParam = jmp.getParameter().createParameter(
666
// "aContext",
667
// Collections.EMPTY_LIST,
668
// false,
669
// jmp.getMultipartId().createMultipartId(sessionCtx.getName(), null, null), // type name
670
// 0,
671
// false);
672
// sessionCtxMethod.getParameters().add(ctxParam);
673
// sessionCtxMethod.setType(Utils.resolveType("void"));
674
// sessionCtxMethod.setModifiers(Modifier.PUBLIC);
675
// sessionCtxMethod.setBodyText("context = aContext;");
676
// sessionCtxMethod.setJavadocText("@see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)");
677
// clazz.getContents().add(sessionCtxMethod);
678
//
679
// //add ejbActivate method
680
// Method ejbActivateMethod = jmp.getMethod().createMethod();
681
// ejbActivateMethod.setName("ejbActivate");
682
// ejbActivateMethod.setType(Utils.resolveType("void"));
683
// ejbActivateMethod.setModifiers(Modifier.PUBLIC);
684
// ejbActivateMethod.setJavadocText("@see javax.ejb.SessionBean#ejbActivate()");
685
// clazz.getContents().add(ejbActivateMethod);
686
//
687
// //add ejbPassivate method
688
// Method ejbPassivateMethod = jmp.getMethod().createMethod();
689
// ejbPassivateMethod.setName("ejbPassivate");
690
// ejbPassivateMethod.setType(Utils.resolveType("void"));
691
// ejbPassivateMethod.setModifiers(Modifier.PUBLIC);
692
// ejbPassivateMethod.setJavadocText("@see javax.ejb.SessionBean#ejbPassivate()");
693
// clazz.getContents().add(ejbPassivateMethod);
694
//
695
// //add ejbRemove method
696
// Method ejbRemoveMethod = jmp.getMethod().createMethod();
697
// ejbRemoveMethod.setName("ejbRemove");
698
// ejbRemoveMethod.setType(Utils.resolveType("void"));
699
// ejbRemoveMethod.setModifiers(Modifier.PUBLIC);
700
// ejbRemoveMethod.setJavadocText("@see javax.ejb.SessionBean#ejbRemove()");
701
// clazz.getContents().add(ejbRemoveMethod);
702
//
703
// //add ejbCreate method
704
// Method ejbCreateMethod = jmp.getMethod().createMethod();
705
// ejbCreateMethod.setName("ejbCreate");
706
// ejbCreateMethod.setType(Utils.resolveType("void"));
707
// ejbCreateMethod.setModifiers(Modifier.PUBLIC);
708
// ejbCreateMethod.setJavadocText("See section 7.10.3 of the EJB 2.0 specification\nSee section 7.11.3 of the EJB 2.1 specification");
709
// clazz.getContents().add(ejbCreateMethod);
710
//
711
// rollbackFlag=false;
712
// } finally {
713
// JavaModel.getJavaRepository().endTrans(rollbackFlag);
714
// }
715
}
716     
717     private EjbJar getEjbJar() {
718         try {
719             // TODO: first one API EjbJar from project is taken... this should be fixed
720
return DDProvider.getDefault().getMergedDDRoot(org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0].getMetadataUnit());
721         } catch (java.io.IOException JavaDoc e) {
722             org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
723         }
724         return null;
725     }
726     
727     public FileObject getMetaInf() {
728         EjbJarProvider provider = (EjbJarProvider)project.getLookup().lookup(EjbJarProvider.class);
729         return provider.getMetaInf();
730     }
731     
732     public FileObject getDeploymentDescriptor() {
733         FileObject metaInfFo = getMetaInf();
734         if (metaInfFo==null) {
735             return null;
736         }
737         return metaInfFo.getFileObject(EjbJarProvider.FILE_DD);
738     }
739     
740     private String JavaDoc getPackageName(FileObject file){
741         FileObject parent = file.getParent();
742         Sources sources = ProjectUtils.getSources(project);
743         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
744         String JavaDoc packageName = null;
745         for (int i = 0; i < groups.length && packageName == null; i++) {
746             packageName = FileUtil.getRelativePath(groups[i].getRootFolder(), parent);
747             if (packageName != null) {
748                 packageName = groups[i].getName() + "/" + packageName;
749             }
750         }
751         return packageName + "";
752     }
753     
754     private FileObject getFileObject(String JavaDoc propname) {
755         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
756         if (prop != null) {
757             return helper.resolveFileObject(prop);
758         }
759         
760         return null;
761     }
762  
763     public FileObject getWsdlFolder(boolean create) throws IOException JavaDoc {
764         FileObject wsdlFolder = null;
765         FileObject metaInf = getMetaInf();
766         
767         if(metaInf != null) {
768             wsdlFolder = metaInf.getFileObject(WSDL_FOLDER);
769             if(wsdlFolder == null && create) {
770                 wsdlFolder = metaInf.createFolder(WSDL_FOLDER);
771             }
772         } else if(create) {
773             // Create was specified, but no META-INF was found, so how do we create it?
774
// Expect an NPE if we return null for this case, but log it anyway.
775
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
776             NbBundle.getMessage(EjbJarWebServicesSupport.class, "MSG_MetaInfNotFoundForWsdlFolder"));
777         }
778         
779         return wsdlFolder;
780     }
781
782     public ClassPath getClassPath() {
783         synchronized (this) {
784             if (projectSourcesClassPath == null) {
785                 ClassPathProviderImpl cpProvider = (ClassPathProviderImpl)project.getLookup().lookup(ClassPathProviderImpl.class);
786                 projectSourcesClassPath = ClassPathSupport.createWeakProxyClassPath(new ClassPath[] {
787                     cpProvider.getProjectSourcesClassPath(ClassPath.SOURCE),
788                     cpProvider.getJ2eePlatformClassPath(),
789                 });
790             }
791             return projectSourcesClassPath;
792         }
793     }
794     
795     // Service stub descriptors
796
private static final JAXRPCStubDescriptor seiServiceStub = new JAXRPCStubDescriptor(
797     StubDescriptor.SEI_SERVICE_STUB,
798     NbBundle.getMessage(EjbJarWebServicesSupport.class,"LBL_SEIServiceStub"), // NOI18N
799
new String JavaDoc [] {"documentliteral", "strict", "useonewayoperations"});
800     
801     private static final JAXRPCStubDescriptor wsdlServiceStub = new JAXRPCStubDescriptor(
802     StubDescriptor.WSDL_SERVICE_STUB,
803     NbBundle.getMessage(EjbJarWebServicesSupport.class,"LBL_WSDLServiceStub"), // NOI18N
804
new String JavaDoc [] { "wsi", "strict" }); // NOI18N
805

806     /** Stub descriptor for services supported by this project type.
807      */

808     private static class JAXRPCStubDescriptor extends StubDescriptor {
809         
810         private String JavaDoc [] defaultFeatures;
811         
812         public JAXRPCStubDescriptor(String JavaDoc name, String JavaDoc displayName, String JavaDoc [] defaultFeatures) {
813             super(name, displayName);
814             
815             this.defaultFeatures = defaultFeatures;
816         }
817         
818         public String JavaDoc [] getDefaultFeatures() {
819             return defaultFeatures;
820         }
821         
822         public String JavaDoc getDefaultFeaturesAsArgument() {
823             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(defaultFeatures.length*32);
824             for(int i = 0; i < defaultFeatures.length; i++) {
825                 if(i > 0) {
826                     buf.append(",");
827                 }
828                 
829                 buf.append(defaultFeatures[i]);
830             }
831             return buf.toString();
832         }
833     }
834 }
835
Popular Tags