KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.types.ZipFileSet;
25 import org.apache.tools.ant.util.FileUtils;
26 import org.apache.tools.zip.ZipOutputStream;
27
28 /**
29  * Creates a EAR archive. Based on WAR task
30  *
31  * @since Ant 1.4
32  *
33  * @ant.task category="packaging"
34  */

35 public class Ear extends Jar {
36     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
37
38     private File JavaDoc deploymentDescriptor;
39     private boolean descriptorAdded;
40
41     /**
42      * Create an Ear task.
43      */

44     public Ear() {
45         super();
46         archiveType = "ear";
47         emptyBehavior = "create";
48     }
49
50     /**
51      * Set the destination file.
52      * @param earFile the destination file
53      * @deprecated since 1.5.x.
54      * Use setDestFile(destfile) instead.
55      */

56     public void setEarfile(File JavaDoc earFile) {
57         setDestFile(earFile);
58     }
59
60     /**
61      * File to incorporate as application.xml.
62      * @param descr the descriptor file
63      */

64     public void setAppxml(File JavaDoc descr) {
65         deploymentDescriptor = descr;
66         if (!deploymentDescriptor.exists()) {
67             throw new BuildException("Deployment descriptor: "
68                                      + deploymentDescriptor
69                                      + " does not exist.");
70         }
71
72         // Create a ZipFileSet for this file, and pass it up.
73
ZipFileSet fs = new ZipFileSet();
74         fs.setFile(deploymentDescriptor);
75         fs.setFullpath("META-INF/application.xml");
76         super.addFileset(fs);
77     }
78
79
80     /**
81      * Adds zipfileset.
82      *
83      * @param fs zipfileset to add
84      */

85     public void addArchives(ZipFileSet fs) {
86         // We just set the prefix for this fileset, and pass it up.
87
// Do we need to do this? LH
88
fs.setPrefix("/");
89         super.addFileset(fs);
90     }
91
92
93     /**
94      * Initialize the output stream.
95      * @param zOut the zip output stream.
96      * @throws IOException on I/O errors
97      * @throws BuildException on other errors
98      */

99     protected void initZipOutputStream(ZipOutputStream zOut)
100         throws IOException JavaDoc, BuildException {
101         // If no webxml file is specified, it's an error.
102
if (deploymentDescriptor == null && !isInUpdateMode()) {
103             throw new BuildException("appxml attribute is required", getLocation());
104         }
105
106         super.initZipOutputStream(zOut);
107     }
108
109     /**
110      * Overridden from Zip class to deal with application.xml
111      * @param file the file to add to the archive
112      * @param zOut the stream to write to
113      * @param vPath the name this entry shall have in the archive
114      * @param mode the Unix permissions to set.
115      * @throws IOException on error
116      */

117     protected void zipFile(File JavaDoc file, ZipOutputStream zOut, String JavaDoc vPath,
118                            int mode)
119         throws IOException JavaDoc {
120         // If the file being added is META-INF/application.xml, we
121
// warn if it's not the one specified in the "appxml"
122
// attribute - or if it's being added twice, meaning the same
123
// file is specified by the "appxml" attribute and in a
124
// <fileset> element.
125
if (vPath.equalsIgnoreCase("META-INF/application.xml")) {
126             if (deploymentDescriptor == null
127                 || !FILE_UTILS.fileNameEquals(deploymentDescriptor, file)
128                 || descriptorAdded) {
129                 log("Warning: selected " + archiveType
130                     + " files include a META-INF/application.xml which will"
131                     + " be ignored (please use appxml attribute to "
132                     + archiveType + " task)", Project.MSG_WARN);
133             } else {
134                 super.zipFile(file, zOut, vPath, mode);
135                 descriptorAdded = true;
136             }
137         } else {
138             super.zipFile(file, zOut, vPath, mode);
139         }
140     }
141
142     /**
143      * Make sure we don't think we already have a application.xml next
144      * time this task gets executed.
145      */

146     protected void cleanUp() {
147         descriptorAdded = false;
148         super.cleanUp();
149     }
150 }
151
Popular Tags