KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > refactoring > J2EEMoveClassRefactoringPlugin


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;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.jmi.reflect.RefObject;
30 import org.netbeans.api.project.FileOwnerQuery;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.api.project.SourceGroup;
35 import org.netbeans.api.project.Sources;
36 import org.netbeans.modules.j2ee.dd.api.web.DDProvider;
37 import org.netbeans.modules.j2ee.dd.api.web.Filter;
38 import org.netbeans.modules.j2ee.dd.api.web.Listener;
39 import org.netbeans.modules.j2ee.dd.api.web.Servlet;
40 import org.netbeans.modules.j2ee.dd.api.web.WebApp;
41 import org.netbeans.jmi.javamodel.Element;
42 import org.netbeans.jmi.javamodel.JavaClass;
43 import org.netbeans.jmi.javamodel.Resource;
44 import org.netbeans.modules.j2ee.dd.api.ejb.Ejb;
45 import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
46 import org.netbeans.modules.j2ee.dd.api.ejb.EnterpriseBeans;
47 import org.netbeans.modules.j2ee.dd.api.ejb.Entity;
48 import org.netbeans.modules.j2ee.dd.api.ejb.EntityAndSession;
49 import org.netbeans.modules.j2ee.dd.api.ejb.MessageDriven;
50 import org.netbeans.modules.j2ee.dd.api.ejb.Session;
51 import org.netbeans.modules.j2ee.dd.api.webservices.PortComponent;
52 import org.netbeans.modules.j2ee.dd.api.webservices.WebserviceDescription;
53 import org.netbeans.modules.j2ee.dd.api.webservices.Webservices;
54 import org.netbeans.modules.j2ee.refactoring.moveclass.EjbJarMoveClassRefactoring;
55 import org.netbeans.modules.j2ee.refactoring.moveclass.JaxWsXmlMoveClassRefactoring;
56 import org.netbeans.modules.j2ee.refactoring.moveclass.PersistenceXmlMoveClassRefactoring;
57 import org.netbeans.modules.j2ee.refactoring.moveclass.SunJaxWsXmlMoveClassRefactoring;
58 import org.netbeans.modules.j2ee.refactoring.moveclass.TldMoveClassRefactoring;
59 import org.netbeans.modules.j2ee.refactoring.moveclass.WebXmlMoveClassRefactoring;
60 import org.netbeans.modules.j2ee.refactoring.moveclass.WebservicesXmlMoveClassRefactoring;
61 import org.netbeans.modules.javacore.api.JavaModel;
62 import org.netbeans.modules.javacore.internalapi.ExternalChange;
63 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
64 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
65 import org.netbeans.modules.refactoring.api.Problem;
66 import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
67 import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
68 import org.netbeans.modules.refactoring.api.MoveClassRefactoring;
69 import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
70 import org.netbeans.modules.web.api.webmodule.WebModule;
71 import org.netbeans.modules.web.taglib.TLDDataObject;
72 import org.netbeans.modules.web.taglib.model.FunctionType;
73 import org.netbeans.modules.web.taglib.model.TagType;
74 import org.netbeans.modules.web.taglib.model.Taglib;
75 import org.netbeans.modules.web.taglib.model.ValidatorType;
76 import org.netbeans.modules.websvc.api.webservices.WebServicesSupport;
77 import org.netbeans.modules.websvc.wsdl.config.api.Configuration;
78 import org.netbeans.spi.project.support.ant.AntProjectHelper;
79 import org.netbeans.spi.project.support.ant.EditableProperties;
80 import org.openide.ErrorManager;
81 import org.openide.filesystems.FileObject;
82 import org.openide.filesystems.FileUtil;
83 import org.openide.loaders.DataObject;
84 import org.openide.loaders.DataObjectNotFoundException;
85 import org.openide.text.PositionBounds;
86 import org.openide.util.NbBundle;
87
88 /**
89  *
90  * @author Martin Grebac
91  */

