KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarProjectGenerator


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
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 JavaDoc;
66 import org.w3c.dom.Element JavaDoc;
67
68 /**
69  * Creates a fresh EarProject from scratch or imports an existing Enterprise
70  * Application.
71  *
72  * @author vince kraemer
73  */

74 public final class EarProjectGenerator {
75     
76     private static final String JavaDoc DEFAULT_DOC_BASE_FOLDER = "src/conf"; //NOI18N
77
private static final String JavaDoc DEFAULT_BUILD_DIR = "build"; //NOI18N
78
private static final String JavaDoc DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
79

80     private static final String JavaDoc SOURCE_ROOT_REF = "${" + EarProjectProperties.SOURCE_ROOT + "}"; //NOI18N
81

82     private final File JavaDoc prjDir;
83     private final String JavaDoc name;
84     private final String JavaDoc j2eeLevel;
85     private final String JavaDoc serverInstanceID;
86     private final String JavaDoc sourceLevel;
87     private FileObject prjDirFO;
88     
89     private EarProjectGenerator(File JavaDoc prjDir, String JavaDoc name, String JavaDoc j2eeLevel,
90             String JavaDoc serverInstanceID, String JavaDoc sourceLevel) {
91         this.prjDir = prjDir;
92         this.name = name;
93         this.j2eeLevel = j2eeLevel;
94         this.serverInstanceID = serverInstanceID;
95         // #89131: these levels are not actually distinct from 1.5.
96
if (sourceLevel != null && (sourceLevel.equals("1.6") || sourceLevel.equals("1.7")))
97             sourceLevel = "1.5";
98         this.sourceLevel = sourceLevel;
99     }
100     
101     /**
102      * Creates a new empty Enterprise Application project.
103      *
104      * @param dir the top-level directory (need not yet exist but if it does it must be empty)
105      * @param name the code name for the project
106      * @return the helper object permitting it to be further customized
107      * @throws IOException in case something went wrong
108      */

109     public static AntProjectHelper createProject(File JavaDoc prjDir, String JavaDoc name, String JavaDoc j2eeLevel,
110             String JavaDoc serverInstanceId, String JavaDoc sourceLevel) throws IOException JavaDoc {
111         EarProjectGenerator earGen = new EarProjectGenerator(prjDir, name, j2eeLevel,
112                 serverInstanceId, sourceLevel);
113         return earGen.doCreateProject();
114     }
115     
116     public static AntProjectHelper importProject(File JavaDoc pDir, File JavaDoc sDir, String JavaDoc name,
117             String JavaDoc j2eeLevel, String JavaDoc serverInstanceID, String JavaDoc platformName,
118             String JavaDoc sourceLevel, final Map JavaDoc<FileObject, ModuleType> userModules)
119             throws IOException JavaDoc {
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 JavaDoc {
126         doRefresh();
127         
128         AntProjectHelper h = setupProject();
129         FileObject docBase = FileUtil.createFolder(prjDirFO, DEFAULT_DOC_BASE_FOLDER);
130         
131         // create a default manifest
132
FileUtil.copyFile(Repository.getDefault().getDefaultFileSystem().findResource(
133                 "org-netbeans-modules-j2ee-earproject/MANIFEST.MF"), docBase, "MANIFEST"); // NOI18N
134

135         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
136         ep.put(EarProjectProperties.SOURCE_ROOT, "."); //NOI18N
137
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 JavaDoc srcPrjDir,
151             Map JavaDoc<FileObject, ModuleType> userModules,
152             String JavaDoc platformName) throws IOException JavaDoc {
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                     // make a backup copy of the application.xml and its siblings
179
Enumeration JavaDoc 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(), // NOI18N
191
fileBeingCopied.getExt());
192                             } catch (IOException JavaDoc ioe) {
193                                 // this is not fatal
194
}
195                         }
196                     }
197                     app = DDProvider.getDefault().getDDRoot(appXml);
198                     Module m[] = app.getModule();
199                     if (null != m && m.length > 0) {
200                         // make sure the config object has told us what to listen to...
201
earProject.getAppModule().getConfigSupport().ensureConfigurationReady();
202                         // delete the modules
203
for (int k = 0; k < m.length; k++) {
204                             app.removeModule(m[k]);
205                         }
206                         app.write(earProject.getAppModule().getDeploymentDescriptor());
207                         // notify the user here....
208
DialogDisplayer.getDefault().notify(
209                                 new NotifyDescriptor.Message(NbBundle.getMessage(EarProjectGenerator.class, "MESSAGE_CheckContextRoots"),
210                                 NotifyDescriptor.WARNING_MESSAGE));
211                     }
212                 }
213             } catch (IOException JavaDoc 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         // XXX all web module URI-to-ContextRoot mapping should happen here
226

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         // XXX clumsy way to refresh, but otherwise it doesn't work for new folders
239
prjDir.mkdirs();
240         File JavaDoc 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 JavaDoc<FileObject, ModuleType> userModules,
253             final String JavaDoc platformName, final AntProjectHelper h, final EarProject earProject) throws IOException JavaDoc {
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 JavaDoc<Project> ejbs = new HashSet JavaDoc<Project>();
260         Set JavaDoc<Project> webAndCars = new HashSet JavaDoc<Project>();
261         for (Map.Entry JavaDoc<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); // #74123
281
}
282     }
283     
284     /**
285      * Adds EJB's artifact to Web and Application Client projects' classpaths.
286      *
287      * @param ejbJarProject must not be <code>null</code>
288      * @param projects may contains also <code>null</code> elements
289      */

