1 19 20 package org.netbeans.modules.j2ee.refactoring.changeparam; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import java.util.Map ; 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 ; 63 import org.w3c.dom.Element ; 64 import org.w3c.dom.Node ; 65 import org.w3c.dom.NodeList ; 66 import org.xml.sax.Attributes ; 67 import org.xml.sax.InputSource ; 68 import org.xml.sax.SAXException ; 69 import org.xml.sax.XMLReader ; 70 71 75 public final class WebservicesXmlChangeParamRefactoring { 76 77 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.changeparam"); 79 80 ArrayList otherRefactors = new ArrayList (); 81 82 83 private JavaClass javaClass = null; 84 85 private Collection 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 wsmodules = Utility.getRelevantWSModules(fo); 97 Iterator wsIter = wsmodules.iterator(); 98 String methodName = method.getName(); 99 ClassDefinition classDef = method.getDeclaringClass(); 101 if ((wsmodules != null) && (wsmodules.size() > 0)) { 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 ioe) { 110 } 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 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 jaxRpcMappingFileStr = jaxRpcMappingFileStr.substring(jaxRpcMappingFileStr.lastIndexOf("/")); 133 jaxRpcMappingFile = ejbJar.getMetaInf().getFileObject(jaxRpcMappingFileStr); 134 } 135 if (jaxRpcMappingFile != null) { 136 String serviceEndpointInterface = getServiceEndpointInterface(jaxRpcMappingFile); 137 JavaClass serviceEndpointInterfaceClass = JMIUtils.findClass(serviceEndpointInterface); 138 if ((serviceEndpointInterface != null) && (classDef.isSubTypeOf(serviceEndpointInterfaceClass))) { 139 String 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 153 PortComponent portComponent = (PortComponent)ports[p]; 154 err.log("Port Component: " + portComponent); 155 156 161 String portName = portComponent.getPortComponentName(); 162 Map properties = ws.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 163 String propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX; 164 String propertyValue = (String )properties.get(propertyName); 165 166 if (propertyValue != null) { FileObject wsConfigXmlFO = null; 168 169 File f = new File (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 ioe) { 180 } 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 servant = iface.getServantName(); 190 String 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); 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 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); 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 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 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 String 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 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 [] args = new Object [] {wsConfigXmlFO.getNameExt()}; 256 String msg = MessageFormat.format(NbBundle.getMessage(WebservicesXmlChangeParamRefactoring.class, "TXT_WebservicesConfigXmlChangeParamInvalidProblem"), args); 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 } } 271 272 } 273 return problem; 274 } 275 276 public boolean isFromWSDL(WebServicesSupport ws, String serviceName) { 277 Element data = ws.getAntProjectHelper().getPrimaryConfigurationData(true); 278 Document doc = data.getOwnerDocument(); 279 NodeList nodes = data.getElementsByTagName(WebServicesConstants.WEB_SERVICES); Element webservices = null; 281 Element wsNameNode = null; 282 if(nodes.getLength() == 1){ 283 webservices = (Element )nodes.item(0); 284 NodeList wsNodes = webservices.getElementsByTagName(WebServicesConstants.WEB_SERVICE); for(int j = 0; j < wsNodes.getLength(); j++) { 286 Element wsNode = (Element )wsNodes.item(j); 287 NodeList wsNameNodes = wsNode.getElementsByTagName(WebServicesConstants.WEB_SERVICE_NAME); if(wsNameNodes.getLength() == 1) { 289 wsNameNode = (Element )wsNameNodes.item(0); 290 NodeList nl = wsNameNode.getChildNodes(); 291 if(nl.getLength() == 1) { 292 Node n = nl.item(0); 293 if(n.getNodeType() == Node.TEXT_NODE) { 294 if(serviceName.equals(n.getNodeValue())) { 295 NodeList fromWSDLNodes = wsNode.getElementsByTagName(WebServicesConstants.WEB_SERVICE_FROM_WSDL); if(fromWSDLNodes.getLength() == 1) { 297 return true; 298 } 299 } 300 } 301 } 302 } 303 } 304 } 305 return false; 306 } 307 308 private String getServiceEndpointInterface(FileObject fo) { 309 javax.xml.parsers.SAXParserFactory fact = javax.xml.parsers.SAXParserFactory.newInstance(); 310 fact.setValidating(false); 311 ServiceHandler sh = new ServiceHandler(); 312 try { 313 javax.xml.parsers.SAXParser parser = fact.newSAXParser(); 314 XMLReader reader = parser.getXMLReader(); 315 reader.setContentHandler(sh); 316 FileInputStream is = new FileInputStream (FileUtil.toFile(fo)); 317 try { 318 if (is != null) { 319 reader.parse(new InputSource (is)); 320 } 321 } catch (SAXException ex) { 322 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 323 } catch (IOException ex) { 324 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 325 } finally { 326 try{ 327 is.close(); 328 } catch (IOException ex) { 329 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 330 } 331 } 332 } catch (javax.xml.parsers.ParserConfigurationException ex) { 333 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 334 } catch (SAXException ex) { 335 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ex.getMessage()); 336 } catch (FileNotFoundException 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 { 346 347 private boolean inServiceInterfaceMapping = false; 348 private boolean inServiceInterface = false; 349 350 private String serviceName = null; 351 352 public void startElement(String uri, String localName, String rawName, Attributes atts) throws SAXException { 353 if ("service-endpoint-interface-mapping".equals(rawName)) { inServiceInterfaceMapping = true; 355 } 356 if ("service-endpoint-interface".equals(rawName)) { inServiceInterface = true; 358 } 359 } 360 361 public void characters(char[] ch, int start, int length) throws SAXException { 362 if (inServiceInterfaceMapping && inServiceInterface) { 363 String text = (new String (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 getServiceName() { 373 return serviceName; 374 } 375 376 public void setServiceName(String serviceName) { 377 this.serviceName = serviceName; 378 } 379 } 380 381 public Problem fastCheckParameters(RefObject refObject) { 382 Problem problem = null; 383 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 413 private static boolean isChanged(Method method, ParameterInfo[] paramInfo) { 414 if (paramInfo.length < method.getParameters().size()) { 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 [] getNewParameters(ParameterInfo[] paramInfo, String [] methodParams) { 432 String [] newParams = new String [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 |