KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > whereused > WebXmlWhereUsedRefactoring


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.whereused;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
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 /**
48  * @author Martin Grebac
49  */

50 public final class WebXmlWhereUsedRefactoring {
51     
52     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.whereused"); // NOI18N
53

54     public WebXmlWhereUsedRefactoring() { }
55
56     /** Find usages in web deployment descriptor: web.xml
57      */

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 JavaDoc 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 JavaDoc wmodules = Utility.getRelevantWebModules(fo);
75             Iterator JavaDoc 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) { // the class is in a web module
84
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 JavaDoc ioe) {
90                             //Ignore
91
}
92                         err.log("webXmlDD: " + webXmlDD);
93                         if ((webXmlDD != null) && (webXmlDD.getStatus()!=WebApp.STATE_INVALID_UNPARSABLE)) {
94
95                             //servlets
96
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 JavaDoc 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                             //listeners
111
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 JavaDoc 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                             //filters
127
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 JavaDoc 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                             // refs
143
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 JavaDoc 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 JavaDoc 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                             // local refs
166
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 JavaDoc 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 JavaDoc 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                 } // while wmIter
218
}
219         } // javaclass
220
return problem;
221     }
222     
223     public final class WebXmlServletWhereUsedRefactoringElement extends AbstractWhereUsedRefactoringElement {
224
225         protected WebApp webXmlDD;
226
227         /** Creates a new instance of WebXmlRefactoringElement */
228         public WebXmlServletWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
229             this.webXmlDD = webXmlDD;
230             this.name = name;
231             this.parentFile = parentFile;
232         }
233
234         /** Returns text describing the refactoring formatted for display (using HTML tags).
235          * @return Formatted text.
236          */

237         public String JavaDoc getDisplayText() {
238             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Creates a new instance of WebXmlRefactoringElement */
248         public WebXmlFilterWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
249             this.webXmlDD = webXmlDD;
250             this.name = name;
251             this.parentFile = parentFile;
252         }
253
254         /** Returns text describing the refactoring formatted for display (using HTML tags).
255          * @return Formatted text.
256          */

257         public String JavaDoc getDisplayText() {
258             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Creates a new instance of WebXmlRefactoringElement */
268         public WebXmlListenerWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
269             this.webXmlDD = webXmlDD;
270             this.name = name;
271             this.parentFile = parentFile;
272         }
273
274         /** Returns text describing the refactoring formatted for display (using HTML tags).
275          * @return Formatted text.
276          */

277         public String JavaDoc getDisplayText() {
278             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Creates a new instance of WebXmlRefHomeWhereUsedRefactoringElement */
288         public WebXmlRefHomeWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
289             this.webXmlDD = webXmlDD;
290             this.name = name;
291             this.parentFile = parentFile;
292         }
293
294         /** Returns text describing the refactoring formatted for display (using HTML tags).
295          * @return Formatted text.
296          */

297         public String JavaDoc getDisplayText() {
298             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Creates a new instance of WebXmlRefRemoteWhereUsedRefactoringElement */
308         public WebXmlRefRemoteWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
309             this.webXmlDD = webXmlDD;
310             this.name = name;
311             this.parentFile = parentFile;
312         }
313
314         /** Returns text describing the refactoring formatted for display (using HTML tags).
315          * @return Formatted text.
316          */

317         public String JavaDoc getDisplayText() {
318             Object JavaDoc[] args = new Object JavaDoc [] {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         /** Creates a new instance of WebXmlRefLocalWhereUsedRefactoringElement */
328         public WebXmlRefLocalWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
329             this.webXmlDD = webXmlDD;
330             this.name = name;
331             this.parentFile = parentFile;
332         }
333
334         /** Returns text describing the refactoring formatted for display (using HTML tags).
335          * @return Formatted text.
336          */

337         public String JavaDoc getDisplayText() {
338             Object JavaDoc[] args = new Object JavaDoc [] {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 JavaDoc name, FileObject parentFile) {
348             this.webXmlDD = webXmlDD;
349             this.name = name;
350             this.parentFile = parentFile;
351         }
352
353         /** Returns text describing the refactoring formatted for display (using HTML tags).
354          * @return Formatted text.
355          */

356         public String JavaDoc 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 JavaDoc name, FileObject parentFile) {
366             this.webXmlDD = webXmlDD;
367             this.name = name;
368             this.parentFile = parentFile;
369         }
370
371         /** Returns text describing the refactoring formatted for display (using HTML tags).
372          * @return Formatted text.
373          */

374         public String JavaDoc 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         /** Creates a new instance of WebXmlRefLocalHomeWhereUsedRefactoringElement */
384         public WebXmlRefLocalHomeWhereUsedRefactoringElement(WebApp webXmlDD, String JavaDoc name, FileObject parentFile) {
385             this.webXmlDD = webXmlDD;
386             this.name = name;
387             this.parentFile = parentFile;
388         }
389
390         /** Returns text describing the refactoring formatted for display (using HTML tags).
391          * @return Formatted text.
392          */

393         public String JavaDoc getDisplayText() {
394             Object JavaDoc[] args = new Object JavaDoc [] {name};
395             return MessageFormat.format(NbBundle.getMessage(WebXmlWhereUsedRefactoring.class, "TXT_WebXmlRefLocalHomeWhereUsed"), args);
396         }
397     }
398     
399 }
400
Popular Tags