KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > moveclass > WebXmlMoveClassRefactoring


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.moveclass;
21
22 import java.io.IOException JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.api.project.FileOwnerQuery;
29 import org.netbeans.api.project.Project;
30 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
31 import org.netbeans.modules.j2ee.dd.api.web.Filter;
32 import org.netbeans.modules.j2ee.dd.api.web.FilterMapping;
33 import org.netbeans.modules.j2ee.dd.api.web.Listener;
34 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
35 import org.netbeans.modules.j2ee.dd.api.web.ServletMapping;
36 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
37 import org.netbeans.jmi.javamodel.JavaClass;
38 import org.netbeans.jmi.javamodel.Resource;
39 import org.netbeans.modules.j2ee.refactoring.Utility;
40 import org.netbeans.modules.javacore.api.JavaModel;
41 import org.netbeans.modules.javacore.internalapi.ExternalChange;
42 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
43 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
44 import org.netbeans.modules.refactoring.api.Problem;
45 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
46 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
47 import org.netbeans.modules.web.api.webmodule.WebModule;
48 import org.openide.ErrorManager;
49 import org.openide.filesystems.FileObject;
50 import org.openide.util.NbBundle;
51
52 /**
53  *
54  * @author Martin Grebac
55  */

56 public final class WebXmlMoveClassRefactoring {
57     
58     private static final ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.rename"); // NOI18N
59

60     public WebXmlMoveClassRefactoring() { }
61
62     /** Find usages in web deployment descriptor: web.xml
63      */

64     public Problem prepare(MoveClassRefactoring moveClassRefactor, Collection JavaDoc resources, RefactoringElementsBag refactoringElements) {
65         Problem problem = null;
66             
67         for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
68             Resource resource = (Resource) i.next();
69             FileObject fo = JavaModel.getFileObject(resource);
70             
71             err.log("resource package name: " + resource.getPackageName());
72             err.log("resource: name " + resource.getName());
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                     
83                     WebModule wm = (WebModule)wmIter.next();
84                     WebModule wmNew = null;
85
86                     if (wm != null) { // resource is in a web module
87

88                         Project prj1 = FileOwnerQuery.getOwner(wm.getDeploymentDescriptor());
89                         Project prj2 = null;
90                         
91                         FileObject target = moveClassRefactor.getTargetClassPathRoot();
92                         if (target != null) {
93                             prj2 = FileOwnerQuery.getOwner(target);
94                         }
95                         
96                         boolean moveToAnotherProject = false;
97                         if (prj2 != null) {
98                             moveToAnotherProject = !prj1.equals(prj2);
99                         }
100                         
101                         if (moveToAnotherProject) {
102                             wmNew = WebModule.getWebModule(target);
103                         }
104                         
105                         if (moveToAnotherProject && wmNew == null) {
106                             continue;
107                         }
108                         
109                         FileObject webXmlFO = wm.getDeploymentDescriptor();
110                         FileObject newWebXmlFO = null;
111                         
112                         WebApp webXmlDD = null;
113                         WebApp newWebXmlDD = null;
114                         
115                         try {
116                             webXmlDD = DDProvider.getDefault().getDDRoot(webXmlFO);
117                             if (moveToAnotherProject) {
118                                 newWebXmlFO = wmNew.getDeploymentDescriptor();
119                                 newWebXmlDD = DDProvider.getDefault().getDDRoot(newWebXmlFO);
120                             }
121                         } catch (IOException JavaDoc ioe) {
122                             // ignore
123
}
124                         if ((webXmlDD != null) && (webXmlDD.getStatus() != WebApp.STATE_INVALID_UNPARSABLE)) {
125                             if (moveToAnotherProject && newWebXmlDD == null) {
126                                 Problem newProblem = new Problem(false, NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlMoveClassInvalidProblem"));
127                                 problem = Utility.addProblemsToEnd(problem, newProblem);
128                                 continue;
129                             }
130                             List JavaDoc classes = resource.getClassifiers();
131                             err.log("Classes in resource: " + classes);
132
133                             for (int j=0; j < classes.size(); j++ ) {
134                                 JavaClass jClass = (JavaClass)classes.get(j);
135                                 err.log("javaclass: "+ jClass);
136
137                                 String JavaDoc name = jClass.getName();
138                                 err.log("name: " + name);
139
140                                 String JavaDoc targetPackage = moveClassRefactor.getTargetPackageName(resource);
141                                 err.log("targetPackage: " + targetPackage);
142
143                                 String JavaDoc newName = targetPackage + "." + jClass.getSimpleName();
144                                 err.log("newname: " + newName);
145                                                                 
146                                 //servlets
147
Servlet[] servlets = webXmlDD.getServlet();
148                                 if ((servlets!=null) && (servlets.length > 0)) {
149                                     for (int s=0; s < servlets.length; s++) {
150                                         Servlet servlet = servlets[s];
151                                         err.log("Servlet: " + servlet);
152                                         String JavaDoc servletClass = servlet.getServletClass();
153                                         err.log("Servlet class: " + servletClass);
154                                         if ((servletClass != null) && (servletClass.equals(name))) {
155                                             if (!moveToAnotherProject) {
156                                                 RefactoringElementImplementation elem =
157                                                         new WebXmlServletMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
158                                                 refactoringElements.add(moveClassRefactor, elem);
159                                             } else {
160                                                 RefactoringElementImplementation elem =
161                                                         new WebXmlServletReferenceMoveClassOtherProjectRefactoringElement(webXmlDD, newWebXmlDD, name, newName, webXmlFO, newWebXmlFO);
162                                                 refactoringElements.add(moveClassRefactor, elem);
163                                             }
164                                         }
165                                     }
166                                 }
167
168                                 //listeners
169
Listener[] listeners = webXmlDD.getListener();
170                                 if ((listeners!=null) && (listeners.length > 0)) {
171                                     for (int s=0; s < listeners.length; s++) {
172                                         Listener listener = listeners[s];
173                                         err.log("Listener: " + listener);
174                                         String JavaDoc listenerClass = listener.getListenerClass();
175                                         err.log("Listener class: " + listenerClass);
176                                         if ((listenerClass != null) && (listenerClass.equals(name))) {
177                                             if (!moveToAnotherProject) {
178                                                 RefactoringElementImplementation elem =
179                                                         new WebXmlListenerMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
180                                                 refactoringElements.add(moveClassRefactor, elem);
181                                             } else {
182                                                 RefactoringElementImplementation elem =
183                                                         new WebXmlListenerReferenceMoveClassOtherProjectRefactoringElement(webXmlDD, newWebXmlDD, name, newName, webXmlFO, newWebXmlFO);
184                                                 refactoringElements.add(moveClassRefactor, elem);
185                                             }
186                                         }
187                                     }
188                                 }
189
190                                 //filters
191
Filter[] filters = webXmlDD.getFilter();
192                                 if ((filters!=null) && (filters.length > 0)) {
193                                     for (int s=0; s < filters.length; s++) {
194                                         Filter filter = filters[s];
195                                         err.log("Filter: " + filter);
196                                         String JavaDoc filterClass = filter.getFilterClass();
197                                         err.log("Filter class: " + filterClass);
198                                         if ((filterClass != null) && (filterClass.equals(name))) {
199                                             if (!moveToAnotherProject) {
200                                                 RefactoringElementImplementation elem =
201                                                         new WebXmlFilterMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
202                                                 refactoringElements.add(moveClassRefactor, elem);
203                                             } else {
204                                                 RefactoringElementImplementation elem =
205                                                         new WebXmlFilterReferenceMoveClassOtherProjectRefactoringElement(webXmlDD, newWebXmlDD, name, newName, webXmlFO, newWebXmlFO);
206                                                 refactoringElements.add(moveClassRefactor, elem);
207                                             }
208                                         }
209                                     }
210                                 }
211                             
212                                 // refs
213
org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef();
214                                 if ((refs != null) && (refs.length > 0)) {
215                                     for (int l=0; l < refs.length; l++) {
216                                         org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = refs[l];
217                                         err.log("EJB ref: " + ref);
218                                         String JavaDoc refHome = ref.getHome();
219                                         err.log("home ref: " + refHome);
220                                         if ((refHome != null) && (refHome.equals(name))) {
221                                             RefactoringElementImplementation elem =
222                                                     new WebXmlRefHomeMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
223                                             refactoringElements.add(moveClassRefactor, elem);
224                                         }
225                                         String JavaDoc refRemote = ref.getRemote();
226                                         err.log("localHome ref: " + refRemote);
227                                         if ((refRemote != null) && (refRemote.equals(name))) {
228                                             RefactoringElementImplementation elem =
229                                                     new WebXmlRefRemoteMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
230                                             refactoringElements.add(moveClassRefactor, elem);
231                                         }
232                                     }
233                                 }
234
235                                 // local refs
236
org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] localRefs = webXmlDD.getEjbLocalRef();
237                                 if ((localRefs != null) && (localRefs.length > 0)) {
238                                     for (int l=0; l < localRefs.length; l++) {
239                                         org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef localRef = localRefs[l];
240                                         err.log("EJB local ref: " + localRef);
241                                         String JavaDoc refLocal = localRef.getLocal();
242                                         err.log("local ref: " + refLocal);
243                                         if ((refLocal != null) && (refLocal.equals(name))) {
244                                             RefactoringElementImplementation elem =
245                                                     new WebXmlRefLocalMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
246                                             refactoringElements.add(moveClassRefactor, elem);
247                                         }
248                                         String JavaDoc refLocalHome = localRef.getLocalHome();
249                                         err.log("localHome ref: " + refLocalHome);
250                                         if ((refLocalHome != null) && (refLocalHome.equals(name))) {
251                                             RefactoringElementImplementation elem =
252                                                     new WebXmlRefLocalHomeMoveClassRefactoringElement(webXmlDD, name, newName, webXmlFO);
253                                             refactoringElements.add(moveClassRefactor, elem);
254                                         }
255                                     }
256                                 }
257                             }
258                         } else {
259                             Problem newProblem = new Problem(false, NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlMoveClassInvalidProblem"));
260                             problem = Utility.addProblemsToEnd(problem, newProblem);
261                         }
262                     }
263                 } //while
264
}
265         } //for
266
return problem;
267     }
268     
269     public final class WebXmlServletMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
270
271         protected WebApp webXmlDD;
272
273         /** Creates a new instance of WebXmlServletMoveClassRefactoringElement */
274         public WebXmlServletMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
275             this.webXmlDD = webXmlDD;
276             this.oldName = oldName;
277             this.newName = newName;
278             this.parentFile = parentFile;
279         }
280
281         /** Returns text describing the refactoring formatted for display (using HTML tags).
282          * @return Formatted text.
283          */

