1 19 20 package org.netbeans.modules.j2ee.refactoring.safedelete; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import org.netbeans.modules.j2ee.dd.api.web.Filter; 28 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping; 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.ServletMapping; 32 import org.netbeans.modules.j2ee.dd.api.web.WebApp; 33 import org.netbeans.jmi.javamodel.JavaClass; 34 import org.netbeans.modules.j2ee.refactoring.DefaultPositionBoundsResolver; 35 import org.netbeans.modules.j2ee.refactoring.J2EERefactoring; 36 import org.netbeans.modules.j2ee.refactoring.Utility; 37 import org.netbeans.modules.javacore.internalapi.ExternalChange; 38 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 39 import org.netbeans.modules.refactoring.api.Problem; 40 import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring; 41 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag; 42 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation; 43 import org.netbeans.modules.web.api.webmodule.WebModule; 44 import org.openide.ErrorManager; 45 import org.openide.filesystems.FileObject; 46 import org.openide.loaders.DataObject; 47 import org.openide.loaders.DataObjectNotFoundException; 48 import org.openide.text.PositionBounds; 49 import org.openide.util.NbBundle; 50 51 57 public final class WebXmlSafeDeleteRefactoring implements J2EERefactoring { 58 59 private JavaClass javaClass; 60 private SafeDeleteRefactoring safeDeleteRefactoring; 61 62 public WebXmlSafeDeleteRefactoring(SafeDeleteRefactoring safeDeleteRefactoring, JavaClass javaClass) { 63 this.javaClass = javaClass; 64 this.safeDeleteRefactoring = safeDeleteRefactoring; 65 } 66 67 71 private List <FileObject> getDeploymentDescriptors(){ 72 Collection modules = Utility.getRelevantWebModules(javaClass); 73 74 List <FileObject> result = new ArrayList <FileObject>(); 75 for (Iterator it = modules.iterator(); it.hasNext();) { 76 WebModule each = (WebModule) it.next(); 77 FileObject webXmlFo = each.getDeploymentDescriptor(); 78 if (webXmlFo != null){ 79 result.add(webXmlFo); 80 } 81 } 82 return result; 83 } 84 85 88 private void processServlets(WebApp webXmlDD, FileObject webXmlFO, RefactoringElementsBag refactoringElements){ 89 Servlet[] servlets = webXmlDD.getServlet(); 90 91 for (int s=0; s < servlets.length; s++) { 92 final Servlet servlet = servlets[s]; 93 final String servletClass = servlet.getServletClass(); 94 95 if (servletClass == null || !servletClass.equals(javaClass.getName())) { 96 continue; 97 } 98 99 final List <ServletMapping> mappings = getServletMappings(webXmlDD, servlet); 100 101 RefactoringElementImplementation elem = new WebXmlSafeDeleteRefactoringElement(webXmlFO, webXmlDD,servletClass) { 102 protected void doChange(){ 103 webApp.removeServlet(servlet); 104 for (ServletMapping elem : mappings) { 105 webApp.removeServletMapping(elem); 106 } 107 } 108 protected void undoChange(){ 109 webApp.addServlet(servlet); 110 for (ServletMapping elem : mappings) { 111 webApp.addServletMapping(elem); 112 } 113 } 114 public String getDisplayText(){ 115 return NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlServletSafeDelete", servletClass); 116 } 117 }; 118 119 refactoringElements.add(safeDeleteRefactoring, elem); 120 } 121 122 } 123 124 128 private List <ServletMapping> getServletMappings(WebApp webApp, Servlet servlet){ 129 ServletMapping[] mappings = webApp.getServletMapping(); 130 List <ServletMapping> result = new ArrayList <ServletMapping>(); 131 132 for (int i = 0; i < mappings.length; i++) { 133 if (servlet.getServletName().equals(mappings[i].getServletName())){ 134 result.add(mappings[i]); 135 } 136 } 137 return result; 138 } 139 140 143 private void processFilters(WebApp webXmlDD, FileObject webXmlFO, RefactoringElementsBag refactoringElements){ 144 Filter[] filters = webXmlDD.getFilter(); 145 146 for (int i = 0; i < filters.length; i++) { 147 final Filter filter = filters[i]; 148 final String filterClass = filter.getFilterClass(); 149 150 if (filterClass == null || !filterClass.equals(javaClass.getName())) { 151 continue; 152 } 153 154 final List <FilterMapping> mappings = getFilterMappings(webXmlDD, filter); 155 156 RefactoringElementImplementation elem = new WebXmlSafeDeleteRefactoringElement(webXmlFO, webXmlDD, filterClass) { 157 protected void doChange() { 158 webApp.removeFilter(filter); 159 for (FilterMapping elem : mappings) { 160 webApp.removeFilterMapping(elem); 161 } 162 } 163 protected void undoChange() { 164 webApp.addFilter(filter); 165 for (FilterMapping elem : mappings) { 166 webApp.addFilterMapping(elem); 167 } 168 } 169 public String getDisplayText(){ 170 return NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlFilterSafeDelete", filterClass); 171 } 172 }; 173 174 refactoringElements.add(safeDeleteRefactoring, elem); 175 } 176 177 } 178 179 183 private List <FilterMapping> getFilterMappings(WebApp webApp, Filter filter){ 184 FilterMapping[] mappings = webApp.getFilterMapping(); 185 List <FilterMapping> result = new ArrayList <FilterMapping>(); 186 187 for (int i = 0; i < mappings.length; i++) { 188 if (filter.getFilterName().equals(mappings[i].getFilterName())){ 189 result.add(mappings[i]); 190 } 191 } 192 return result; 193 } 194 195 198 private void processListeners(WebApp webXmlDD, FileObject webXmlFO, RefactoringElementsBag refactoringElements){ 199 Listener[] listeners = webXmlDD.getListener(); 200 for (int i = 0; i < listeners.length; i++) { 201 final Listener listener = listeners[i]; 202 final String listenerClass = listener.getListenerClass(); 203 if (listenerClass == null || !listenerClass.equals(javaClass.getName())) { 204 continue; 205 } 206 207 RefactoringElementImplementation elem = new WebXmlSafeDeleteRefactoringElement(webXmlFO, webXmlDD, listenerClass) { 208 protected void doChange() { 209 webApp.removeListener(listener); 210 } 211 protected void undoChange() { 212 webApp.addListener(listener); 213 } 214 public String getDisplayText(){ 215 return NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlListenerSafeDelete", listenerClass); 216 } 217 }; 218 219 refactoringElements.add(safeDeleteRefactoring, elem); 220 } 221 222 } 223 224 public Problem prepare(RefactoringElementsBag refactoringElements) { 225 226 Problem problem = null; 227 228 List <FileObject> webXmlDDs = getDeploymentDescriptors(); 229 230 for (FileObject webXmlFO : webXmlDDs) { 231 WebApp webXmlDD = null; 232 233 try { 234 webXmlDD = org.netbeans.modules.j2ee.dd.api.web.DDProvider.getDefault().getDDRoot(webXmlFO); 235 } catch (IOException ioe) { 236 ErrorManager.getDefault().notify(ioe); 237 } 238 239 if (webXmlDD != null && webXmlDD.getStatus() != WebApp.STATE_INVALID_UNPARSABLE){ 240 processServlets(webXmlDD, webXmlFO, refactoringElements); 241 processFilters(webXmlDD, webXmlFO, refactoringElements); 242 processListeners(webXmlDD, webXmlFO, refactoringElements); 243 244 } else { 245 Problem newProblem = new Problem(false, NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlInvalidProblem", webXmlFO.getPath())); 246 problem = Utility.addProblemsToEnd(problem, newProblem); 247 } 248 249 } 250 251 return problem; 252 } 253 254 public Problem preCheck() { 255 return null; 256 } 257 258 public Problem checkParameters() { 259 return null; 260 } 261 262 public Problem fastCheckParameters() { 263 return null; 264 } 265 266 267 private abstract static class WebXmlSafeDeleteRefactoringElement extends AbstractSafeDeleteRefactoringElement implements ExternalChange{ 268 269 protected WebApp webApp; 270 private String className; 271 272 public WebXmlSafeDeleteRefactoringElement(FileObject parentFile, WebApp webApp, String className){ 273 super(parentFile); 274 this.webApp = webApp; 275 this.className = className; 276 } 277 278 public final void performChange() { 279 JavaMetamodel.getManager().registerExtChange(this); 280 } 281 282 285 protected abstract void doChange(); 286 287 290 protected abstract void undoChange(); 291 292 public final void performExternalChange() { 293 doChange(); 294 try{ 295 webApp.write(parentFile); 296 } catch (IOException ioe){ 297 ErrorManager.getDefault().notify(ioe); 298 } 299 } 300 301 public final void undoExternalChange() { 302 undoChange(); 303 try{ 304 webApp.write(parentFile); 305 } catch (IOException ioe){ 306 ErrorManager.getDefault().notify(ioe); 307 } 308 } 309 310 public PositionBounds getPosition() { 311 try { 312 return new DefaultPositionBoundsResolver(DataObject.find(getParentFile()), className).getPositionBounds(); 313 } catch (DataObjectNotFoundException ex) { 314 ErrorManager.getDefault().notify(ex); 315 } 316 return null; 317 } 318 319 } 320 } 321 | Popular Tags |