KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > project > WebProjectWebServicesClientSupport


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

65 public class WebProjectWebServicesClientSupport implements WebServicesClientSupportImpl{
66     private WebProject project;
67     private AntProjectHelper helper;
68     private ReferenceHelper referenceHelper;
69     
70     /** Creates a new instance of WebProjectWebServicesSupport */
71     public WebProjectWebServicesClientSupport(WebProject project, AntProjectHelper helper, ReferenceHelper referenceHelper) {
72         this.project = project;
73         this.helper = helper;
74         this.referenceHelper = referenceHelper;
75     }
76     
77     private WebApp getWebApp() {
78         try {
79             FileObject deploymentDescriptor = getDeploymentDescriptor();
80             if(deploymentDescriptor != null) {
81                 return DDProvider.getDefault().getDDRoot(deploymentDescriptor);
82             }
83         } catch (java.io.IOException JavaDoc e) {
84             org.openide.ErrorManager.getDefault().log(e.getLocalizedMessage());
85         }
86         return null;
87     }
88     
89 // /**
90
// * Get the webservices.xml file object
91
// * descriptive in interface, e.g., getWebserviceDD
92
// */
93
// public FileObject getWebservicesDD() {
94
// FileObject webInfFo = getWebInf();
95
// if (webInfFo==null) {
96
// if (isProjectOpened()) {
97
// DialogDisplayer.getDefault().notify(
98
// new NotifyDescriptor.Message(NbBundle.getMessage(WebProjectWebServicesSupport.class,"MSG_WebInfCorrupted"),
99
// NotifyDescriptor.ERROR_MESSAGE));
100
// }
101
// return null;
102
// }
103
// return getWebInf().getFileObject(WEBSERVICES_DD, "xml");
104
// }
105

106     /**
107      * Returns the directory that contains webservices.xml in the project
108      */

109     public FileObject getWsDDFolder() {
110         return getWebInf();
111     }
112     
113     /**
114      * Returns the name of the directory that contains the webservices.xml in
115      * the archive
116      */

117     public String JavaDoc getArchiveDDFolderName() {
118         return "WEB-INF"; // NOI18N
119
}
120     
121     /**
122      * Returns the name of the implementation bean class
123      * given the servlet-link name
124      */

125     public String JavaDoc getImplementationBean(String JavaDoc linkName) {
126         WebApp webApp = getWebApp();
127         org.netbeans.modules.j2ee.dd.api.web.Servlet[] servlets = webApp.getServlet();
128         for(int i = 0; i < servlets.length; i++) {
129             if(servlets[i].getServletName().equals(linkName)) {
130                 return servlets[i].getServletClass();
131             }
132         }
133         return null;
134     }
135     
136     public void removeServiceEntry(String JavaDoc linkName) {
137         //remove servlet entry in web.xml
138
WebApp webApp = getWebApp();
139         Servlet[] servlets = webApp.getServlet();
140         for(int i = 0; i < servlets.length; i++) {
141             Servlet servlet = servlets[i];
142             if(servlet.getServletName().equals(linkName)) {
143                 webApp.removeServlet(servlet);
144                 break;
145             }
146         }
147         ServletMapping[] mappings = webApp.getServletMapping();
148         for(int j = 0; j < mappings.length; j++ ) {
149             ServletMapping mapping = mappings[j];
150             if(mapping.getServletName().equals(linkName)) {
151                 webApp.removeServletMapping(mapping);
152             }
153         }
154         try {
155             // This also saves server specific configuration, if necessary.
156
webApp.write(getDeploymentDescriptor());
157         }
158         catch(java.io.IOException JavaDoc e) {
159             NotifyDescriptor ndd =
160             new NotifyDescriptor.Message(NbBundle.getMessage(this.getClass(), "MSG_Unable_WRITE_WS_DD"), // NOI18N
161
NotifyDescriptor.ERROR_MESSAGE);
162             DialogDisplayer.getDefault().notify(ndd);
163         }
164     }
165     
166     public AntProjectHelper getAntProjectHelper() {
167         return helper;
168     }
169        
170     public ReferenceHelper getReferenceHelper(){
171         return referenceHelper;
172     }
173
174     // Implementation of WebServiceClientSupportImpl
175
public void addServiceClient(String JavaDoc serviceName, String JavaDoc packageName, String JavaDoc sourceUrl, FileObject configFile, ClientStubDescriptor stubDescriptor) {
176         this.addServiceClient(serviceName, packageName, sourceUrl, configFile, stubDescriptor, null);
177     }
178
179         
180     // Implementation of WebServiceClientSupportImpl
181
public void addServiceClient(String JavaDoc serviceName, String JavaDoc packageName, String JavaDoc sourceUrl, FileObject configFile, ClientStubDescriptor stubDescriptor, String JavaDoc[] wscompileFeatures) {
182         // !PW FIXME I have two concerns with this implementation:
183
// 1. Since it modifies project.xml, I suspect it should be acquiring
184
// ProjectManager.mutex() for write access.
185
// 2. It seems like it ought to be implemented via the AuxiliaryConfiguration
186
// interface.
187
boolean needsSave = false;
188         boolean modifiedProjectProperties = false;
189         boolean modifiedPrivateProperties = false;
190         
191         /** Locate root of web service client node structure in project,xml, creating it
192          * if it's not found.
193          */

194         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
195         Document JavaDoc doc = data.getOwnerDocument();
196         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
197         Element JavaDoc clientElements = null;
198         
199         if(nodes.getLength() == 0) {
200             // 'needsSave' deliberately left false here because this is a trival change
201
// that only should be saved if additional changes are also made below.
202
clientElements = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENTS);
203             NodeList JavaDoc srcRoots = data.getElementsByTagNameNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, "source-roots"); // NOI18N
204
assert srcRoots.getLength() == 1 : "Invalid project.xml."; // NOI18N
205
data.insertBefore(clientElements, srcRoots.item(0));
206         } else {
207             clientElements = (Element JavaDoc) nodes.item(0);
208         }
209         
210         /** Make sure this service is not already registered in project.xml
211          */