284         public String JavaDoc getDisplayText() {
285             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
286             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlServletMoveClass"), args);
287         }
288
289         public void performExternalChange() {
290             Servlet servlet = (Servlet)webXmlDD.findBeanByName("Servlet","ServletClass",oldName); //NOI18N
291
if (servlet != null) {
292                 servlet.setServletClass(newName);
293                 try {
294                     webXmlDD.write(parentFile);
295                 } catch (IOException JavaDoc ioe) {
296                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
297                 }
298             }
299         }
300         
301         public void undoExternalChange() {
302             Servlet servlet = (Servlet)webXmlDD.findBeanByName("Servlet","ServletClass",newName); //NOI18N
303
if (servlet != null) {
304                 servlet.setServletClass(oldName);
305                 try {
306                     webXmlDD.write(parentFile);
307                 } catch (IOException JavaDoc ioe) {
308                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
309                 }
310             }
311         }
312
313         /** Performs the change represented by this refactoring element.
314          */

315         public void performChange() {
316             JavaMetamodel.getManager().registerExtChange(this);
317         }
318
319     }
320
321     public final class WebXmlServletReferenceMoveClassOtherProjectRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
322
323         protected WebApp webXmlDD;
324         protected WebApp newWebXmlDD;
325
326         protected FileObject newXmlFO;
327
328         private Servlet servlet = null;
329         private List JavaDoc servletMappings = null;
330         
331         /** Creates a new instance of WebXmlServletReferenceMoveClassOtherProjectRefactoringElement */
332         public WebXmlServletReferenceMoveClassOtherProjectRefactoringElement(WebApp webXmlDD, WebApp newWebXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile, FileObject newXmlFO) {
333             this.webXmlDD = webXmlDD;
334             this.newWebXmlDD = newWebXmlDD;
335             this.oldName = oldName;
336             this.newName = newName;
337             this.parentFile = parentFile;
338             this.newXmlFO = newXmlFO;
339         }
340
341         /** Returns text describing the refactoring formatted for display (using HTML tags).
342          * @return Formatted text.
343          */

