KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > War


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Locale JavaDoc;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.types.ZipFileSet;
27 import org.apache.tools.ant.util.FileUtils;
28 import org.apache.tools.zip.ZipOutputStream;
29
30
31 /**
32  * An extension of <jar> to create a WAR archive.
33  * Contains special treatment for files that should end up in the
34  * <code>WEB-INF/lib</code>, <code>WEB-INF/classes</code> or
35  * <code>WEB-INF</code> directories of the Web Application Archive.</p>
36  * <p>(The War task is a shortcut for specifying the particular layout of a WAR file.
37  * The same thing can be accomplished by using the <i>prefix</i> and <i>fullpath</i>
38  * attributes of zipfilesets in a Zip or Jar task.)</p>
39  * <p>The extended zipfileset element from the zip task
40  * (with attributes <i>prefix</i>, <i>fullpath</i>, and <i>src</i>)
41  * is available in the War task.</p>
42  *
43  * @since Ant 1.2
44  *
45  * @ant.task category="packaging"
46  * @see Jar
47  */

48 public class War extends Jar {
49
50     /**
51      * our web.xml deployment descriptor
52      */

53     private File JavaDoc deploymentDescriptor;
54
55     /**
56      * flag set if the descriptor is added
57      */

58     private boolean needxmlfile = true;
59     private File JavaDoc addedWebXmlFile;
60
61     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
62     /** path to web.xml file */
63     private static final String JavaDoc XML_DESCRIPTOR_PATH = "WEB-INF/web.xml";
64     /** lower case version for comparisons */
65     private static final String JavaDoc XML_DESCRIPTOR_PATH_LC =
66             XML_DESCRIPTOR_PATH.toLowerCase(Locale.ENGLISH);
67
68     /** Constructor for the War Task. */
69     public War() {
70         super();
71         archiveType = "war";
72         emptyBehavior = "create";
73     }
74
75     /**
76      * <i>Deprecated<i> name of the file to create
77      * -use <tt>destfile</tt> instead.
78      * @param warFile the destination file
79      * @deprecated since 1.5.x.
80      * Use setDestFile(File) instead
81      * @ant.attribute ignore="true"
82      */

83     public void setWarfile(File JavaDoc warFile) {
84         setDestFile(warFile);
85     }
86
87     /**
88      * set the deployment descriptor to use (WEB-INF/web.xml);
89      * required unless <tt>update=true</tt>
90      * @param descr the deployment descriptor file
91      */

92     public void setWebxml(File JavaDoc descr) {
93         deploymentDescriptor = descr;
94         if (!deploymentDescriptor.exists()) {
95             throw new BuildException("Deployment descriptor: "
96                                      + deploymentDescriptor
97                                      + " does not exist.");
98         }
99
100         // Create a ZipFileSet for this file, and pass it up.
101
ZipFileSet fs = new ZipFileSet();
102         fs.setFile(deploymentDescriptor);
103         fs.setFullpath(XML_DESCRIPTOR_PATH);
104         super.addFileset(fs);
105     }
106
107
108     /**
109      * Set the policy on the web.xml file, that is, whether or not it is needed
110      * @param needxmlfile whether a web.xml file is needed. Default: true
111      */

112     public void setNeedxmlfile(boolean needxmlfile) {
113         this.needxmlfile = needxmlfile;
114     }
115
116     /**
117      * add files under WEB-INF/lib/
118      * @param fs the zip file set to add
119      */

120
121     public void addLib(ZipFileSet fs) {
122         // We just set the prefix for this fileset, and pass it up.
123
fs.setPrefix("WEB-INF/lib/");
124         super.addFileset(fs);
125     }
126
127     /**
128      * add files under WEB-INF/classes
129      * @param fs the zip file set to add
130      */

131     public void addClasses(ZipFileSet fs) {
132         // We just set the prefix for this fileset, and pass it up.
133
fs.setPrefix("WEB-INF/classes/");
134         super.addFileset(fs);
135     }
136
137     /**
138      * files to add under WEB-INF;
139      * @param fs the zip file set to add
140      */

141     public void addWebinf(ZipFileSet fs) {
142         // We just set the prefix for this fileset, and pass it up.
143
fs.setPrefix("WEB-INF/");
144         super.addFileset(fs);
145     }
146
147     /**
148      * override of parent; validates configuration
149      * before initializing the output stream.
150      * @param zOut the zip output stream
151      * @throws IOException on output error
152      * @throws BuildException if invalid configuration
153      */

154     protected void initZipOutputStream(ZipOutputStream zOut)
155         throws IOException JavaDoc, BuildException {
156         super.initZipOutputStream(zOut);
157     }
158
159     /**
160      * Overridden from Zip class to deal with web.xml
161      *
162      * Here are cases that can arise
163      * -not a web.xml file : add
164      * -first web.xml : add, remember we added it
165      * -same web.xml again: skip
166      * -alternate web.xml : warn and skip
167      *
168      * @param file the file to add to the archive
169      * @param zOut the stream to write to
170      * @param vPath the name this entry shall have in the archive
171      * @param mode the Unix permissions to set.
172      * @throws IOException on output error
173      */

174     protected void zipFile(File JavaDoc file, ZipOutputStream zOut, String JavaDoc vPath,
175                            int mode)
176         throws IOException JavaDoc {
177         // If the file being added is WEB-INF/web.xml, we warn if it's
178
// not the one specified in the "webxml" attribute - or if
179
// it's being added twice, meaning the same file is specified
180
// by the "webxml" attribute and in a <fileset> element.
181
String JavaDoc vPathLowerCase = vPath.toLowerCase(Locale.ENGLISH);
182         //by default, we add the file.
183
boolean addFile = true;
184         if (XML_DESCRIPTOR_PATH_LC.equals(vPathLowerCase)) {
185             //a web.xml file was found. See if it is a duplicate or not
186
if (addedWebXmlFile != null) {
187                 //a second web.xml file, so skip it
188
addFile = false;
189                 //check to see if we warn or not
190
if (!FILE_UTILS.fileNameEquals(addedWebXmlFile, file)) {
191                     log("Warning: selected " + archiveType
192                             + " files include a second " + XML_DESCRIPTOR_PATH
193                             + " which will be ignored.\n"
194                             + "The duplicate entry is at " + file + '\n'
195                             + "The file that will be used is "
196                             + addedWebXmlFile,
197                             Project.MSG_WARN);
198                 }
199             } else {
200                 //no added file, yet
201
addedWebXmlFile = file;
202                 //there is no web.xml file, so add it
203
addFile = true;
204                 //and remember that we did
205
deploymentDescriptor = file;
206             }
207         }
208         if (addFile) {
209             super.zipFile(file, zOut, vPath, mode);
210         }
211     }
212
213
214     /**
215      * Make sure we don't think we already have a web.xml next time this task
216      * gets executed.
217      */

218     protected void cleanUp() {
219         if (addedWebXmlFile == null
220             && deploymentDescriptor == null
221             && needxmlfile
222             && !isInUpdateMode()) {
223             throw new BuildException("No WEB-INF/web.xml file was added.\n"
224                     + "If this is your intent, set needxml='false' ");
225         }
226         addedWebXmlFile = null;
227         super.cleanUp();
228     }
229 }
230
Popular Tags