001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Project;
024: import org.apache.tools.ant.types.ZipFileSet;
025: import org.apache.tools.ant.util.FileUtils;
026: import org.apache.tools.zip.ZipOutputStream;
027:
028: /**
029: * Creates a EAR archive. Based on WAR task
030: *
031: * @since Ant 1.4
032: *
033: * @ant.task category="packaging"
034: */
035: public class Ear extends Jar {
036: private static final FileUtils FILE_UTILS = FileUtils
037: .getFileUtils();
038:
039: private File deploymentDescriptor;
040: private boolean descriptorAdded;
041:
042: /**
043: * Create an Ear task.
044: */
045: public Ear() {
046: super ();
047: archiveType = "ear";
048: emptyBehavior = "create";
049: }
050:
051: /**
052: * Set the destination file.
053: * @param earFile the destination file
054: * @deprecated since 1.5.x.
055: * Use setDestFile(destfile) instead.
056: */
057: public void setEarfile(File earFile) {
058: setDestFile(earFile);
059: }
060:
061: /**
062: * File to incorporate as application.xml.
063: * @param descr the descriptor file
064: */
065: public void setAppxml(File descr) {
066: deploymentDescriptor = descr;
067: if (!deploymentDescriptor.exists()) {
068: throw new BuildException("Deployment descriptor: "
069: + deploymentDescriptor + " does not exist.");
070: }
071:
072: // Create a ZipFileSet for this file, and pass it up.
073: ZipFileSet fs = new ZipFileSet();
074: fs.setFile(deploymentDescriptor);
075: fs.setFullpath("META-INF/application.xml");
076: super .addFileset(fs);
077: }
078:
079: /**
080: * Adds zipfileset.
081: *
082: * @param fs zipfileset to add
083: */
084: public void addArchives(ZipFileSet fs) {
085: // We just set the prefix for this fileset, and pass it up.
086: // Do we need to do this? LH
087: fs.setPrefix("/");
088: super .addFileset(fs);
089: }
090:
091: /**
092: * Initialize the output stream.
093: * @param zOut the zip output stream.
094: * @throws IOException on I/O errors
095: * @throws BuildException on other errors
096: */
097: protected void initZipOutputStream(ZipOutputStream zOut)
098: throws IOException, BuildException {
099: // If no webxml file is specified, it's an error.
100: if (deploymentDescriptor == null && !isInUpdateMode()) {
101: throw new BuildException("appxml attribute is required",
102: getLocation());
103: }
104:
105: super .initZipOutputStream(zOut);
106: }
107:
108: /**
109: * Overridden from Zip class to deal with application.xml
110: * @param file the file to add to the archive
111: * @param zOut the stream to write to
112: * @param vPath the name this entry shall have in the archive
113: * @param mode the Unix permissions to set.
114: * @throws IOException on error
115: */
116: protected void zipFile(File file, ZipOutputStream zOut,
117: String vPath, int mode) throws IOException {
118: // If the file being added is META-INF/application.xml, we
119: // warn if it's not the one specified in the "appxml"
120: // attribute - or if it's being added twice, meaning the same
121: // file is specified by the "appxml" attribute and in a
122: // <fileset> element.
123: if (vPath.equalsIgnoreCase("META-INF/application.xml")) {
124: if (deploymentDescriptor == null
125: || !FILE_UTILS.fileNameEquals(deploymentDescriptor,
126: file) || descriptorAdded) {
127: log(
128: "Warning: selected "
129: + archiveType
130: + " files include a META-INF/application.xml which will"
131: + " be ignored (please use appxml attribute to "
132: + archiveType + " task)",
133: Project.MSG_WARN);
134: } else {
135: super .zipFile(file, zOut, vPath, mode);
136: descriptorAdded = true;
137: }
138: } else {
139: super .zipFile(file, zOut, vPath, mode);
140: }
141: }
142:
143: /**
144: * Make sure we don't think we already have a application.xml next
145: * time this task gets executed.
146: */
147: protected void cleanUp() {
148: descriptorAdded = false;
149: super.cleanUp();
150: }
151: }
|