1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 import java.util.jar.JarEntry ; 33 import java.util.jar.JarFile ; 34 import org.netbeans.api.project.ProjectManager; 35 import org.netbeans.api.queries.CollocationQuery; 36 import org.netbeans.modules.apisupport.project.ui.customizer.SingleModuleProperties; 37 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo; 38 import org.netbeans.modules.apisupport.project.universe.ModuleList; 39 import org.netbeans.spi.project.support.ant.AntProjectHelper; 40 import org.netbeans.spi.project.support.ant.EditableProperties; 41 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper; 42 import org.netbeans.spi.project.support.ant.PropertyUtils; 43 import org.openide.ErrorManager; 44 import org.openide.filesystems.FileLock; 45 import org.openide.filesystems.FileObject; 46 import org.openide.filesystems.FileUtil; 47 import org.openide.util.Mutex; 48 import org.openide.util.MutexException; 49 import org.openide.xml.XMLUtil; 50 import org.w3c.dom.Document ; 51 import org.w3c.dom.Element ; 52 53 58 public class NbModuleProjectGenerator { 59 60 public static final String PLATFORM_PROPERTIES_PATH = 61 "nbproject/platform.properties"; 63 64 private NbModuleProjectGenerator() {} 65 66 67 public static void createStandAloneModule(final File projectDir, final String cnb, 68 final String name, final String bundlePath, 69 final String layerPath, final String platformID) throws IOException { 70 try { 71 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 72 public Object run() throws IOException { 73 final FileObject dirFO = FileUtil.createFolder(projectDir); 74 if (ProjectManager.getDefault().findProject(dirFO) != null) { 75 throw new IllegalArgumentException ("Already a project in " + dirFO); } 77 createProjectXML(dirFO, cnb, NbModuleProvider.STANDALONE); 78 createPlatformProperties(dirFO, platformID); 79 createManifest(dirFO, cnb, bundlePath, layerPath); 80 if (bundlePath != null) { 81 createBundle(dirFO, bundlePath, name); 82 } 83 if (layerPath != null) { 84 createLayerInSrc(dirFO, layerPath); 85 } 86 createEmptyTestDir(dirFO); 87 ModuleList.refresh(); 88 ProjectManager.getDefault().clearNonProjectCache(); 89 return null; 90 } 91 }); 92 } catch (MutexException e) { 93 throw (IOException ) e.getException(); 94 } 95 } 96 97 98 public static void createSuiteComponentModule(final File projectDir, final String cnb, 99 final String name, final String bundlePath, 100 final String layerPath, final File suiteDir) throws IOException { 101 try { 102 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 103 public Object run() throws IOException { 104 final FileObject dirFO = FileUtil.createFolder(projectDir); 105 if (ProjectManager.getDefault().findProject(dirFO) != null) { 106 throw new IllegalArgumentException ("Already a project in " + dirFO); } 108 createProjectXML(dirFO, cnb, NbModuleProvider.SUITE_COMPONENT); 109 createSuiteProperties(dirFO, suiteDir); 110 createManifest(dirFO, cnb, bundlePath, layerPath); 111 if (bundlePath != null) { 112 createBundle(dirFO, bundlePath, name); 113 } 114 if (layerPath != null) { 115 createLayerInSrc(dirFO, layerPath); 116 } 117 createEmptyTestDir(dirFO); 118 ModuleList.refresh(); 119 ProjectManager.getDefault().clearNonProjectCache(); 120 appendToSuite(cnb, dirFO, suiteDir); 121 return null; 122 } 123 }); 124 } catch (MutexException e) { 125 throw (IOException ) e.getException(); 126 } 127 } 128 129 130 public static void createSuiteLibraryModule(final File projectDir, final String cnb, 131 final String name, final String bundlePath, final File suiteDir, 132 final File license, final File [] jars) throws IOException { 133 try { 134 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 135 public Object run() throws IOException { 136 final FileObject dirFO = FileUtil.createFolder(projectDir); 137 if (ProjectManager.getDefault().findProject(dirFO) != null) { 138 throw new IllegalArgumentException ("Already a project in " + dirFO); } 140 141 EditableProperties props = new EditableProperties(true); 142 props.put(SingleModuleProperties.IS_AUTOLOAD, "true"); SortedSet <String > packageList = new TreeSet (); 144 Map classPathExtensions = new HashMap (); 145 146 File releaseDir = new File (projectDir, "release/modules/ext"); if (!releaseDir.mkdirs()) { 148 Util.err.log("cannot create release directory."); 150 } 151 FileObject relDirFo = FileUtil.toFileObject(releaseDir); 152 for (int i = 0; i < jars.length; i++) { 153 FileObject orig = FileUtil.toFileObject(FileUtil.normalizeFile(jars[i])); 154 if (orig != null) { 155 JarFile jf = null; 156 try { 157 FileUtil.copyFile(orig, relDirFo, orig.getName()); 158 jf = new JarFile (jars[i]); 159 Enumeration en = jf.entries(); 160 while (en.hasMoreElements()) { 161 JarEntry entry = (JarEntry )en.nextElement(); 162 if (!entry.isDirectory() && entry.getName().endsWith(".class")) { String nm = entry.getName(); 164 if (!Util.isValidJavaFQN(nm.substring(0, nm.length() - 6).replace('/', '.'))) { 165 continue; } 167 int index = nm.lastIndexOf('/'); 168 if (index > -1) { 169 String path = nm.substring(0, index); 170 packageList.add(path.replace('/', '.')); 171 } 172 } 173 } 174 classPathExtensions.put("ext/" + orig.getNameExt(), "release/modules/ext/" + orig.getNameExt()); } catch (IOException e) { 176 Util.err.notify(e); 178 } finally { 179 if (jf != null) { 180 try { 181 jf.close(); 182 } catch (IOException e) { 183 Util.err.notify(ErrorManager.INFORMATIONAL, e); 184 } 185 } 186 } 187 } 188 } 189 190 if (license != null && license.exists()) { 191 FileObject fo = FileUtil.toFileObject(license); 192 try { 193 FileUtil.copyFile(fo, dirFO, fo.getName()); 194 props.put(SingleModuleProperties.LICENSE_FILE, "${basedir}/" + fo.getNameExt()); } catch (IOException e) { 197 Util.err.notify(e); 199 } 200 201 } 202 ProjectXMLManager.generateLibraryModuleTemplate( 203 createFileObject(dirFO, AntProjectHelper.PROJECT_XML_PATH), 204 cnb, NbModuleProvider.SUITE_COMPONENT, packageList, classPathExtensions); 205 createSuiteProperties(dirFO, suiteDir); 206 createManifest(dirFO, cnb, bundlePath, null); 207 createBundle(dirFO, bundlePath, name); 208 209 FileObject bundleFO = createFileObject( 211 dirFO, "nbproject/project.properties"); Util.storeProperties(bundleFO, props); 213 214 ModuleList.refresh(); 215 ProjectManager.getDefault().clearNonProjectCache(); 216 appendToSuite(cnb, dirFO, suiteDir); 217 return null; 218 } 219 }); 220 } catch (MutexException e) { 221 throw (IOException ) e.getException(); 222 } 223 } 224 225 228 public static void createNetBeansOrgModule(final File projectDir, final String cnb, 229 final String name, final String bundlePath, final String layerPath) throws IOException { 230 try { 231 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() { 232 public Object run() throws IOException { 233 File nborg = ModuleList.findNetBeansOrg(projectDir); 234 if (nborg == null) { 235 throw new IllegalArgumentException (projectDir + " doesn't " + "point to directory within the netbeans.org CVS tree"); } 238 final FileObject dirFO = FileUtil.createFolder(projectDir); 239 if (ProjectManager.getDefault().findProject(dirFO) != null) { 240 throw new IllegalArgumentException ("Already a project in " + dirFO); } 242 createNetBeansOrgBuildXML(dirFO, cnb, nborg); 243 createProjectXML(dirFO, cnb, NbModuleProvider.NETBEANS_ORG); 244 createManifest(dirFO, cnb, bundlePath, layerPath); 245 createBundle(dirFO, bundlePath, name); 246 createLayerInSrc(dirFO, layerPath); 247 createEmptyTestDir(dirFO); 248 ModuleList.refresh(); 249 ProjectManager.getDefault().clearNonProjectCache(); 250 return null; 251 } 252 }); 253 } catch (MutexException e) { 254 throw (IOException ) e.getException(); 255 } 256 } 257 258 263 private static void createProjectXML(FileObject projectDir, 264 String cnb, NbModuleProvider.NbModuleType type) throws IOException { 265 ProjectXMLManager.generateEmptyModuleTemplate( 266 createFileObject(projectDir, AntProjectHelper.PROJECT_XML_PATH), 267 cnb, type); 268 } 269 270 274 private static void createNetBeansOrgBuildXML(FileObject projectDir, String cnb, 275 File nborg) throws IOException { 276 FileObject buildScript = NbModuleProjectGenerator.createFileObject( 277 projectDir, GeneratedFilesHelper.BUILD_XML_PATH); 278 Document prjDoc = XMLUtil.createDocument("project", null, null, null); Element prjEl = prjDoc.getDocumentElement(); 280 prjEl.setAttribute("name", PropertyUtils.relativizeFile(nborg, FileUtil.toFile(projectDir))); 282 prjEl.setAttribute("default", "netbeans"); prjEl.setAttribute("basedir", "."); 285 Element el = prjDoc.createElement("description"); el.appendChild(prjDoc.createTextNode("Builds, tests, and runs the " + "project " + cnb)); prjEl.appendChild(el); 289 290 el = prjDoc.createElement("import"); el.setAttribute("file", PropertyUtils.relativizeFile(FileUtil.toFile(projectDir), new File (nborg, "nbbuild/templates/projectized.xml"))); prjEl.appendChild(el); 294 295 FileLock lock = buildScript.lock(); 297 try { 298 OutputStream os = buildScript.getOutputStream(lock); 299 try { 300 XMLUtil.write(prjDoc, os, "UTF-8"); } finally { 302 os.close(); 303 } 304 } finally { 305 lock.releaseLock(); 306 } 307 } 308 309 315 public static void createSuiteProperties(FileObject projectDir, File suiteDir) throws IOException { 316 File projectDirF = FileUtil.toFile(projectDir); 317 String suiteLocation; 318 String suitePropertiesLocation; 319 if (CollocationQuery.areCollocated(projectDirF, suiteDir)) { 320 suiteLocation = "${basedir}/" + PropertyUtils.relativizeFile(projectDirF, suiteDir); suitePropertiesLocation = "nbproject/suite.properties"; } else { 323 suiteLocation = suiteDir.getAbsolutePath(); 324 suitePropertiesLocation = "nbproject/private/suite-private.properties"; } 326 EditableProperties props = new EditableProperties(true); 327 props.setProperty("suite.dir", suiteLocation); FileObject suiteProperties = createFileObject(projectDir, suitePropertiesLocation); 329 Util.storeProperties(suiteProperties, props); 330 } 331 332 339 private static void appendToSuite(String cnb, FileObject projectDir, File suiteDir) throws IOException { 340 File projectDirF = FileUtil.toFile(projectDir); 341 File suiteGlobalPropsFile = new File (suiteDir, "nbproject/project.properties"); FileObject suiteGlobalPropFO; 343 if (suiteGlobalPropsFile.exists()) { 344 suiteGlobalPropFO = FileUtil.toFileObject(suiteGlobalPropsFile); 345 } else { 346 suiteGlobalPropFO = createFileObject(suiteGlobalPropsFile); 347 } 348 EditableProperties globalProps = Util.loadProperties(suiteGlobalPropFO); 349 String projectPropKey = "project." + cnb; if (CollocationQuery.areCollocated(projectDirF, suiteDir)) { 351 globalProps.setProperty(projectPropKey, 352 PropertyUtils.relativizeFile(suiteDir, projectDirF)); 353 } else { 354 File suitePrivPropsFile = new File (suiteDir, "nbproject/private/private.properties"); FileObject suitePrivPropFO; 356 if (suitePrivPropsFile.exists()) { 357 suitePrivPropFO = FileUtil.toFileObject(suitePrivPropsFile); 358 } else { 359 suitePrivPropFO = createFileObject(suitePrivPropsFile); 360 } 361 EditableProperties privProps= Util.loadProperties(suitePrivPropFO); 362 privProps.setProperty(projectPropKey, projectDirF.getAbsolutePath()); 363 Util.storeProperties(suitePrivPropFO, privProps); 364 } 365 String modulesProp = globalProps.getProperty("modules"); if (modulesProp == null) { 367 modulesProp = ""; 368 } 369 if (modulesProp.length() > 0) { 370 modulesProp += ":"; } 372 modulesProp += "${" + projectPropKey + "}"; globalProps.setProperty("modules", modulesProp.split("(?<=:)", -1)); Util.storeProperties(suiteGlobalPropFO, globalProps); 375 } 376 377 private static void createPlatformProperties(FileObject projectDir, String platformID) throws IOException { 378 FileObject plafPropsFO = createFileObject( 379 projectDir, NbModuleProjectGenerator.PLATFORM_PROPERTIES_PATH); 380 EditableProperties props = new EditableProperties(true); 381 props.put("nbplatform.active", platformID); Util.storeProperties(plafPropsFO, props); 383 } 384 385 private static void createManifest(FileObject projectDir, String cnb, 386 String bundlePath, String layerPath) throws IOException { 387 FileObject manifestFO = createFileObject( 388 projectDir, "manifest.mf"); ManifestManager.createManifest(manifestFO, cnb, "1.0", bundlePath, layerPath); } 391 392 private static void createBundle(FileObject projectDir, String bundlePath, 393 String name) throws IOException { 394 String pathToBundle = "src/" + bundlePath.replace('\\','/'); FileObject bundleFO = createFileObject(projectDir, pathToBundle); 396 EditableProperties props = new EditableProperties(true); 397 props.put(LocalizedBundleInfo.NAME, name); 398 Util.storeProperties(bundleFO, props); 399 } 400 401 private static void createLayerInSrc(FileObject projectDir, String layerPath) throws IOException { 402 createLayer(projectDir, "src/" + layerPath); } 404 405 public static FileObject createLayer(FileObject projectDir, String layerPath) throws IOException { 406 FileObject layerFO = createFileObject(projectDir, layerPath); FileLock lock = layerFO.lock(); 408 try { 409 InputStream is = NbModuleProjectGenerator.class.getResourceAsStream("ui/resources/layer_template.xml"); try { 411 OutputStream os = layerFO.getOutputStream(lock); 412 try { 413 FileUtil.copy(is, os); 414 } finally { 415 os.close(); 416 } 417 } finally { 418 is.close(); 419 } 420 } finally { 421 lock.releaseLock(); 422 } 423 return layerFO; 424 } 425 426 private static void createEmptyTestDir(FileObject projectDir) throws IOException { 427 FileUtil.createFolder(projectDir, "test/unit/src"); } 429 430 435 private static FileObject createFileObject(FileObject dir, String relToDir) throws IOException { 436 FileObject createdFO = dir.getFileObject(relToDir); 437 if (createdFO != null) { 438 throw new IllegalArgumentException ("File " + createdFO + " already exists."); } 440 createdFO = FileUtil.createData(dir, relToDir); 441 return createdFO; 442 } 443 444 449 private static FileObject createFileObject(File fileToCreate) throws IOException { 450 File parent = fileToCreate.getParentFile(); 451 if (parent == null) { 452 throw new IllegalArgumentException ("Cannot create: " + fileToCreate); } 454 if (!parent.exists()) { 455 parent.mkdirs(); 456 } 457 return createFileObject( 458 FileUtil.toFileObject(parent), fileToCreate.getName()); 459 } 460 461 } 462 | Popular Tags |