1 19 20 package org.netbeans.modules.apisupport.project.suite; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import org.netbeans.api.project.ProjectManager; 26 import org.netbeans.modules.apisupport.project.ProjectXMLManager; 27 import org.netbeans.modules.apisupport.project.universe.ModuleList; 28 import org.netbeans.spi.project.support.ant.AntProjectHelper; 29 import org.netbeans.spi.project.support.ant.EditableProperties; 30 import org.openide.filesystems.FileLock; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.util.Mutex; 34 import org.openide.util.MutexException; 35 36 41 public class SuiteProjectGenerator { 42 43 private static final String PLATFORM_PROPERTIES_PATH = 44 "nbproject/platform.properties"; public static final String PROJECT_PROPERTIES_PATH = "nbproject/project.properties"; public static final String PRIVATE_PROPERTIES_PATH = "nbproject/private/private.properties"; 48 49 private SuiteProjectGenerator() {} 50 51 52 public static void createSuiteProject(final File projectDir, final String platformID) throws IOException { 53 try { 54 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 55 public Object run() throws IOException { 56 final FileObject dirFO = FileUtil.createFolder(projectDir); 57 if (ProjectManager.getDefault().findProject(dirFO) != null) { 58 throw new IllegalArgumentException ("Already a project in " + dirFO); } 60 createSuiteProjectXML(dirFO); 61 createPlatformProperties(dirFO, platformID); 62 createProjectProperties(dirFO); 63 ModuleList.refresh(); 64 ProjectManager.getDefault().clearNonProjectCache(); 65 return null; 66 } 67 }); 68 } catch (MutexException e) { 69 throw (IOException ) e.getException(); 70 } 71 } 72 73 78 private static void createSuiteProjectXML(FileObject projectDir) throws IOException { 79 ProjectXMLManager.generateEmptySuiteTemplate( 80 createFileObject(projectDir, AntProjectHelper.PROJECT_XML_PATH), 81 projectDir.getNameExt()); 82 } 83 84 private static void createPlatformProperties(FileObject projectDir, String platformID) throws IOException { 85 FileObject plafPropsFO = createFileObject( 86 projectDir, PLATFORM_PROPERTIES_PATH); 87 EditableProperties props = new EditableProperties(true); 88 props.setProperty("nbplatform.active", platformID); storeProperties(plafPropsFO, props); 90 } 91 92 private static void createProjectProperties(FileObject projectDir) throws IOException { 93 FileObject propsFO = createFileObject(projectDir, PROJECT_PROPERTIES_PATH); 95 EditableProperties props = new EditableProperties(true); 96 props.setProperty("modules", ""); storeProperties(propsFO, props); 98 } 99 100 101 private static void storeProperties(FileObject bundleFO, EditableProperties props) throws IOException { 102 FileLock lock = bundleFO.lock(); 103 try { 104 OutputStream os = bundleFO.getOutputStream(lock); 105 try { 106 props.store(os); 107 } finally { 108 os.close(); 109 } 110 } finally { 111 lock.releaseLock(); 112 } 113 } 114 115 120 private static FileObject createFileObject(FileObject dir, String relToDir) throws IOException { 121 FileObject createdFO = dir.getFileObject(relToDir); 122 if (createdFO != null) { 123 throw new IllegalArgumentException ("File " + createdFO + " already exists."); } 125 createdFO = FileUtil.createData(dir, relToDir); 126 return createdFO; 127 } 128 } 129 130 131 | Popular Tags |