212         boolean serviceAlreadyAdded = false;
213         NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
214         for(int i = 0; i < clientNameList.getLength(); i++ ) {
215             Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
216             NodeList JavaDoc nl = clientNameElement.getChildNodes();
217             if(nl.getLength() >= 1) {
218                 org.w3c.dom.Node JavaDoc n = nl.item(0);
219                 if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
220                     if(serviceName.equalsIgnoreCase(n.getNodeValue())) {
221                         serviceAlreadyAdded = true;
222                         
223                         // !PW FIXME should force stub type to match value passed in
224
// in case someone is overwriting a current service with a different
225
// stub type.
226
}
227                 }
228             }
229         }
230         
231         /** Add entry for the client to project.xml and regenerate build-impl.xml.
232          */

233         if(!serviceAlreadyAdded) {
234             Element JavaDoc clientElement = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT);
235             clientElements.appendChild(clientElement);
236             Element JavaDoc clientElementName = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
237             clientElement.appendChild(clientElementName);
238             clientElementName.appendChild(doc.createTextNode(serviceName));
239             Element JavaDoc clientElementStubType = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_STUB_TYPE);
240             clientElement.appendChild(clientElementStubType);
241             clientElementStubType.appendChild(doc.createTextNode(stubDescriptor.getName()));
242             Element JavaDoc clientElementSourceUrl = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
243             clientElement.appendChild(clientElementSourceUrl);
244             clientElementSourceUrl.appendChild(doc.createTextNode(sourceUrl));
245             helper.putPrimaryConfigurationData(data, true);
246             needsSave = true;
247         }
248         
249         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
250         EditableProperties privateProperties = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
251         
252         // Add property for wscompile features
253
{
254             String JavaDoc featurePropertyName = "wscompile.client." + serviceName + ".features"; // NOI18N
255
String JavaDoc defaultFeatures = "wsi, strict"; // NOI18N -- defaults if stub descriptor is bad type (should never happen?)
256
if(stubDescriptor instanceof JAXRPCClientStubDescriptor) {
257                 JAXRPCClientStubDescriptor stubDesc = (JAXRPCClientStubDescriptor) stubDescriptor;
258                 if (wscompileFeatures!=null) stubDesc.setDefaultFeatures(wscompileFeatures);
259                 defaultFeatures = stubDesc.getDefaultFeaturesAsArgument();
260             } else {
261                 // !PW FIXME wrong stub type -- log error message.
262
}
263             String JavaDoc oldFeatures = projectProperties.getProperty(featurePropertyName);
264             if(!defaultFeatures.equals(oldFeatures)) {
265                 projectProperties.put(featurePropertyName, defaultFeatures);
266                 modifiedProjectProperties = true;
267             }
268         }
269         
270         // Add package name property
271
{
272             String JavaDoc packagePropertyName = "wscompile.client." + serviceName + ".package"; // NOI18N
273
String JavaDoc oldPackageName = projectProperties.getProperty(packagePropertyName);
274             if(!packageName.equals(oldPackageName)) {
275                 projectProperties.put(packagePropertyName, packageName);
276                 modifiedProjectProperties = true;
277             }
278         }
279         
280         // create wscompile:httpproxy option
281
if (proxyHost!=null && proxyHost.length()>0) {
282             String JavaDoc proxyProperty = "wscompile.client." + serviceName + ".proxy"; // NOI18N
283
String JavaDoc oldProxyProperty = privateProperties.getProperty(proxyProperty);
284             if(!proxyProperty.equals(oldProxyProperty)) {
285                 privateProperties.put(proxyProperty, proxyHost+":"+(proxyPort==null?"8080":proxyPort)); //NOI18N
286
modifiedPrivateProperties = true;
287             }
288         }
289         
290         if(modifiedProjectProperties) {
291             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties);
292             needsSave = true;
293         }
294         if(modifiedPrivateProperties) {
295             helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, privateProperties);
296             needsSave = true;
297         }
298         
299         // Update wscompile related properties. boolean return indicates whether
300
// any changes were made.
301
if(updateWsCompileProperties(serviceName)) {
302             needsSave = true;
303         }
304         
305         // !PW Lastly, save the project if we actually made any changes to any
306
// properties or the build script.
307
if(needsSave) {
308             try {
309                 ProjectManager.getDefault().saveProject(project);
310             } catch(IOException JavaDoc ex) {
311                 NotifyDescriptor desc = new NotifyDescriptor.Message(
312                 NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_ErrorSavingOnWSClientAdd", serviceName, ex.getMessage()), // NOI18N
313
NotifyDescriptor.ERROR_MESSAGE);
314                 DialogDisplayer.getDefault().notify(desc);
315             }
316         }
317     }
318     
319     public void addInfrastructure(String JavaDoc implBeanClass, FileObject pkg) {
320         //nothing to do here, there are no infrastructure elements
321
}
322     
323     public FileObject getDeploymentDescriptor() {
324         FileObject webInfFo = getWebInf();
325         if (webInfFo==null) {
326             if (isProjectOpened()) {
327                 DialogDisplayer.getDefault().notify(
328                 new NotifyDescriptor.Message(NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_WebInfCorrupted"), // NOI18N
329
NotifyDescriptor.ERROR_MESSAGE));
330             }
331             return null;
332         }
333         return getWebInf().getFileObject(ProjectWebModule.FILE_DD);
334     }
335     
336     public FileObject getWebInf() {
337         FileObject documentBase = getDocumentBase();
338         if (documentBase != null)
339             return documentBase.getFileObject(ProjectWebModule.FOLDER_WEB_INF);
340         else
341             return null;
342     }
343     
344     public FileObject getDocumentBase() {
345         return getFileObject("web.docbase.dir"); // NOI18N
346
}
347     
348     private FileObject getFileObject(String JavaDoc propname) {
349         String JavaDoc prop = helper.getStandardPropertyEvaluator().getProperty(propname);
350         if (prop != null) {
351             return helper.resolveFileObject(prop);
352         } else {
353             return null;
354         }
355     }
356     
357     private boolean updateWsCompileProperties(String JavaDoc serviceName) {
358         /** Ensure wscompile.classpath and wscompile.tools.classpath are
359          * properly defined.
360          *
361          * wscompile.classpath goes in project properties and includes
362          * jaxrpc and qname right now.
363          *
364          * wscompile.tools.classpath is for tools.jar which is needed when
365          * running under the Sun JDK to invoke javac. It is placed in
366          * user.properties so that if we compute it incorrectly (say on a mac)
367          * the user can change it and we will not blow away the change.
368          * Hopefully we can do this better for release.
369          */

370         boolean globalPropertiesChanged = false;
371         
372         EditableProperties globalProperties = PropertyUtils.getGlobalProperties();
373         if(globalProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
374             globalProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar"); // NOI18N
375

376             try {
377                 PropertyUtils.putGlobalProperties(globalProperties);
378             } catch(IOException JavaDoc ex) {
379                 NotifyDescriptor desc = new NotifyDescriptor.Message(
380                 NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_ErrorSavingGlobalProperties", serviceName, ex.getMessage()), // NOI18N
381
NotifyDescriptor.ERROR_MESSAGE);
382                 DialogDisplayer.getDefault().notify(desc);
383             }
384             
385             globalPropertiesChanged = true;
386         }
387         
388         
389         boolean projectPropertiesChanged = false;
390         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
391         
392         { // Block that adjusts wscompile.client.classpath as necessary.
393
HashSet JavaDoc wscJars = new HashSet JavaDoc();
394             boolean newWscJars = false;
395             String JavaDoc wscClientClasspath = projectProperties.getProperty(WSCOMPILE_CLASSPATH);
396             if(wscClientClasspath != null) {
397                 String JavaDoc [] libs = PropertyUtils.tokenizePath(wscClientClasspath);
398                 for(int i = 0; i < libs.length; i++) {
399                     wscJars.add(libs[i]);
400                 }
401             }
402             
403             for(int i = 0; i < WSCOMPILE_JARS.length; i++) {
404                 if(!wscJars.contains(WSCOMPILE_JARS[i])) {
405                     wscJars.add(WSCOMPILE_JARS[i]);
406                     newWscJars = true;
407                 }
408             }
409             
410             if(newWscJars) {
411                 StringBuffer JavaDoc newClasspathBuf = new StringBuffer JavaDoc(256);
412                 for(Iterator JavaDoc iter = wscJars.iterator(); iter.hasNext(); ) {
413                     newClasspathBuf.append(iter.next().toString());
414                     if(iter.hasNext()) {
415                         newClasspathBuf.append(':');
416                     }
417                 }
418                 projectProperties.put(WSCOMPILE_CLASSPATH, newClasspathBuf.toString());
419                 projectPropertiesChanged = true;
420             }
421         }
422         
423         // set tools.jar property if not set
424
if(projectProperties.getProperty(WSCOMPILE_TOOLS_CLASSPATH) == null) {
425             projectProperties.setProperty(WSCOMPILE_TOOLS_CLASSPATH, "${java.home}\\..\\lib\\tools.jar"); // NOI18N
426
projectPropertiesChanged = true;
427         }
428         
429         if(projectPropertiesChanged) {
430             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProperties);
431         }
432         
433         return globalPropertiesChanged || projectPropertiesChanged;
434     }
435     
436     public void removeServiceClient(String JavaDoc serviceName) {
437         // 2. Remove service from project.xml
438
// Side effect: Regenerate build-impl.xsl
439
// Optional - if last service, remove properties we generated.
440
boolean needsSave = false;
441         boolean needsSave1 = false;
442         
443         /** Remove properties from project.properties
444          */

445         String JavaDoc featureProperty = "wscompile.client." + serviceName + ".features"; // NOI18N
446
String JavaDoc packageProperty = "wscompile.client." + serviceName + ".package"; // NOI18N
447
String JavaDoc proxyProperty = "wscompile.client." + serviceName + ".proxy"; //NOI18N
448

449         EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
450         EditableProperties ep1 = helper.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
451         
452         if(ep.getProperty(featureProperty) != null) {
453             ep.remove(featureProperty);
454             needsSave = true;
455         }
456         
457         if(ep.getProperty(packageProperty) != null) {
458             ep.remove(packageProperty);
459             needsSave = true;
460         }
461         
462         if(ep1.getProperty(proxyProperty) != null) {
463             ep1.remove(proxyProperty);
464             needsSave1 = true;
465         }
466         
467         if(needsSave) {
468             helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
469         }
470         
471         if(needsSave1) {
472             helper.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep1);
473         }
474         
475         /** Locate root of web service client node structure in project,xml
476          */

