1 19 20 package org.netbeans.modules.j2ee.refactoring.rename; 21 22 import java.io.IOException ; 23 import java.text.MessageFormat ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import javax.jmi.reflect.RefObject; 27 import org.netbeans.modules.j2ee.dd.api.web.DDProvider; 28 import org.netbeans.modules.j2ee.dd.api.web.Filter; 29 import org.netbeans.modules.j2ee.dd.api.web.Listener; 30 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 31 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 32 import org.netbeans.jmi.javamodel.JavaClass; 33 import org.netbeans.jmi.javamodel.Resource; 34 import org.netbeans.modules.j2ee.refactoring.Utility; 35 import org.netbeans.modules.javacore.api.JavaModel; 36 import org.netbeans.modules.javacore.internalapi.ExternalChange; 37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 38 import org.netbeans.modules.refactoring.api.AbstractRefactoring; 39 import org.netbeans.modules.refactoring.api.Problem; 40 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 41 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 42 import org.netbeans.modules.web.api.webmodule.WebModule; 43 import org.openide.ErrorManager; 44 import org.openide.filesystems.FileObject; 45 import org.openide.util.NbBundle; 46 47 51 public final class WebXmlRenameRefactoring { 52 53 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.rename"); 55 public WebXmlRenameRefactoring() { } 56 57 public Problem precheck() { 58 return null; 59 } 60 61 63 public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, String newName, RefactoringElementsBag refactoringElements) { 64 65 Problem problem = null; 66 if (refObject instanceof JavaClass) { 68 JavaClass jClass = (JavaClass)refObject; 69 70 String name = jClass.getName(); 71 err.log("name: " + name); 72 73 newName = name.substring(0, name.lastIndexOf('.') + 1) + newName; 74 err.log("newName: " + newName); 75 76 Resource res = jClass.getResource(); 77 FileObject fo = JavaModel.getFileObject(res); 78 Collection wmodules = Utility.getRelevantWebModules(fo); 79 Iterator wmIter = null; 80 81 if (wmodules != null) { 82 wmIter = wmodules.iterator(); 83 } 84 if (wmIter != null) { 85 while (wmIter.hasNext()) { 86 WebModule wm = (WebModule)wmIter.next(); 87 if (wm != null) { FileObject webXmlFO = wm.getDeploymentDescriptor(); 89 WebApp webXmlDD = null; 90 try { 91 webXmlDD = DDProvider.getDefault().getDDRoot(webXmlFO); 92 } catch (IOException ioe) { 93 } 95 if ((webXmlDD != null) && (webXmlDD.getStatus()!=WebApp.STATE_INVALID_UNPARSABLE)) { 96 Servlet[] servlets = webXmlDD.getServlet(); 98 if ((servlets!=null) && (servlets.length > 0)) { 99 for (int s=0; s < servlets.length; s++) { 100 Servlet servlet = (Servlet)servlets[s]; 101 err.log("Servlet: " + servlet); 102 String servletClass = servlet.getServletClass(); 103 err.log("Servlet class: " + servletClass); 104 if ((servletClass != null) && (servletClass.equals(name))) { 105 RefactoringElementImplementation elem = 106 new WebXmlServletRenameRefactoringElement(webXmlDD, name, newName, 107 servlet, webXmlFO 108 ); 109 refactoringElements.add(refactoring, elem); 110 String servletName = servlet.getServletName(); 112 String unqualifiedName = Utility.unqualify(name); 113 if (servletName != null && servletName.equals(unqualifiedName)) { 114 RefactoringElementImplementation nameElem = 115 new WebXmlServletNameRenameRefactoringElement(webXmlDD, unqualifiedName, Utility.unqualify(newName), 116 servlet, webXmlFO 117 ); 118 refactoringElements.add(refactoring, nameElem); 119 } 120 } 121 122 } 123 } 124 Listener[] listeners = webXmlDD.getListener(); 126 if ((listeners!=null) && (listeners.length > 0)) { 127 for (int s=0; s < listeners.length; s++) { 128 Listener listener = (Listener)listeners[s]; 129 err.log("Listener: " + listener); 130 String listenerClass = listener.getListenerClass(); 131 err.log("Listener class: " + listenerClass); 132 if ((listenerClass != null) && (listenerClass.equals(name))) { 133 RefactoringElementImplementation elem = 134 new WebXmlListenerRenameRefactoringElement(webXmlDD, name, newName, 135 listener, webXmlFO 136 ); 137 refactoringElements.add(refactoring, elem); 138 } 139 } 140 } 141 Filter[] filters = webXmlDD.getFilter(); 143 if ((filters!=null) && (filters.length > 0)) { 144 for (int s=0; s < filters.length; s++) { 145 Filter filter = (Filter)filters[s]; 146 err.log("Filter: " + filter); 147 String filterClass = filter.getFilterClass(); 148 err.log("Filter class: " + filterClass); 149 if ((filterClass != null) && (filterClass.equals(name))) { 150 RefactoringElementImplementation elem = 151 new WebXmlFilterRenameRefactoringElement(webXmlDD, name, newName, 152 filter, webXmlFO 153 ); 154 refactoringElements.add(refactoring, elem); 155 } 156 } 157 } 158 159 org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef(); 161 if ((refs != null) && (refs.length > 0)) { 162 for (int l=0; l < refs.length; l++) { 163 org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = (org.netbeans.modules.j2ee.dd.api.common.EjbRef)refs[l]; 164 err.log("EJB ref: " + ref); 165 String refHome = ref.getHome(); 166 err.log("home ref: " + refHome); 167 if ((refHome != null) && (refHome.equals(name))) { 168 RefactoringElementImplementation elem = 169 new WebXmlRefHomeRenameRefactoringElement(webXmlDD, name, newName, 170 (org.netbeans.modules.j2ee.dd.api.common.EjbRef)ref, 171 webXmlFO 172 ); 173 refactoringElements.add(refactoring, elem); 174 } 175 String refRemote = ref.getRemote(); 176 err.log("localHome ref: " + refRemote); 177 if ((refRemote != null) && (refRemote.equals(name))) { 178 RefactoringElementImplementation elem = 179 new WebXmlRefRemoteRenameRefactoringElement(webXmlDD, name, newName, 180 (org.netbeans.modules.j2ee.dd.api.common.EjbRef)ref, 181 webXmlFO 182 ); 183 refactoringElements.add(refactoring, elem); 184 } 185 } 186 } 187 188 org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] localRefs = webXmlDD.getEjbLocalRef(); 190 if ((localRefs != null) && (localRefs.length > 0)) { 191 for (int l=0; l < localRefs.length; l++) { 192 org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef localRef = (org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef)localRefs[l]; 193 err.log("EJB local ref: " + localRef); 194 String refLocal = localRef.getLocal(); 195 err.log("local ref: " + refLocal); 196 if ((refLocal != null) && (refLocal.equals(name))) { 197 RefactoringElementImplementation elem = 198 new WebXmlRefLocalRenameRefactoringElement(webXmlDD, name, newName, 199 (org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef)localRef, 200 webXmlFO 201 ); 202 refactoringElements.add(refactoring, elem); 203 } 204 String refLocalHome = localRef.getLocalHome(); 205 err.log("localHome ref: " + refLocalHome); 206 if ((refLocalHome != null) && (refLocalHome.equals(name))) { 207 RefactoringElementImplementation elem = 208 new WebXmlRefLocalHomeRenameRefactoringElement(webXmlDD, name, newName, 209 (org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef)localRef, 210 webXmlFO 211 ); 212 refactoringElements.add(refactoring, elem); 213 } 214 } 215 } 216 217 } else { 218 Problem newProblem = new Problem(false, NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlInvalidProblem")); 219 problem = Utility.addProblemsToEnd(problem, newProblem); 220 } 221 } 222 } } 224 } 225 return problem; 226 } 227 230 public final class WebXmlServletNameRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 231 232 protected WebApp webXmlDD; 233 private Servlet servlet; 234 235 236 public WebXmlServletNameRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, Servlet servlet, FileObject parentFile) { 237 this.webXmlDD = webXmlDD; 238 this.oldName = oldName; 239 this.newName = newName; 240 this.servlet = servlet; 241 this.parentFile = parentFile; 242 this.enabled = false; 244 } 245 246 249 public String getDisplayText() { 250 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 251 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlServletNameRename"), args); 252 } 253 254 public void performExternalChange() { 255 servlet.setServletName(newName); 256 try { 257 webXmlDD.write(parentFile); 258 } catch (IOException ioe) { 259 } 261 } 262 263 public void undoExternalChange() { 264 servlet.setServletName(oldName); 265 try { 266 webXmlDD.write(parentFile); 267 } catch (IOException ioe) { 268 } 270 } 271 272 274 public void performChange() { 275 JavaMetamodel.getManager().registerExtChange(this); 276 } 277 278 } 279 280 public final class WebXmlServletRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 281 282 protected WebApp webXmlDD; 283 private Servlet servlet; 284 285 286 public WebXmlServletRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, Servlet servlet, FileObject parentFile) { 287 this.webXmlDD = webXmlDD; 288 this.oldName = oldName; 289 this.newName = newName; 290 this.servlet = servlet; 291 this.parentFile = parentFile; 292 } 293 294 297 public String getDisplayText() { 298 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 299 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlServletRename"), args); 300 } 301 302 public void performExternalChange() { 303 servlet.setServletClass(newName); 304 try { 305 webXmlDD.write(parentFile); 306 } catch (IOException ioe) { 307 } 309 } 310 311 public void undoExternalChange() { 312 servlet.setServletClass(oldName); 313 try { 314 webXmlDD.write(parentFile); 315 } catch (IOException ioe) { 316 } 318 } 319 320 322 public void performChange() { 323 JavaMetamodel.getManager().registerExtChange(this); 324 } 325 326 } 327 328 public final class WebXmlListenerRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 329 330 protected WebApp webXmlDD; 331 private Listener listener; 332 333 334 public WebXmlListenerRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, Listener listener, FileObject parentFile) { 335 this.webXmlDD = webXmlDD; 336 this.oldName = oldName; 337 this.newName = newName; 338 this.listener = listener; 339 this.parentFile = parentFile; 340 } 341 342 345 public String getDisplayText() { 346 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 347 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlListenerRename"), args); 348 } 349 350 352 public void performChange() { 353 JavaMetamodel.getManager().registerExtChange(this); 354 } 355 356 public void performExternalChange() { 357 listener.setListenerClass(newName); 358 try { 359 webXmlDD.write(parentFile); 360 } catch (IOException ioe) { 361 } 363 } 364 365 public void undoExternalChange() { 366 listener.setListenerClass(oldName); 367 try { 368 webXmlDD.write(parentFile); 369 } catch (IOException ioe) { 370 } 372 } 373 } 374 375 public final class WebXmlFilterRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 376 377 protected WebApp webXmlDD; 378 private Filter filter; 379 380 381 public WebXmlFilterRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, Filter filter, FileObject parentFile) { 382 this.webXmlDD = webXmlDD; 383 this.oldName = oldName; 384 this.newName = newName; 385 this.filter = filter; 386 this.parentFile = parentFile; 387 } 388 389 392 public String getDisplayText() { 393 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 394 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlFilterRename"), args); 395 } 396 397 399 public void performChange() { 400 JavaMetamodel.getManager().registerExtChange(this); 401 } 402 403 public void performExternalChange() { 404 filter.setFilterClass(newName); 405 try { 406 webXmlDD.write(parentFile); 407 } catch (IOException ioe) { 408 } 410 } 411 412 public void undoExternalChange() { 413 filter.setFilterClass(oldName); 414 try { 415 webXmlDD.write(parentFile); 416 } catch (IOException ioe) { 417 } 419 } 420 } 421 422 public final class WebXmlRefHomeRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 423 424 protected WebApp webXmlDD; 425 private org.netbeans.modules.j2ee.dd.api.common.EjbRef ref; 426 427 428 public WebXmlRefHomeRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, org.netbeans.modules.j2ee.dd.api.common.EjbRef ref, FileObject parentFile) { 429 this.webXmlDD = webXmlDD; 430 this.oldName = oldName; 431 this.newName = newName; 432 this.ref = ref; 433 this.parentFile = parentFile; 434 } 435 436 439 public String getDisplayText() { 440 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 441 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlRefHomeRename"), args); 442 } 443 444 446 public void performChange() { 447 JavaMetamodel.getManager().registerExtChange(this); 448 } 449 450 public void performExternalChange() { 451 ref.setHome(newName); 452 try { 453 webXmlDD.write(parentFile); 454 } catch (IOException ioe) { 455 } 457 } 458 459 public void undoExternalChange() { 460 ref.setHome(oldName); 461 try { 462 webXmlDD.write(parentFile); 463 } catch (IOException ioe) { 464 } 466 } 467 } 468 469 public final class WebXmlRefRemoteRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 470 471 protected WebApp webXmlDD; 472 private org.netbeans.modules.j2ee.dd.api.common.EjbRef ref; 473 474 475 public WebXmlRefRemoteRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, org.netbeans.modules.j2ee.dd.api.common.EjbRef ref, FileObject parentFile) { 476 this.webXmlDD = webXmlDD; 477 this.oldName = oldName; 478 this.newName = newName; 479 this.ref = ref; 480 this.parentFile = parentFile; 481 } 482 483 486 public String getDisplayText() { 487 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 488 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlRefRemoteRename"), args); 489 } 490 491 493 public void performChange() { 494 JavaMetamodel.getManager().registerExtChange(this); 495 } 496 497 public void performExternalChange() { 498 ref.setRemote(newName); 499 try { 500 webXmlDD.write(parentFile); 501 } catch (IOException ioe) { 502 } 504 } 505 506 public void undoExternalChange() { 507 ref.setRemote(oldName); 508 try { 509 webXmlDD.write(parentFile); 510 } catch (IOException ioe) { 511 } 513 } 514 } 515 516 public final class WebXmlRefLocalRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 517 518 protected WebApp webXmlDD; 519 private org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref; 520 521 522 public WebXmlRefLocalRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref, FileObject parentFile) { 523 this.webXmlDD = webXmlDD; 524 this.oldName = oldName; 525 this.newName = newName; 526 this.ref = ref; 527 this.parentFile = parentFile; 528 } 529 530 533 public String getDisplayText() { 534 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 535 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlRefLocalRename"), args); 536 } 537 538 540 public void performChange() { 541 JavaMetamodel.getManager().registerExtChange(this); 542 } 543 544 public void performExternalChange() { 545 ref.setLocal(newName); 546 try { 547 webXmlDD.write(parentFile); 548 } catch (IOException ioe) { 549 } 551 } 552 553 public void undoExternalChange() { 554 ref.setLocal(oldName); 555 try { 556 webXmlDD.write(parentFile); 557 } catch (IOException ioe) { 558 } 560 } 561 } 562 563 public final class WebXmlRefLocalHomeRenameRefactoringElement extends AbstractRenameRefactoringElement implements ExternalChange { 564 565 protected WebApp webXmlDD; 566 private org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref; 567 568 569 public WebXmlRefLocalHomeRenameRefactoringElement(WebApp webXmlDD, String oldName, String newName, org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref, FileObject parentFile) { 570 this.webXmlDD = webXmlDD; 571 this.oldName = oldName; 572 this.newName = newName; 573 this.ref = ref; 574 this.parentFile = parentFile; 575 } 576 577 580 public String getDisplayText() { 581 Object [] args = new Object [] {parentFile.getNameExt(), oldName, newName}; 582 return MessageFormat.format(NbBundle.getMessage(WebXmlRenameRefactoring.class, "TXT_WebXmlRefLocalHomeRename"), args); 583 } 584 585 587 public void performChange() { 588 JavaMetamodel.getManager().registerExtChange(this); 589 } 590 591 public void performExternalChange() { 592 ref.setLocalHome(newName); 593 try { 594 webXmlDD.write(parentFile); 595 } catch (IOException ioe) { 596 } 598 } 599 600 public void undoExternalChange() { 601 ref.setLocalHome(oldName); 602 try { 603 webXmlDD.write(parentFile); 604 } catch (IOException ioe) { 605 } 607 } 608 } 609 610 } 611 | Popular Tags |