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: AbsExplodedArchive.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.ant.archive.exploded;
025:
026: import java.io.File;
027:
028: import org.apache.tools.ant.Project;
029: import org.apache.tools.ant.Task;
030: import org.apache.tools.ant.taskdefs.Copy;
031: import org.apache.tools.ant.types.FileSet;
032: import org.apache.tools.ant.types.ZipFileSet;
033: import org.ow2.easybeans.ant.archive.api.IArchive;
034: import org.ow2.easybeans.ant.archive.info.ArchiveInfo;
035:
036: /**
037: * Abstract task for creating exploded archive.
038: * @author Florent Benoit
039: */
040: public abstract class AbsExplodedArchive extends Task implements
041: IArchive {
042:
043: /**
044: * Path to the Persistence deployment descriptor.
045: */
046: private static final String PERSISTENCE_DEPLOYMENT_DESCRIPTOR = "META-INF/persistence.xml";
047:
048: /**
049: * Reference to the archive info object.
050: */
051: private ArchiveInfo archiveInfo = null;
052:
053: /**
054: * Creates an archive for the given project.
055: * @param p the given project
056: */
057: public AbsExplodedArchive(final Project p) {
058: super ();
059: setProject(p);
060: }
061:
062: /**
063: * Gets the path to the standard deployment descriptor.
064: * @return the path to the standard deployment descriptor.
065: */
066: public abstract String getDDStandardName();
067:
068: /**
069: * Sets the information about an archive.
070: * @param archiveInfo the object that holds data information.
071: */
072: public void setArchiveInfo(final ArchiveInfo archiveInfo) {
073: this .archiveInfo = archiveInfo;
074: }
075:
076: /**
077: * Add the standard deployment descriptor in the archive.
078: */
079: public void addDD() {
080: if (archiveInfo.getDd() != null) {
081: // Add a fileset
082: ZipFileSet zipFileSet = new ZipFileSet();
083: zipFileSet.setProject(getProject());
084: zipFileSet.setFile(archiveInfo.getDd());
085: zipFileSet.setFullpath(getDDStandardName());
086: archiveInfo.getFileSetList().add(zipFileSet);
087: }
088:
089: // persistence dd if any
090: if (archiveInfo.getPersistenceDD() != null) {
091: // Add a fileset
092: ZipFileSet zipFileSet = new ZipFileSet();
093: zipFileSet.setProject(getProject());
094: zipFileSet.setFile(archiveInfo.getPersistenceDD());
095: zipFileSet.setFullpath(PERSISTENCE_DEPLOYMENT_DESCRIPTOR);
096: archiveInfo.getFileSetList().add(zipFileSet);
097: }
098:
099: }
100:
101: /**
102: * Execute the task.
103: */
104: @Override
105: public void execute() {
106: // Include the DD if any
107: addDD();
108:
109: // Suffix dest dir with the name of the file
110: File destdir = archiveInfo.getDest();
111:
112: // For each fileset, execute a copy
113: for (FileSet fileSet : archiveInfo.getFileSetList()) {
114: Copy copy = new Copy();
115: copy.setProject(getProject());
116: copy.setTaskName(getTaskName());
117: copy.setTodir(destdir);
118:
119: // Change the dest dir if there is a prefix
120: if (ZipFileSet.class.getName().equals(
121: fileSet.getClass().getName())) {
122: ZipFileSet archiveFileSet = (ZipFileSet) fileSet;
123: String prefix = archiveFileSet.getPrefix(getProject());
124: if (prefix != null && !prefix.equals("")) {
125: File toDir = new File(destdir, prefix);
126: copy.setTodir(toDir);
127: }
128:
129: String fullpath = archiveFileSet
130: .getFullpath(getProject());
131: if (fullpath != null && !fullpath.equals("")) {
132: File toFile = new File(destdir, fullpath);
133: copy.setTodir(null);
134: copy.setTofile(toFile);
135: }
136: }
137:
138: copy.addFileset(fileSet);
139: copy.execute();
140: }
141:
142: }
143:
144: }
|