KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > changeparam > WebservicesXmlChangeParamRefactoring


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.refactoring.changeparam;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31 import javax.jmi.reflect.RefObject;
32 import org.netbeans.api.project.FileOwnerQuery;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.jmi.javamodel.ClassDefinition;
35 import org.netbeans.jmi.javamodel.JavaClass;
36 import org.netbeans.jmi.javamodel.Method;
37 import org.netbeans.jmi.javamodel.Resource;
38 import org.netbeans.jmi.javamodel.Type;
39 import org.netbeans.jmi.javamodel.UnresolvedClass;
40 import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
41 import org.netbeans.modules.j2ee.common.JMIUtils;
42 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
43 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
44 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
45 import org.netbeans.modules.j2ee.refactoring.Utility;
46 import org.netbeans.modules.javacore.api.JavaModel;
47 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
48 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring;
49 import org.netbeans.modules.refactoring.api.ChangeParametersRefactoring.ParameterInfo;
50 import org.netbeans.modules.refactoring.api.Problem;
51 import org.netbeans.modules.refactoring.api.RefactoringSession;
52 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
53 import org.netbeans.modules.web.api.webmodule.WebModule;
54 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
55 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
56 import org.netbeans.modules.websvc.wsdl.config.api.Configuration;
57 import org.netbeans.modules.websvc.wsdl.config.api.Wsdl;
58 import org.openide.ErrorManager;
59 import org.openide.filesystems.FileObject;
60 import org.openide.filesystems.FileUtil;
61 import org.openide.util.NbBundle;
62 import org.w3c.dom.Document JavaDoc;
63 import org.w3c.dom.Element JavaDoc;
64 import org.w3c.dom.Node JavaDoc;
65 import org.w3c.dom.NodeList JavaDoc;
66 import org.xml.sax.Attributes JavaDoc;
67 import org.xml.sax.InputSource JavaDoc;
68 import org.xml.sax.SAXException JavaDoc;
69 import org.xml.sax.XMLReader JavaDoc;
70
71 /**
72  *
73  * @author Martin Grebac
74  */

