KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URI JavaDoc;
23 import java.net.URISyntaxException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.io.IOException JavaDoc;
30 import org.netbeans.modules.j2ee.dd.api.common.NameAlreadyUsedException;
31 import org.netbeans.modules.j2ee.dd.api.common.PortComponentRef;
32 import org.netbeans.modules.j2ee.dd.api.common.RootInterface;
33 import org.netbeans.modules.j2ee.dd.api.common.ServiceRef;
34 import org.netbeans.modules.websvc.api.client.ClientStubDescriptor;
35 import static org.netbeans.modules.websvc.api.client.WebServicesClientConstants.*;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39 import org.netbeans.spi.project.support.ant.AntProjectHelper;
40 import org.netbeans.modules.websvc.spi.client.WebServicesClientSupportImpl;
41 import org.netbeans.spi.project.support.ant.EditableProperties;
42 import org.netbeans.spi.project.support.ant.PropertyUtils;
43 import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
44 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
45 import org.netbeans.api.project.ProjectManager;
46 import org.netbeans.api.project.ProjectUtils;
47 import org.netbeans.api.project.SourceGroup;
48 import org.netbeans.api.project.Sources;
49 import org.openide.ErrorManager;
50 import org.openide.DialogDisplayer;
51 import org.openide.NotifyDescriptor;
52 import org.openide.filesystems.FileObject;
53 import org.openide.filesystems.FileUtil;
54 import org.openide.util.NbBundle;
55 import org.netbeans.api.java.project.JavaProjectConstants;
56 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
57 import org.netbeans.modules.websvc.api.client.WsCompileClientEditorSupport;
58 import org.netbeans.spi.project.support.ant.ReferenceHelper;
59
60 /**
61  *
62  * @author rico
63  * Implementation of WebServicesSupportImpl and WebServicesClientSupportImpl
64  */