92 public class J2EEMoveClassRefactoringPlugin implements RefactoringPlugin {
93     
94     /** This one is important creature - makes sure that cycles between plugins won't appear */
95     private static ThreadLocal JavaDoc semafor = new ThreadLocal JavaDoc();
96     
97     private EjbJarMoveClassRefactoring ejbJarMoveClassRefactor;
98     private WebXmlMoveClassRefactoring webXmlMoveClassRefactor;
99     private TldMoveClassRefactoring tldMoveClassRefactor;
100     private WebservicesXmlMoveClassRefactoring webservicesXmlMoveClassRefactor;
101     private PersistenceXmlMoveClassRefactoring persistenceXmlMoveClassRefactor;
102     private JaxWsXmlMoveClassRefactoring jaxWsXmlMoveClassRefactor;
103     private SunJaxWsXmlMoveClassRefactoring sunJaxWsXmlMoveClassRefactor;
104     
105     private AbstractRefactoring refactoring;
106     
107     public void cancelRequest() {
108         
109     }
110     
111     private static ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.j2ee.refactoring.moveclass"); // NOI18N
112

113     /** Creates a new instance of J2EEMoveClassRefactoringPlugin */
114     public J2EEMoveClassRefactoringPlugin(AbstractRefactoring refactoring) {
115         this.refactoring = refactoring;
116     }
117     
118     /** Checks pre-conditions of the refactoring and returns problems.
119      * @return Problems found or null (if no problems were identified)
120      */

121     public Problem preCheck() {
122         return null;
123     }
124     
125     public Problem fastCheckParameters() {
126         Problem problem = null;
127         if (semafor.get() == null) {
128             semafor.set(new Object JavaDoc());
129             MoveClassRefactoring moveRefactor = ((MoveClassRefactoring)refactoring);
130             
131             Collection JavaDoc resources = moveRefactor.getResources();
132             err.log("resources: " + resources);
133             
134             FileObject target = moveRefactor.getTargetClassPathRoot();
135             err.log("target: " + target);
136             
137             if (!isEjb30()){
138                 Problem ejbJarProblem = getEjbJarMoveClassRefactor().fastCheckParameters(resources, target);
139                 problem = Utility.addProblemsToEnd(problem, ejbJarProblem);
140             } else if (hasEjbJar()){
141                 Problem ejbJarProblem = new Problem(false, NbBundle.getMessage(J2EEMoveClassRefactoringPlugin.class, "TXT_EjbJarMoveClassWarning")); //NOI18N
142
problem = Utility.addProblemsToEnd(problem, ejbJarProblem);
143             }
144             
145             semafor.set(null);
146         }
147         return problem;
148     }
149    
150     private boolean hasEjbJar(){
151         MoveClassRefactoring moveRefactor = ((MoveClassRefactoring)refactoring);
152         for (Object JavaDoc elem : moveRefactor.getResources()) {
153             Resource res = (Resource) elem;
154             for (Object JavaDoc elem2 : res.getClassifiers()) {
155                 JavaClass javaClass = (JavaClass) elem2;
156                 if (Utility.hasEjbJar(Utility.getRelevantEjbModules(javaClass))){
157                     return true;
158                 }
159             }
160         }
161         return false;
162     }
163     
164     private boolean isEjb30(){
165         MoveClassRefactoring moveRefactor = ((MoveClassRefactoring)refactoring);
166         for (Object JavaDoc elem : moveRefactor.getResources()) {
167             Resource res = (Resource) elem;
168             for (Object JavaDoc elem2 : res.getClassifiers()) {
169                 JavaClass javaClass = (JavaClass) elem2;
170                 if (Utility.isEjb30(javaClass)){
171                     return true;
172                 }
173             }
174         }
175         return false;
176     }
177     
178     /** Checks parameters of the refactoring.
179      * @return Problems found or null (if no problems were identified)
180      */

181     public Problem checkParameters() {
182         Problem problem = null;
183         if (semafor.get() == null) {
184             semafor.set(new Object JavaDoc());
185             MoveClassRefactoring moveRefactor = ((MoveClassRefactoring)refactoring);
186             
187             Collection JavaDoc resources = moveRefactor.getResources();
188             err.log("resources: " + resources);
189             
190             FileObject target = moveRefactor.getTargetClassPathRoot();
191             err.log("target: " + target);
192             
193             if (!isEjb30()){
194                 Problem ejbJarProblem = getEjbJarMoveClassRefactor().checkParameters(resources, target);
195                 problem = Utility.addProblemsToEnd(problem, ejbJarProblem);
196             }
197             semafor.set(null);
198         }
199         return problem;
200     }
201     
202     /** Collects refactoring elements for a given refactoring.
203      * @param refactoringElements Collection of refactoring elements - the implementation of this method
204      * should add refactoring elements to this collections. It should make no assumptions about the collection
205      * content.
206      * @return Problems found or null (if no problems were identified)
207      */

208     public Problem prepare(RefactoringElementsBag refactoringElements) {
209         
210         // FYI - Rename Package is a set of Move Class refactorings
211
Problem problem = null;
212         
213         if (semafor.get() == null) {
214             semafor.set(new Object JavaDoc());
215             
216             MoveClassRefactoring moveClassRefactor = ((MoveClassRefactoring)refactoring);
217             Collection JavaDoc resources = moveClassRefactor.getResources();
218             err.log("resources: " + resources);
219             
220             
221             Problem webXmlProblem = getWebXmlMoveClassRefactor().prepare(moveClassRefactor, resources, refactoringElements);
222             problem = Utility.addProblemsToEnd(problem, webXmlProblem);
223             
224             Problem tldProblem = getTldMoveClassRefactor().prepare(moveClassRefactor, resources, refactoringElements);
225             problem = Utility.addProblemsToEnd(problem, tldProblem);
226             
227             if (!isEjb30()){
228                 Problem ejbJarProblem = getEjbJarMoveClassRefactor().prepare(moveClassRefactor, resources, refactoringElements);
229                 problem = Utility.addProblemsToEnd(problem, ejbJarProblem);
230             }
231             
232             Problem webservicesProblem = getWebservicesXmlMoveClassRefactor().prepare(moveClassRefactor, resources, refactoringElements);
233             problem = Utility.addProblemsToEnd(problem, webservicesProblem);
234             
235             Problem persistenceXmlMoveClassRefactoring = getPersistenceXmlMoveClassRefactoring().prepare(moveClassRefactor, resources, refactoringElements);
236             problem = Utility.addProblemsToEnd(problem, persistenceXmlMoveClassRefactoring);
237             
238             Problem jaxWsXmlMoveClassRefactoring = getJaxWsXmlMoveClassRefactoring().prepare(moveClassRefactor, resources, refactoringElements);
239             problem = Utility.addProblemsToEnd(problem, jaxWsXmlMoveClassRefactoring);
240             
241             Problem sunJaxWsXmlMoveClassRefactoring = getSunJaxWsXmlMoveClassRefactoring().prepare(moveClassRefactor, resources, refactoringElements);
242             problem = Utility.addProblemsToEnd(problem, sunJaxWsXmlMoveClassRefactoring);
243             
244             semafor.set(null);
245         }
246         
247         err.log("Gonna return problem: " + problem);
248         return problem;
249     }
250     
251     private EjbJarMoveClassRefactoring getEjbJarMoveClassRefactor() {
252         if (ejbJarMoveClassRefactor == null) {
253             ejbJarMoveClassRefactor = new EjbJarMoveClassRefactoring();
254         }
255         return ejbJarMoveClassRefactor;
256     }
257     
258     private WebXmlMoveClassRefactoring getWebXmlMoveClassRefactor() {
259         if (webXmlMoveClassRefactor == null) {
260             webXmlMoveClassRefactor = new WebXmlMoveClassRefactoring();
261         }
262         return webXmlMoveClassRefactor;
263     }
264     
265     private TldMoveClassRefactoring getTldMoveClassRefactor() {
266         if (tldMoveClassRefactor == null) {
267             tldMoveClassRefactor = new TldMoveClassRefactoring();
268         }
269         return tldMoveClassRefactor;
270     }
271     
272     private WebservicesXmlMoveClassRefactoring getWebservicesXmlMoveClassRefactor() {
273         if (webservicesXmlMoveClassRefactor == null) {
274             webservicesXmlMoveClassRefactor = new WebservicesXmlMoveClassRefactoring();
275         }
276         return webservicesXmlMoveClassRefactor;
277     }
278     
279     private PersistenceXmlMoveClassRefactoring getPersistenceXmlMoveClassRefactoring(){
280         if (persistenceXmlMoveClassRefactor == null){
281             persistenceXmlMoveClassRefactor = new PersistenceXmlMoveClassRefactoring();
282         }
283         return persistenceXmlMoveClassRefactor;
284     }
285     
286     private JaxWsXmlMoveClassRefactoring getJaxWsXmlMoveClassRefactoring(){
287         if (jaxWsXmlMoveClassRefactor == null){
288             jaxWsXmlMoveClassRefactor = new JaxWsXmlMoveClassRefactoring();
289         }
290         return jaxWsXmlMoveClassRefactor;
291     }
292     
293     private SunJaxWsXmlMoveClassRefactoring getSunJaxWsXmlMoveClassRefactoring(){
294         if (sunJaxWsXmlMoveClassRefactor == null){
295             sunJaxWsXmlMoveClassRefactor= new SunJaxWsXmlMoveClassRefactoring();
296         }
297         return sunJaxWsXmlMoveClassRefactor;
298     }
299     
300 }
301
Popular Tags