KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > TarFileSet


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.types;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.Project;
22
23 /**
24  * A TarFileSet is a FileSet with extra attributes useful in the context of
25  * Tar/Jar tasks.
26  *
27  * A TarFileSet extends FileSets with the ability to extract a subset of the
28  * entries of a Tar file for inclusion in another Tar file. It also includes
29  * a prefix attribute which is prepended to each entry in the output Tar file.
30  *
31  */

32 public class TarFileSet extends ArchiveFileSet {
33
34     private boolean userNameSet;
35     private boolean groupNameSet;
36     private boolean userIdSet;
37     private boolean groupIdSet;
38
39     private String JavaDoc userName = "";
40     private String JavaDoc groupName = "";
41     private int uid;
42     private int gid;
43
44     /** Constructor for TarFileSet */
45     public TarFileSet() {
46         super();
47     }
48
49     /**
50      * Constructor using a fileset arguement.
51      * @param fileset the fileset to use
52      */

53     protected TarFileSet(FileSet fileset) {
54         super(fileset);
55     }
56
57     /**
58      * Constructor using a tarfileset arguement.
59      * @param fileset the tarfileset to use
60      */

61     protected TarFileSet(TarFileSet fileset) {
62         super(fileset);
63     }
64
65     /**
66      * The username for the tar entry
67      * This is not the same as the UID.
68      * @param userName the user name for the tar entry.
69      */

70     public void setUserName(String JavaDoc userName) {
71         checkTarFileSetAttributesAllowed();
72         userNameSet = true;
73         this.userName = userName;
74     }
75
76     /**
77      * @return the user name for the tar entry
78      */

79     public String JavaDoc getUserName() {
80         if (isReference()) {
81             return ((TarFileSet) getCheckedRef()).getUserName();
82         }
83         return userName;
84     }
85
86     /**
87      * @return whether the user name has been explicitly set.
88      */

89     public boolean hasUserNameBeenSet() {
90         return userNameSet;
91     }
92
93     /**
94      * The uid for the tar entry
95      * This is not the same as the User name.
96      * @param uid the id of the user for the tar entry.
97      */

98     public void setUid(int uid) {
99         checkTarFileSetAttributesAllowed();
100         userIdSet = true;
101         this.uid = uid;
102     }
103
104     /**
105      * @return the uid for the tar entry
106      */

107     public int getUid() {
108         if (isReference()) {
109             return ((TarFileSet) getCheckedRef()).getUid();
110         }
111         return uid;
112     }
113
114     /**
115      * @return whether the user id has been explicitly set.
116      */

117     public boolean hasUserIdBeenSet() {
118         return userIdSet;
119     }
120
121     /**
122      * The groupname for the tar entry; optional, default=""
123      * This is not the same as the GID.
124      * @param groupName the group name string.
125      */

126     public void setGroup(String JavaDoc groupName) {
127         checkTarFileSetAttributesAllowed();
128         groupNameSet = true;
129         this.groupName = groupName;
130     }
131
132     /**
133      * @return the group name string.
134      */

135     public String JavaDoc getGroup() {
136         if (isReference()) {
137             return ((TarFileSet) getCheckedRef()).getGroup();
138         }
139         return groupName;
140     }
141
142     /**
143      * @return whether the group name has been explicitly set.
144      */

145     public boolean hasGroupBeenSet() {
146         return groupNameSet;
147     }
148
149     /**
150      * The GID for the tar entry; optional, default="0"
151      * This is not the same as the group name.
152      * @param gid the group id.
153      */

154     public void setGid(int gid) {
155         checkTarFileSetAttributesAllowed();
156         groupIdSet = true;
157         this.gid = gid;
158     }
159
160     /**
161      * @return the group identifier.
162      */

163     public int getGid() {
164         if (isReference()) {
165             return ((TarFileSet) getCheckedRef()).getGid();
166         }
167         return gid;
168     }
169
170     /**
171      * @return whether the group id has been explicitly set.
172      */

173     public boolean hasGroupIdBeenSet() {
174         return groupIdSet;
175     }
176
177     /**
178      * Create a new scanner.
179      * @return the created scanner.
180      */

181     protected ArchiveScanner newArchiveScanner() {
182         TarScanner zs = new TarScanner();
183         return zs;
184     }
185
186     /**
187      * Makes this instance in effect a reference to another instance.
188      *
189      * <p>You must not set another attribute or nest elements inside
190      * this element if you make it a reference.</p>
191      * @param r the <code>Reference</code> to use.
192      * @throws BuildException on error
193      */

194     public void setRefid(Reference r) throws BuildException {
195         if (userNameSet || userIdSet || groupNameSet || groupIdSet) {
196             throw tooManyAttributes();
197         }
198         super.setRefid(r);
199     }
200
201     /**
202      * A TarFileset accepts another TarFileSet or a FileSet as reference
203      * FileSets are often used by the war task for the lib attribute
204      * @param p the project to use
205      * @return the abstract fileset instance
206      */

207     protected AbstractFileSet getRef(Project p) {
208         dieOnCircularReference(p);
209         Object JavaDoc o = getRefid().getReferencedObject(p);
210         if (o instanceof TarFileSet) {
211             return (AbstractFileSet) o;
212         } else if (o instanceof FileSet) {
213             TarFileSet zfs = new TarFileSet((FileSet) o);
214             configureFileSet(zfs);
215             return zfs;
216         } else {
217             String JavaDoc msg = getRefid().getRefId() + " doesn\'t denote a tarfileset or a fileset";
218             throw new BuildException(msg);
219         }
220     }
221
222     /**
223      * Configure a fileset based on this fileset.
224      * If the fileset is a TarFileSet copy in the tarfileset
225      * specific attributes.
226      * @param zfs the archive fileset to configure.
227      */

228     protected void configureFileSet(ArchiveFileSet zfs) {
229         super.configureFileSet(zfs);
230         if (zfs instanceof TarFileSet) {
231             TarFileSet tfs = (TarFileSet) zfs;
232             tfs.setUserName(userName);
233             tfs.setGroup(groupName);
234             tfs.setUid(uid);
235             tfs.setGid(gid);
236         }
237     }
238
239     /**
240      * Return a TarFileSet that has the same properties
241      * as this one.
242      * @return the cloned tarFileSet
243      */

244     public Object JavaDoc clone() {
245         if (isReference()) {
246             return ((TarFileSet) getRef(getProject())).clone();
247         } else {
248             return super.clone();
249         }
250     }
251
252     /**
253      * A check attributes for TarFileSet.
254      * If there is a reference, and
255      * it is a TarFileSet, the tar fileset attributes
256      * cannot be used.
257      */

258     private void checkTarFileSetAttributesAllowed() {
259         if (getProject() == null
260             || (isReference()
261                 && (getRefid().getReferencedObject(
262                         getProject())
263                     instanceof TarFileSet))) {
264             checkAttributesAllowed();
265         }
266     }
267
268 }
269
Popular Tags