344         public String JavaDoc getDisplayText() {
345             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), parentFile.getPath(), newXmlFO.getPath()};
346             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlServletReferenceMoveClassOtherProject"), args);
347         }
348
349         public void performExternalChange() {
350             servlet = (Servlet)webXmlDD.findBeanByName("Servlet","ServletClass",oldName); //NOI18N
351

352             List JavaDoc mappingsToMove = getServletMappings(webXmlDD, servlet.getServletName());
353             for (int i=0; i<mappingsToMove.size(); i++) {
354                 webXmlDD.removeServletMapping((ServletMapping)mappingsToMove.get(i));
355                 newWebXmlDD.addServletMapping((ServletMapping)mappingsToMove.get(i));
356             }
357
358             servlet.setServletClass(newName);
359             webXmlDD.removeServlet(servlet);
360             newWebXmlDD.addServlet(servlet);
361             
362             try {
363                 webXmlDD.write(parentFile);
364                 newWebXmlDD.write(newXmlFO);
365             } catch (IOException JavaDoc ioe) {
366                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
367             }
368         }
369         
370         public void undoExternalChange() {
371             if (servletMappings != null) {
372                 for (int i=0; i<servletMappings.size(); i++) {
373                     newWebXmlDD.removeServletMapping((ServletMapping)servletMappings.get(i));
374                     webXmlDD.addServletMapping((ServletMapping)servletMappings.get(i));
375                 }
376             }
377
378             servlet.setServletClass(oldName);
379             newWebXmlDD.removeServlet(servlet);
380             webXmlDD.addServlet(servlet);
381             
382             try {
383                 webXmlDD.write(parentFile);
384                 newWebXmlDD.write(newXmlFO);
385             } catch (IOException JavaDoc ioe) {
386                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
387             }
388         }
389         
390         private List JavaDoc getServletMappings(WebApp webApp, String JavaDoc servletName) {
391             if (servletName==null) return null;
392             ServletMapping[] oldMaps = webApp.getServletMapping();
393             List JavaDoc toMove = new ArrayList JavaDoc();
394             for (int i=0;i<oldMaps.length;i++) {
395                 if (servletName.equals(oldMaps[i].getServletName())) {
396                     toMove.add(oldMaps[i]);
397                 }
398             }
399             return toMove;
400         }
401
402         /** Performs the change represented by this refactoring element.
403          */

