1 19 20 package org.netbeans.modules.j2ee.refactoring.rename; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import javax.jmi.reflect.RefObject; 30 import org.netbeans.api.project.FileOwnerQuery; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.jmi.javamodel.ClassDefinition; 34 import org.netbeans.jmi.javamodel.Method; 35 import org.netbeans.jmi.javamodel.UnresolvedClass; 36 import org.netbeans.modules.j2ee.dd.api.web.Filter; 37 import org.netbeans.modules.j2ee.dd.api.web.Listener; 38 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 39 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 40 import org.netbeans.jmi.javamodel.JavaClass; 41 import org.netbeans.jmi.javamodel.Resource; 42 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent; 43 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponentHandler; 44 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription; 45 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices; 46 import org.netbeans.modules.j2ee.refactoring.Utility; 47 import org.netbeans.modules.javacore.api.JavaModel; 48 import org.netbeans.modules.javacore.internalapi.ExternalChange; 49 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 50 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 51 import org.netbeans.modules.refactoring.api.Problem; 52 import org.netbeans.modules.refactoring.api.RefactoringSession; 53 import org.netbeans.modules.refactoring.api.RenameRefactoring; 54 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 55 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 56 import org.netbeans.modules.schema2beans.QName; 57 import org.netbeans.modules.web.api.webmodule.WebModule; 58 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport; 59 import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants; 60 import org.netbeans.modules.websvc.wsdl.config.api.Configuration; 61 import org.netbeans.spi.project.support.ant.AntProjectHelper; 62 import org.netbeans.spi.project.support.ant.EditableProperties; 63 import org.openide.ErrorManager; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileUtil; 66 import org.openide.util.NbBundle; 67 68 72 public final class WebservicesXmlRenameRefactoring { 73 74 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.rename"); 76 77 ArrayList otherRefactors = new ArrayList (); 78 79 public WebservicesXmlRenameRefactoring() { } 80 81 public Problem preCheck(RefObject refObject) { 82 83 Problem problem = null; 84 if (refObject instanceof Method) { 85 86 Method method = (Method)refObject; 87 Resource res = method.getResource(); 88 FileObject fo = JavaModel.getFileObject(res); 89 Collection wsmodules = Utility.getRelevantWSModules(fo); 90 Iterator wsIter = wsmodules.iterator(); 91 String methodName = method.getName(); 92 ClassDefinition classDef = method.getDeclaringClass(); 94 if ((wsmodules != null) && (wsmodules.size() > 0)) { while (wsIter.hasNext()) { 96 WebServicesSupport ws = (WebServicesSupport)wsIter.next(); 97 if (ws != null) { 98 FileObject webservicesXmlFO = ws.getWebservicesDD(); 99 Webservices webservicesXmlDD = null; 100 try { 101 webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO); 102 } catch (IOException ioe) { 103 } 105 106 if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) { 107 WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription(); 108 err.log("Web Services: " + wservices); if ((wservices != null) && (wservices.length > 0)) { 110 for (int w=0; w < wservices.length; w++) { 111 WebserviceDescription webService = (WebserviceDescription)wservices[w]; 112 err.log("Web Service: " + webService); 114 PortComponent[] ports = webService.getPortComponent(); 115 err.log("Ports: " + ports); 116 if ((ports != null) && (ports.length > 0)) { 117 for (int p=0; p < ports.length; p++) { 118 119 120 PortComponent portComponent = (PortComponent)ports[p]; 121 err.log("Port Component: " + portComponent); 123 128 129 String portName = portComponent.getPortComponentName(); 130 Project project = FileOwnerQuery.getOwner(webservicesXmlFO); 131 Map properties = ws.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 132 String propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX; 133 String propertyValue = (String )properties.get(propertyName); 134 135 if (propertyValue != null) { FileObject wsConfigXmlFO = null; 137 138 File f = new File (propertyValue); 139 if (f.isAbsolute()) { 140 wsConfigXmlFO = FileUtil.toFileObject(f); 141 } else { 142 wsConfigXmlFO = project.getProjectDirectory().getFileObject(propertyValue); 143 } 144 if (wsConfigXmlFO != null) { 145 Configuration wsConfigXmlDD = null; 146 try { 147 if (wsConfigXmlFO != null) { 148 wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO); 149 } 150 } catch (IOException ioe) { 151 } 153 if (wsConfigXmlDD != null) { 154 org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService(); 155 if (service != null) { 156 org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface(); 157 if ((interfaces != null) && (interfaces.length > 0)) { 158 for (int ii=0; ii < interfaces.length; ii++) { 159 org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii]; 160 String servant = iface.getServantName(); 161 String iname = iface.getName(); 162 163 if (classDef.getName().equals(iname)) { 164 165 JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(servant); 166 Method beanMethod = null; 167 if (!(jcl instanceof UnresolvedClass)) { 168 beanMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); err.log("beanmethod: " + beanMethod); } 171 172 RenameRefactoring renameBeanMethod = new RenameRefactoring(beanMethod); 173 Problem prob = renameBeanMethod.preCheck(); 174 problem = Utility.addProblemsToEnd(problem, prob); 175 otherRefactors.add(renameBeanMethod); 176 177 } else if (classDef.getName().equals(servant)) { 178 179 JavaClass jcl = (JavaClass)JavaModel.getDefaultExtent().getType().resolve(iname); 180 Method iMethod = null; 181 if (!(jcl instanceof UnresolvedClass)) { 182 iMethod = jcl.getMethod(methodName, Utility.getTypedParams(method.getParameters()), false); err.log("ifacemethod: " + iMethod); } 185 186 RenameRefactoring renameIfaceMethod = new RenameRefactoring(iMethod); 187 Problem prob = renameIfaceMethod.preCheck(); 188 problem = Utility.addProblemsToEnd(problem, prob); 189 otherRefactors.add(renameIfaceMethod); 190 } 191 } 192 } 193 } 194 } else { 195 Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesConfigXmlRenameInvalidProblem")); problem = Utility.addProblemsToEnd(problem, newProblem); 197 } 198 } 199 } 200 } 201 } 202 } 203 } 204 } 205 } 206 } } 208 } 210 err.log("other refactors: " + otherRefactors); return problem; 212 } 213 214 public Problem fastCheckParameters(RefObject refObject, String newName) { 215 Problem problem = null; 216 if (refObject instanceof Method) { 217 for (int i=0; i < otherRefactors.size(); i++) { 218 RenameRefactoring rr = (RenameRefactoring)otherRefactors.get(i); 219 rr.setNewName(newName); 220 Problem p = rr.fastCheckParameters(); 221 problem = Utility.addProblemsToEnd(problem, p); 222 } 223 } 224 return problem; 225 } 226 227 public Problem checkParameters(RefObject refObject, String newName) { 228 return fastCheckParameters(refObject, newName); 229 } 230 231 233 public Problem prepare(AbstractRefactoring refactor, RefObject refObject, String newName, RefactoringElementsBag refactoringElements) { 234 235 RefactoringSession session = refactoringElements.getSession(); 236 Problem problem = null; 237 if (refObject instanceof JavaClass) { 238 239 JavaClass jClass = (JavaClass)refObject; 240 241 String name = jClass.getName(); 242 err.log("name: " + name); 244 String newSimpleName = newName; 245 err.log("newSimpleName: " + newSimpleName); 247 newName = name.substring(0, name.lastIndexOf('.') + 1) + newName; 248 err.log("newName: " + newName); 250 Resource res = jClass.getResource(); 251 FileObject fo = JavaModel.getFileObject(res); 252 253 WebServicesSupport wsm = WebServicesSupport.getWebServicesSupport(fo); 254 if (wsm != null) { 256 FileObject webservicesXmlFO = wsm.getWebservicesDD(); 257 Webservices webservicesXmlDD = null; 258 try { 259 webservicesXmlDD = org.netbeans.modules.j2ee.dd.api.webservices.DDProvider.getDefault().getDDRoot(webservicesXmlFO); 260 } catch (IOException ioe) { 261 } 263 264 if ((webservicesXmlDD != null) && (webservicesXmlDD.getStatus()!=Webservices.STATE_INVALID_UNPARSABLE)) { 265 WebserviceDescription[] wservices = webservicesXmlDD.getWebserviceDescription(); 266 err.log("Web Services: " + wservices); if ((wservices != null) && (wservices.length > 0)) { 268 for (int w=0; w < wservices.length; w++) { 269 WebserviceDescription webService = (WebserviceDescription)wservices[w]; 270 err.log("Web Service: " + webService); 272 273 PortComponent[] ports = webService.getPortComponent(); 274 err.log("Ports: " + ports); 275 if ((ports != null) && (ports.length > 0)) { 276 for (int p=0; p < ports.length; p++) { 277 278 279 PortComponent portComponent = (PortComponent)ports[p]; 280 err.log("Port Component: " + portComponent); 282 PortComponentHandler[] handlers = portComponent.getHandler(); 284 for (int k = 0; k < handlers.length; k++) { 285 if (name.equals(handlers[k].getHandlerClass())){ 286 RefactoringElementImplementation elem = new WebservicesHandlerRenameRefactoringElement(webservicesXmlDD, name, newName, handlers[k], webservicesXmlFO); 287 refactoringElements.add(refactor, elem); 288 } 289 } 290 291 QName wsdlPort = portComponent.getWsdlPort(); 293 err.log("WSDL Port: " + wsdlPort); if (wsdlPort != null) { 295 String localPart = wsdlPort.getLocalPart(); 296 if (jClass.getSimpleName().concat("Port").equals(localPart)) { String newLocalPart = newSimpleName.concat("Port"); QName newPort = new QName(wsdlPort.getNamespaceURI(), newLocalPart, wsdlPort.getPrefix()); 299 RefactoringElementImplementation elem = 300 new WebservicesWsdlPortRenameRefactoringElement( 301 webservicesXmlDD, wsdlPort, newPort, portComponent, webservicesXmlFO); refactoringElements.add(refactor, elem); 303 } 304 } 305 306 String serviceEndpointInterface = portComponent.getServiceEndpointInterface(); 307 err.log("serviceEndpointInterface: " + serviceEndpointInterface); if ((serviceEndpointInterface != null) && (serviceEndpointInterface.equals(name))) { 309 RefactoringElementImplementation elem = new WebservicesSEIRenameRefactoringElement(webservicesXmlDD, name, newName, portComponent, webservicesXmlFO); 310 refactoringElements.add(refactor, elem); 311 } 312 317 String portName = portComponent.getPortComponentName(); 318 Project project = FileOwnerQuery.getOwner(webservicesXmlFO); 319 Map properties = wsm.getAntProjectHelper().getStandardPropertyEvaluator().getProperties(); 320 String propertyName = portName + WebServicesConstants.CONFIG_PROP_SUFFIX; 321 String propertyValue = (String )properties.get(propertyName); 322 if (propertyValue != null) { FileObject wsConfigXmlFO = null; 324 325 File f = new File (propertyValue); 326 if (f.isAbsolute()) { 327 wsConfigXmlFO = FileUtil.toFileObject(f); 328 } else { 329 wsConfigXmlFO = project.getProjectDirectory().getFileObject(propertyValue); 330 } 331 if (wsConfigXmlFO != null) { 332 Configuration wsConfigXmlDD = null; 333 try { 334 wsConfigXmlDD = org.netbeans.modules.websvc.wsdl.config.api.DDProvider.getDefault().getDDRoot(wsConfigXmlFO); 335 } catch (IOException ioe) { 336 } 338 if (wsConfigXmlDD != null) { 339 org.netbeans.modules.websvc.wsdl.config.api.Service service = wsConfigXmlDD.getService(); 340 if (service != null) { 341 org.netbeans.modules.websvc.wsdl.config.api.Interface[] interfaces = service.getInterface(); 342 if ((interfaces != null) && (interfaces.length > 0)) { 343 for (int ii=0; ii < interfaces.length; ii++) { 344 org.netbeans.modules.websvc.wsdl.config.api.Interface iface = interfaces[ii]; 345 String servant = iface.getServantName(); 346 String iname = iface.getName(); 347 if ((servant != null) && (servant.equals(name))) { 348 RefactoringElementImplementation elem = new WebservicesConfigXmlServantRenameRefactoringElement(wsConfigXmlDD, name, newName, iface, wsConfigXmlFO); 349 refactoringElements.add(refactor, elem); 350 } 351 if ((iname != null) && (iname.equals(name))) { 352 RefactoringElementImplementation elem = new WebservicesConfigXmlInterfaceRenameRefactoringElement(wsConfigXmlDD, name, newName, iface, wsConfigXmlFO); 353 refactoringElements.add(refactor, elem); 354 } 355 } 356 } 357 } 358 } else { 359 Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesConfigXmlRenameInvalidProblem")); problem = Utility.addProblemsToEnd(problem, newProblem); 361 } 362 } 363 } 364 } 365 } 366 } 367 } 368 } else if (webservicesXmlDD != null) { 369 Problem newProblem = new Problem(false, NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesXmlRenameInvalidProblem")); if (problem == null) { 371 problem = newProblem; 372 } else { 373 if (newProblem != null) problem.setNext(newProblem); 374 } 375 } 376 } } 379 if (refObject instanceof Method) { 380 for (int i=0; i < otherRefactors.size(); i++) { 381 RenameRefactoring rr = (RenameRefactoring)otherRefactors.get(i); 382 Problem p = rr.prepare(session); 383 problem = Utility.addProblemsToEnd(problem, p); 384 } 385 return problem; 386 } 387 388 return problem; 389 } 390 391 public final class WebservicesSEIRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 392 393 protected Webservices webservicesXmlDD; 394 private PortComponent portComponent; 395 396 397 public WebservicesSEIRenameRefactoringElement(Webservices webservicesXmlDD, String oldName, String newName, PortComponent portComponent, FileObject parentFile) { 398 this.webservicesXmlDD = webservicesXmlDD; 399 this.oldName = oldName; 400 this.newName = newName; 401 this.portComponent = portComponent; 402 this.parentFile = parentFile; 403 } 404 405 408 public String getDisplayText() { 409 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 410 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesXmlSEIRename"), args); } 412 413 415 public void performChange() { 416 JavaMetamodel.getManager().registerExtChange(this); 417 } 418 419 public void performExternalChange() { 420 portComponent.setServiceEndpointInterface(newName); 421 try { 422 webservicesXmlDD.write(parentFile); 423 } catch (IOException ioe) { 424 } 426 } 427 428 public void undoExternalChange() { 429 portComponent.setServiceEndpointInterface(oldName); 430 try { 431 webservicesXmlDD.write(parentFile); 432 } catch (IOException ioe) { 433 } 435 } 436 } 437 438 441 public final class WebservicesHandlerRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 442 443 protected Webservices webservicesXmlDD; 444 private PortComponentHandler handler; 445 446 public WebservicesHandlerRenameRefactoringElement(Webservices webservicesXmlDD, String oldName, String newName, PortComponentHandler handler, FileObject parentFile) { 447 this.webservicesXmlDD = webservicesXmlDD; 448 this.oldName = oldName; 449 this.newName = newName; 450 this.handler = handler; 451 this.parentFile = parentFile; 452 } 453 454 457 public String getDisplayText() { 458 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 459 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesXmlHandlerRename"), args); } 461 462 464 public void performChange() { 465 JavaMetamodel.getManager().registerExtChange(this); 466 } 467 468 public void performExternalChange() { 469 handler.setHandlerClass(newName); 470 write(); 471 } 472 473 public void undoExternalChange() { 474 handler.setHandlerClass(oldName); 475 write(); 476 } 477 478 private void write(){ 479 try { 480 webservicesXmlDD.write(parentFile); 481 } catch (IOException ioe) { 482 ErrorManager.getDefault().notify(ioe); 483 } 484 } 485 } 486 public final class WebservicesWsdlPortRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 487 488 protected Webservices webservicesXmlDD; 489 private PortComponent portComponent; 490 491 private QName oldPort; 492 private QName newPort; 493 494 495 public WebservicesWsdlPortRenameRefactoringElement(Webservices webservicesXmlDD, QName oldPort, QName newPort, PortComponent portComponent, FileObject parentFile) { 496 this.webservicesXmlDD = webservicesXmlDD; 497 this.oldPort = oldPort; 498 this.newPort = newPort; 499 this.portComponent = portComponent; 500 this.parentFile = parentFile; 501 } 502 503 506 public String getDisplayText() { 507 String oldStr = oldPort.getPrefix() + ":" + oldPort.getLocalPart(); String newStr = newPort.getPrefix() + ":" + newPort.getLocalPart(); 510 Object [] args = new Object [] {parentFile.getNameExt(), oldStr, newStr}; 511 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesXmlWsdlPortRename"), args); } 513 514 516 public void performChange() { 517 JavaMetamodel.getManager().registerExtChange(this); 518 } 519 520 public void performExternalChange() { 521 portComponent.setWsdlPort(newPort); 522 try { 523 webservicesXmlDD.write(parentFile); 524 } catch (IOException ioe) { 525 } 527 } 528 529 public void undoExternalChange() { 530 portComponent.setWsdlPort(oldPort); 531 try { 532 webservicesXmlDD.write(parentFile); 533 } catch (IOException ioe) { 534 } 536 } 537 } 538 539 public final class WebservicesConfigXmlServantRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 540 541 protected Configuration wsConfigXmlDD; 542 private org.netbeans.modules.websvc.wsdl.config.api.Interface iface; 543 544 545 public WebservicesConfigXmlServantRenameRefactoringElement(Configuration wsConfigXmlDD, String oldName, String newName, org.netbeans.modules.websvc.wsdl.config.api.Interface iface, FileObject parentFile) { 546 this.wsConfigXmlDD = wsConfigXmlDD; 547 this.oldName = oldName; 548 this.newName = newName; 549 this.iface = iface; 550 this.parentFile = parentFile; 551 } 552 553 556 public String getDisplayText() { 557 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 558 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesConfigXmlServantRename"), args); } 560 561 563 public void performChange() { 564 JavaMetamodel.getManager().registerExtChange(this); 565 } 566 567 public void performExternalChange() { 568 iface.setServantName(newName); 569 try { 570 wsConfigXmlDD.write(parentFile); 571 } catch (IOException ioe) { 572 } 574 } 575 576 public void undoExternalChange() { 577 iface.setServantName(oldName); 578 try { 579 wsConfigXmlDD.write(parentFile); 580 } catch (IOException ioe) { 581 } 583 } 584 } 585 586 public final class WebservicesConfigXmlInterfaceRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 587 588 protected Configuration wsConfigXmlDD; 589 private org.netbeans.modules.websvc.wsdl.config.api.Interface iface; 590 591 592 public WebservicesConfigXmlInterfaceRenameRefactoringElement(Configuration wsConfigXmlDD, String oldName, String newName, org.netbeans.modules.websvc.wsdl.config.api.Interface iface, FileObject parentFile) { 593 this.wsConfigXmlDD = wsConfigXmlDD; 594 this.oldName = oldName; 595 this.newName = newName; 596 this.iface = iface; 597 this.parentFile = parentFile; 598 } 599 600 603 public String getDisplayText() { 604 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 605 return MessageFormat.format(NbBundle.getMessage(WebservicesXmlRenameRefactoring.class, "TXT_WebservicesConfigXmlInterfaceRename"), args); } 607 608 610 public void performChange() { 611 JavaMetamodel.getManager().registerExtChange(this); 612 } 613 614 public void performExternalChange() { 615 iface.setName(newName); 616 try { 617 wsConfigXmlDD.write(parentFile); 618 } catch (IOException ioe) { 619 } 621 } 622 623 public void undoExternalChange() { 624 iface.setName(oldName); 625 try { 626 wsConfigXmlDD.write(parentFile); 627 } catch (IOException ioe) { 628 } 630 } 631 } 632 633 } 634 | Popular Tags |