290     public static void addEJBToClassPaths(final Project ejbJarProject,
291             final Project... projects) throws IOException JavaDoc {
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 JavaDoc[] locations = artifact.getArtifactLocations();
303                 if (pcpe != null && locations.length > 0) { // sanity check
304
pcpe.addAntArtifact(artifact, locations[0].normalize());
305                 }
306             }
307         }
308     }
309     
310     private Project addModule(final ModuleType type, final EarProjectProperties epp,
311             final String JavaDoc platformName, final FileObject subprojectRoot)
312             throws IllegalArgumentException JavaDoc, IOException JavaDoc {
313         
314         FileObject javaRoot = getJavaRoot(subprojectRoot);
315         File JavaDoc srcFolders[] = getSourceFolders(javaRoot);
316         File JavaDoc subProjDir = FileUtil.normalizeFile(
317                 new File JavaDoc(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 JavaDoc subProjDir, final String JavaDoc platformName) throws IOException JavaDoc {
342         FileObject docBaseFO = FileUtil.createFolder(subprojectRoot, DEFAULT_DOC_BASE_FOLDER);
343         File JavaDoc docBase = FileUtil.toFile(docBaseFO);
344         AntProjectHelper subProjHelper = AppClientProjectGenerator.importProject(
345                 subProjDir, subprojectRoot.getName(),
346                 new File JavaDoc[] { FileUtil.toFile(javaRoot) },
347                 new File JavaDoc[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 JavaDoc subProjDir, final String JavaDoc platformName) throws IOException JavaDoc {
356         FileObject docBaseFO = FileUtil.createFolder(subprojectRoot, DEFAULT_DOC_BASE_FOLDER);
357         File JavaDoc docBase = FileUtil.toFile(docBaseFO);
358         AntProjectHelper subProjHelper = EjbJarProjectGenerator.importProject(
359                 subProjDir, subprojectRoot.getName(),
360                 new File JavaDoc[] {FileUtil.toFile(javaRoot)},
361                 new File JavaDoc[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 JavaDoc srcFolders[], final File JavaDoc subProjDir, final String JavaDoc platformName) throws IOException JavaDoc {
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 JavaDoc[0]);
376         createData.setDocBase(FileUtil.createFolder(subprojectRoot, "web")); //NOI18N
377
createData.setLibFolder(null);
378         createData.setJavaEEVersion(checkJ2eeVersion(j2eeLevel, serverInstanceID, J2eeModule.WAR));
379         createData.setServerInstanceID(serverInstanceID);
380         createData.setBuildfile("build.xml"); //NOI18N
381
createData.setJavaPlatformName(platformName);
382         createData.setSourceLevel(sourceLevel);
383         return WebProjectUtilities.importProject(createData);
384     }
385     
386     static FileObject setupDD(final String JavaDoc j2eeLevel, final FileObject docBase,
387             final EarProject earProject) throws IOException JavaDoc {
388         FileObject dd = docBase.getFileObject(ProjectEar.FILE_DD);
389         if (dd != null) {
390             return dd; // already created
391
}
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"); // NOI18N
396
} else if (J2eeModule.JAVA_EE_5.equals(j2eeLevel)) {
397             template = Repository.getDefault().getDefaultFileSystem().findResource(
398                     "org-netbeans-modules-j2ee-earproject/ear-5.xml"); // NOI18N
399
} else {
400             assert false : "Unknown j2eeLevel: " + j2eeLevel;
401         }
402         if (template != null) {
403             dd = FileUtil.copyFile(template, docBase, "application"); // NOI18N
404
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     /** Check that the J2EE version requested for the EAR is also supported for
416      * the module type and if not suggest a different version.
417      * For now the only check is to use J2EE 1.4 if JavaEE5 is not supported.
418      * Otherwise use the requestedVersion.
419      */

420     public static String JavaDoc checkJ2eeVersion(String JavaDoc requestedVersion, String JavaDoc serverInstanceID, Object JavaDoc moduleType) {
421         J2eePlatform platform = Deployment.getDefault().getJ2eePlatform(serverInstanceID);
422         Set JavaDoc <String JavaDoc> 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 JavaDoc relativePath(FileObject parent, FileObject child) {
430         if (child.equals(parent)) {
431             return "";
432         }
433         if (!FileUtil.isParentOf(parent, child)) {
434             throw new IllegalArgumentException JavaDoc("Cannot find relative path, " + parent + " is not parent of " + child); // NOI18N
435
}
436         return child.getPath().substring(parent.getPath().length() + 1);
437     }
438     
439     private AntProjectHelper setupProject() throws IOException JavaDoc {
440         AntProjectHelper h = ProjectGenerator.createProject(prjDirFO, EarProjectType.TYPE);
441         Element JavaDoc data = h.getPrimaryConfigurationData(true);
442         Document JavaDoc doc = data.getOwnerDocument();
443         Element JavaDoc nameEl = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "name"); // NOI18N
444
nameEl.appendChild(doc.createTextNode(name));
445         data.appendChild(nameEl);
446         Element JavaDoc minant = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "minimum-ant-version"); // NOI18N
447
minant.appendChild(doc.createTextNode("1.6")); // NOI18N
448
data.appendChild(minant);
449         
450         Element JavaDoc wmLibs = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, EarProjectProperties.TAG_WEB_MODULE_LIBRARIES); //NOI18N
451
data.appendChild(wmLibs);
452         
453         Element JavaDoc addLibs = doc.createElementNS(EarProjectType.PROJECT_CONFIGURATION_NAMESPACE, EarProjectProperties.TAG_WEB_MODULE__ADDITIONAL_LIBRARIES); //NOI18N
454
data.appendChild(addLibs);
455         
456         h.putPrimaryConfigurationData(data, true);
457         EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
458         // XXX the following just for testing, TBD:
459
ep.setProperty(EarProjectProperties.DIST_DIR, "dist"); // NOI18N
460
ep.setProperty(EarProjectProperties.DIST_JAR, "${"+EarProjectProperties.DIST_DIR+"}/" + name + ".ear"); // NOI18N
461

462         ep.setProperty(EarProjectProperties.J2EE_PLATFORM, j2eeLevel);
463         
464         ep.setProperty(EarProjectProperties.JAR_NAME, name + ".ear"); // NOI18N
465
ep.setProperty(EarProjectProperties.JAR_COMPRESS, "false"); // NOI18N
466
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"); // NOI18N
471
Deployment deployment = Deployment.getDefault();
472         ep.setProperty(EarProjectProperties.J2EE_SERVER_TYPE, deployment.getServerID(serverInstanceID));
473         
474         String JavaDoc srcLevel = sourceLevel;
475         if (srcLevel == null) {
476             JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
477             SpecificationVersion v = defaultPlatform.getSpecification().getVersion();
478             srcLevel = v.toString();
479             // #89131: these levels are not actually distinct from 1.5.
480
if (srcLevel.equals("1.6") || srcLevel.equals("1.7"))
481                 srcLevel = "1.5";
482         }
483         ep.setProperty(EarProjectProperties.JAVAC_SOURCE, srcLevel); //NOI18N
484
ep.setProperty(EarProjectProperties.JAVAC_DEBUG, "true"); // NOI18N
485
ep.setProperty(EarProjectProperties.JAVAC_DEPRECATION, "false"); // NOI18N
486

487         //xxx Default should be 1.2
488
//http://projects.netbeans.org/buildsys/j2se-project-ui-spec.html#Build_Compiling_Sources
489
ep.setProperty(EarProjectProperties.JAVAC_TARGET, srcLevel); //NOI18N
490

491         ep.setProperty(EarProjectProperties.BUILD_DIR, DEFAULT_BUILD_DIR);
492         ep.setProperty(EarProjectProperties.BUILD_GENERATED_DIR, "${"+EarProjectProperties.BUILD_DIR+"}/generated"); // NOI18N
493
ep.setProperty(EarProjectProperties.BUILD_CLASSES_EXCLUDES, "**/*.java,**/*.form,**/.nbattrs"); // NOI18N
494
ep.setProperty(EarProjectProperties.NO_DEPENDENCIES, "false"); // NOI18N
495
ep.setProperty(EarProjectProperties.JAVA_PLATFORM, "default_platform"); // NOI18N
496
ep.setProperty(EarProjectProperties.DEBUG_CLASSPATH,
497                 "${"+EarProjectProperties.JAVAC_CLASSPATH+"}::${"+ // NOI18N
498
EarProjectProperties.JAR_CONTENT_ADDITIONAL+"}:${"+ // NOI18N
499
EarProjectProperties.RUN_CLASSPATH+"}"); // NOI18N
500

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         // ant deployment support
509
try {
510             AntDeploymentHelper.writeDeploymentScript(new File JavaDoc(prjDir,
511                     EarProjectProperties.ANT_DEPLOY_BUILD_SCRIPT),
512                     J2eeModule.EAR, serverInstanceID);
513         } catch (IOException JavaDoc ioe) {
514             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
515         }
516         File JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc sourceLevel) {
542         ProjectManager.mutex().writeAccess(new Runnable JavaDoc() {
543             public void run() {
544                 try {
545                     EditableProperties ep = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
546                     // #89131: these levels are not actually distinct from 1.5.
547
String JavaDoc 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 JavaDoc e) {
555                     ErrorManager.getDefault().notify(e);
556                 }
557             }
558         });
559     }
560     
561     public static String JavaDoc toClasspathString(File JavaDoc[] classpathEntries) {
562         if (classpathEntries == null) {
563             return "";
564         }
565         StringBuffer JavaDoc classpath = new StringBuffer JavaDoc();
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 JavaDoc {
576         FileObject javaRoot = moduleRoot.getFileObject("src/java"); // NOI18N
577
// XXX this is a hack. Remove once 56487 is resolved
578
if (null == javaRoot) {
579             FileObject srcDir = moduleRoot.getFileObject("src"); // NOI18N
580
if (null == srcDir) {
581                 srcDir = moduleRoot.createFolder("src"); // NOI18N
582
}
583             javaRoot = srcDir.createFolder("java"); // NOI18N
584
}
585         return javaRoot;
586         // end hack for 56487
587
}
588     
589     private File JavaDoc[] getSourceFolders(final FileObject javaRoot) {
590         return null == javaRoot ? new File JavaDoc[0] :
591             new File JavaDoc[] { FileUtil.toFile(javaRoot) };
592     }
593     
594 }
595
Popular Tags