404         public void performChange() {
405             JavaMetamodel.getManager().registerExtChange(this);
406         }
407
408     }
409     
410     public final class WebXmlListenerMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
411
412         protected WebApp webXmlDD;
413
414         /** Creates a new instance of WebXmlListenerMoveClassRefactoringElement */
415         public WebXmlListenerMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
416             this.webXmlDD = webXmlDD;
417             this.oldName = oldName;
418             this.newName = newName;
419             this.parentFile = parentFile;
420         }
421
422         /** Returns text describing the refactoring formatted for display (using HTML tags).
423          * @return Formatted text.
424          */

425         public String JavaDoc getDisplayText() {
426             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
427             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlListenerMoveClass"), args);
428         }
429
430         /** Performs the change represented by this refactoring element.
431          */

432         public void performChange() {
433             JavaMetamodel.getManager().registerExtChange(this);
434         }
435         
436         public void performExternalChange() {
437             Listener listener = (Listener)webXmlDD.findBeanByName("Listener","ListenerClass",oldName); //NOI18N
438
if (listener != null) {
439                 listener.setListenerClass(newName);
440                 try {
441                     webXmlDD.write(parentFile);
442                 } catch (IOException JavaDoc ioe) {
443                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
444                 }
445             }
446         }
447         
448         public void undoExternalChange() {
449             Listener listener = (Listener)webXmlDD.findBeanByName("Listener","ListenerClass",newName); //NOI18N
450
if (listener != null) {
451                 listener.setListenerClass(oldName);
452                 try {
453                     webXmlDD.write(parentFile);
454                 } catch (IOException JavaDoc ioe) {
455                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
456                 }
457             }
458         }
459     }
460
461     public final class WebXmlListenerReferenceMoveClassOtherProjectRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
462
463         protected WebApp webXmlDD;
464         protected WebApp newWebXmlDD;
465
466         protected FileObject newXmlFO;
467
468         private Listener listener = null;
469
470         /** Creates a new instance of WebXmlListenerReferenceMoveClassOtherProjectRefactoringElement */
471         public WebXmlListenerReferenceMoveClassOtherProjectRefactoringElement(WebApp webXmlDD, WebApp newWebXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile, FileObject newXmlFO) {
472             this.webXmlDD = webXmlDD;
473             this.newWebXmlDD = newWebXmlDD;
474             this.oldName = oldName;
475             this.newName = newName;
476             this.parentFile = parentFile;
477             this.newXmlFO = newXmlFO;
478         }
479
480         /** Returns text describing the refactoring formatted for display (using HTML tags).
481          * @return Formatted text.
482          */

