001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.ejbjarproject.api;
043:
044: import java.io.File;
045: import java.io.IOException;
046: import java.math.BigDecimal;
047: import java.nio.charset.Charset;
048: import java.util.logging.Level;
049: import java.util.logging.Logger;
050: import org.netbeans.api.java.platform.JavaPlatform;
051: import org.netbeans.api.java.platform.JavaPlatformManager;
052: import org.netbeans.api.java.project.JavaProjectConstants;
053: import org.netbeans.api.queries.FileEncodingQuery;
054: import org.netbeans.modules.j2ee.deployment.devmodules.api.AntDeploymentHelper;
055: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
056: import org.netbeans.spi.project.support.ant.PropertyUtils;
057: import org.netbeans.spi.project.support.ant.ReferenceHelper;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.filesystems.Repository;
061: import org.netbeans.api.project.Project;
062: import org.netbeans.api.project.ProjectManager;
063: import org.netbeans.api.project.libraries.LibraryManager;
064: import org.netbeans.api.queries.FileEncodingQuery;
065: import org.netbeans.modules.j2ee.common.SharabilityUtility;
066: import org.netbeans.modules.j2ee.common.project.ui.ProjectProperties;
067: import org.netbeans.spi.project.support.ant.AntProjectHelper;
068: import org.netbeans.spi.project.support.ant.EditableProperties;
069: import org.netbeans.spi.project.support.ant.ProjectGenerator;
070: import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment;
071: import org.netbeans.modules.j2ee.dd.api.ejb.DDProvider;
072: import org.netbeans.modules.j2ee.dd.api.ejb.EjbJar;
073: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eePlatform;
074: import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProject;
075: import org.netbeans.modules.j2ee.ejbjarproject.EjbJarProjectType;
076: import org.netbeans.modules.j2ee.ejbjarproject.Utils;
077: import org.netbeans.modules.j2ee.ejbjarproject.ui.customizer.EjbJarProjectProperties;
078: import org.netbeans.modules.java.api.common.ant.UpdateHelper;
079: import org.netbeans.modules.java.api.common.ui.PlatformUiSupport;
080: import org.netbeans.modules.websvc.spi.webservices.WebServicesConstants;
081: import org.openide.filesystems.FileSystem;
082: import org.openide.filesystems.FileSystem.AtomicAction;
083: import org.openide.modules.SpecificationVersion;
084: import org.openide.util.Exceptions;
085: import org.openide.util.Mutex;
086: import org.openide.util.MutexException;
087: import org.openide.util.NbBundle;
088: import org.w3c.dom.Document;
089: import org.w3c.dom.Element;
090: import org.w3c.dom.NodeList;
091:
092: /**
093: * Create a fresh EjbProject from scratch or by importing and exisitng ejb module
094: * in one of the recognized directory structures.
095: *
096: * @author Pavel Buzek
097: */
098: public class EjbJarProjectGenerator {
099:
100: private static final String DEFAULT_DOC_BASE_FOLDER = "conf"; //NOI18N
101: private static final String DEFAULT_SRC_FOLDER = "src"; //NOI18N
102: private static final String DEFAULT_TEST_FOLDER = "test"; //NOI18N
103: private static final String DEFAULT_RESOURCE_FOLDER = "setup"; //NOI18N
104: private static final String DEFAULT_JAVA_FOLDER = "java"; //NOI18N
105: private static final String DEFAULT_BUILD_DIR = "build"; //NOI18N
106:
107: public static final String MINIMUM_ANT_VERSION = "1.6";
108:
109: private EjbJarProjectGenerator() {
110: }
111:
112: /**
113: * Create a new empty EjbJar project.
114: * @param dir the top-level directory (need not yet exist but if it does it must be empty)
115: * @param name the code name for the project
116: * @param j2eeLevel Java EE level
117: * @param serverInstanceID server instance ID
118: * @return the helper object permitting it to be further customized
119: * @throws IOException in case something went wrong
120: */
121: public static AntProjectHelper createProject(File dir,
122: final String name, final String j2eeLevel,
123: final String serverInstanceID) throws IOException {
124:
125: return createProject(dir, name, j2eeLevel, serverInstanceID,
126: null, null);
127: }
128:
129: public static AntProjectHelper createProject(File dir,
130: final String name, final String j2eeLevel,
131: final String serverInstanceID,
132: final String librariesDefinition,
133: final String serverLibraryName) throws IOException {
134: final FileObject projectDir = FileUtil.createFolder(dir);
135: final AntProjectHelper[] h = new AntProjectHelper[1];
136:
137: // create project in one FS atomic action:
138: FileSystem fs = projectDir.getFileSystem();
139: fs.runAtomicAction(new FileSystem.AtomicAction() {
140: public void run() throws IOException {
141: AntProjectHelper helper = createProjectImpl(projectDir,
142: name, j2eeLevel, serverInstanceID,
143: librariesDefinition, serverLibraryName);
144: h[0] = helper;
145: }
146: });
147: return h[0];
148: }
149:
150: private static AntProjectHelper createProjectImpl(
151: FileObject projectDir, String name, String j2eeLevel,
152: final String serverInstanceID,
153: final String librariesDefinition,
154: final String serverLibraryName) throws IOException {
155:
156: FileObject srcRoot = projectDir
157: .createFolder(DEFAULT_SRC_FOLDER); // NOI18N
158: srcRoot.createFolder(DEFAULT_JAVA_FOLDER); //NOI18N
159: projectDir.createFolder(DEFAULT_TEST_FOLDER);
160: FileObject confRoot = srcRoot
161: .createFolder(DEFAULT_DOC_BASE_FOLDER); // NOI18N
162:
163: //create a default manifest
164: FileUtil
165: .copyFile(
166: Repository
167: .getDefault()
168: .getDefaultFileSystem()
169: .findResource(
170: "org-netbeans-modules-j2ee-ejbjarproject/MANIFEST.MF"),
171: confRoot, "MANIFEST"); //NOI18N
172:
173: final AntProjectHelper h = setupProject(projectDir, name,
174: "src", "test", null, null, null, j2eeLevel,
175: serverInstanceID, librariesDefinition,
176: serverLibraryName);
177: EditableProperties ep = h
178: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
179: ep.put(EjbJarProjectProperties.SOURCE_ROOT, DEFAULT_SRC_FOLDER); //NOI18N
180: ep.setProperty(EjbJarProjectProperties.META_INF, "${"
181: + EjbJarProjectProperties.SOURCE_ROOT + "}/"
182: + DEFAULT_DOC_BASE_FOLDER); //NOI18N
183: ep.setProperty(EjbJarProjectProperties.SRC_DIR, "${"
184: + EjbJarProjectProperties.SOURCE_ROOT + "}/"
185: + DEFAULT_JAVA_FOLDER); //NOI18N
186: ep.setProperty(EjbJarProjectProperties.META_INF_EXCLUDES,
187: "sun-cmp-mappings.xml"); // NOI18N
188: Charset enc = FileEncodingQuery.getDefaultEncoding();
189: ep.setProperty(EjbJarProjectProperties.SOURCE_ENCODING, enc
190: .name());
191: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
192:
193: EjbJarProject p = (EjbJarProject) ProjectManager.getDefault()
194: .findProject(h.getProjectDirectory());
195: ProjectManager.getDefault().saveProject(p);
196:
197: final ReferenceHelper refHelper = p.getReferenceHelper();
198: try {
199: ProjectManager.mutex().writeAccess(
200: new Mutex.ExceptionAction<Void>() {
201: public Void run() throws Exception {
202: copyRequiredLibraries(h, refHelper,
203: serverInstanceID, serverLibraryName);
204: return null;
205: }
206: });
207: } catch (MutexException ex) {
208: Exceptions.printStackTrace(ex.getException());
209: }
210:
211: // create ejb-jar.xml
212: if (!J2eeModule.JAVA_EE_5.equals(j2eeLevel)) {
213: String resource = "org-netbeans-modules-j2ee-ejbjarproject/ejb-jar-2.1.xml";
214: FileObject ddFile = FileUtil.copyFile(Repository
215: .getDefault().getDefaultFileSystem().findResource(
216: resource), confRoot, "ejb-jar"); //NOI18N
217: EjbJar ejbJar = DDProvider.getDefault().getDDRoot(ddFile);
218: ejbJar.setDisplayName(name);
219: ejbJar.write(ddFile);
220: }
221:
222: return h;
223: }
224:
225: /**
226: * Import project from source or exploded archive
227: * @param dir root directory of project
228: * @param name name of the project
229: * @param sourceFolders Array of folders that hold the projects source
230: * or exploded archive
231: * @param testFolders folders that hold test code for the project
232: * @param configFilesBase Folder that holds the projects config files
233: * like deployment descriptors
234: * @param libFolder the libraries associated with the project
235: * @param j2eeLevel spec revision level
236: * @param serverInstanceID id of target server
237: * @param fromJavaSources flag whether the project is from source or
238: * exploded archive of class files
239: * @throws java.io.IOException if something goes wrong
240: * @return The AntProjectHelper for the project
241: */
242: public static AntProjectHelper importProject(final File dir,
243: final String name, final File[] sourceFolders,
244: final File[] testFolders, final File configFilesBase,
245: final File libFolder, final String j2eeLevel,
246: String serverInstanceID, boolean fromJavaSources)
247: throws IOException {
248:
249: return importProject(dir, name, sourceFolders, testFolders,
250: configFilesBase, libFolder, j2eeLevel,
251: serverInstanceID, fromJavaSources, null, null);
252: }
253:
254: public static AntProjectHelper importProject(final File dir,
255: final String name, final File[] sourceFolders,
256: final File[] testFolders, final File configFilesBase,
257: final File libFolder, final String j2eeLevel,
258: String serverInstanceID, boolean fromJavaSources,
259: String librariesDefinition, String serverLibraryName)
260: throws IOException {
261:
262: AntProjectHelper retVal = importProject(dir, name,
263: sourceFolders, testFolders, configFilesBase, libFolder,
264: j2eeLevel, serverInstanceID, librariesDefinition,
265: serverLibraryName);
266: EditableProperties subEp = retVal
267: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
268: subEp.setProperty(EjbJarProjectProperties.JAVA_SOURCE_BASED,
269: fromJavaSources + ""); // NOI18N
270: retVal.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
271: subEp);
272: Project subP = ProjectManager.getDefault().findProject(
273: retVal.getProjectDirectory());
274: ProjectManager.getDefault().saveProject(subP);
275: return retVal;
276: }
277:
278: public static AntProjectHelper importProject(final File dir,
279: final String name, final File[] sourceFolders,
280: final File[] testFolders, final File configFilesBase,
281: final File libFolder, final String j2eeLevel,
282: final String serverInstanceID) throws IOException {
283:
284: return importProject(dir, name, sourceFolders, testFolders,
285: configFilesBase, libFolder, j2eeLevel,
286: serverInstanceID, null, null);
287: }
288:
289: public static AntProjectHelper importProject(final File dir,
290: final String name, final File[] sourceFolders,
291: final File[] testFolders, final File configFilesBase,
292: final File libFolder, final String j2eeLevel,
293: final String serverInstanceID,
294: final String librariesDefinition,
295: final String serverLibraryName) throws IOException {
296: final FileObject projectDir = FileUtil.createFolder(dir);
297: final AntProjectHelper[] h = new AntProjectHelper[1];
298:
299: // create project in one FS atomic action:
300: FileSystem fs = projectDir.getFileSystem();
301: fs.runAtomicAction(new FileSystem.AtomicAction() {
302: public void run() throws IOException {
303: AntProjectHelper helper = importProjectImpl(projectDir,
304: name, sourceFolders, testFolders,
305: configFilesBase, libFolder, j2eeLevel,
306: serverInstanceID, librariesDefinition,
307: serverLibraryName);
308: h[0] = helper;
309: }
310: });
311: return h[0];
312: }
313:
314: private static AntProjectHelper importProjectImpl(
315: final FileObject projectDir, final String name,
316: final File[] sourceFolders, final File[] testFolders,
317: final File configFilesBase, final File libFolder,
318: final String j2eeLevel, String serverInstanceID,
319: String librariesDefinition, String serverLibraryName)
320: throws IOException {
321:
322: assert sourceFolders != null && testFolders != null : "Package roots can't be null"; //NOI18N
323: // this constructor creates only java application type
324:
325: final AntProjectHelper h = setupProject(projectDir, name, null,
326: null, configFilesBase, (libFolder == null ? null
327: : libFolder), null, j2eeLevel,
328: serverInstanceID, librariesDefinition,
329: serverLibraryName);
330: final EjbJarProject p = (EjbJarProject) ProjectManager
331: .getDefault().findProject(projectDir);
332: final ReferenceHelper refHelper = p.getReferenceHelper();
333: try {
334: ProjectManager.mutex().writeAccess(
335: new Mutex.ExceptionAction<Void>() {
336: public Void run() throws Exception {
337: Element data = h
338: .getPrimaryConfigurationData(true);
339: Document doc = data.getOwnerDocument();
340: NodeList nl = data
341: .getElementsByTagNameNS(
342: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
343: "source-roots"); //NOI18N
344: assert nl.getLength() == 1;
345: Element sourceRoots = (Element) nl.item(0);
346: nl = data
347: .getElementsByTagNameNS(
348: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
349: "test-roots"); //NOI18N
350: assert nl.getLength() == 1;
351: Element testRoots = (Element) nl.item(0);
352: for (int i = 0; i < sourceFolders.length; i++) {
353: String propName = "src.dir"
354: + (i == 0 ? "" : Integer
355: .toString(i + 1)); //NOI18N
356: String srcReference = refHelper
357: .createForeignFileReference(
358: sourceFolders[i],
359: JavaProjectConstants.SOURCES_TYPE_JAVA);
360: Element root = doc
361: .createElementNS(
362: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
363: "root"); //NOI18N
364: root.setAttribute("id", propName); //NOI18N
365: sourceRoots.appendChild(root);
366: EditableProperties props = h
367: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
368: props.put(propName, srcReference);
369: h
370: .putProperties(
371: AntProjectHelper.PROJECT_PROPERTIES_PATH,
372: props); // #47609
373: }
374:
375: if (testFolders.length == 0) {
376: EditableProperties props = h
377: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
378: props.put("test.src.dir", ""); // NOI18N
379: h
380: .putProperties(
381: AntProjectHelper.PROJECT_PROPERTIES_PATH,
382: props); // #47609
383: } else {
384: for (int i = 0; i < testFolders.length; i++) {
385: if (!testFolders[i].exists()) {
386: FileUtil
387: .createFolder(testFolders[i]);
388: }
389: String propName = "test.src.dir"
390: + (i == 0 ? "" : Integer
391: .toString(i + 1)); //NOI18N
392: String testReference = refHelper
393: .createForeignFileReference(
394: testFolders[i],
395: JavaProjectConstants.SOURCES_TYPE_JAVA);
396: Element root = doc
397: .createElementNS(
398: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
399: "root"); //NOI18N
400: root.setAttribute("id", propName); //NOI18N
401: testRoots.appendChild(root);
402: EditableProperties props = h
403: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); // #47609
404: props.put(propName, testReference);
405: h
406: .putProperties(
407: AntProjectHelper.PROJECT_PROPERTIES_PATH,
408: props);
409: }
410: }
411: h.putPrimaryConfigurationData(data, true);
412: ProjectManager.getDefault().saveProject(p);
413: return null;
414: }
415: });
416: } catch (MutexException me) {
417: IOException ex = new IOException("project creation failed");
418: ex.initCause(me);
419: throw ex;
420: }
421:
422: // AB: fix for #53170: if j2eeLevel is 1.4 and ejb-jar.xml is version 2.0, we upgrade it to version 2.1
423: FileObject ejbJarXml = FileUtil.toFileObject(configFilesBase)
424: .getFileObject("ejb-jar.xml"); // NOI18N
425: if (ejbJarXml != null) {
426: try {
427: EjbJar root = DDProvider.getDefault().getDDRoot(
428: ejbJarXml);
429: boolean writeDD = false;
430: if (new BigDecimal(EjbJar.VERSION_2_0).equals(root
431: .getVersion())
432: && j2eeLevel.equals(ProjectProperties.J2EE_1_4)) { // NOI18N
433: root.setVersion(new BigDecimal(EjbJar.VERSION_2_1));
434: writeDD = true;
435: }
436: // also set the display name if not set (#55733)
437: String dispName = root.getDefaultDisplayName();
438: if (null == dispName || dispName.trim().length() == 0) {
439: root.setDisplayName(name);
440: writeDD = true;
441: }
442: if (writeDD) {
443: root.write(ejbJarXml);
444: }
445: } catch (IOException e) {
446: Exceptions.printStackTrace(e);
447: }
448: }
449: return h;
450: }
451:
452: private static void copyRequiredLibraries(AntProjectHelper h,
453: ReferenceHelper rh, String serverInstanceId,
454: String serverlibraryName) throws IOException {
455:
456: if (!h.isSharableProject()) {
457: return;
458: }
459: if (rh.getProjectLibraryManager().getLibrary("junit") == null) { // NOI18N
460: rh.copyLibrary(LibraryManager.getDefault().getLibrary(
461: "junit")); // NOI18N
462: }
463: if (rh.getProjectLibraryManager().getLibrary("junit_4") == null) { // NOI18N
464: rh.copyLibrary(LibraryManager.getDefault().getLibrary(
465: "junit_4")); // NOI18N
466: }
467:
468: if (h.isSharableProject()
469: && serverlibraryName != null
470: && SharabilityUtility.findSharedServerLibrary(h
471: .resolveFile(h.getLibrariesLocation()),
472: serverlibraryName) == null) {
473:
474: SharabilityUtility.createLibrary(h.resolveFile(h
475: .getLibrariesLocation()), serverlibraryName,
476: serverInstanceId);
477: }
478: }
479:
480: private static String createFileReference(
481: ReferenceHelper refHelper, FileObject projectFO,
482: FileObject referencedFO) {
483: if (FileUtil.isParentOf(projectFO, referencedFO)) {
484: return relativePath(projectFO, referencedFO);
485: } else {
486: return refHelper.createForeignFileReference(FileUtil
487: .toFile(referencedFO), null);
488: }
489: }
490:
491: private static String relativePath(FileObject parent,
492: FileObject child) {
493: if (child.equals(parent)) {
494: return "";
495: }
496: if (!FileUtil.isParentOf(parent, child)) {
497: throw new IllegalArgumentException(
498: "Cannot find relative path, " + parent
499: + " is not parent of " + child);
500: }
501: return child.getPath().substring(parent.getPath().length() + 1);
502: }
503:
504: private static AntProjectHelper setupProject(FileObject dirFO,
505: String name, String srcRoot, String testRoot,
506: File configFiles, File libraries, String resources,
507: String j2eeLevel, String serverInstanceID,
508: String librariesDefinition, String serverLibraryName)
509: throws IOException {
510: AntProjectHelper h = ProjectGenerator.createProject(dirFO,
511: EjbJarProjectType.TYPE, librariesDefinition);
512: final EjbJarProject prj = (EjbJarProject) ProjectManager
513: .getDefault().findProject(h.getProjectDirectory());
514: final ReferenceHelper referenceHelper = prj
515: .getReferenceHelper();
516: Element data = h.getPrimaryConfigurationData(true);
517: Document doc = data.getOwnerDocument();
518: Element nameEl = doc.createElementNS(
519: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
520: "name"); // NOI18N
521: nameEl.appendChild(doc.createTextNode(name));
522: data.appendChild(nameEl);
523: Element minant = doc.createElementNS(
524: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
525: "minimum-ant-version"); // NOI18N
526: minant.appendChild(doc.createTextNode(MINIMUM_ANT_VERSION));
527: data.appendChild(minant);
528:
529: // TODO: ma154696: not sure if needed
530: // Element addLibs = doc.createElementNS(EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE, "ejb-module-additional-libraries"); //NOI18N
531: // data.appendChild(addLibs);
532:
533: EditableProperties ep = h
534: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
535: Element sourceRoots = doc.createElementNS(
536: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
537: "source-roots"); //NOI18N
538: if (srcRoot != null) {
539: Element root = doc.createElementNS(
540: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
541: "root"); //NOI18N
542: root.setAttribute("id", "src.dir"); //NOI18N
543: root.setAttribute("name", NbBundle.getMessage(
544: EjbJarProjectGenerator.class, "NAME_src.dir"));
545: sourceRoots.appendChild(root);
546: ep.setProperty("src.dir", srcRoot); // NOI18N
547: }
548: data.appendChild(sourceRoots);
549: Element testRoots = doc.createElementNS(
550: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
551: "test-roots"); //NOI18N
552: if (testRoot != null) {
553: Element root = doc.createElementNS(
554: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
555: "root"); //NOI18N
556: root.setAttribute("id", "test.src.dir"); //NOI18N
557: root.setAttribute("name", NbBundle.getMessage(
558: EjbJarProjectGenerator.class, "NAME_test.src.dir"));
559: testRoots.appendChild(root);
560: ep.setProperty("test.src.dir", testRoot); // NOI18N
561: }
562: data.appendChild(testRoots);
563: h.putPrimaryConfigurationData(data, true);
564:
565: if (resources != null) {
566: ep.setProperty(EjbJarProjectProperties.RESOURCE_DIR,
567: resources);
568: } else {
569: ep.setProperty(EjbJarProjectProperties.RESOURCE_DIR,
570: DEFAULT_RESOURCE_FOLDER);
571: }
572:
573: JavaPlatform defaultPlatform = JavaPlatformManager.getDefault()
574: .getDefaultPlatform();
575: SpecificationVersion v = defaultPlatform.getSpecification()
576: .getVersion();
577: String sourceLevel = v.toString();
578: // #89131: these levels are not actually distinct from 1.5.
579: if (sourceLevel.equals("1.6") || sourceLevel.equals("1.7")) {
580: sourceLevel = "1.5";
581: }
582: ep.setProperty(EjbJarProjectProperties.JAVAC_SOURCE,
583: sourceLevel); //NOI18N
584: ep.setProperty(EjbJarProjectProperties.JAVAC_TARGET,
585: sourceLevel); //NOI18N
586:
587: ep.setProperty(EjbJarProjectProperties.DIST_DIR, "dist");
588: ep.setProperty(EjbJarProjectProperties.DIST_JAR, "${"
589: + EjbJarProjectProperties.DIST_DIR + "}/" + "${"
590: + EjbJarProjectProperties.JAR_NAME + "}");
591: //XXX the name of the dist.ear.jar file should be different, but now it cannot be since the name is used as a key in module provider mapping
592: ep.setProperty(EjbJarProjectProperties.DIST_EAR_JAR, "${"
593: + EjbJarProjectProperties.DIST_DIR + "}/" + "${"
594: + EjbJarProjectProperties.JAR_NAME + "}");
595: ep
596: .setProperty(EjbJarProjectProperties.J2EE_PLATFORM,
597: j2eeLevel);
598: ep.setProperty(EjbJarProjectProperties.JAR_NAME, PropertyUtils
599: .getUsablePropertyName(name)
600: + ".jar");
601: ep.setProperty(EjbJarProjectProperties.JAR_COMPRESS, "false");
602: // ep.setProperty(EjbJarProjectProperties.JAR_CONTENT_ADDITIONAL, "");
603:
604: Deployment deployment = Deployment.getDefault();
605: ep.setProperty(EjbJarProjectProperties.J2EE_SERVER_TYPE,
606: deployment.getServerID(serverInstanceID));
607:
608: if (h.isSharableProject() && serverLibraryName != null) {
609: // TODO constants
610: ep.setProperty(ProjectProperties.JAVAC_CLASSPATH, "${libs."
611: + serverLibraryName + "." + "classpath" + "}"); // NOI18N
612: ep.setProperty(
613: EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH,
614: "${libs." + serverLibraryName + "." + "classpath"
615: + "}"); //NOI18N
616: ep
617: .setProperty(
618: WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
619: "${libs." + serverLibraryName + "."
620: + "wscompile" + "}"); //NOI18N
621: ep
622: .setProperty(
623: WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH,
624: "${libs." + serverLibraryName + "."
625: + "wsimport" + "}"); //NOI18N
626: ep.setProperty(
627: WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH,
628: "${libs." + serverLibraryName + "." + "wsgenerate"
629: + "}"); //NOI18N
630: ep.setProperty(
631: WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH,
632: "${libs." + serverLibraryName + "." + "wsinterop"
633: + "}"); //NOI18N
634: ep.setProperty(
635: WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH,
636: "${libs." + serverLibraryName + "." + "wsjwsdp"
637: + "}"); //NOI18N
638: } else {
639: ep.setProperty(ProjectProperties.JAVAC_CLASSPATH, "");
640: }
641:
642: ep.setProperty(EjbJarProjectProperties.JAVAC_DEBUG, "true");
643: ep.setProperty(EjbJarProjectProperties.JAVAC_DEPRECATION,
644: "false");
645:
646: ep.setProperty(ProjectProperties.JAVAC_TEST_CLASSPATH,
647: new String[] { "${javac.classpath}:", // NOI18N
648: "${build.classes.dir}:", // NOI18N
649: "${libs.junit.classpath}:", // NOI18N
650: "${libs.junit_4.classpath}", // NOI18N
651: });
652: ep.setProperty(ProjectProperties.RUN_TEST_CLASSPATH,
653: new String[] { "${javac.test.classpath}:", // NOI18N
654: "${build.test.classes.dir}", // NOI18N
655: });
656: ep.setProperty(EjbJarProjectProperties.DEBUG_TEST_CLASSPATH,
657: new String[] { "${run.test.classpath}", // NOI18N
658: });
659:
660: ep.setProperty(EjbJarProjectProperties.BUILD_DIR,
661: DEFAULT_BUILD_DIR);
662: ep.setProperty(ProjectProperties.BUILD_TEST_CLASSES_DIR,
663: "${build.dir}/test/classes"); // NOI18N
664: ep.setProperty(EjbJarProjectProperties.BUILD_TEST_RESULTS_DIR,
665: "${build.dir}/test/results"); // NOI18N
666: ep.setProperty(EjbJarProjectProperties.BUILD_GENERATED_DIR,
667: "${" + EjbJarProjectProperties.BUILD_DIR
668: + "}/generated");
669: ep.setProperty(ProjectProperties.BUILD_CLASSES_DIR, "${"
670: + EjbJarProjectProperties.BUILD_DIR + "}/jar");
671: ep.setProperty(EjbJarProjectProperties.BUILD_EAR_CLASSES_DIR,
672: "${" + EjbJarProjectProperties.BUILD_DIR + "}/jar");
673: ep.setProperty(EjbJarProjectProperties.BUILD_CLASSES_EXCLUDES,
674: "**/*.java,**/*.form,**/.nbattrs");
675: ep.setProperty(EjbJarProjectProperties.DIST_JAVADOC_DIR, "${"
676: + EjbJarProjectProperties.DIST_DIR + "}/javadoc");
677: ep.setProperty(EjbJarProjectProperties.JAVA_PLATFORM,
678: "default_platform");
679: ep.setProperty(EjbJarProjectProperties.DEBUG_CLASSPATH, "${"
680: + ProjectProperties.JAVAC_CLASSPATH + "}:${"
681: + ProjectProperties.BUILD_CLASSES_DIR + "}");
682: ep
683: .setProperty(EjbJarProjectProperties.JAVADOC_PRIVATE,
684: "false"); // NOI18N
685: ep
686: .setProperty(EjbJarProjectProperties.JAVADOC_NO_TREE,
687: "false"); // NOI18N
688: ep.setProperty(EjbJarProjectProperties.JAVADOC_USE, "true"); // NOI18N
689: ep.setProperty(EjbJarProjectProperties.JAVADOC_NO_NAVBAR,
690: "false"); // NOI18N
691: ep.setProperty(EjbJarProjectProperties.JAVADOC_NO_INDEX,
692: "false"); // NOI18N
693: ep.setProperty(EjbJarProjectProperties.JAVADOC_SPLIT_INDEX,
694: "true"); // NOI18N
695: ep.setProperty(EjbJarProjectProperties.JAVADOC_AUTHOR, "false"); // NOI18N
696: ep
697: .setProperty(EjbJarProjectProperties.JAVADOC_VERSION,
698: "false"); // NOI18N
699: ep
700: .setProperty(
701: EjbJarProjectProperties.JAVADOC_WINDOW_TITLE,
702: ""); // NOI18N
703: ep.setProperty(EjbJarProjectProperties.JAVADOC_ENCODING, "${"
704: + EjbJarProjectProperties.SOURCE_ENCODING + "}"); // NOI18N
705: ep.setProperty(EjbJarProjectProperties.JAVADOC_PREVIEW, "true"); // NOI18N
706: ep.setProperty(EjbJarProjectProperties.JAVADOC_ADDITIONALPARAM,
707: ""); // NOI18N
708:
709: // use the default encoding
710: Charset enc = FileEncodingQuery.getDefaultEncoding();
711: ep.setProperty(EjbJarProjectProperties.SOURCE_ENCODING, enc
712: .name());
713:
714: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
715:
716: if (configFiles != null) {
717: String ref = createFileReference(referenceHelper, dirFO,
718: FileUtil.toFileObject(configFiles));
719: EditableProperties props = h
720: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
721: props.setProperty(EjbJarProjectProperties.META_INF, ref);
722: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
723: props);
724: }
725: if (libraries != null) {
726: String ref = createFileReference(referenceHelper, dirFO,
727: FileUtil.toFileObject(libraries));
728: EditableProperties props = h
729: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
730: props.setProperty(EjbJarProjectProperties.LIBRARIES_DIR,
731: ref);
732: h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH,
733: props);
734: }
735:
736: ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
737: ep.setProperty(EjbJarProjectProperties.J2EE_SERVER_INSTANCE,
738: serverInstanceID);
739:
740: // set j2ee.platform.classpath
741: J2eePlatform j2eePlatform = Deployment.getDefault()
742: .getJ2eePlatform(serverInstanceID);
743: if (!j2eePlatform.getSupportedSpecVersions(J2eeModule.EJB)
744: .contains(j2eeLevel)) {
745: Logger.getLogger("global").log(
746: Level.WARNING,
747: "J2EE level:"
748: + j2eeLevel
749: + " not supported by server "
750: + Deployment.getDefault()
751: .getServerInstanceDisplayName(
752: serverInstanceID)
753: + " for module type EJB"); // NOI18N
754: }
755:
756: if (!h.isSharableProject() || serverLibraryName == null) {
757: String classpath = Utils.toClasspathString(j2eePlatform
758: .getClasspathEntries());
759: ep.setProperty(
760: EjbJarProjectProperties.J2EE_PLATFORM_CLASSPATH,
761: classpath);
762:
763: // set j2ee.platform.wscompile.classpath
764: if (j2eePlatform
765: .isToolSupported(J2eePlatform.TOOL_WSCOMPILE)) {
766: File[] wsClasspath = j2eePlatform
767: .getToolClasspathEntries(J2eePlatform.TOOL_WSCOMPILE);
768: ep
769: .setProperty(
770: WebServicesConstants.J2EE_PLATFORM_WSCOMPILE_CLASSPATH,
771: Utils.toClasspathString(wsClasspath));
772: }
773:
774: // set j2ee.platform.wsimport.classpath
775: if (j2eePlatform
776: .isToolSupported(J2eePlatform.TOOL_WSIMPORT)) {
777: File[] wsClasspath = j2eePlatform
778: .getToolClasspathEntries(J2eePlatform.TOOL_WSIMPORT);
779: ep
780: .setProperty(
781: WebServicesConstants.J2EE_PLATFORM_WSIMPORT_CLASSPATH,
782: Utils.toClasspathString(wsClasspath));
783: }
784:
785: // set j2ee.platform.wsgen.classpath
786: if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSGEN)) {
787: File[] wsClasspath = j2eePlatform
788: .getToolClasspathEntries(J2eePlatform.TOOL_WSGEN);
789: ep
790: .setProperty(
791: WebServicesConstants.J2EE_PLATFORM_WSGEN_CLASSPATH,
792: Utils.toClasspathString(wsClasspath));
793: }
794:
795: if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_WSIT)) {
796: File[] wsClasspath = j2eePlatform
797: .getToolClasspathEntries(J2eePlatform.TOOL_WSIT);
798: ep
799: .setProperty(
800: WebServicesConstants.J2EE_PLATFORM_WSIT_CLASSPATH,
801: Utils.toClasspathString(wsClasspath));
802: }
803:
804: if (j2eePlatform.isToolSupported(J2eePlatform.TOOL_JWSDP)) {
805: File[] wsClasspath = j2eePlatform
806: .getToolClasspathEntries(J2eePlatform.TOOL_JWSDP);
807: ep
808: .setProperty(
809: WebServicesConstants.J2EE_PLATFORM_JWSDP_CLASSPATH,
810: Utils.toClasspathString(wsClasspath));
811: }
812: }
813:
814: // ant deployment support
815: File projectFolder = FileUtil.toFile(dirFO);
816: try {
817: AntDeploymentHelper.writeDeploymentScript(new File(
818: projectFolder,
819: EjbJarProjectProperties.ANT_DEPLOY_BUILD_SCRIPT),
820: J2eeModule.EJB, serverInstanceID);
821: } catch (IOException ioe) {
822: Logger.getLogger("global").log(Level.INFO, null, ioe);
823: }
824: File deployAntPropsFile = AntDeploymentHelper
825: .getDeploymentPropertiesFile(serverInstanceID);
826: if (deployAntPropsFile != null) {
827: ep.setProperty(
828: EjbJarProjectProperties.DEPLOY_ANT_PROPS_FILE,
829: deployAntPropsFile.getAbsolutePath());
830: }
831:
832: h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
833: Project p = ProjectManager.getDefault().findProject(dirFO);
834: ProjectManager.getDefault().saveProject(p);
835: return h;
836: }
837:
838: // AB: this method is also called from the enterprise application, so we can't pass UpdateHelper here
839: // well, actually we can, but let's not expose too many classes
840: public static void setPlatform(final AntProjectHelper helper,
841: final String platformName, final String sourceLevel) {
842: FileObject projectDir = helper.getProjectDirectory();
843: if (projectDir == null) {
844: return;
845: }
846: // issue 89278: do not fire file change events under ProjectManager.MUTEX,
847: // it is deadlock-prone
848: try {
849: projectDir.getFileSystem().runAtomicAction(
850: new AtomicAction() {
851: public void run() throws IOException {
852: ProjectManager.mutex().writeAccess(
853: new Runnable() {
854: public void run() {
855: try {
856: EjbJarProject project = (EjbJarProject) ProjectManager
857: .getDefault()
858: .findProject(
859: helper
860: .getProjectDirectory());
861: UpdateHelper updateHelper = project
862: .getUpdateHelper();
863: EditableProperties ep = helper
864: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
865: String finalPlatformName = platformName;
866: if (finalPlatformName == null) {
867: finalPlatformName = JavaPlatformManager
868: .getDefault()
869: .getDefaultPlatform()
870: .getDisplayName();
871: }
872:
873: PlatformUiSupport
874: .storePlatform(
875: ep,
876: updateHelper,
877: EjbJarProjectType.PROJECT_CONFIGURATION_NAMESPACE,
878: finalPlatformName,
879: sourceLevel != null ? new SpecificationVersion(
880: sourceLevel)
881: : null);
882: helper
883: .putProperties(
884: AntProjectHelper.PROJECT_PROPERTIES_PATH,
885: ep);
886: ProjectManager
887: .getDefault()
888: .saveProject(
889: ProjectManager
890: .getDefault()
891: .findProject(
892: helper
893: .getProjectDirectory()));
894: } catch (IOException e) {
895: Exceptions
896: .printStackTrace(e);
897: }
898: }
899: });
900: }
901: });
902: } catch (IOException e) {
903: Exceptions.printStackTrace(e);
904: }
905:
906: }
907: }
|