1 19 20 package org.netbeans.modules.j2ee.earproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URI ; 25 import java.util.Enumeration ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import org.netbeans.api.java.platform.JavaPlatform; 30 import org.netbeans.api.java.platform.JavaPlatformManager; 31 import org.netbeans.api.java.project.JavaProjectConstants; 32 import org.netbeans.api.project.Project; 33 import org.netbeans.api.project.ProjectManager; 34 import org.netbeans.api.project.ProjectUtils; 35 import org.netbeans.api.project.ant.AntArtifact; 36 import org.netbeans.api.project.ant.AntArtifactQuery; 37 import org.netbeans.modules.j2ee.clientproject.api.AppClientProjectGenerator; 38 import org.netbeans.modules.j2ee.dd.api.application.Application; 39 import org.netbeans.modules.j2ee.dd.api.application.DDProvider; 40 import org.netbeans.modules.j2ee.dd.api.application.Module; 41 import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper; 42 import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; 43 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule; 44 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform; 45 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties; 46 import org.netbeans.modules.j2ee.earproject.ui.customizer.VisualClassPathItem; 47 import org.netbeans.modules.j2ee.ejbjarproject.api.EjbJarProjectGenerator; 48 import org.netbeans.modules.web.project.api.WebProjectCreateData; 49 import org.netbeans.modules.web.project.api.WebProjectUtilities; 50 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 51 import org.netbeans.spi.project.AuxiliaryConfiguration; 52 import org.netbeans.spi.project.support.ant.AntProjectHelper; 53 import org.netbeans.spi.project.support.ant.EditableProperties; 54 import org.netbeans.spi.project.support.ant.ProjectGenerator; 55 import org.netbeans.spi.project.support.ant.ReferenceHelper; 56 import org.openide.DialogDisplayer; 57 import org.openide.ErrorManager; 58 import org.openide.NotifyDescriptor; 59 import org.openide.filesystems.FileObject; 60 import org.openide.filesystems.FileStateInvalidException; 61 import org.openide.filesystems.FileUtil; 62 import org.openide.filesystems.Repository; 63 import org.openide.modules.SpecificationVersion; 64 import org.openide.util.NbBundle; 65 import org.w3c.dom.Document ; 66 import org.w3c.dom.Element ; 67 68 74 public final class EarProjectGenerator { 75 76 private static final String DEFAULT_DOC_BASE_FOLDER = "src/conf"; private static final String DEFAULT_BUILD_DIR = "build"; private static final String DEFAULT_RESOURCE_FOLDER = "setup"; 80 private static final String SOURCE_ROOT_REF = "${" + EarProjectProperties.SOURCE_ROOT + "}"; 82 private final File prjDir; 83 private final String name; 84 private final String j2eeLevel; 85 private final String serverInstanceID; 86 private final String sourceLevel; 87 private FileObject prjDirFO; 88 89 private EarProjectGenerator(File prjDir, String name, String j2eeLevel, 90 String serverInstanceID, String sourceLevel) { 91 this.prjDir = prjDir; 92 this.name = name; 93 this.j2eeLevel = j2eeLevel; 94 this.serverInstanceID = serverInstanceID; 95 if (sourceLevel != null && (sourceLevel.equals("1.6") || sourceLevel.equals("1.7"))) 97 sourceLevel = "1.5"; 98 this.sourceLevel = sourceLevel; 99 } 100 101 109 public static AntProjectHelper createProject(File prjDir, String name, String j2eeLevel, 110 String serverInstanceId, String sourceLevel) throws IOException { 111 EarProjectGenerator earGen = new EarProjectGenerator(prjDir, name, j2eeLevel, 112 serverInstanceId, sourceLevel); 113 return earGen.doCreateProject(); 114 } 115 116 public static AntProjectHelper importProject(File pDir, File sDir, String name, 117 String j2eeLevel, String serverInstanceID, String platformName, 118 String sourceLevel, final Map <FileObject, ModuleType> userModules) 119 throws IOException { 120 EarProjectGenerator earGen = new EarProjectGenerator(pDir, name, 121 j2eeLevel, serverInstanceID, sourceLevel); 122 return earGen.doImportProject(sDir, userModules, platformName); 123 } 124 125 private AntProjectHelper doCreateProject() throws IOException { 126 doRefresh(); 127 128 AntProjectHelper h = setupProject(); 129 FileObject docBase = FileUtil.createFolder(prjDirFO, DEFAULT_DOC_BASE_FOLDER); 130 131 FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource( 133 "org-netbeans-modules-j2ee-earproject/MANIFEST.MF"), docBase, "MANIFEST"); 135 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 136 ep.put(EarProjectProperties.SOURCE_ROOT, "."); ep.setProperty(EarProjectProperties.META_INF, DEFAULT_DOC_BASE_FOLDER); 138 ep.setProperty(EarProjectProperties.RESOURCE_DIR, DEFAULT_RESOURCE_FOLDER); 139 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 140 141 Project p = ProjectManager.getDefault().findProject(h.getProjectDirectory()); 142 ProjectManager.getDefault().saveProject(p); 143 EarProject earProject = (EarProject) p.getLookup().lookup(EarProject.class); 144 assert earProject != null; 145 setupDD(j2eeLevel, docBase, earProject); 146 147 return h; 148 } 149 150 private AntProjectHelper doImportProject(final File srcPrjDir, 151 Map <FileObject, ModuleType> userModules, 152 String platformName) throws IOException { 153 154 doRefresh(); 155 156 FileObject srcPrjDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(srcPrjDir)); 157 FileObject docBase = FileUtil.createFolder(srcPrjDirFO, DEFAULT_DOC_BASE_FOLDER); 158 159 AntProjectHelper earHelper = setupProject(); 160 EditableProperties ep = earHelper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 161 ReferenceHelper referenceHelper = new ReferenceHelper(earHelper, 162 earHelper.createAuxiliaryConfiguration(), earHelper.getStandardPropertyEvaluator()); 163 ep.setProperty(EarProjectProperties.SOURCE_ROOT, 164 referenceHelper.createForeignFileReference(srcPrjDir, null)); 165 ep.setProperty(EarProjectProperties.META_INF, createFileReference(referenceHelper, srcPrjDirFO, docBase)); 166 earHelper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 167 168 FileObject earDirFO = earHelper.getProjectDirectory(); 169 EarProject earProject = (EarProject) ProjectManager.getDefault(). 170 findProject(earDirFO).getLookup().lookup(EarProject.class); 171 172 if (null != earProject) { 173 Application app = null; 174 try { 175 FileObject appXml = earProject.getAppModule().getDeploymentDescriptor(); 176 FileObject fileBeingCopied = null; 177 if (null != appXml) { 178 Enumeration filesToBackup = 180 appXml.getParent().getChildren(false); 181 while (null != filesToBackup && 182 filesToBackup.hasMoreElements()) { 183 fileBeingCopied = 184 (FileObject) filesToBackup.nextElement(); 185 if (fileBeingCopied.isData() && 186 fileBeingCopied.canRead()) { 187 try { 188 FileUtil.copyFile(fileBeingCopied, 189 appXml.getParent(), 190 "original_"+fileBeingCopied.getName(), fileBeingCopied.getExt()); 192 } catch (IOException ioe) { 193 } 195 } 196 } 197 app = DDProvider.getDefault().getDDRoot(appXml); 198 Module m[] = app.getModule(); 199 if (null != m && m.length > 0) { 200 earProject.getAppModule().getConfigSupport().ensureConfigurationReady(); 202 for (int k = 0; k < m.length; k++) { 204 app.removeModule(m[k]); 205 } 206 app.write(earProject.getAppModule().getDeploymentDescriptor()); 207 DialogDisplayer.getDefault().notify( 209 new NotifyDescriptor.Message(NbBundle.getMessage(EarProjectGenerator.class, "MESSAGE_CheckContextRoots"), 210 NotifyDescriptor.WARNING_MESSAGE)); 211 } 212 } 213 } catch (IOException ioe) { 214 ErrorManager.getDefault().log(ioe.getLocalizedMessage()); 215 } 216 } 217 218 setupDD(j2eeLevel, docBase, earProject); 219 220 if (userModules == null || userModules.isEmpty()) { 221 userModules = ModuleType.detectModules(srcPrjDirFO); 222 } 223 addUserModules(userModules, platformName, earHelper, earProject); 224 225 227 ProjectManager.getDefault().saveProject(earProject); 228 229 earProject.getAppModule().getConfigSupport().createInitialConfiguration(); 230 if (sourceLevel != null) { 231 EarProjectGenerator.setPlatformSourceLevel(earHelper, sourceLevel); 232 } 233 234 return earHelper; 235 } 236 237 private void doRefresh() throws FileStateInvalidException { 238 prjDir.mkdirs(); 240 File rootF = prjDir; 241 while (rootF.getParentFile() != null) { 242 rootF = rootF.getParentFile(); 243 } 244 FileObject rootFO = FileUtil.toFileObject(FileUtil.normalizeFile(rootF)); 245 assert rootFO != null : "At least disk roots must be mounted! " + rootF; 246 rootFO.getFileSystem().refresh(false); 247 this.prjDirFO = FileUtil.toFileObject(FileUtil.normalizeFile(prjDir)); 248 assert prjDirFO != null : "No such dir on disk: " + prjDir; 249 assert prjDirFO.isFolder() : "Not really a dir: " + prjDir; 250 } 251 252 private void addUserModules(final Map <FileObject, ModuleType> userModules, 253 final String platformName, final AntProjectHelper h, final EarProject earProject) throws IOException { 254 255 AuxiliaryConfiguration aux = h.createAuxiliaryConfiguration(); 256 ReferenceHelper refHelper = new ReferenceHelper(h, aux, h.getStandardPropertyEvaluator()); 257 EarProjectProperties epp = new EarProjectProperties(earProject, refHelper, new EarProjectType()); 258 259 Set <Project> ejbs = new HashSet <Project>(); 260 Set <Project> webAndCars = new HashSet <Project>(); 261 for (Map.Entry <FileObject, ModuleType> entry : userModules.entrySet()) { 262 FileObject subprojectDir = entry.getKey(); 263 ModuleType type = entry.getValue(); 264 Project subProject = addModule(type, epp, platformName, subprojectDir); 265 assert subProject != null : "Directory " + subprojectDir + " does not contain valid project"; 266 switch (type) { 267 case EJB: 268 ejbs.add(subProject); 269 break; 270 case WEB: 271 case CLIENT: 272 webAndCars.add(subProject); 273 break; 274 default: 275 assert false : "Unknown module type: " + type; 276 } 277 } 278 Project[] webAndCarsArray = webAndCars.toArray(new Project[webAndCars.size()]); 279 for (Project ejb : ejbs) { 280 addEJBToClassPaths(ejb, webAndCarsArray); } 282 } 283 284 290 public static void addEJBToClassPaths(final Project ejbJarProject, 291 final Project... projects) throws IOException { 292 assert ejbJarProject != null; 293 AntArtifact[] ejbArtifacts = AntArtifactQuery.findArtifactsByType( 294 ejbJarProject, JavaProjectConstants.ARTIFACT_TYPE_JAR); 295 for (AntArtifact artifact : ejbArtifacts) { 296 for (Project project : projects) { 297 if (project == null) { 298 continue; 299 } 300 ProjectClassPathExtender pcpe = (ProjectClassPathExtender) 301 project.getLookup().lookup(ProjectClassPathExtender.class); 302 URI [] locations = artifact.getArtifactLocations(); 303 if (pcpe != null && locations.length > 0) { pcpe.addAntArtifact(artifact, locations[0].normalize()); 305 } 306 } 307 } 308 } 309 310 private Project addModule(final ModuleType type, final EarProjectProperties epp, 311 final String platformName, final FileObject subprojectRoot) 312 throws IllegalArgumentException , IOException { 313 314 FileObject javaRoot = getJavaRoot(subprojectRoot); 315 File srcFolders[] = getSourceFolders(javaRoot); 316 File subProjDir = FileUtil.normalizeFile( 317 new File (prjDir, subprojectRoot.getNameExt())); 318 AntProjectHelper subProjHelper = null; 319 switch (type) { 320 case WEB: 321 subProjHelper = addWebModule(subprojectRoot, srcFolders, subProjDir, platformName); 322 break; 323 case EJB: 324 subProjHelper = addEJBModule(javaRoot, subprojectRoot, subProjDir, platformName); 325 break; 326 case CLIENT: 327 subProjHelper = addAppClientModule(javaRoot, subprojectRoot, subProjDir, platformName); 328 break; 329 default: 330 assert false : "Unknown module type: " + type; 331 } 332 Project subProject = null; 333 if (null != subProjHelper) { 334 subProject = ProjectManager.getDefault().findProject( 335 subProjHelper.getProjectDirectory()); 336 epp.addJ2eeSubprojects(new Project[] { subProject }); 337 } 338 return subProject; 339 } 340 341 private AntProjectHelper addAppClientModule(final FileObject javaRoot, final FileObject subprojectRoot, final File subProjDir, final String platformName) throws IOException { 342 FileObject docBaseFO = FileUtil.createFolder(subprojectRoot, DEFAULT_DOC_BASE_FOLDER); 343 File docBase = FileUtil.toFile(docBaseFO); 344 AntProjectHelper subProjHelper = AppClientProjectGenerator.importProject( 345 subProjDir, subprojectRoot.getName(), 346 new File [] { FileUtil.toFile(javaRoot) }, 347 new File [0], docBase, 348 null, checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.CLIENT), serverInstanceID); 349 if (platformName != null || sourceLevel != null) { 350 AppClientProjectGenerator.setPlatform(subProjHelper, platformName, sourceLevel); 351 } 352 return subProjHelper; 353 } 354 355 private AntProjectHelper addEJBModule(final FileObject javaRoot, final FileObject subprojectRoot, final File subProjDir, final String platformName) throws IOException { 356 FileObject docBaseFO = FileUtil.createFolder(subprojectRoot, DEFAULT_DOC_BASE_FOLDER); 357 File docBase = FileUtil.toFile(docBaseFO); 358 AntProjectHelper subProjHelper = EjbJarProjectGenerator.importProject( 359 subProjDir, subprojectRoot.getName(), 360 new File [] {FileUtil.toFile(javaRoot)}, 361 new File [0], docBase, 362 null, checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.EJB), serverInstanceID); 363 if (platformName != null || sourceLevel != null) { 364 EjbJarProjectGenerator.setPlatform(subProjHelper, platformName, sourceLevel); 365 } 366 return subProjHelper; 367 } 368 369 private AntProjectHelper addWebModule(final FileObject subprojectRoot, final File srcFolders[], final File subProjDir, final String platformName) throws IOException { 370 WebProjectCreateData createData = new WebProjectCreateData(); 371 createData.setProjectDir(subProjDir); 372 createData.setName(subprojectRoot.getName()); 373 createData.setWebModuleFO(subprojectRoot); 374 createData.setSourceFolders(srcFolders); 375 createData.setTestFolders(new File [0]); 376 createData.setDocBase(FileUtil.createFolder(subprojectRoot, "web")); createData.setLibFolder(null); 378 createData.setJavaEEVersion(checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.WAR)); 379 createData.setServerInstanceID(serverInstanceID); 380 createData.setBuildfile("build.xml"); createData.setJavaPlatformName(platformName); 382 createData.setSourceLevel(sourceLevel); 383 return WebProjectUtilities.importProject(createData); 384 } 385 386 static FileObject setupDD(final String j2eeLevel, final FileObject docBase, 387 final EarProject earProject) throws IOException { 388 FileObject dd = docBase.getFileObject(ProjectEar.FILE_DD); 389 if (dd != null) { 390 return dd; } 392 FileObject template = null; 393 if (J2eeModule.J2EE_14.equals(j2eeLevel)) { 394 template = Repository.getDefault().getDefaultFileSystem().findResource( 395 "org-netbeans-modules-j2ee-earproject/ear-1.4.xml"); } else if (J2eeModule.JAVA_EE_5.equals(j2eeLevel)) { 397 template = Repository.getDefault().getDefaultFileSystem().findResource( 398 "org-netbeans-modules-j2ee-earproject/ear-5.xml"); } else { 400 assert false : "Unknown j2eeLevel: " + j2eeLevel; 401 } 402 if (template != null) { 403 dd = FileUtil.copyFile(template, docBase, "application"); Application app = DDProvider.getDefault().getDDRoot(dd); 405 app.setDisplayName(ProjectUtils.getInformation(earProject).getDisplayName()); 406 EarProjectProperties epp = earProject.getProjectProperties(); 407 for (VisualClassPathItem vcpi : epp.getJarContentAdditional()) { 408 epp.addItemToAppDD(app, vcpi); 409 } 410 app.write(dd); 411 } 412 return dd; 413 } 414 415 420 public static String checkJ2eeVersion(String requestedVersion, String serverInstanceID, Object moduleType) { 421 J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceID); 422 Set <String > versions = platform.getSupportedSpecVersions(moduleType); 423 if (!versions.contains(requestedVersion) && (versions.contains(J2eeModule.J2EE_14))) { 424 return J2eeModule.J2EE_14; 425 } 426 return requestedVersion; 427 } 428 429 private static String relativePath(FileObject parent, FileObject child) { 430 if (child.equals(parent)) { 431 return ""; 432 } 433 if (!FileUtil.isParentOf(parent, child)) { 434 throw new IllegalArgumentException ("Cannot find relative path, " + parent + " is not parent of " + child); } 436 return child.getPath().substring(parent.getPath().length() + 1); 437 } 438 439 private AntProjectHelper setupProject() throws IOException { 440 AntProjectHelper h = ProjectGenerator.createProject(prjDirFO, EarProjectType.TYPE); 441 Element data = h.getPrimaryConfigurationData(true); 442 Document doc = data.getOwnerDocument(); 443 Element nameEl = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); nameEl.appendChild(doc.createTextNode(name)); 445 data.appendChild(nameEl); 446 Element minant = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); minant.appendChild(doc.createTextNode("1.6")); data.appendChild(minant); 449 450 Element wmLibs = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, EarProjectProperties.TAG_WEB_MODULE_LIBRARIES); data.appendChild(wmLibs); 452 453 Element addLibs = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, EarProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES); data.appendChild(addLibs); 455 456 h.putPrimaryConfigurationData(data, true); 457 EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 458 ep.setProperty(EarProjectProperties.DIST_DIR, "dist"); ep.setProperty(EarProjectProperties.DIST_JAR, "${"+EarProjectProperties.DIST_DIR+"}/" + name + ".ear"); 462 ep.setProperty(EarProjectProperties.J2EE_PLATFORM, j2eeLevel); 463 464 ep.setProperty(EarProjectProperties.JAR_NAME, name + ".ear"); ep.setProperty(EarProjectProperties.JAR_COMPRESS, "false"); ep.setProperty(EarProjectProperties.JAR_CONTENT_ADDITIONAL, ""); 467 468 ep.setProperty(EarProjectProperties.CLIENT_MODULE_URI, ""); 469 ep.setProperty(EarProjectProperties.LAUNCH_URL_RELATIVE, ""); 470 ep.setProperty(EarProjectProperties.DISPLAY_BROWSER, "true"); Deployment deployment = Deployment.getDefault(); 472 ep.setProperty(EarProjectProperties.J2EE_SERVER_TYPE, deployment.getServerID(serverInstanceID)); 473 474 String srcLevel = sourceLevel; 475 if (srcLevel == null) { 476 JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); 477 SpecificationVersion v = defaultPlatform.getSpecification().getVersion(); 478 srcLevel = v.toString(); 479 if (srcLevel.equals("1.6") || srcLevel.equals("1.7")) 481 srcLevel = "1.5"; 482 } 483 ep.setProperty(EarProjectProperties.JAVAC_SOURCE, srcLevel); ep.setProperty(EarProjectProperties.JAVAC_DEBUG, "true"); ep.setProperty(EarProjectProperties.JAVAC_DEPRECATION, "false"); 487 ep.setProperty(EarProjectProperties.JAVAC_TARGET, srcLevel); 491 ep.setProperty(EarProjectProperties.BUILD_DIR, DEFAULT_BUILD_DIR); 492 ep.setProperty(EarProjectProperties.BUILD_GENERATED_DIR, "${"+EarProjectProperties.BUILD_DIR+"}/generated"); ep.setProperty(EarProjectProperties.BUILD_CLASSES_EXCLUDES, "**/*.java,**/*.form,**/.nbattrs"); ep.setProperty(EarProjectProperties.NO_DEPENDENCIES, "false"); ep.setProperty(EarProjectProperties.JAVA_PLATFORM, "default_platform"); ep.setProperty(EarProjectProperties.DEBUG_CLASSPATH, 497 "${"+EarProjectProperties.JAVAC_CLASSPATH+"}::${"+ EarProjectProperties.JAR_CONTENT_ADDITIONAL+"}:${"+ EarProjectProperties.RUN_CLASSPATH+"}"); 501 J2eePlatform j2eePlatform = deployment.getJ2eePlatform(serverInstanceID); 502 EarProjectProperties.setACProperties(j2eePlatform, ep); 503 h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 504 505 ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH); 506 ep.setProperty(EarProjectProperties.J2EE_SERVER_INSTANCE, serverInstanceID); 507 508 try { 510 AntDeploymentHelper.writeDeploymentScript(new File (prjDir, 511 EarProjectProperties.ANT_DEPLOY_BUILD_SCRIPT), 512 J2eeModule.EAR, serverInstanceID); 513 } catch (IOException ioe) { 514 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 515 } 516 File deployAntPropsFile = AntDeploymentHelper.getDeploymentPropertiesFile(serverInstanceID); 517 if (deployAntPropsFile != null) { 518 ep.setProperty(EarProjectProperties.DEPLOY_ANT_PROPS_FILE, deployAntPropsFile.getAbsolutePath()); 519 } 520 521 EarProjectProperties.setACPrivateProperties(j2eePlatform, serverInstanceID, ep); 522 523 h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep); 524 Project p = ProjectManager.getDefault().findProject(prjDirFO); 525 ProjectManager.getDefault().saveProject(p); 526 return h; 527 } 528 529 private String createFileReference(ReferenceHelper refHelper, 530 FileObject sourceprojectFO, FileObject referencedFO) { 531 if (FileUtil.isParentOf(prjDirFO, referencedFO)) { 532 return relativePath(prjDirFO, referencedFO); 533 } else if (FileUtil.isParentOf(sourceprojectFO, referencedFO)) { 534 String s = relativePath(sourceprojectFO, referencedFO); 535 return s.length() > 0 ? SOURCE_ROOT_REF + '/' + s : SOURCE_ROOT_REF; 536 } else { 537 return refHelper.createForeignFileReference(FileUtil.toFile(referencedFO), null); 538 } 539 } 540 541 public static void setPlatformSourceLevel(final AntProjectHelper helper, final String sourceLevel) { 542 ProjectManager.mutex().writeAccess(new Runnable () { 543 public void run() { 544 try { 545 EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 546 String srcLevel = sourceLevel; 548 if (sourceLevel.equals("1.6") || sourceLevel.equals("1.7")) 549 srcLevel = "1.5"; 550 ep.setProperty(EarProjectProperties.JAVAC_SOURCE, srcLevel); 551 ep.setProperty(EarProjectProperties.JAVAC_TARGET, srcLevel); 552 helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 553 ProjectManager.getDefault().saveProject(ProjectManager.getDefault().findProject(helper.getProjectDirectory())); 554 } catch (IOException e) { 555 ErrorManager.getDefault().notify(e); 556 } 557 } 558 }); 559 } 560 561 public static String toClasspathString(File [] classpathEntries) { 562 if (classpathEntries == null) { 563 return ""; 564 } 565 StringBuffer classpath = new StringBuffer (); 566 for (int i = 0; i < classpathEntries.length; i++) { 567 classpath.append(classpathEntries[i].getAbsolutePath()); 568 if (i + 1 < classpathEntries.length) { 569 classpath.append(':'); 570 } 571 } 572 return classpath.toString(); 573 } 574 575 private FileObject getJavaRoot(final FileObject moduleRoot) throws IOException { 576 FileObject javaRoot = moduleRoot.getFileObject("src/java"); if (null == javaRoot) { 579 FileObject srcDir = moduleRoot.getFileObject("src"); if (null == srcDir) { 581 srcDir = moduleRoot.createFolder("src"); } 583 javaRoot = srcDir.createFolder("java"); } 585 return javaRoot; 586 } 588 589 private File [] getSourceFolders(final FileObject javaRoot) { 590 return null == javaRoot ? new File [0] : 591 new File [] { FileUtil.toFile(javaRoot) }; 592 } 593 594 } 595 | Popular Tags |