65 public class EjbJarWebServicesClientSupport implements WebServicesClientSupportImpl{
66     
67     private EjbJarProject project;
68     private AntProjectHelper helper;
69     private ReferenceHelper referenceHelper;
70     
71     /** Creates a new instance of EjbJarWebServicesSupport */
72     public EjbJarWebServicesClientSupport(EjbJarProject project, AntProjectHelper helper, ReferenceHelper referenceHelper) {
73         this.project = project;
74         this.helper = helper;
75         this.referenceHelper = referenceHelper;
76     }
77     
78 // /**
79
// * Get the webservices.xml file object
80
// */
81
// public FileObject getWebservicesDD() {
82
// FileObject metaInfFo = getMetaInf();
83
// if (metaInfFo==null) {
84
// return null;
85
// }
86
// return getMetaInf().getFileObject(WEBSERVICES_DD, "xml");
87
// }
88

89 // /**
90
// * Returns the directory that contains webservices.xml in the project
91
// */
92
// public FileObject getWsDDFolder() {
93
// return getMetaInf();
94
// }
95

96 // /**
97
// * Returns the name of the directory that contains the webservices.xml in
98
// * the archive
99
// */
100
// public String getArchiveDDFolderName() {
101
// return "META-INF"; // NOI18N
102
// }
103

104  
105     public AntProjectHelper getAntProjectHelper() {
106         return helper;
107     }
108     
109     public ReferenceHelper getReferenceHelper(){
110         return referenceHelper;
111     }
112     
113     private boolean updateWsCompileProperties(String JavaDoc serviceName) {
114         /** Ensure wscompile.classpath and wscompile.tools.classpath are
115          * properly defined.
116          *
117          * wscompile.classpath goes in project properties and includes
118          * jaxrpc and qname right now.
119          *
120          * wscompile.tools.classpath is for tools.jar which is needed when
121          * running under the Sun JDK to invoke javac. It is placed in
122          * user.properties so that if we compute it incorrectly (say on a mac)
123          * the user can change it and we will not blow away the change.
124          * Hopefully we can do this better for release.
125          */

126         boolean globalPropertiesChanged = false;
127         
128         EditableProperties globalProperties = PropertyUtils.getGlobalProperties();
129         if(globalProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
130             globalProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar");
131             
132             try {
133                 PropertyUtils.putGlobalProperties(globalProperties);
134             } catch(java.io.IOException JavaDoc ex) {
135                 String JavaDoc mes = "Error saving global properties when adding wscompile.tools.classpath for service '" + serviceName + "'\r\n" + ex.getMessage();
136                 NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
137                 DialogDisplayer.getDefault().notify(desc);
138             }
139             
140             globalPropertiesChanged = true;
141         }
142         
143         boolean projectPropertiesChanged = false;
144         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
145         
146         { // Block that adjusts wscompile.client.classpath as necessary.
147
HashSet JavaDoc wscJars = new HashSet JavaDoc();
148             boolean newWscJars = false;
149             String JavaDoc wscClientClasspath = projectProperties.getProperty(WSCOMPILE_CLASSPATH);
150             if(wscClientClasspath != null) {
151                 String JavaDoc [] libs = PropertyUtils.tokenizePath(wscClientClasspath);
152                 for(int i = 0; i < libs.length; i++) {
153                     wscJars.add(libs[i]);
154                 }
155             }
156             
157             for(int i = 0; i < WSCOMPILE_JARS.length; i++) {
158                 if(!wscJars.contains(WSCOMPILE_JARS[i])) {
159                     wscJars.add(WSCOMPILE_JARS[i]);
160                     newWscJars = true;
161                 }
162             }
163             
164             if(newWscJars) {
165                 StringBuffer JavaDoc newClasspathBuf = new StringBuffer JavaDoc(256);
166                 for(Iterator JavaDoc iter = wscJars.iterator(); iter.hasNext(); ) {
167                     newClasspathBuf.append(iter.next().toString());
168                     if(iter.hasNext()) {
169                         newClasspathBuf.append(":");
170                     }
171                 }
172                 projectProperties.put(WSCOMPILE_CLASSPATH, newClasspathBuf.toString());
173                 projectPropertiesChanged = true;
174             }
175         }
176         
177         // set tools.jar property if not set
178
if(projectProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
179             projectProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar"); // NOI18N
180
projectPropertiesChanged = true;
181         }
182         
183         if(projectPropertiesChanged) {
184             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties);
185         }
186         return globalPropertiesChanged || projectPropertiesChanged;
187     }
188     
189
190     private EjbJar getEjbJar() {
191         try {
192             // TODO: first one API EjbJar from project is taken... this should be fixed
193
return DDProvider.getDefault().getMergedDDRoot(org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0].getMetadataUnit());
194         } catch (java.io.IOException JavaDoc e) {
195             org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
196         }
197         return null;
198     }
199     
200     public FileObject getMetaInf() {
201         EjbJarProvider provider = (EjbJarProvider)project.getLookup().lookup(EjbJarProvider.class);
202         return provider.getMetaInf();
203     }
204     
205     public FileObject getDeploymentDescriptor() {
206         FileObject metaInfFo = getMetaInf();
207         if (metaInfFo==null) {
208             return null;
209         }
210         return metaInfFo.getFileObject(EjbJarProvider.FILE_DD);
211     }
212     
213     private String JavaDoc getPackageName(FileObject file){
214         FileObject parent = file.getParent();
215         Sources sources = ProjectUtils.getSources(project);
216         SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
217         String JavaDoc packageName = null;
218         for (int i = 0; i < groups.length && packageName == null; i++) {
219             packageName = FileUtil.getRelativePath(groups[i].getRootFolder(), parent);
220             if (packageName != null) {
221                 packageName = groups[i].getName() + "/" + packageName;
222             }
223         }
224         return packageName + "";
225     }
226     
227     private FileObject getFileObject(String JavaDoc propname) {
228         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
229         if (prop != null) {
230             return helper.resolveFileObject(prop);
231         }
232         
233         return null;
234     }
235  
236     // Implementation of WebServiceClientSupportImpl
237
public void addServiceClient(String JavaDoc serviceName, String JavaDoc packageName, String JavaDoc sourceUrl, FileObject configFile, ClientStubDescriptor stubDescriptor) {
238         this.addServiceClient(serviceName, packageName, sourceUrl, configFile, stubDescriptor, null);
239     }
240
241     // Implementation of WebServiceClientSupportImpl
242
public void addServiceClient(String JavaDoc serviceName, String JavaDoc packageName, String JavaDoc sourceUrl, FileObject configFile, ClientStubDescriptor stubDescriptor, String JavaDoc[] wscompileFeatures) {
243         // !PW FIXME I have two concerns with this implementation:
244
// 1. Since it modifies project.xml, I suspect it should be acquiring
245
// ProjectManager.mutex() for write access.
246
// 2. It seems like it ought to be implemented via the AuxiliaryConfiguration
247
// interface.
248
boolean needsSave = false;
249         boolean modifiedProjectProperties = false;
250         
251         /** Locate root of web service client node structure in project,xml, creating it
252          * if it's not found.
253          */

254         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
255         Document JavaDoc doc = data.getOwnerDocument();
256         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
257         Element JavaDoc clientElements = null;
258         
259         if(nodes.getLength() == 0) {
260             // 'needsSave' deliberately left false here because this is a trival change
261
// that only should be saved if additional changes are also made below.
262
clientElements = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENTS);
263             NodeList JavaDoc srcRoots = data.getElementsByTagNameNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "source-roots"); // NOI18N
264
assert srcRoots.getLength() == 1 : "Invalid project.xml."; // NOI18N
265
data.insertBefore(clientElements, srcRoots.item(0));
266         } else {
267             clientElements = (Element JavaDoc) nodes.item(0);
268         }
269         
270         /** Make sure this service is not already registered in project.xml
271          */

