001: /**
002: * EasyBeans
003: * Copyright (C) 2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: EarFile.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.ant.archive.file;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.ArrayList;
029: import java.util.List;
030:
031: import org.apache.tools.ant.BuildException;
032: import org.apache.tools.ant.Project;
033: import org.apache.tools.ant.taskdefs.Jar;
034: import org.apache.tools.ant.types.FileSet;
035: import org.apache.tools.ant.types.ZipFileSet;
036: import org.ow2.easybeans.ant.archive.AbsArchive;
037: import org.ow2.easybeans.ant.archive.Ejb;
038: import org.ow2.easybeans.ant.archive.War;
039: import org.ow2.easybeans.ant.archive.api.IEar;
040: import org.ow2.easybeans.ant.archive.info.ArchiveInfo;
041: import org.ow2.easybeans.ant.archive.info.EarInfo;
042:
043: /**
044: * Creates an EAR file.
045: * @author Florent Benoit
046: */
047: public class EarFile extends Jar implements IEar {
048:
049: /**
050: * Path to the Standard deployment descriptor.
051: */
052: private static final String DEPLOYMENT_DESCRIPTOR = "META-INF/application.xml";
053:
054: /**
055: * Ear info object.
056: */
057: private EarInfo earInfo = null;
058:
059: /**
060: * Creates an archive for the given project.
061: * @param p the given project
062: */
063: public EarFile(final Project p) {
064: super ();
065: setProject(p);
066: }
067:
068: /**
069: * Sets the information about an EAR archive.
070: * @param earInfo the object that holds data information.
071: */
072: public void setEarInfo(final EarInfo earInfo) {
073: this .earInfo = earInfo;
074: }
075:
076: /**
077: * Execute the task.
078: */
079: @Override
080: public void execute() {
081:
082: // First, package each ejb, war, etc. (sub modules)
083: // Execute children by changing the destination directory
084: List<Ejb> ejbs = earInfo.getEjbs();
085: List<War> wars = earInfo.getWars();
086:
087: // Add all archives
088: List<AbsArchive> archives = new ArrayList<AbsArchive>();
089: if (ejbs != null) {
090: for (Ejb ejb : ejbs) {
091: archives.add(ejb);
092: }
093: }
094: if (wars != null) {
095: for (War war : wars) {
096: archives.add(war);
097: }
098: }
099:
100: // Update archives
101: for (AbsArchive archive : archives) {
102: // Update path
103: updateArchive(archive);
104:
105: // Build the file
106: archive.execute();
107:
108: // Now, add each archive built in the fileset of the Ear
109: addArchiveInFileSet(archive);
110: }
111:
112: // Deployment descriptor
113: if (earInfo.getDd() != null) {
114: setDD(earInfo.getDd());
115: }
116:
117: // dest file
118: setDestFile(earInfo.getDest());
119:
120: // fileset
121: for (FileSet fileSet : earInfo.getFileSetList()) {
122: addFileset(fileSet);
123: }
124:
125: // Create directory if it is not existing
126: File earFileParentDirectory = earInfo.getDest().getParentFile();
127: if (!earFileParentDirectory.exists()) {
128: log("Creating directory '" + earFileParentDirectory + "'",
129: Project.MSG_INFO);
130: earFileParentDirectory.mkdirs();
131: }
132:
133: super .execute();
134:
135: // Clean all temporary files
136: for (AbsArchive archive : archives) {
137: File tmpFile = archive.getDest();
138: if (tmpFile.exists()) {
139: tmpFile.delete();
140: }
141: }
142:
143: }
144:
145: /**
146: * Update settings of the given archive.
147: * @param archive the archive to update.
148: */
149: void updateArchive(final AbsArchive archive) {
150: // change exploded mode to false if ear level is false
151: if (archive.isExploded() && !earInfo.isExploded()) {
152: log(
153: "Changing exploded mode to false as the EAR is not in exploded mode.",
154: Project.MSG_INFO);
155: archive.setExploded(false);
156: }
157:
158: // name is defined ? if not set it to the last name of the dest
159: if (archive.getName() == null) {
160: archive.setName(archive.getDest().getName());
161: }
162:
163: try {
164: archive.setDest(File
165: .createTempFile("easybeans-ant", ".tmp"));
166: } catch (IOException e) {
167: throw new BuildException("Cannot create a temp file", e);
168: }
169: // Delete the temporary file, it should be created by the archive
170: // packaging
171: archive.getDest().delete();
172: }
173:
174: /**
175: * Add the given DD file into the archive.
176: * @param dd the path to the DDesc file.
177: */
178: public void setDD(final File dd) {
179: ZipFileSet zipFileSet = new ZipFileSet();
180: zipFileSet.setFile(dd);
181: zipFileSet.setFullpath(DEPLOYMENT_DESCRIPTOR);
182: addFileset(zipFileSet);
183: }
184:
185: /**
186: * Add the given archive in a fileset.
187: * @param archive the path to the file to include.
188: */
189: public void addArchiveInFileSet(final AbsArchive archive) {
190: ZipFileSet zipFileSet = new ZipFileSet();
191: zipFileSet.setFile(archive.getDest());
192: zipFileSet.setFullpath(archive.getName());
193: addFileset(zipFileSet);
194: }
195:
196: /**
197: * Sets the information about an archive.
198: * @param archiveInfo the object that holds data information.
199: */
200: public void setArchiveInfo(final ArchiveInfo archiveInfo) {
201: // nothing
202: }
203:
204: }
|