1 19 20 package org.netbeans.modules.j2ee.ejbjarproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import java.util.Properties ; 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 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 fileName, List result) { 59 FileObject file = projectDirectory.getFileObject(fileName); 60 61 if (file != null) { 62 result.add(file); 63 } 64 } 65 66 public List getMetadataFiles() { 67 FileObject projectDirectory = project.getProjectDirectory(); 68 List files = new ArrayList (); 69 70 addFile(projectDirectory, "nbproject", files); addFile(projectDirectory, "build.xml", files); addFile(projectDirectory, "catalog.xml", files); 74 return files; 75 } 76 77 public List getDataFiles() { 78 List files = new ArrayList (); 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 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 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 { 118 EjbJarActionProvider ap = (EjbJarActionProvider) project.getLookup().lookup(EjbJarActionProvider.class); 119 120 assert ap != null; 121 122 Lookup context = Lookups.fixed(new Object [0]); 123 Properties p = new Properties (); 124 String [] 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 { 137 project.getAntProjectHelper().notifyDeleted(); 138 } 139 140 public void notifyCopying() { 141 } 143 144 public void notifyCopied(Project original, File originalPath, String nueName) { 145 if (original == null) { 146 return ; 148 } 149 150 project.getReferenceHelper().fixReferences(originalPath); 151 fixOtherReferences(originalPath); 152 153 project.setName(nueName); 154 } 155 156 public void notifyMoving() throws IOException { 157 notifyDeleting(); 158 } 159 160 public void notifyMoved(Project original, File originalPath, final String newName) { 161 if (original == null) { 162 project.getAntProjectHelper().notifyDeleted(); 163 return ; 164 } 165 166 final String oldProjectName = project.getName(); 167 168 project.setName(newName); 169 project.getReferenceHelper().fixReferences(originalPath); 170 fixOtherReferences(originalPath); 171 172 ProjectManager.mutex().writeAccess(new Runnable () { 173 public void run() { 174 AntProjectHelper helper = project.getAntProjectHelper(); 175 EditableProperties projectProps = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 176 177 String jarName = (String ) projectProps.get(EjbJarProjectProperties.JAR_NAME); 178 String oldName = jarName.substring(0, jarName.length() - 4); 179 if (jarName.endsWith(".jar") && oldName.equals(oldProjectName)) projectProps.put(EjbJarProjectProperties.JAR_NAME, newName + ".jar"); 182 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, projectProps); 183 } 184 }); 185 } 186 187 private void fixOtherReferences(final File originalPath) { 188 final String property = EjbJarProjectProperties.META_INF; 189 final File projectDir = FileUtil.toFile(project.getProjectDirectory()); 190 191 ProjectManager.mutex().writeAccess(new Runnable () { 192 public void run() { 193 EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 194 String path = props.getProperty(property); 195 if (path == null) { 196 return; 197 } 198 199 if (path.startsWith(originalPath.getAbsolutePath())) { 200 String relative = PropertyUtils.relativizeFile(originalPath, new File (path)); 201 String fixedPath = new File (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 folder, File 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 |