1 19 20 package org.netbeans.modules.j2ee.refactoring.whereused; 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.Filter; 28 import org.netbeans.modules.j2ee.dd.api.web.Listener; 29 import org.netbeans.modules.j2ee.dd.api.web.Servlet; 30 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 31 import org.netbeans.modules.j2ee.dd.api.common.ServiceRef; 32 import org.netbeans.modules.j2ee.dd.api.common.VersionNotSupportedException; 33 import org.netbeans.modules.j2ee.dd.api.common.ServiceRefHandler; 34 import org.netbeans.jmi.javamodel.JavaClass; 35 import org.netbeans.jmi.javamodel.Resource; 36 import org.netbeans.modules.j2ee.refactoring.Utility; 37 import org.netbeans.modules.javacore.api.JavaModel; 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 50 public final class WebXmlWhereUsedRefactoring { 51 52 private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.whereused"); 54 public WebXmlWhereUsedRefactoring() { } 55 56 58 public Problem prepare(AbstractRefactoring refactoring, RefObject refObject, RefactoringElementsBag refactoringElements) { 59 60 Problem problem = null; 61 if (refObject instanceof JavaClass) { 62 63 JavaClass jClass = (JavaClass)refObject; 64 err.log("jClass: " + jClass); 65 66 String name = jClass.getName(); 67 err.log("name: " + name); 68 Resource res = jClass.getResource(); 69 err.log("res: " + res); 70 71 FileObject fo = JavaModel.getFileObject(res); 72 err.log("fo: " + fo); 73 74 Collection wmodules = Utility.getRelevantWebModules(fo); 75 Iterator wmIter = null; 76 77 if (wmodules != null) { 78 wmIter = wmodules.iterator(); 79 } 80 if (wmIter != null) { 81 while (wmIter.hasNext()) { 82 WebModule wm = (WebModule)wmIter.next(); 83 if (wm != null) { FileObject webXmlFO = wm.getDeploymentDescriptor(); 85 err.log("webxmlfo: " + webXmlFO); 86 WebApp webXmlDD = null; 87 try { 88 webXmlDD = org.netbeans.modules.j2ee.dd.api.web.DDProvider.getDefault().getDDRoot(webXmlFO); 89 } catch (IOException ioe) { 90 } 92 err.log("webXmlDD: " + webXmlDD); 93 if ((webXmlDD != null) && (webXmlDD.getStatus()!=WebApp.STATE_INVALID_UNPARSABLE)) { 94 95 Servlet[] servlets = webXmlDD.getServlet(); 97 if ((servlets!=null) && (servlets.length > 0)) { 98 for (int s=0; s < servlets.length; s++) { 99 Servlet servlet = servlets[s]; 100 err.log("Servlet: " + servlet); 101 String servletClass = servlet.getServletClass(); 102 err.log("Servlet class: " + servletClass); 103 if ((servletClass != null) && (servletClass.equals(name))) { 104 RefactoringElementImplementation elem = 105 new WebXmlServletWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 106 refactoringElements.add(refactoring, elem); 107 } 108 } 109 } 110 Listener[] listeners = webXmlDD.getListener(); 112 if ((listeners!=null) && (listeners.length > 0)) { 113 for (int s=0; s < listeners.length; s++) { 114 Listener listener = listeners[s]; 115 err.log("Listener: " + listener); 116 String listenerClass = listener.getListenerClass(); 117 err.log("Listener class: " + listenerClass); 118 if ((listenerClass != null) && (listenerClass.equals(name))) { 119 RefactoringElementImplementation elem = 120 new WebXmlListenerWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 121 refactoringElements.add(refactoring, elem); 122 } 123 } 124 } 125 126 Filter[] filters = webXmlDD.getFilter(); 128 if ((filters!=null) && (filters.length > 0)) { 129 for (int s=0; s < filters.length; s++) { 130 Filter filter = filters[s]; 131 err.log("Filter: " + filter); 132 String filterClass = filter.getFilterClass(); 133 err.log("Filter class: " + filterClass); 134 if ((filterClass != null) && (filterClass.equals(name))) { 135 RefactoringElementImplementation elem = 136 new WebXmlFilterWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 137 refactoringElements.add(refactoring, elem); 138 } 139 } 140 } 141 142 org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef(); 144 if ((refs != null) && (refs.length > 0)) { 145 for (int l=0; l < refs.length; l++) { 146 org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = refs[l]; 147 err.log("EJB ref: " + ref); 148 String refHome = ref.getHome(); 149 err.log("home ref: " + refHome); 150 if ((refHome != null) && (refHome.equals(name))) { 151 RefactoringElementImplementation elem = 152 new WebXmlRefHomeWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 153 refactoringElements.add(refactoring, elem); 154 } 155 String refRemote = ref.getRemote(); 156 err.log("remote ref: " + refRemote); 157 if ((refRemote != null) && (refRemote.equals(name))) { 158 RefactoringElementImplementation elem = 159 new WebXmlRefRemoteWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 160 refactoringElements.add(refactoring, elem); 161 } 162 } 163 } 164 165 org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] localRefs = webXmlDD.getEjbLocalRef(); 167 if ((localRefs != null) && (localRefs.length > 0)) { 168 for (int l=0; l < localRefs.length; l++) { 169 org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef localRef = localRefs[l]; 170 err.log("EJB local ref: " + localRef); 171 String refLocal = localRef.getLocal(); 172 err.log("local ref: " + refLocal); 173 if ((refLocal != null) && (refLocal.equals(name))) { 174 RefactoringElementImplementation elem = 175 new WebXmlRefLocalWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 176 refactoringElements.add(refactoring, elem); 177 } 178 String refLocalHome = localRef.getLocalHome(); 179 err.log("localHome ref: " + refLocalHome); 180 if ((refLocalHome != null) && (refLocalHome.equals(name))) { 181 RefactoringElementImplementation elem = 182 new WebXmlRefLocalHomeWhereUsedRefactoringElement(webXmlDD, name, webXmlFO); 183 refactoringElements.add(refactoring, elem); 184 } 185 } 186 } 187 ServiceRef[] serviceRef; 188 try { 189 serviceRef = webXmlDD.getServiceRef(); 190 } catch (VersionNotSupportedException e) { 191 serviceRef = null; 192 } 193 if (serviceRef != null) { 194 for (int i = 0; i < serviceRef.length; i++) { 195 ServiceRefHandler[] handlers = serviceRef[i].getHandler(); 196 for (int j = 0; j < handlers.length; j++) { 197 if (name.equals(handlers[j].getHandlerName())) { 198 RefactoringElementImplementation elem = 199 new WebXmlHandlerNameWhereUsedRefactoringElement(webXmlDD, name, 200 webXmlFO); 201 refactoringElements.add(refactoring, elem); 202 } 203 if (name.equals(handlers[j].getHandlerClass())) { 204 RefactoringElementImplementation elem = 205 new WebXmlHandlerClassWhereUsedRefactoringElement(webXmlDD, name, 206 webXmlFO); 207 refactoringElements.add(refactoring, elem); 208 } 209 } 210 } 211 } 212 } else { 213 Problem newProblem = new Problem(false, NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlWhereUsedInvalidProblem")); 214 problem = Utility.addProblemsToEnd(problem, newProblem); 215 } 216 } 217 } } 219 } return problem; 221 } 222 223 public final class WebXmlServletWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 224 225 protected WebApp webXmlDD; 226 227 228 public WebXmlServletWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 229 this.webXmlDD = webXmlDD; 230 this.name = name; 231 this.parentFile = parentFile; 232 } 233 234 237 public String getDisplayText() { 238 Object [] args = new Object [] {name}; 239 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlServletWhereUsed"), args); 240 } 241 } 242 243 public final class WebXmlFilterWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 244 245 protected WebApp webXmlDD; 246 247 248 public WebXmlFilterWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 249 this.webXmlDD = webXmlDD; 250 this.name = name; 251 this.parentFile = parentFile; 252 } 253 254 257 public String getDisplayText() { 258 Object [] args = new Object [] {name}; 259 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlFilterWhereUsed"), args); 260 } 261 } 262 263 public final class WebXmlListenerWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 264 265 protected WebApp webXmlDD; 266 267 268 public WebXmlListenerWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 269 this.webXmlDD = webXmlDD; 270 this.name = name; 271 this.parentFile = parentFile; 272 } 273 274 277 public String getDisplayText() { 278 Object [] args = new Object [] {name}; 279 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlListenerWhereUsed"), args); 280 } 281 } 282 283 public final class WebXmlRefHomeWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 284 285 protected WebApp webXmlDD; 286 287 288 public WebXmlRefHomeWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 289 this.webXmlDD = webXmlDD; 290 this.name = name; 291 this.parentFile = parentFile; 292 } 293 294 297 public String getDisplayText() { 298 Object [] args = new Object [] {name}; 299 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlRefHomeWhereUsed"), args); 300 } 301 } 302 303 public final class WebXmlRefRemoteWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 304 305 protected WebApp webXmlDD; 306 307 308 public WebXmlRefRemoteWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 309 this.webXmlDD = webXmlDD; 310 this.name = name; 311 this.parentFile = parentFile; 312 } 313 314 317 public String getDisplayText() { 318 Object [] args = new Object [] {name}; 319 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlRefRemoteWhereUsed"), args); 320 } 321 } 322 323 public final class WebXmlRefLocalWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 324 325 protected WebApp webXmlDD; 326 327 328 public WebXmlRefLocalWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 329 this.webXmlDD = webXmlDD; 330 this.name = name; 331 this.parentFile = parentFile; 332 } 333 334 337 public String getDisplayText() { 338 Object [] args = new Object [] {name}; 339 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlRefLocalWhereUsed"), args); 340 } 341 } 342 343 public final class WebXmlHandlerClassWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 344 345 protected WebApp webXmlDD; 346 347 public WebXmlHandlerClassWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 348 this.webXmlDD = webXmlDD; 349 this.name = name; 350 this.parentFile = parentFile; 351 } 352 353 356 public String getDisplayText() { 357 return NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlHandlerClassWhereUsed", name); 358 } 359 } 360 361 public final class WebXmlHandlerNameWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 362 363 protected WebApp webXmlDD; 364 365 public WebXmlHandlerNameWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 366 this.webXmlDD = webXmlDD; 367 this.name = name; 368 this.parentFile = parentFile; 369 } 370 371 374 public String getDisplayText() { 375 return NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlHandlerNameWhereUsed", name); 376 } 377 } 378 379 public final class WebXmlRefLocalHomeWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement { 380 381 protected WebApp webXmlDD; 382 383 384 public WebXmlRefLocalHomeWhereUsedRefactoringElement(WebApp webXmlDD, String name, FileObject parentFile) { 385 this.webXmlDD = webXmlDD; 386 this.name = name; 387 this.parentFile = parentFile; 388 } 389 390 393 public String getDisplayText() { 394 Object [] args = new Object [] {name}; 395 return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlRefLocalHomeWhereUsed"), args); 396 } 397 } 398 399 } 400 | Popular Tags |