483         public String JavaDoc getDisplayText() {
484             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), parentFile.getPath(), newXmlFO.getPath()};
485             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlListenerReferenceMoveClassOtherProject"), args);
486         }
487
488         public void performExternalChange() {
489             listener = (Listener)webXmlDD.findBeanByName("Listener","ListenerClass",oldName); //NOI18N
490

491             listener.setListenerClass(newName);
492             webXmlDD.removeListener(listener);
493             newWebXmlDD.addListener(listener);
494             
495             try {
496                 webXmlDD.write(parentFile);
497                 newWebXmlDD.write(newXmlFO);
498             } catch (IOException JavaDoc ioe) {
499                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
500             }
501         }
502         
503         public void undoExternalChange() {
504             listener.setListenerClass(oldName);
505             newWebXmlDD.removeListener(listener);
506             webXmlDD.addListener(listener);
507             
508             try {
509                 webXmlDD.write(parentFile);
510                 newWebXmlDD.write(newXmlFO);
511             } catch (IOException JavaDoc ioe) {
512                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
513             }
514         }
515         
516         /** Performs the change represented by this refactoring element.
517          */

518         public void performChange() {
519             JavaMetamodel.getManager().registerExtChange(this);
520         }
521
522     }
523     
524     
525     public final class WebXmlFilterMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
526
527         protected WebApp webXmlDD;
528
529         /** Creates a new instance of WebXmlFilterMoveClassRefactoringElement */
530         public WebXmlFilterMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
531             this.webXmlDD = webXmlDD;
532             this.oldName = oldName;
533             this.newName = newName;
534             this.parentFile = parentFile;
535         }
536
537         /** Returns text describing the refactoring formatted for display (using HTML tags).
538          * @return Formatted text.
539          */

540         public String JavaDoc getDisplayText() {
541             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
542             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlFilterMoveClass"), args);
543         }
544
545         /** Performs the change represented by this refactoring element.
546          */