477         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
478         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
479         Element JavaDoc clientElements = null;
480         
481         /* If there is a root, get all the names of the child services and search
482          * for the one we want to remove.
483          */

484         if(nodes.getLength() >= 1) {
485             clientElements = (Element JavaDoc) nodes.item(0);
486             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
487             for(int i = 0; i < clientNameList.getLength(); i++ ) {
488                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
489                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
490                 if(nl.getLength() == 1) {
491                     org.w3c.dom.Node JavaDoc n = nl.item(0);
492                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
493                         if(serviceName.equalsIgnoreCase(n.getNodeValue())) {
494                             // Found it! Now remove it.
495
org.w3c.dom.Node JavaDoc serviceNode = clientNameElement.getParentNode();
496                             clientElements.removeChild(serviceNode);
497                             helper.putPrimaryConfigurationData(data, true);
498                             needsSave = true;
499                         }
500                     }
501                 }
502             }
503         }
504         
505         // !PW Lastly, save the project if we actually made any changes to any
506
// properties or the build script.
507
if(needsSave || needsSave1) {
508             try {
509                 ProjectManager.getDefault().saveProject(project);
510             } catch(IOException JavaDoc ex) {
511                 NotifyDescriptor desc = new NotifyDescriptor.Message(
512                 NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_ErrorSavingOnWSClientRemove", serviceName, ex.getMessage()), // NOI18N
513
NotifyDescriptor.ERROR_MESSAGE);
514                 DialogDisplayer.getDefault().notify(desc);
515             }
516         }
517     }
518     
519     public FileObject getWsdlFolder(boolean create) throws IOException JavaDoc {
520         FileObject wsdlFolder = null;
521         FileObject webInf = getWebInf();
522         
523         if(webInf != null) {
524             wsdlFolder = webInf.getFileObject(WSDL_FOLDER);
525             if(wsdlFolder == null && create) {
526                 wsdlFolder = webInf.createFolder(WSDL_FOLDER);
527             }
528         } else if(create) {
529             // Create was specified, but no WEB-INF was found, so how do we create it?
530
// Expect an NPE if we return null for this case, but log it anyway.
531
ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL,
532             NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_WebInfNotFoundForWsdlFolder"));
533         }
534         
535         return wsdlFolder;
536     }
537     
538     public List JavaDoc/*ClientStubDescriptor*/ getStubDescriptors() {
539         ArrayList JavaDoc stubs = new ArrayList JavaDoc(2);
540         String JavaDoc version = project.getWebModule().getJ2eePlatformVersion();
541         if(WebModule.J2EE_14_LEVEL.equals(version)) {
542             stubs.add(jsr109ClientStub);
543         }
544         stubs.add(jaxrpcClientStub);
545         return stubs;
546     }
547     
548     private boolean isProjectOpened() {
549         // XXX workaround: OpenProjects.getDefault() can be null
550
// when called from ProjectOpenedHook.projectOpened() upon IDE startup
551
if (OpenProjects.getDefault() == null)
552             return true;
553         
554         Project[] projects = OpenProjects.getDefault().getOpenProjects();
555         for (int i = 0; i < projects.length; i++) {
556             if (projects[i].equals(project))
557                 return true;
558         }
559         return false;
560     }
561     
562     /** !PW This method is exposed in the client support API. Though it's
563      * implementation makes more sense here than anywhere else, perhaps this
564      * and the other project.xml/project.properties related methods in this
565      * object should be refactored into another object that this one delegates
566      * to. That way, this method would be directly available within the web
567      * web module, as it is needed, and remain missing from the API (where it
568      * probably does not belong at this time.
569      */