75 public final class WebservicesXmlChangeParamRefactoring {
76     
77     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.changeparam"); // NOI18N
78

79     /* stores collection of other refactorings to be invoked */
80     ArrayList JavaDoc otherRefactors = new ArrayList JavaDoc();
81     
82     /** Stores resolved class - filled in preCheck */
83     private JavaClass javaClass = null;
84     /** Stores collection of potentialy affected modules by proposed change - value filled in preCheck*/
85     private Collection JavaDoc emodules = null;
86     
87     public WebservicesXmlChangeParamRefactoring() { }
88     
89     public Problem preCheck(RefObject refObject) {
90         Problem problem = null;
91         if (refObject instanceof Method) {
92
93             Method method = (Method)refObject;
94             Resource res = method.getResource();
95             FileObject fo = JavaModel.getFileObject(res);
96             Collection JavaDoc wsmodules = Utility.getRelevantWSModules(fo);
97             Iterator JavaDoc wsIter = wsmodules.iterator();
98             String JavaDoc methodName = method.getName();
99             ClassDefinition classDef = method.getDeclaringClass(); // get affected class
100

101             if ((wsmodules != null) && (wsmodules.size() > 0)) { // there are web/ejb modules that can be affected by the change
102
while (wsIter.hasNext()) {
103                     WebServicesSupport ws = (WebServicesSupport)wsIter.next();
104                     if (ws != null) {
105                         FileObject webservicesXmlFO = ws.getWebservicesDD();
106                         Webservices webservicesXmlDD = null;
107                         try {
108                             webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO);
109                         } catch (IOException JavaDoc ioe) {
110                             //ignore
111
}
112
113                         if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) {
114                             WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription();
115                             err.log("Web Services: " + wservices);
116                             if ((wservices != null) && (wservices.length > 0)) {
117                                 for (int w=0; w < wservices.length; w++) {
118                                     WebserviceDescription webService = (WebserviceDescription)wservices[w];
119                                     err.log("Web Service: " + webService);
120                                     
121                                     Project project = FileOwnerQuery.getOwner(webservicesXmlFO);
122                                     if (isFromWSDL(ws, wservices[w].getWebserviceDescriptionName())) {
123                                         String JavaDoc jaxRpcMappingFileStr = webService.getJaxrpcMappingFile();
124                                         WebModule wmd = WebModule.getWebModule(project.getProjectDirectory());
125                                         EjbJar ejbJar = EjbJar.getEjbJar(project.getProjectDirectory());
126                                         
127                                         FileObject jaxRpcMappingFile = null;
128                                         if (wmd != null) {
129                                             jaxRpcMappingFile = wmd.getDocumentBase().getFileObject(jaxRpcMappingFileStr);
130                                         } else if (ejbJar != null) {
131                                             // TODO: searching should be probably enhanced
132
jaxRpcMappingFileStr = jaxRpcMappingFileStr.substring(jaxRpcMappingFileStr.lastIndexOf("/"));
133                                             jaxRpcMappingFile = ejbJar.getMetaInf().getFileObject(jaxRpcMappingFileStr);
134                                         }
135                                         if (jaxRpcMappingFile != null) {
136                                             String JavaDoc serviceEndpointInterface = getServiceEndpointInterface(jaxRpcMappingFile);
137                                             JavaClass serviceEndpointInterfaceClass = JMIUtils.findClass(serviceEndpointInterface);
138                                             if ((serviceEndpointInterface != null) && (classDef.isSubTypeOf(serviceEndpointInterfaceClass))) {
139                                                 String JavaDoc msg = NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebSvcFromWSDLWarningChangeParam");
140                                                 Problem newProblem = new Problem(true, msg);
141                                                 problem = Utility.addProblemsToEnd(problem, newProblem);
142                                                 return problem;
143                                             }
144                                         }
145                                     }
146                                             
147                                     PortComponent[] ports = webService.getPortComponent();
148                                     err.log("Ports: " + ports);
149                                     if ((ports != null) && (ports.length > 0)) {
150                                         for (int p=0; p < ports.length; p++) {
151                                             
152                                             /* First check for presence in webservices.xml port */
153                                             PortComponent portComponent = (PortComponent)ports[p];
154                                             err.log("Port Component: " + portComponent);
155                                             
156                                             /* try to locate the local wscompile config file:
157                                              * get project for webservices.xml, get name of the webservice and try to find properties of the type
158                                              * $propertyname-config.name - if found, get the property valu and the path and locate the descriptor,
159                                              * then get the dd api descriptor object
160                                              */

161                                             String JavaDoc portName = portComponent.getPortComponentName();
162                                             Map JavaDoc properties = ws.getAntProjectHelper().getStandardPropertyEvaluator().getProperties();
163                                             String JavaDoc propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX;
164                                             String JavaDoc propertyValue = (String JavaDoc)properties.get(propertyName);
165                                             
166                                             if (propertyValue != null) { // there's such property in the project
167
FileObject wsConfigXmlFO = null;
168                                                 
169                                                 File JavaDoc f = new File JavaDoc(propertyValue);
170                                                 if (f.isAbsolute()) {
171                                                     wsConfigXmlFO = FileUtil.toFileObject(f);
172                                                 } else {
173                                                     wsConfigXmlFO = project.getProjectDirectory().getFileObject(propertyValue);
174                                                 }
175                                                 if (wsConfigXmlFO != null) {
176                                                     Configuration wsConfigXmlDD = null;
177                                                     try {
178                                                         wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO);
179                                                     } catch (IOException JavaDoc ioe) {
180                                                         //ignore
181
}
182                                                     if (wsConfigXmlDD != null) {
183                                                         org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService();
184                                                         if (service != null) {
185                                                             org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface();
186                                                             if ((interfaces != null) && (interfaces.length > 0)) {
187                                                                 for (int ii=0; ii < interfaces.length; ii++) {
188                                                                     org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii];
189                                                                     String JavaDoc servant = iface.getServantName();
190                                                                     String JavaDoc iname = iface.getName();
191
192                                                                     if (classDef.getName().equals(iname)) {
193
194                                                                         JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(servant);
195                                                                         Method beanMethod = null;
196                                                                         if (!(jcl instanceof UnresolvedClass)) {
197                                                                             beanMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); //NOI18N
198
err.log("beanmethod: " + beanMethod);
199                                                                         }
200                                                                         
201                                                                         if (!isFromWSDL(ws, service.getName())) {
202                                                                             ChangeParametersRefactoring changeParamBeanMethod = new ChangeParametersRefactoring(beanMethod);
203                                                                             Problem prob = changeParamBeanMethod.preCheck();
204                                                                             problem = Utility.addProblemsToEnd(problem, prob);
205                                                                             otherRefactors.add(changeParamBeanMethod);
206                                                                         } else {
207                                                                             String JavaDoc msg = NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebSvcFromWSDLWarningChangeParam");
208                                                                             Problem newProblem = new Problem(true, msg);
209                                                                             problem = Utility.addProblemsToEnd(problem, newProblem);
210                                                                             return problem;
211                                                                         }
212
213                                                                     } else if (classDef.getName().equals(servant)) {
214
215                                                                         JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(iname);
216                                                                         Method iMethod = null;
217                                                                         if (!(jcl instanceof UnresolvedClass)) {
218                                                                             iMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); //NOI18N
219
err.log("ifacemethod: " + iMethod);
220                                                                         }
221
222                                                                         if (!isFromWSDL(ws, service.getName())) {
223                                                                             ChangeParametersRefactoring changeParamIfaceMethod = new ChangeParametersRefactoring(iMethod);
224                                                                             Problem prob = changeParamIfaceMethod.preCheck();
225                                                                             problem = Utility.addProblemsToEnd(problem, prob);
226                                                                             String JavaDoc msg = NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebSvcBeanMethodWarningChangeParam");
227                                                                             Problem newProblem = new Problem(false, msg);
228                                                                             problem = Utility.addProblemsToEnd(problem, newProblem);
229                                                                             otherRefactors.add(changeParamIfaceMethod);
230                                                                         } else {
231                                                                             String JavaDoc msg = NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebSvcFromWSDLWarningChangeParam");
232                                                                             Problem newProblem = new Problem(true, msg);
233                                                                             problem = Utility.addProblemsToEnd(problem, newProblem);
234                                                                             return problem;
235                                                                         }
236                                                                     }
237
238                                                                 }
239                                                             }
240                                                         } else {
241                                                             // check if the method is in this interface
242
String JavaDoc sei = portComponent.getServiceEndpointInterface();
243                                                             JavaClass seiClass = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(sei);
244                                                             JavaClass jc = (JavaClass)method.getDeclaringClass();
245
246                                                             if (jc.equals(seiClass)) {
247                                                                 Wsdl wsdl = wsConfigXmlDD.getWsdl();
248                                                                 String JavaDoc msg = NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebSvcFromWSDLWarningChangeParam");
249                                                                 Problem newProblem = new Problem(true, msg);
250                                                                 problem = Utility.addProblemsToEnd(problem, newProblem);
251                                                                 return problem;
252                                                             }
253                                                         }
254                                                     } else {
255                                                         Object JavaDoc[] args = new Object JavaDoc [] {wsConfigXmlFO.getNameExt()};
256                                                         String JavaDoc msg = MessageFormat.format(NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebservicesConfigXmlChangeParamInvalidProblem"), args); //NOI18N
257
Problem newProblem = new Problem(false, msg);
258                                                         problem = Utility.addProblemsToEnd(problem, newProblem);
259                                                         return problem;
260                                                     }
261                                                 }
262                                             }
263                                         }
264                                     }
265                                 }
266                             }
267                         }
268                     }
269                 } //while
270
}
271             
272         }
273         return problem;
274     }
275
276     public boolean isFromWSDL(WebServicesSupport ws, String JavaDoc serviceName) {
277         Element JavaDoc data = ws.getAntProjectHelper().getPrimaryConfigurationData(true);
278         Document JavaDoc doc = data.getOwnerDocument();
279         NodeList JavaDoc nodes = data.getElementsByTagName(WebServicesConstants.WEB_SERVICES); //NOI18N
280
Element JavaDoc webservices = null;
281         Element JavaDoc wsNameNode = null;
282         if(nodes.getLength() == 1){
283             webservices = (Element JavaDoc)nodes.item(0);
284             NodeList JavaDoc wsNodes = webservices.getElementsByTagName(WebServicesConstants.WEB_SERVICE); //NOI18N
285
for(int j = 0; j < wsNodes.getLength(); j++) {
286                 Element JavaDoc wsNode = (Element JavaDoc)wsNodes.item(j);
287                 NodeList JavaDoc wsNameNodes = wsNode.getElementsByTagName(WebServicesConstants.WEB_SERVICE_NAME); //NOI18N
288
if(wsNameNodes.getLength() == 1) {
289                     wsNameNode = (Element JavaDoc)wsNameNodes.item(0);
290                     NodeList JavaDoc nl = wsNameNode.getChildNodes();
291                     if(nl.getLength() == 1) {
292                         Node JavaDoc n = nl.item(0);
293                         if(n.getNodeType() == Node.TEXT_NODE) {
294                             if(serviceName.equals(n.getNodeValue())) {
295                                 NodeList JavaDoc fromWSDLNodes = wsNode.getElementsByTagName(WebServicesConstants.WEB_SERVICE_FROM_WSDL); //NOI18N
296
if(fromWSDLNodes.getLength() == 1) {
297                                     return true;
298                                 }
299                             }
300                         }
301                     }
302                 }
303             }
304         }
305         return false;
306     }
307
308     private String JavaDoc getServiceEndpointInterface(FileObject fo) {
309         javax.xml.parsers.SAXParserFactory JavaDoc fact = javax.xml.parsers.SAXParserFactory.newInstance();
310         fact.setValidating(false);
311         ServiceHandler sh = new ServiceHandler();
312         try {
313             javax.xml.parsers.SAXParser JavaDoc parser = fact.newSAXParser();
314             XMLReader JavaDoc reader = parser.getXMLReader();
315             reader.setContentHandler(sh);
316             FileInputStream JavaDoc is = new FileInputStream JavaDoc(FileUtil.toFile(fo));
317             try {
318                 if (is != null) {
319                     reader.parse(new InputSource JavaDoc(is));
320                 }
321             } catch (SAXException JavaDoc ex) {
322                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
323             } catch (IOException JavaDoc ex) {
324                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
325             } finally {
326                 try{
327                     is.close();
328                 } catch (IOException JavaDoc ex) {
329                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
330                 }
331             }
332         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
333             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
334         } catch (SAXException JavaDoc ex) {
335             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
336         } catch (FileNotFoundException JavaDoc ex) {
337             ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage());
338         }
339         if (sh != null) {
340             return sh.getServiceName();
341         }
342         return null;
343     }
344
345     private class ServiceHandler extends org.xml.sax.helpers.DefaultHandler JavaDoc {
346         
347         private boolean inServiceInterfaceMapping = false;
348         private boolean inServiceInterface = false;
349         
350         private String JavaDoc serviceName = null;
351         
352         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName, Attributes JavaDoc atts) throws SAXException JavaDoc {
353             if ("service-endpoint-interface-mapping".equals(rawName)) { //NOI18N
354
inServiceInterfaceMapping = true;
355             }
356             if ("service-endpoint-interface".equals(rawName)) { //NOI18N
357
inServiceInterface = true;
358             }
359         }
360
361         public void characters(char[] ch, int start, int length) throws SAXException JavaDoc{
362             if (inServiceInterfaceMapping && inServiceInterface) {
363                 String JavaDoc text = (new String JavaDoc(ch, start, length)).trim();
364                 if (text != null && text.length() > 0) {
365                     serviceName = text;
366                     inServiceInterface = false;
367                     inServiceInterfaceMapping = false;
368                 }
369             }
370         }
371         
372         public String JavaDoc getServiceName() {
373             return serviceName;
374         }
375
376         public void setServiceName(String JavaDoc serviceName) {
377             this.serviceName = serviceName;
378         }
379     }
380     
381     public Problem fastCheckParameters(RefObject refObject) {
382         Problem problem = null;
383 // if ((javaClass != null) && !(javaClass instanceof UnresolvedClass)) {
384
// err.log("resolved class: " + javaClass);
385
//
386
// }
387
return problem;
388     }
389
390     public Problem checkParameters(RefObject refObject) {
391         return fastCheckParameters(refObject);
392     }
393     
394     public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, ParameterInfo[] paramInfo, int modifier, RefactoringElementsBag refactoringElements) {
395         RefactoringSession session = refactoringElements.getSession();
396         Problem problem = null;
397         
398         if (refObject instanceof Method) {
399         
400             for (int i=0; i < otherRefactors.size(); i++) {
401                 ChangeParametersRefactoring chr = (ChangeParametersRefactoring)otherRefactors.get(i);
402                 chr.setModifiers(modifier);
403                 chr.setParameterInfo(paramInfo);
404                 Problem p = chr.prepare(session);
405                 problem = Utility.addProblemsToEnd(problem, p);
406             }
407         }
408         
409         return problem;
410     }
411     
412     /** Checks if the parameters changed (name of parameter is not checked, as it's not reflected */
413     private static boolean isChanged(Method method, ParameterInfo[] paramInfo) {
414         if (paramInfo.length < method.getParameters().size()) { // parameter was removed
415
return true;
416         }
417         if ((paramInfo != null) && (paramInfo.length > 0)) {
418             for (int i=0; i<paramInfo.length; i++) {
419                 ParameterInfo pi = paramInfo[i];
420                 if ((pi.getDefaultValue() != null) ||
421                     (pi.getOriginalIndex() != i) ||
422                     (pi.getType() != null)
423                    ) {
424                     return true;
425                 }
426             }
427         }
428         return false;
429     }
430     
431     private static String JavaDoc[] getNewParameters(ParameterInfo[] paramInfo, String JavaDoc[] methodParams) {
432         String JavaDoc[] newParams = new String JavaDoc[paramInfo.length];
433         if ((paramInfo != null) && (paramInfo.length > 0)) {
434             for (int i=0; i<paramInfo.length; i++) {
435                 ParameterInfo pi = paramInfo[i];
436                 Type newParamType = pi.getType();
437                 if (newParamType == null) {
438                     newParams[i] = methodParams[pi.getOriginalIndex()];
439                 } else {
440                     newParams[i] = newParamType.getName();
441                 }
442             }
443         }
444         return newParams;
445     }
446 }
447
Popular Tags