547         public void performChange() {
548             JavaMetamodel.getManager().registerExtChange(this);
549         }
550
551         public void performExternalChange() {
552             Filter filter = (Filter)webXmlDD.findBeanByName("Filter","FilterClass",oldName); //NOI18N
553
if (filter!=null) {
554                 filter.setFilterClass(newName);
555                 try {
556                     webXmlDD.write(parentFile);
557                 } catch (IOException JavaDoc ioe) {
558                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
559                 }
560             }
561         }
562         
563         public void undoExternalChange() {
564             Filter filter = (Filter)webXmlDD.findBeanByName("Filter","FilterClass",newName); //NOI18N
565
if (filter!=null) {
566                 filter.setFilterClass(oldName);
567                 try {
568                     webXmlDD.write(parentFile);
569                 } catch (IOException JavaDoc ioe) {
570                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
571                 }
572             }
573         }
574     }
575     
576     public final class WebXmlFilterReferenceMoveClassOtherProjectRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
577
578         protected WebApp webXmlDD;
579         protected WebApp newWebXmlDD;
580
581         protected FileObject newXmlFO;
582
583         private Filter filter = null;
584         private List JavaDoc filterMappings = null;
585         
586         /** Creates a new instance of WebXmlFilterReferenceMoveClassOtherProjectRefactoringElement */
587         public WebXmlFilterReferenceMoveClassOtherProjectRefactoringElement(WebApp webXmlDD, WebApp newWebXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile, FileObject newXmlFO) {
588             this.webXmlDD = webXmlDD;
589             this.newWebXmlDD = newWebXmlDD;
590             this.oldName = oldName;
591             this.newName = newName;
592             this.parentFile = parentFile;
593             this.newXmlFO = newXmlFO;
594         }
595
596         /** Returns text describing the refactoring formatted for display (using HTML tags).
597          * @return Formatted text.
598          */

599         public String JavaDoc getDisplayText() {
600             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), parentFile.getPath(), newXmlFO.getPath()};
601             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlFilterReferenceMoveClassOtherProject"), args);
602         }
603
604         public void performExternalChange() {
605             filter = (Filter)webXmlDD.findBeanByName("Filter","FilterClass",oldName); //NOI18N
606

607             List JavaDoc mappingsToMove = getFilterMappings(webXmlDD, filter.getFilterName());
608             for (int i=0; i<mappingsToMove.size(); i++) {
609                 FilterMapping mapping = (FilterMapping) mappingsToMove.get(i);
610                 webXmlDD.removeFilterMapping(mapping);
611                 newWebXmlDD.addFilterMapping(mapping);
612             }
613
614             filter.setFilterClass(newName);
615             webXmlDD.removeFilter(filter);
616             newWebXmlDD.addFilter(filter);
617             
618             try {
619                 webXmlDD.write(parentFile);
620                 newWebXmlDD.write(newXmlFO);
621             } catch (IOException JavaDoc ioe) {
622                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
623             }
624         }
625         
626         public void undoExternalChange() {
627             if (filterMappings != null) {
628                 for (int i=0; i<filterMappings.size(); i++) {
629                     newWebXmlDD.removeFilterMapping((FilterMapping)filterMappings.get(i));
630                     webXmlDD.addFilterMapping((FilterMapping)filterMappings.get(i));
631                 }
632             }
633
634             filter.setFilterClass(oldName);
635             newWebXmlDD.removeFilter(filter);
636             webXmlDD.addFilter(filter);
637             
638             try {
639                 webXmlDD.write(parentFile);
640                 newWebXmlDD.write(newXmlFO);
641             } catch (IOException JavaDoc ioe) {
642                 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
643             }
644         }
645         
646         private List JavaDoc getFilterMappings(WebApp webApp, String JavaDoc filterName) {
647             if (filterName==null) return null;
648             FilterMapping[] oldMaps = webApp.getFilterMapping();
649             List JavaDoc toMove = new ArrayList JavaDoc();
650             for (int i=0;i<oldMaps.length;i++) {
651                 if (filterName.equals(oldMaps[i].getFilterName())) {
652                     toMove.add(oldMaps[i]);
653                 }
654             }
655             return toMove;
656         }
657
658         /** Performs the change represented by this refactoring element.
659          */

660         public void performChange() {
661             JavaMetamodel.getManager().registerExtChange(this);
662         }
663
664     }
665
666     public final class WebXmlRefHomeMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
667
668         protected WebApp webXmlDD;
669
670         /** Creates a new instance of WebXmlRefHomeMoveClassRefactoringElement */
671         public WebXmlRefHomeMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
672             this.webXmlDD = webXmlDD;
673             this.oldName = oldName;
674             this.newName = newName;
675             this.parentFile = parentFile;
676         }
677
678         /** Returns text describing the refactoring formatted for display (using HTML tags).
679          * @return Formatted text.
680          */