272         boolean serviceAlreadyAdded = false;
273         NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
274         EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
275         for(int i = 0; i < clientNameList.getLength(); i++ ) {
276             Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
277             NodeList JavaDoc nl = clientNameElement.getChildNodes();
278             if(nl.getLength() >= 1) {
279                 org.w3c.dom.Node JavaDoc n = nl.item(0);
280                 if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
281                     if(serviceName.equalsIgnoreCase(n.getNodeValue())) {
282                         serviceAlreadyAdded = true;
283                         
284                         // !PW FIXME should force stub type to match value passed in
285
// in case someone is overwriting a current service with a different
286
// stub type.
287
}
288                 }
289             }
290         }
291         
292         /** Add entry for the client to project.xml and regenerate build-impl.xml.
293          */

294         if(!serviceAlreadyAdded) {
295             Element JavaDoc clientElement = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT);
296             clientElements.appendChild(clientElement);
297             Element JavaDoc clientElementName = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
298             clientElement.appendChild(clientElementName);
299             clientElementName.appendChild(doc.createTextNode(serviceName));
300             Element JavaDoc clientElementStubType = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_STUB_TYPE);
301             clientElement.appendChild(clientElementStubType);
302             clientElementStubType.appendChild(doc.createTextNode(stubDescriptor.getName()));
303             Element JavaDoc clientElementSourceUrl = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
304             clientElement.appendChild(clientElementSourceUrl);
305             clientElementSourceUrl.appendChild(doc.createTextNode(sourceUrl));
306             helper.putPrimaryConfigurationData(data, true);
307             needsSave = true;
308         }
309         
310         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
311         // Add property for wscompile features
312
{
313             String JavaDoc featurePropertyName = "wscompile.client." + serviceName + ".features"; // NOI18N
314
String JavaDoc defaultFeatures = "wsi, strict"; // NOI18N -- defaults if stub descriptor is bad type (should never happen?)
315
if (stubDescriptor instanceof JAXRPCClientStubDescriptor) {
316                 JAXRPCClientStubDescriptor stubDesc = (JAXRPCClientStubDescriptor) stubDescriptor;
317                 if (wscompileFeatures!=null) stubDesc.setDefaultFeatures(wscompileFeatures);
318                 defaultFeatures = stubDesc.getDefaultFeaturesAsArgument();
319             } else {
320                 // !PW FIXME wrong stub type -- log error message.
321
}
322             String JavaDoc oldFeatures = projectProperties.getProperty(featurePropertyName);
323             if(!defaultFeatures.equals(oldFeatures)) {
324                 projectProperties.put(featurePropertyName, defaultFeatures);
325                 modifiedProjectProperties = true;
326             }
327         }
328         
329         // Add package name property
330
{
331             String JavaDoc packagePropertyName = "wscompile.client." + serviceName + ".package"; // NOI18N
332
String JavaDoc oldPackageName = projectProperties.getProperty(packagePropertyName);
333             if(!packageName.equals(oldPackageName)) {
334                 projectProperties.put(packagePropertyName, packageName);
335                 modifiedProjectProperties = true;
336             }
337         }
338         
339         if(modifiedProjectProperties) {
340             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties);
341             needsSave = true;
342         }
343         
344         // Update wscompile related properties. boolean return indicates whether
345
// any changes were made.
346
if(updateWsCompileProperties(serviceName)) {
347             needsSave = true;
348         }
349         
350         // !PW Lastly, save the project if we actually made any changes to any
351
// properties or the build script.
352
if(needsSave) {
353             try {
354                 ProjectManager.getDefault().saveProject(project);
355             } catch(IOException JavaDoc ex) {
356                 NotifyDescriptor desc = new NotifyDescriptor.Message(
357                 NbBundle.getMessage(EjbJarWebServicesSupport.class,
358                 "MSG_ErrorSavingOnWSClientAdd", serviceName, ex.getMessage()), // NOI18N
359
NotifyDescriptor.ERROR_MESSAGE);
360                 DialogDisplayer.getDefault().notify(desc);
361             }
362         }
363     }
364     
365     public FileObject[] getJavaSources() {
366         return project.getSourceRoots().getRoots();
367     }
368
369     public void addServiceClientReference(String JavaDoc serviceName, String JavaDoc fqServiceName, String JavaDoc relativeWsdlPath, String JavaDoc relativeMappingPath, String JavaDoc[] portSEIInfo) {
370
371         FileObject ddFO = getDeploymentDescriptor();
372
373         // If we get null for the deployment descriptor, ignore this step.
374
if (ddFO != null) {
375
376             try {
377                 // TODO: first one API EjbJar from project is taken... this should be fixed
378
RootInterface rootDD = DDProvider.getDefault().getMergedDDRoot(org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJars(project)[0].getMetadataUnit());
379
380                 ServiceRef serviceRef = (ServiceRef) rootDD.findBeanByName("ServiceRef", "ServiceRefName", serviceName); // NOI18N
381
if (serviceRef == null) {
382                     serviceRef = (ServiceRef) rootDD.addBean("ServiceRef", // NOI18N
383
new String JavaDoc [] { /* property list */
384                             "ServiceRefName", // NOI18N
385
"ServiceInterface", // NOI18N
386
"WsdlFile", // NOI18N
387
"JaxrpcMappingFile" // NOI18N
388
},
389                         new String JavaDoc [] { /* property values */
390                             // service name
391
serviceName,
392                             // interface package . service name
393
fqServiceName,
394                             // web doc base / wsdl folder / wsdl file name
395
relativeWsdlPath,
396                             // web doc base / mapping file name
397
relativeMappingPath
398                         },
399                         "ServiceRefName"); // NOI18N
400
} else {
401                     serviceRef.setServiceInterface(fqServiceName);
402                     serviceRef.setWsdlFile(new URI JavaDoc(relativeWsdlPath));
403                     serviceRef.setJaxrpcMappingFile(relativeMappingPath);
404                 }
405
406                 PortComponentRef [] portRefArray = new PortComponentRef [portSEIInfo.length];
407                 for (int pi = 0; pi < portRefArray.length; pi++) {
408                     portRefArray[pi] = (PortComponentRef) serviceRef.createBean("PortComponentRef"); // NOI18N
409
portRefArray[pi].setServiceEndpointInterface(portSEIInfo[pi]); // NOI18N
410
}
411                 serviceRef.setPortComponentRef(portRefArray);
412                 rootDD.write(ddFO);
413
414             } catch (IOException JavaDoc ex) {
415                 // Strange thing happen
416
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
417             } catch (NameAlreadyUsedException ex) {
418                 // Should never happen because we look for it by name first.
419
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
420             } catch (URISyntaxException JavaDoc ex) {
421                 // Programmer error - validation of input data should ensure this never happens.
422
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
423             } catch (ClassNotFoundException JavaDoc ex) {
424                 // Programmer error - mistyped object name.
425
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
426             }
427             
428         }
429         
430     }
431     
432     public void removeServiceClient(String JavaDoc serviceName) {
433         // 2. Remove service from project.xml
434
// Side effect: Regenerate build-impl.xsl
435
// Optional - if last service, remove properties we generated.
436
boolean needsSave = false;
437         
438         /** Remove properties from project.properties
439          */

440         String JavaDoc featureProperty = "wscompile.client." + serviceName + ".features"; // NOI18N
441
String JavaDoc packageProperty = "wscompile.client." + serviceName + ".package"; // NOI18N
442
EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
443         
444         if(ep.getProperty(featureProperty) != null) {
445             ep.remove(featureProperty);
446             needsSave = true;
447         }
448         
449         if(ep.getProperty(packageProperty) != null) {
450             ep.remove(packageProperty);
451             needsSave = true;
452         }
453         
454         if(needsSave) {
455             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
456         }
457         
458         /** Locate root of web service client node structure in project,xml
459          */

460         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
461         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
462         Element JavaDoc clientElements = null;
463         
464         /* If there is a root, get all the names of the child services and search
465          * for the one we want to remove.
466          */

467         if(nodes.getLength() >= 1) {
468             clientElements = (Element JavaDoc) nodes.item(0);
469             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
470             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
471             for(int i = 0; i < clientNameList.getLength(); i++ ) {
472                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
473                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
474                 if(nl.getLength() == 1) {
475                     org.w3c.dom.Node JavaDoc n = nl.item(0);
476                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
477                         if(serviceName.equalsIgnoreCase(n.getNodeValue())) {
478                             // Found it! Now remove it.
479
org.w3c.dom.Node JavaDoc serviceNode = clientNameElement.getParentNode();
480                             clientElements.removeChild(serviceNode);
481                             helper.putPrimaryConfigurationData(data, true);
482                             needsSave = true;
483                         }
484                     }
485                 }
486             }
487         }
488         
489         // !PW Lastly, save the project if we actually made any changes to any
490
// properties or the build script.
491
if(needsSave) {
492             try {
493                 ProjectManager.getDefault().saveProject(project);
494             } catch(IOException JavaDoc ex) {
495                 NotifyDescriptor desc = new NotifyDescriptor.Message(
496                 NbBundle.getMessage(EjbJarWebServicesSupport.class,
497                 "MSG_ErrorSavingOnWSClientRemove", serviceName, ex.getMessage()), // NOI18N
498
NotifyDescriptor.ERROR_MESSAGE);
499                 DialogDisplayer.getDefault().notify(desc);
500             }
501         }
502     }
503     
504     public FileObject getWsdlFolder(boolean create) throws IOException JavaDoc {
505         FileObject wsdlFolder = null;
506         FileObject metaInf = getMetaInf();
507         
508         if(metaInf != null) {
509             wsdlFolder = metaInf.getFileObject(WSDL_FOLDER);
510             if(wsdlFolder == null && create) {
511                 wsdlFolder = metaInf.createFolder(WSDL_FOLDER);
512             }
513         } else if(create) {
514             // Create was specified, but no META-INF was found, so how do we create it?
515
// Expect an NPE if we return null for this case, but log it anyway.
516
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
517             NbBundle.getMessage(EjbJarWebServicesSupport.class, "MSG_MetaInfNotFoundForWsdlFolder"));
518         }
519         
520         return wsdlFolder;
521     }
522     
523     public List JavaDoc/*StubDescriptor*/ getStubDescriptors() {
524         ArrayList JavaDoc stubs = new ArrayList JavaDoc(2);
525         /*
526         String version = project.getEjbModule().getJ2eePlatformVersion();
527         if(EjbProjectConstants.J2EE_14_LEVEL.equals(version)) {
528             stubs.add(jsr109ClientStub);
529         }*/

530         stubs.add(jaxrpcClientStub);
531         return stubs;
532     }
533     
534     /** !PW This method is exposed in the client support API. Though it's
535      * implementation makes more sense here than anywhere else, perhaps this
536      * and the other project.xml/project.properties related methods in this
537      * object should be refactored into another object that this one delegates
538      * to. That way, this method would be directly available within the web
539      * web module, as it is needed, and remain missing from the API (where it
540      * probably does not belong at this time.
541      */

