KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbjarproject > EjbJarProjectOperations


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.ejbjarproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import org.apache.tools.ant.module.api.support.ActionUtils;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.modules.j2ee.ejbjarproject.classpath.EjbJarProjectClassPathExtender;
31 import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
32 import org.netbeans.spi.project.ActionProvider;
33 import org.netbeans.spi.project.CopyOperationImplementation;
34 import org.netbeans.spi.project.DeleteOperationImplementation;
35 import org.netbeans.spi.project.MoveOperationImplementation;
36 import org.netbeans.spi.project.support.ant.AntProjectHelper;
37 import org.netbeans.spi.project.support.ant.EditableProperties;
38 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
39 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
40 import org.netbeans.spi.project.support.ant.PropertyUtils;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.Lookups;
45
46 /**
47  *
48  * @author Jan Lahoda
49  */

50 public class EjbJarProjectOperations implements DeleteOperationImplementation, CopyOperationImplementation, MoveOperationImplementation {
51     
52     private EjbJarProject project;
53     
54     public EjbJarProjectOperations(EjbJarProject project) {
55         this.project = project;
56     }
57     
58     private static void addFile(FileObject projectDirectory, String JavaDoc fileName, List JavaDoc/*<FileObject>*/ result) {
59         FileObject file = projectDirectory.getFileObject(fileName);
60         
61         if (file != null) {
62             result.add(file);
63         }
64     }
65     
66     public List JavaDoc/*<FileObject>*/ getMetadataFiles() {
67         FileObject projectDirectory = project.getProjectDirectory();
68         List JavaDoc/*<FileObject>*/ files = new ArrayList JavaDoc();
69         
70         addFile(projectDirectory, "nbproject", files); // NOI18N
71
addFile(projectDirectory, "build.xml", files); // NOI18N
72
addFile(projectDirectory, "catalog.xml", files); //NOI18N
73

74         return files;
75     }
76     
77     public List JavaDoc/*<FileObject>*/ getDataFiles() {
78         List JavaDoc/*<FileObject>*/ files = new ArrayList JavaDoc();
79         
80         FileObject metaInf = project.getEjbModule().getMetaInf();
81         if (metaInf != null)
82             files.add(metaInf);
83         
84         SourceRoots src = project.getSourceRoots();
85         FileObject[] srcRoots = src.getRoots();
86         
87         for (int cntr = 0; cntr < srcRoots.length; cntr++) {
88             files.add(srcRoots[cntr]);
89         }
90         
91         PropertyEvaluator evaluator = project.evaluator();
92         String JavaDoc prop = evaluator.getProperty(EjbJarProjectProperties.SOURCE_ROOT);
93         if (prop != null) {
94             FileObject projectDirectory = project.getProjectDirectory();
95             FileObject srcDir = project.getAntProjectHelper().resolveFileObject(prop);
96             if (projectDirectory != srcDir && !files.contains(srcDir))
97                 files.add(srcDir);
98         }
99   
100         SourceRoots test = project.getTestSourceRoots();
101         FileObject[] testRoots = test.getRoots();
102         
103         for (int cntr = 0; cntr < testRoots.length; cntr++) {
104             files.add(testRoots[cntr]);
105         }
106         
107         File JavaDoc resourceDir = project.getEjbModule().getEnterpriseResourceDirectory();
108         if (resourceDir != null) {
109             FileObject resourceFO = FileUtil.toFileObject(resourceDir);
110             if (resourceFO != null)
111                 files.add(resourceFO);
112         }
113         
114         return files;
115     }
116     
117     public void notifyDeleting() throws IOException JavaDoc {
118         EjbJarActionProvider ap = (EjbJarActionProvider) project.getLookup().lookup(EjbJarActionProvider.class);
119         
120         assert ap != null;
121         
122         Lookup context = Lookups.fixed(new Object JavaDoc[0]);
123         Properties JavaDoc p = new Properties JavaDoc();
124         String JavaDoc[] targetNames = ap.getTargetNames(ActionProvider.COMMAND_CLEAN, context, p);
125         FileObject buildXML = project.getProjectDirectory().getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
126         
127         assert targetNames != null;
128         assert targetNames.length > 0;
129         
130         ActionUtils.runTarget(buildXML, targetNames, p).waitFinished();
131         
132         EjbJarProjectClassPathExtender extender = (EjbJarProjectClassPathExtender)project.getLookup().lookup(EjbJarProjectClassPathExtender.class);
133         extender.notifyDeleting();
134     }
135     
136     public void notifyDeleted() throws IOException JavaDoc {
137         project.getAntProjectHelper().notifyDeleted();
138     }
139     
140     public void notifyCopying() {
141         //nothing.
142
}
143     
144     public void notifyCopied(Project original, File JavaDoc originalPath, String JavaDoc nueName) {
145         if (original == null) {
146             //do nothing for the original project.
147
return ;
148         }
149         
150         project.getReferenceHelper().fixReferences(originalPath);
151         fixOtherReferences(originalPath);
152         
153         project.setName(nueName);
154     }
155     
156     public void notifyMoving() throws IOException JavaDoc {
157         notifyDeleting();
158     }
159     
160     public void notifyMoved(Project original, File JavaDoc originalPath, final String JavaDoc newName) {
161         if (original == null) {
162             project.getAntProjectHelper().notifyDeleted();
163             return ;
164         }
165         
166     final String JavaDoc oldProjectName = project.getName();
167
168         project.setName(newName);
169         project.getReferenceHelper().fixReferences(originalPath);
170         fixOtherReferences(originalPath);
171     
172         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
173             public void run() {
174         AntProjectHelper helper = project.getAntProjectHelper();
175         EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
176
177         String JavaDoc jarName = (String JavaDoc) projectProps.get(EjbJarProjectProperties.JAR_NAME);
178         String JavaDoc oldName = jarName.substring(0, jarName.length() - 4);
179         if (jarName.endsWith(".jar") && oldName.equals(oldProjectName)) //NOI18N
180
projectProps.put(EjbJarProjectProperties.JAR_NAME, newName + ".jar"); //NOI18N
181

182         helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps);
183             }
184         });
185     }
186     
187     private void fixOtherReferences(final File JavaDoc originalPath) {
188         final String JavaDoc property = EjbJarProjectProperties.META_INF;
189         final File JavaDoc projectDir = FileUtil.toFile(project.getProjectDirectory());
190                 
191         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
192             public void run() {
193                 EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
194                 String JavaDoc path = props.getProperty(property);
195                 if (path == null) {
196                     return;
197                 }
198
199                 if (path.startsWith(originalPath.getAbsolutePath())) {
200                     String JavaDoc relative = PropertyUtils.relativizeFile(originalPath, new File JavaDoc(path));
201                     String JavaDoc fixedPath = new File JavaDoc(projectDir, relative).getAbsolutePath();
202                     props.setProperty(property, fixedPath);
203                     project.getAntProjectHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props);
204                 }
205             }
206         });
207     }
208     
209     private static boolean isParent(File JavaDoc folder, File JavaDoc fo) {
210         if (folder.equals(fo))
211             return false;
212         
213         while (fo != null) {
214             if (fo.equals(folder))
215                 return true;
216             
217             fo = fo.getParentFile();
218         }
219         
220         return false;
221     }
222
223 }
224
Popular Tags