681         public String JavaDoc getDisplayText() {
682             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
683             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlRefHomeMoveClass"), args);
684         }
685
686         /** Performs the change represented by this refactoring element.
687          */

688         public void performChange() {
689             JavaMetamodel.getManager().registerExtChange(this);
690         }
691
692         public void performExternalChange() {
693             org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef();
694             org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = null;
695             for (int i=0; i < refs.length; i++) {
696                 String JavaDoc refStr = refs[i].getHome();
697                 if (oldName.equals(refStr)) {
698                     ref = refs[i];
699                     break;
700                 }
701             }
702             if (ref!=null) {
703                 ref.setHome(newName);
704                 try {
705                     webXmlDD.write(parentFile);
706                 } catch (IOException JavaDoc ioe) {
707                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
708                 }
709             }
710         }
711         
712         public void undoExternalChange() {
713             org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef();
714             org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = null;
715             for (int i=0; i < refs.length; i++) {
716                 String JavaDoc refStr = refs[i].getHome();
717                 if (newName.equals(refStr)) {
718                     ref = refs[i];
719                     break;
720                 }
721             }
722             if (ref!=null) {
723                 ref.setHome(oldName);
724                 try {
725                     webXmlDD.write(parentFile);
726                 } catch (IOException JavaDoc ioe) {
727                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
728                 }
729             }
730         }
731     }
732
733     public final class WebXmlRefRemoteMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
734
735         protected WebApp webXmlDD;
736
737         /** Creates a new instance of WebXmlRefRemoteMoveClassRefactoringElement */
738         public WebXmlRefRemoteMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
739             this.webXmlDD = webXmlDD;
740             this.oldName = oldName;
741             this.newName = newName;
742             this.parentFile = parentFile;
743         }
744
745         /** Returns text describing the refactoring formatted for display (using HTML tags).
746          * @return Formatted text.
747          */

748         public String JavaDoc getDisplayText() {
749             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
750             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlRefRemoteMoveClass"), args);
751         }
752
753         /** Performs the change represented by this refactoring element.
754          */

755         public void performChange() {
756             JavaMetamodel.getManager().registerExtChange(this);
757         }
758
759         public void performExternalChange() {
760             org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef();
761             org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = null;
762             for (int i=0; i < refs.length; i++) {
763                 String JavaDoc refStr = refs[i].getRemote();
764                 if (oldName.equals(refStr)) {
765                     ref = refs[i];
766                     break;
767                 }
768             }
769             if (ref!=null) {
770                 ref.setRemote(newName);
771                 try {
772                     webXmlDD.write(parentFile);
773                 } catch (IOException JavaDoc ioe) {
774                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
775                 }
776             }
777         }
778         
779         public void undoExternalChange() {
780             org.netbeans.modules.j2ee.dd.api.common.EjbRef[] refs = webXmlDD.getEjbRef();
781             org.netbeans.modules.j2ee.dd.api.common.EjbRef ref = null;
782             for (int i=0; i < refs.length; i++) {
783                 String JavaDoc refStr = refs[i].getRemote();
784                 if (newName.equals(refStr)) {
785                     ref = refs[i];
786                     break;
787                 }
788             }
789             if (ref!=null) {
790                 ref.setRemote(oldName);
791                 try {
792                     webXmlDD.write(parentFile);
793                 } catch (IOException JavaDoc ioe) {
794                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
795                 }
796             }
797         }
798     }
799     
800     public final class WebXmlRefLocalMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
801
802         protected WebApp webXmlDD;
803
804         /** Creates a new instance of WebXmlRefLocalMoveClassRefactoringElement */
805         public WebXmlRefLocalMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
806             this.webXmlDD = webXmlDD;
807             this.oldName = oldName;
808             this.newName = newName;
809             this.parentFile = parentFile;
810         }
811
812         /** Returns text describing the refactoring formatted for display (using HTML tags).
813          * @return Formatted text.
814          */