542     private static final String JavaDoc [] WSCOMPILE_CLIENT_FEATURES = {
543         "datahandleronly", // - portable
544
// "documentliteral", // SEI ONLY
545
// "rpcliteral", // SEI ONLY
546
"explicitcontext",
547         // "infix:<name>", // difficult to implement.
548
"jaxbenumtype",
549         "nodatabinding", // - portable
550
"noencodedtypes",
551         "nomultirefs",
552         "norpcstructures", // - portable
553
"novalidation", // - portable
554
"resolveidref",
555         "searchschema", // - portable
556
"serializeinterfaces",
557         "strict", // - portable
558
// "useonewayoperations", // SEI ONLY
559
"wsi", // - portable
560
"unwrap",// - portable
561
"donotoverride", // - portable
562
"donotunwrap", // - portable
563
};
564     
565     private static final List JavaDoc allClientFeatures = Arrays.asList(WSCOMPILE_CLIENT_FEATURES);
566     
567     private static final String JavaDoc [] WSCOMPILE_KEY_CLIENT_FEATURES = {
568         "wsi",
569         "strict",
570         "unwrap",
571         "donotunwrap",
572         "donotoverride",
573         "datahandleronly",
574         "nodatabinding",
575         "norpcstructures",
576         "novalidation",
577         "searchschema",
578     };
579     
580     private static final List JavaDoc importantClientFeatures = Arrays.asList(WSCOMPILE_KEY_CLIENT_FEATURES);
581     
582     public List JavaDoc getServiceClients() {
583         List JavaDoc serviceNames = new ArrayList JavaDoc();
584         
585         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
586         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
587         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
588         
589         if(nodes.getLength() != 0) {
590             Element JavaDoc clientElements = (Element JavaDoc) nodes.item(0);
591             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
592             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
593             for(int i = 0; i < clientNameList.getLength(); i++ ) {
594                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
595                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
596                 if(nl.getLength() == 1) {
597                     org.w3c.dom.Node JavaDoc n = nl.item(0);
598                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
599                         String JavaDoc serviceName = n.getNodeValue();
600                         String JavaDoc currentFeatures = projectProperties.getProperty("wscompile.client." + serviceName + ".features");
601                         if(currentFeatures == null) {
602                             // !PW should probably retrieve default features for stub type.
603
// For now, this will work because this is the same value we'd get doing that.
604
//
605
// Defaults if we can't find any feature property for this client
606
// Mostly for upgrading EA1, EA2 projects which did not have
607
// this property, but also useful if the user deletes it from
608
// project.properties.
609
currentFeatures = "wsi, strict";
610                         }
611                         ClientStubDescriptor stubType = getClientStubDescriptor(clientNameElement.getParentNode());
612                         boolean propVerbose = "true".equalsIgnoreCase( //NOI18N
613
projectProperties.getProperty("wscompile.client." + serviceName + ".verbose")); //NOI18N
614
boolean propDebug = "true".equalsIgnoreCase( //NOI18N
615
projectProperties.getProperty("wscompile.client." + serviceName + ".debug")); //NOI18N
616
boolean propPrintStackTrace = "true".equalsIgnoreCase( //NOI18N
617
projectProperties.getProperty("wscompile.client." + serviceName + ".xPrintStackTrace")); //NOI18N
618
boolean propExtensible = "true".equalsIgnoreCase( //NOI18N
619
projectProperties.getProperty("wscompile.client." + serviceName + ".xSerializable")); //NOI18N
620
boolean propOptimize = "true".equalsIgnoreCase( //NOI18N
621
projectProperties.getProperty("wscompile.client." + serviceName + ".optimize")); //NOI18N
622
boolean[] options = new boolean[] { //NOI18N
623
propVerbose,propDebug,propPrintStackTrace,propExtensible,propOptimize
624                         };
625                         WsCompileClientEditorSupport.ServiceSettings settings = new WsCompileClientEditorSupport.ServiceSettings(
626                         serviceName, stubType, options, currentFeatures, allClientFeatures, importantClientFeatures);
627                         serviceNames.add(settings);
628                     } else {
629                         // !PW FIXME node is wrong type?! - log message or trace?
630
}
631                 } else {
632                     // !PW FIXME no name for this service entry - notify user
633
}
634             }
635         }
636         
637         return serviceNames;
638     }
639     
640     private ClientStubDescriptor getClientStubDescriptor(org.w3c.dom.Node JavaDoc parentNode) {
641         ClientStubDescriptor result = null;
642         
643         if(parentNode instanceof Element JavaDoc) {
644             Element JavaDoc parentElement = (Element JavaDoc) parentNode;
645             NodeList JavaDoc clientNameList = parentElement.getElementsByTagNameNS(
646             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesConstants.WEB_SERVICE_STUB_TYPE);
647             if(clientNameList.getLength() == 1) {
648                 Element JavaDoc clientStubElement = (Element JavaDoc) clientNameList.item(0);
649                 NodeList JavaDoc nl = clientStubElement.getChildNodes();
650                 if(nl.getLength() == 1) {
651                     org.w3c.dom.Node JavaDoc n = nl.item(0);
652                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
653                         String JavaDoc stubName = n.getNodeValue();
654                         if (ClientStubDescriptor.JSR109_CLIENT_STUB.equals(stubName)) {
655                             result = jsr109ClientStub;
656                         } else if (ClientStubDescriptor.JAXRPC_CLIENT_STUB.equals(stubName)) {
657                             result = jaxrpcClientStub;
658                         }
659                     }
660                 }
661             }
662         }
663         
664         return result;
665     }
666     
667     public String JavaDoc getWsdlSource(String JavaDoc serviceName) {
668         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
669         String JavaDoc wsdlSource = null;
670         
671         Element JavaDoc clientElement = getWebServiceClientNode(data, serviceName);
672         if(clientElement != null) {
673             NodeList JavaDoc fromWsdlList = clientElement.getElementsByTagNameNS(
674             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesConstants.CLIENT_SOURCE_URL);
675             if(fromWsdlList.getLength() == 1) {
676                 Element JavaDoc fromWsdlElement = (Element JavaDoc) fromWsdlList.item(0);
677                 NodeList JavaDoc nl = fromWsdlElement.getChildNodes();
678                 if(nl.getLength() == 1) {
679                     org.w3c.dom.Node JavaDoc n = nl.item(0);
680                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
681                         wsdlSource = n.getNodeValue();
682                     }
683                 }
684             }
685         }
686         
687         return wsdlSource;
688     }
689     
690     public void setWsdlSource(String JavaDoc serviceName, String JavaDoc wsdlSource) {
691         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
692         Document JavaDoc doc = data.getOwnerDocument();
693         boolean needsSave = false;
694         
695         Element JavaDoc clientElement = getWebServiceClientNode(data, serviceName);
696         if(clientElement != null) {
697             NodeList JavaDoc fromWsdlList = clientElement.getElementsByTagNameNS(
698             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WebServicesConstants.CLIENT_SOURCE_URL);
699             if(fromWsdlList.getLength() > 0) {
700                 Element JavaDoc fromWsdlElement = (Element JavaDoc) fromWsdlList.item(0);
701                 NodeList JavaDoc nl = fromWsdlElement.getChildNodes();
702                 if(nl.getLength() > 0) {
703                     org.w3c.dom.Node JavaDoc n = nl.item(0);
704                     n.setNodeValue(wsdlSource);
705                 } else {
706                     fromWsdlElement.appendChild(doc.createTextNode(wsdlSource));
707                 }
708             } else {
709                 Element JavaDoc clientElementSourceUrl = doc.createElementNS(
710                 EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
711                 clientElement.appendChild(clientElementSourceUrl);
712                 clientElementSourceUrl.appendChild(doc.createTextNode(wsdlSource));
713             }
714             
715             needsSave = true;
716         }
717         
718         // !PW Save the project if we were able to make the change.
719
if(needsSave) {
720             try {
721                 ProjectManager.getDefault().saveProject(project);
722             } catch(IOException JavaDoc ex) {
723                 NotifyDescriptor desc = new NotifyDescriptor.Message(
724                 NbBundle.getMessage(EjbJarWebServicesSupport.class,
725                 "MSG_ErrorSavingOnWSClientAdd", serviceName, ex.getMessage()), // NOI18N
726
NotifyDescriptor.ERROR_MESSAGE);
727                 DialogDisplayer.getDefault().notify(desc);
728             }
729         }
730     }
731     
732     private Element JavaDoc getWebServiceClientNode(Element JavaDoc data, String JavaDoc serviceName) {
733         Element JavaDoc clientElement = null;
734         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
735         
736         if(nodes.getLength() != 0) {
737             Element JavaDoc clientElements = (Element JavaDoc) nodes.item(0);
738             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
739             EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
740             for(int i = 0; i < clientNameList.getLength(); i++ ) {
741                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
742                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
743                 if(nl.getLength() == 1) {
744                     org.w3c.dom.Node JavaDoc n = nl.item(0);
745                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
746                         String JavaDoc name = n.getNodeValue();
747                         if(serviceName.equals(name)) {
748                             org.w3c.dom.Node JavaDoc node = clientNameElement.getParentNode();
749                             clientElement = (node instanceof Element JavaDoc) ? (Element JavaDoc) node : null;
750                             break;
751                         }
752                     } else {
753                         // !PW FIXME node is wrong type?! - log message or trace?
754
}
755                 }
756             }
757         }
758         
759         return clientElement;
760     }
761     
762     // Client stub descriptors
763
private static final JAXRPCClientStubDescriptor jsr109ClientStub = new JAXRPCClientStubDescriptor(
764     ClientStubDescriptor.JSR109_CLIENT_STUB,
765     NbBundle.getMessage(EjbJarWebServicesSupport.class,"LBL_JSR109ClientStub"),
766     new String JavaDoc [] { "wsi", "strict" });
767     
768     private static final JAXRPCClientStubDescriptor jaxrpcClientStub = new JAXRPCClientStubDescriptor(
769     ClientStubDescriptor.JAXRPC_CLIENT_STUB,
770     NbBundle.getMessage(EjbJarWebServicesSupport.class,"LBL_JAXRPCStaticClientStub"),
771     new String JavaDoc [] { "wsi", "strict" });
772     
773     /** Does nothing in ejb-jar project */
774     public void setProxyJVMOptions(String JavaDoc proxyHost, String JavaDoc proxyPort) {
775     }
776
777     public String JavaDoc getServiceRefName(String JavaDoc serviceName) {
778         return "service/" + serviceName;
779     }
780     
781
782     /** Stub descriptor for clients supported by this project type.
783      */

784     private static class JAXRPCClientStubDescriptor extends ClientStubDescriptor {
785         
786         private String JavaDoc [] defaultFeatures;
787         
788         public JAXRPCClientStubDescriptor(String JavaDoc name, String JavaDoc displayName, String JavaDoc [] defaultFeatures) {
789             super(name, displayName);
790             
791             this.defaultFeatures = defaultFeatures;
792         }
793         
794         public String JavaDoc [] getDefaultFeatures() {
795             return defaultFeatures;
796         }
797         
798         public String JavaDoc getDefaultFeaturesAsArgument() {
799             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(defaultFeatures.length*32);
800             for(int i = 0; i < defaultFeatures.length; i++) {
801                 if(i > 0) {
802                     buf.append(",");
803                 }
804                 
805                 buf.append(defaultFeatures[i]);
806             }
807             return buf.toString();
808         }
809         
810         void setDefaultFeatures(String JavaDoc[] defaultFeatures) {
811             this.defaultFeatures=defaultFeatures;
812         }
813     }
814 }
815
Popular Tags