2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* Portions Copyrighted 2007 Sun Microsystems, Inc.
28
package org.netbeans.modules.j2ee.ejbrefactoring;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Collection;
33
import java.util.Collections;
34
import java.util.Iterator;
35
import java.util.List;
37
import org.netbeans.api.java.source.TreePathHandle;
38
import org.netbeans.api.fileinfo.NonRecursiveFolder;
39
import org.netbeans.api.project.FileOwnerQuery;
40
import org.netbeans.api.project.Project;
41
import org.netbeans.api.project.ui.OpenProjects;
42
import org.netbeans.modules.j2ee.api.ejbjar.EjbJar;
43
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeApplicationProvider;
44
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
45
import org.netbeans.modules.refactoring.api.AbstractRefactoring;
46
import org.netbeans.modules.refactoring.api.MoveRefactoring;
47
import org.netbeans.modules.refactoring.api.SafeDeleteRefactoring;
48
import org.netbeans.modules.refactoring.api.WhereUsedQuery;
49
import org.netbeans.modules.refactoring.api.RenameRefactoring;
50
import org.netbeans.modules.refactoring.spi.RefactoringPlugin;
51
import org.netbeans.modules.refactoring.spi.RefactoringPluginFactory;
52
import org.netbeans.spi.project.SubprojectProvider;
53
import org.openide.filesystems.FileObject;
54
import org.openide.filesystems.FileUtil;
55
import org.openide.util.NbBundle;
58
* Refactoring factory for EJB refactorings. At the moment there is no actual support,
59
* the EJB refactoring plugin only displays a warning message if there is an ejb-jar.xml
60
* file in any of the affected projects.
62
* @author Erno Mononen
64
public class EjbRefactoringFactory implements RefactoringPluginFactory{
66
public EjbRefactoringFactory() {
69
public RefactoringPlugin createInstance(AbstractRefactoring refactoring) {
71
FileObject source = getRefactoringSource(refactoring);
76
List<EjbJar> ejbJars = getEjbJars(source);
77
if (ejbJars.isEmpty()){
81
String ejbJarPaths = getEjbJarPaths(ejbJars);
84
if (refactoring instanceof RenameRefactoring){
85
msg = "TXT_EjbJarRenameWarning"; //NO18N
86
} else if (refactoring instanceof SafeDeleteRefactoring){
87
msg = "TXT_EjbJarSafeDeleteWarning"; //NO18N
88
} else if (refactoring instanceof MoveRefactoring){
89
msg = "TXT_EjbJarMoveClassWarning"; //NO18N
90
} else if (refactoring instanceof WhereUsedQuery){
91
msg = "TXT_EjbJarWhereUsedWarning";//NO18N
93
msg = "TXT_EjbJarGeneralWarning";//NO18N
95
return new EjbRefactoringPlugin(NbBundle.getMessage(EjbRefactoringFactory.class, msg, ejbJarPaths));
99
*@return a comma separated string representing the locations of the ejb-jar.xml
100
* files of the given <code>ejbJars</code>.
102
private String getEjbJarPaths(List<EjbJar> ejbJars){
103
// TODO: it would be probably better to display the project names instead
104
StringBuilder ejbJarPaths = new StringBuilder();
105
for (Iterator<EjbJar> it = ejbJars.iterator(); it.hasNext();){
106
EjbJar ejbJar = it.next();
107
String path = FileUtil.getFileDisplayName(ejbJar.getDeploymentDescriptor());
108
ejbJarPaths.append(path);
110
ejbJarPaths.append(", ");
113
return ejbJarPaths.toString();
117
private FileObject getRefactoringSource(AbstractRefactoring refactoring){
118
FileObject source = refactoring.getRefactoringSource().lookup(FileObject.class);
122
TreePathHandle tph = refactoring.getRefactoringSource().lookup(TreePathHandle.class);
124
return tph.getFileObject();
126
NonRecursiveFolder folder = refactoring.getRefactoringSource().lookup(NonRecursiveFolder.class);
128
return folder.getFolder();
134
*@return the <code>EjbJar</code>s representing the EJB Modules that are relevant
135
* to the given <code>source</code>, i.e. the ones that depend on the project
136
* owning the <code>source</code>.
138
private List<EjbJar> getEjbJars(FileObject source){
139
List<EjbJar> result = new ArrayList<EjbJar>();
140
for (EjbJar each : getRelevantEjbModules(source)) {
141
FileObject ejbJarFO = each.getDeploymentDescriptor();
142
if (ejbJarFO != null){
149
/** Finds all ejb projects that depend on a project which is owner of FileObject 'fo' */
150
private static Collection<EjbJar> getRelevantEjbModules(FileObject fo) {
151
Project affectedProject = FileOwnerQuery.getOwner(fo);
152
List<EjbJar> ejbmodules = new ArrayList<EjbJar>();
153
List<Project> projects = new ArrayList<Project>();
155
if (affectedProject != null) {
156
// first check if the project which directly contains fo is relevant
157
org.netbeans.modules.j2ee.api.ejbjar.EjbJar emod =
158
org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(affectedProject.getProjectDirectory());
160
projects.add(affectedProject);
162
return Collections.EMPTY_SET;
164
for (Project project : OpenProjects.getDefault().getOpenProjects()){
165
Object isJ2eeApp = project.getLookup().lookup(J2eeApplicationProvider.class);
166
if (isJ2eeApp != null) {
167
J2eeApplicationProvider j2eeApp = (J2eeApplicationProvider) isJ2eeApp;
168
J2eeModuleProvider[] j2eeModules = j2eeApp.getChildModuleProviders();
170
if (j2eeModules != null) {
171
J2eeModuleProvider affectedPrjProvider =
172
(J2eeModuleProvider) affectedProject.getLookup().lookup(J2eeModuleProvider.class);
174
if (affectedPrjProvider != null) {
175
if (Arrays.asList(j2eeModules).contains(affectedPrjProvider)) {
176
for (int k = 0; k < j2eeModules.length; k++) {
177
FileObject[] sourceRoots = j2eeModules[k].getSourceRoots();
178
if (sourceRoots != null && sourceRoots.length > 0){
179
FileObject srcRoot = sourceRoots[0];
180
Project p = FileOwnerQuery.getOwner(srcRoot);
181
if ((p != null) && (!projects.contains(p))) {
190
Object obj = project.getLookup().lookup(SubprojectProvider.class);
191
if ((obj != null) && (obj instanceof SubprojectProvider)) {
192
Set subprojects = ((SubprojectProvider) obj).getSubprojects();
193
if (subprojects.contains(affectedProject)) {
194
org.netbeans.modules.j2ee.api.ejbjar.EjbJar em = org.netbeans.modules.j2ee.api.ejbjar.EjbJar
195
.getEjbJar(project.getProjectDirectory());
197
if (!projects.contains(project)) { // include each project only once
198
projects.add(project);
206
for (int j=0; j < projects.size(); j++) {
207
Project prj = (Project)((ArrayList)projects).get(j);
208
org.netbeans.modules.j2ee.api.ejbjar.EjbJar ejb =
209
org.netbeans.modules.j2ee.api.ejbjar.EjbJar.getEjbJar(prj.getProjectDirectory());