815         public String JavaDoc getDisplayText() {
816             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
817             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlRefLocalMoveClass"), args);
818         }
819
820         /** Performs the change represented by this refactoring element.
821          */

822         public void performChange() {
823             JavaMetamodel.getManager().registerExtChange(this);
824         }
825
826         public void performExternalChange() {
827             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] refs = webXmlDD.getEjbLocalRef();
828             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref = null;
829             for (int i=0; i < refs.length; i++) {
830                 String JavaDoc refStr = refs[i].getLocal();
831                 if (oldName.equals(refStr)) {
832                     ref = refs[i];
833                     break;
834                 }
835             }
836             if (ref!=null) {
837                 ref.setLocal(newName);
838                 try {
839                     webXmlDD.write(parentFile);
840                 } catch (IOException JavaDoc ioe) {
841                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
842                 }
843             }
844         }
845         
846         public void undoExternalChange() {
847             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] refs = webXmlDD.getEjbLocalRef();
848             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref = null;
849             for (int i=0; i < refs.length; i++) {
850                 String JavaDoc refStr = refs[i].getLocal();
851                 if (newName.equals(refStr)) {
852                     ref = refs[i];
853                     break;
854                 }
855             }
856             if (ref!=null) {
857                 ref.setLocal(oldName);
858                 try {
859                     webXmlDD.write(parentFile);
860                 } catch (IOException JavaDoc ioe) {
861                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
862                 }
863             }
864         }
865     }
866     
867     public final class WebXmlRefLocalHomeMoveClassRefactoringElement extends AbstractMoveClassRefactoringElement implements ExternalChange {
868
869         protected WebApp webXmlDD;
870
871         /** Creates a new instance of WebXmlRefLocalHomeMoveClassRefactoringElement */
872         public WebXmlRefLocalHomeMoveClassRefactoringElement(WebApp webXmlDD, String JavaDoc oldName, String JavaDoc newName, FileObject parentFile) {
873             this.webXmlDD = webXmlDD;
874             this.oldName = oldName;
875             this.newName = newName;
876             this.parentFile = parentFile;
877         }
878
879         /** Returns text describing the refactoring formatted for display (using HTML tags).
880          * @return Formatted text.
881          */

882         public String JavaDoc getDisplayText() {
883             Object JavaDoc[] args = new Object JavaDoc [] {parentFile.getNameExt(), oldName, newName};
884             return MessageFormat.format(NbBundle.getMessage(WebXmlMoveClassRefactoring.class, "TXT_WebXmlRefLocalHomeMoveClass"), args);
885         }
886
887         /** Performs the change represented by this refactoring element.
888          */

889         public void performChange() {
890             JavaMetamodel.getManager().registerExtChange(this);
891         }
892
893         public void performExternalChange() {
894             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] refs = webXmlDD.getEjbLocalRef();
895             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref = null;
896             for (int i=0; i < refs.length; i++) {
897                 String JavaDoc refStr = refs[i].getLocalHome();
898                 if (oldName.equals(refStr)) {
899                     ref = refs[i];
900                     break;
901                 }
902             }
903             if (ref!=null) {
904                 ref.setLocalHome(newName);
905                 try {
906                     webXmlDD.write(parentFile);
907                 } catch (IOException JavaDoc ioe) {
908                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
909                 }
910             }
911         }
912         
913         public void undoExternalChange() {
914             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef[] refs = webXmlDD.getEjbLocalRef();
915             org.netbeans.modules.j2ee.dd.api.common.EjbLocalRef ref = null;
916             for (int i=0; i < refs.length; i++) {
917                 String JavaDoc refStr = refs[i].getLocalHome();
918                 if (newName.equals(refStr)) {
919                     ref = refs[i];
920                     break;
921                 }
922             }
923             if (ref!=null) {
924                 ref.setLocalHome(oldName);
925                 try {
926                     webXmlDD.write(parentFile);
927                 } catch (IOException JavaDoc ioe) {
928                     ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, ioe.getMessage());
929                 }
930             }
931         }
932     }
933     
934 }
Popular Tags