KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > safedelete > WebXmlSafeDeleteRefactoring


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.refactoring.safedelete;
21
22 import java.io.IOException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
52  * Safe delete refactoring for web.xml. Handles removing of servlets, listeners,
53  * filters and their associated mappings.
54  *
55  * @author Erno Mononen
56  */

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     /**
68      * @return list containing DDs from web modules that are relevant
69      * to our <code>javaClass</class>.
70      */

71     private List JavaDoc<FileObject> getDeploymentDescriptors(){
72         Collection JavaDoc modules = Utility.getRelevantWebModules(javaClass);
73         
74         List JavaDoc<FileObject> result = new ArrayList JavaDoc<FileObject>();
75         for (Iterator JavaDoc 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     /**
86      * Handles removing of servlets and their associated servlet mappings.
87      */

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 JavaDoc servletClass = servlet.getServletClass();
94             
95             if (servletClass == null || !servletClass.equals(javaClass.getName())) {
96                 continue;
97             }
98             
99             final List JavaDoc<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 JavaDoc getDisplayText(){
115                     return NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlServletSafeDelete", servletClass);
116                 }
117             };
118             
119             refactoringElements.add(safeDeleteRefactoring, elem);
120         }
121         
122     }
123     
124     /**
125      * @return List of servlet mappings that are associated
126      * with the given <code>servlet</code>.
127      */

128     private List JavaDoc<ServletMapping> getServletMappings(WebApp webApp, Servlet servlet){
129         ServletMapping[] mappings = webApp.getServletMapping();
130         List JavaDoc<ServletMapping> result = new ArrayList JavaDoc<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     /**
141      * Handles removing of filters and their associated filter mappings.
142      */

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 JavaDoc filterClass = filter.getFilterClass();
149             
150             if (filterClass == null || !filterClass.equals(javaClass.getName())) {
151                 continue;
152             }
153             
154             final List JavaDoc<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 JavaDoc getDisplayText(){
170                     return NbBundle.getMessage(WebXmlSafeDeleteRefactoring.class, "TXT_WebXmlFilterSafeDelete", filterClass);
171                 }
172             };
173             
174             refactoringElements.add(safeDeleteRefactoring, elem);
175         }
176         
177     }
178     
179     /**
180      * @return list of filter mappings that are associated with
181      * the given <code>filter</code>.
182      */

183     private List JavaDoc<FilterMapping> getFilterMappings(WebApp webApp, Filter filter){
184         FilterMapping[] mappings = webApp.getFilterMapping();
185         List JavaDoc<FilterMapping> result = new ArrayList JavaDoc<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     /**
196      * Handles removing of listeners.
197      */

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 JavaDoc 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 JavaDoc 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 JavaDoc<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 JavaDoc 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 JavaDoc className;
271         
272         public WebXmlSafeDeleteRefactoringElement(FileObject parentFile, WebApp webApp, String JavaDoc 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         /**
283          * Do the actual change.
284          */

285         protected abstract void doChange();
286         
287         /**
288          * Undo the change.
289          */

290         protected abstract void undoChange();
291         
292         public final void performExternalChange() {
293             doChange();
294             try{
295                 webApp.write(parentFile);
296             } catch (IOException JavaDoc 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 JavaDoc 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