570     private static final String JavaDoc [] WSCOMPILE_CLIENT_FEATURES = {
571         "datahandleronly", // - portable
572
// "documentliteral", // SEI ONLY
573
// "rpcliteral", // SEI ONLY
574
"explicitcontext",
575         // "infix:<name>", // difficult to implement.
576
"jaxbenumtype",
577         "nodatabinding", // - portable
578
"noencodedtypes",
579         "nomultirefs",
580         "norpcstructures", // - portable
581
"novalidation", // - portable
582
"resolveidref",
583         "searchschema", // - portable
584
"serializeinterfaces",
585         "strict", // - portable
586
// "useonewayoperations", // SEI ONLY
587
"wsi", // - portable
588
"unwrap",// - portable
589
"donotoverride", // - portable
590
"donotunwrap", // - portable
591
};
592     
593     private static final List JavaDoc allClientFeatures = Arrays.asList(WSCOMPILE_CLIENT_FEATURES);
594     
595     private static final String JavaDoc [] WSCOMPILE_KEY_CLIENT_FEATURES = {
596         "wsi",
597         "strict",
598         "norpcstructures",
599         "unwrap",
600         "donotunwrap",
601         "donotoverride",
602         "datahandleronly",
603         "nodatabinding",
604         "novalidation",
605         "searchschema",
606     };
607     
608     private static final List JavaDoc importantClientFeatures = Arrays.asList(WSCOMPILE_KEY_CLIENT_FEATURES);
609     
610     public List JavaDoc getServiceClients() {
611         List JavaDoc serviceNames = new ArrayList JavaDoc();
612         
613         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
614         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
615         EditableProperties projectProperties = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
616         
617         if(nodes.getLength() != 0) {
618             Element JavaDoc clientElements = (Element JavaDoc) nodes.item(0);
619             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
620             WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
621             for(int i = 0; i < clientNameList.getLength(); i++ ) {
622                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
623                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
624                 if(nl.getLength() == 1) {
625                     org.w3c.dom.Node JavaDoc n = nl.item(0);
626                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
627                         String JavaDoc serviceName = n.getNodeValue();
628                         String JavaDoc currentFeatures = projectProperties.getProperty("wscompile.client." + serviceName + ".features");
629                         if(currentFeatures == null) {
630                             // !PW should probably retrieve default features for stub type.
631
// For now, this will work because this is the same value we'd get doing that.
632
//
633
// Defaults if we can't find any feature property for this client
634
// Mostly for upgrading EA1, EA2 projects which did not have
635
// this property, but also useful if the user deletes it from
636
// project.properties.
637
currentFeatures = "wsi, strict";
638                         }
639                         ClientStubDescriptor stubType = getClientStubDescriptor(clientNameElement.getParentNode());
640                         boolean propVerbose = "true".equalsIgnoreCase( //NOI18N
641
projectProperties.getProperty("wscompile.client." + serviceName + ".verbose")); //NOI18N
642
boolean propDebug = "true".equalsIgnoreCase( //NOI18N
643
projectProperties.getProperty("wscompile.client." + serviceName + ".debug")); //NOI18N
644
boolean propPrintStackTrace = "true".equalsIgnoreCase( //NOI18N
645
projectProperties.getProperty("wscompile.client." + serviceName + ".xPrintStackTrace")); //NOI18N
646
boolean propExtensible = "true".equalsIgnoreCase( //NOI18N
647
projectProperties.getProperty("wscompile.client." + serviceName + ".xSerializable")); //NOI18N
648
boolean propOptimize = "true".equalsIgnoreCase( //NOI18N
649
projectProperties.getProperty("wscompile.client." + serviceName + ".optimize")); //NOI18N
650
boolean[] options = new boolean[] { //NOI18N
651
propVerbose,propDebug,propPrintStackTrace,propExtensible,propOptimize
652                         };
653                         WsCompileClientEditorSupport.ServiceSettings settings = new WsCompileClientEditorSupport.ServiceSettings(
654                         serviceName, stubType, options, currentFeatures, allClientFeatures, importantClientFeatures);
655                         serviceNames.add(settings);
656                     } else {
657                         // !PW FIXME node is wrong type?! - log message or trace?
658
}
659                 } else {
660                     // !PW FIXME no name for this service entry - notify user
661
}
662             }
663         }
664         
665         return serviceNames;
666     }
667     
668     private ClientStubDescriptor getClientStubDescriptor(org.w3c.dom.Node JavaDoc parentNode) {
669         ClientStubDescriptor result = null;
670         
671         if(parentNode instanceof Element JavaDoc) {
672             Element JavaDoc parentElement = (Element JavaDoc) parentNode;
673             NodeList JavaDoc clientNameList = parentElement.getElementsByTagNameNS(
674             WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_STUB_TYPE);
675             if(clientNameList.getLength() == 1) {
676                 Element JavaDoc clientStubElement = (Element JavaDoc) clientNameList.item(0);
677                 NodeList JavaDoc nl = clientStubElement.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                         String JavaDoc stubName = n.getNodeValue();
682                         if(ClientStubDescriptor.JSR109_CLIENT_STUB.equals(stubName)) {
683                             result = jsr109ClientStub;
684                         } else if(ClientStubDescriptor.JAXRPC_CLIENT_STUB.equals(stubName)) {
685                             result = jaxrpcClientStub;
686                         }
687                     }
688                 }
689             }
690         }
691         
692         return result;
693     }
694     
695     public String JavaDoc getWsdlSource(String JavaDoc serviceName) {
696         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
697         String JavaDoc wsdlSource = null;
698         
699         Element JavaDoc clientElement = getWebServiceClientNode(data, serviceName);
700         if(clientElement != null) {
701             NodeList JavaDoc fromWsdlList = clientElement.getElementsByTagNameNS(
702             WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
703             if(fromWsdlList.getLength() == 1) {
704                 Element JavaDoc fromWsdlElement = (Element JavaDoc) fromWsdlList.item(0);
705                 NodeList JavaDoc nl = fromWsdlElement.getChildNodes();
706                 if(nl.getLength() == 1) {
707                     org.w3c.dom.Node JavaDoc n = nl.item(0);
708                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
709                         wsdlSource = n.getNodeValue();
710                     }
711                 }
712             }
713         }
714         
715         return wsdlSource;
716     }
717     
718     public void setWsdlSource(String JavaDoc serviceName, String JavaDoc wsdlSource) {
719         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
720         Document JavaDoc doc = data.getOwnerDocument();
721         boolean needsSave = false;
722         
723         Element JavaDoc clientElement = getWebServiceClientNode(data, serviceName);
724         if(clientElement != null) {
725             NodeList JavaDoc fromWsdlList = clientElement.getElementsByTagNameNS(
726             WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
727             if(fromWsdlList.getLength() > 0) {
728                 Element JavaDoc fromWsdlElement = (Element JavaDoc) fromWsdlList.item(0);
729                 NodeList JavaDoc nl = fromWsdlElement.getChildNodes();
730                 if(nl.getLength() > 0) {
731                     org.w3c.dom.Node JavaDoc n = nl.item(0);
732                     n.setNodeValue(wsdlSource);
733                 } else {
734                     fromWsdlElement.appendChild(doc.createTextNode(wsdlSource));
735                 }
736             } else {
737                 Element JavaDoc clientElementSourceUrl = doc.createElementNS(WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, CLIENT_SOURCE_URL);
738                 clientElement.appendChild(clientElementSourceUrl);
739                 clientElementSourceUrl.appendChild(doc.createTextNode(wsdlSource));
740             }
741             
742             needsSave = true;
743         }
744         
745         // !PW Save the project if we were able to make the change.
746
if(needsSave) {
747             try {
748                 ProjectManager.getDefault().saveProject(project);
749             } catch(IOException JavaDoc ex) {
750                 NotifyDescriptor desc = new NotifyDescriptor.Message(
751                 NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"MSG_ErrorSavingOnWSClientAdd", serviceName, ex.getMessage()), // NOI18N
752
NotifyDescriptor.ERROR_MESSAGE);
753                 DialogDisplayer.getDefault().notify(desc);
754             }
755         }
756     }
757     
758     private Element JavaDoc getWebServiceClientNode(Element JavaDoc data, String JavaDoc serviceName) {
759         Element JavaDoc clientElement = null;
760         NodeList JavaDoc nodes = data.getElementsByTagName(WEB_SERVICE_CLIENTS);
761         
762         if(nodes.getLength() != 0) {
763             Element JavaDoc clientElements = (Element JavaDoc) nodes.item(0);
764             NodeList JavaDoc clientNameList = clientElements.getElementsByTagNameNS(
765             WebProjectType.PROJECT_CONFIGURATION_NAMESPACE, WEB_SERVICE_CLIENT_NAME);
766             for(int i = 0; i < clientNameList.getLength(); i++ ) {
767                 Element JavaDoc clientNameElement = (Element JavaDoc) clientNameList.item(i);
768                 NodeList JavaDoc nl = clientNameElement.getChildNodes();
769                 if(nl.getLength() == 1) {
770                     org.w3c.dom.Node JavaDoc n = nl.item(0);
771                     if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
772                         String JavaDoc name = n.getNodeValue();
773                         if(serviceName.equals(name)) {
774                             org.w3c.dom.Node JavaDoc node = clientNameElement.getParentNode();
775                             clientElement = (node instanceof Element JavaDoc) ? (Element JavaDoc) node : null;
776                             break;
777                         }
778                     } else {
779                         // !PW FIXME node is wrong type?! - log message or trace?
780
}
781                 }
782             }
783         }
784         
785         return clientElement;
786     }
787     
788     // Client stub descriptors
789
private static final JAXRPCClientStubDescriptor jsr109ClientStub = new JAXRPCClientStubDescriptor(
790         ClientStubDescriptor.JSR109_CLIENT_STUB,
791         NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"LBL_JSR109ClientStub"),
792         new String JavaDoc [] { "wsi", "strict" });
793     
794     private static final JAXRPCClientStubDescriptor jaxrpcClientStub = new JAXRPCClientStubDescriptor(
795         ClientStubDescriptor.JAXRPC_CLIENT_STUB,
796         NbBundle.getMessage(WebProjectWebServicesClientSupport.class,"LBL_JAXRPCStaticClientStub"),
797         new String JavaDoc [] { "wsi", "strict" });
798
799     public void addServiceClientReference(String JavaDoc serviceName, String JavaDoc fqServiceName, String JavaDoc relativeWsdlPath, String JavaDoc relativeMappingPath, String JavaDoc[] portSEIInfo) {
800
801         FileObject ddFO = getDeploymentDescriptor();
802
803         // If we get null for the deployment descriptor, ignore this step.
804
if (ddFO != null) {
805
806             try {
807                 RootInterface rootDD = DDProvider.getDefault().getDDRoot(ddFO);
808
809                 ServiceRef serviceRef = (ServiceRef) rootDD.findBeanByName("ServiceRef", "ServiceRefName", serviceName); // NOI18N
810
if(serviceRef == null) {
811                     serviceRef = (ServiceRef) rootDD.addBean("ServiceRef", // NOI18N
812
new String JavaDoc [] { /* property list */
813                             "ServiceRefName", // NOI18N
814
"ServiceInterface", // NOI18N
815
"WsdlFile", // NOI18N
816
"JaxrpcMappingFile" // NOI18N
817
},
818                         new String JavaDoc [] { /* property values */
819                             // service name
820
serviceName,
821                             // interface package . service name
822
fqServiceName,
823                             // web doc base / wsdl folder / wsdl file name
824
relativeWsdlPath,
825                             // web doc base / mapping file name
826
relativeMappingPath
827                         },
828                         "ServiceRefName"); // NOI18N
829
} else {
830                     serviceRef.setServiceInterface(fqServiceName);
831                     serviceRef.setWsdlFile(new URI JavaDoc(relativeWsdlPath));
832                     serviceRef.setJaxrpcMappingFile(relativeMappingPath);
833                 }
834
835                 PortComponentRef [] portRefArray = new PortComponentRef [portSEIInfo.length];
836                 for (int pi = 0; pi < portRefArray.length; pi++) {
837                     portRefArray[pi] = (PortComponentRef) serviceRef.createBean("PortComponentRef"); // NOI18N
838
portRefArray[pi].setServiceEndpointInterface(portSEIInfo[pi]); // NOI18N
839
}
840                 serviceRef.setPortComponentRef(portRefArray);
841                 rootDD.write(ddFO);
842
843             } catch (IOException JavaDoc ex) {
844                 // Strange thing happen
845
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
846             } catch (NameAlreadyUsedException ex) {
847                 // Should never happen because we look for it by name first.
848
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
849             } catch (URISyntaxException JavaDoc ex) {
850                 // Programmer error - validation of input data should ensure this never happens.
851
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
852             } catch (ClassNotFoundException JavaDoc ex) {
853                 // Programmer error - mistyped object name.
854
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
855             }
856             
857         }
858         
859     }
860     
861     private String JavaDoc proxyHost,proxyPort;
862     
863     /** Does nothing in web project */
864     public void setProxyJVMOptions(String JavaDoc proxyHost, String JavaDoc proxyPort) {
865         this.proxyHost=proxyHost;
866         this.proxyPort=proxyPort;
867     }
868
869     public String JavaDoc getServiceRefName(String JavaDoc serviceName) {
870         return "service/" + serviceName;
871     }
872     
873     /** Stub descriptor for services and clients supported by this project type.
874      */

875     private static class JAXRPCClientStubDescriptor extends ClientStubDescriptor {
876         
877         private String JavaDoc [] defaultFeatures;
878         
879         public JAXRPCClientStubDescriptor(String JavaDoc name, String JavaDoc displayName, String JavaDoc [] defaultFeatures) {
880             super(name, displayName);
881             
882             this.defaultFeatures = defaultFeatures;
883         }
884         
885         public String JavaDoc [] getDefaultFeatures() {
886             return defaultFeatures;
887         }
888         
889         public String JavaDoc getDefaultFeaturesAsArgument() {
890             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(defaultFeatures.length*32);
891             for(int i = 0; i < defaultFeatures.length; i++) {
892                 if(i > 0) {
893                     buf.append(',');
894                 }
895
896                 buf.append(defaultFeatures[i]);
897             }
898             return buf.toString();
899         }
900         
901         void setDefaultFeatures(String JavaDoc[] defaultFeatures) {
902             this.defaultFeatures=defaultFeatures;
903         }
904     }
905     
906 }
907
Popular Tags