KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.taskdefs.condition.Os;
26 import org.apache.tools.ant.types.Commandline;
27 import org.apache.tools.ant.types.FileSet;
28 import org.apache.tools.ant.types.PatternSet;
29
30 /**
31  * Chmod equivalent for unix-like environments.
32  *
33  * @since Ant 1.1
34  *
35  * @ant.task category="filesystem"
36  * @todo Refactor so it does not extend from ExecuteOn and then turn around
37  * and unsupport several attributes.
38  */

39 public class Chmod extends ExecuteOn {
40
41     private FileSet defaultSet = new FileSet();
42     private boolean defaultSetDefined = false;
43     private boolean havePerm = false;
44
45     /**
46      * Chmod task for setting file and directory permissions.
47      */

48     public Chmod() {
49         super.setExecutable("chmod");
50         super.setParallel(true);
51         super.setSkipEmptyFilesets(true);
52     }
53
54     /**
55      * Set the project of this task.
56      * Calls the super class and sets the project on dhe default FileSet.
57      * @param project the project for this task.
58      * @see org.apache.tools.ant.ProjectComponent#setProject
59      */

60     public void setProject(Project project) {
61         super.setProject(project);
62         defaultSet.setProject(project);
63     }
64
65     /**
66      * The file or single directory of which the permissions must be changed.
67      * @param src the source file or directory.
68      */

69     public void setFile(File JavaDoc src) {
70         FileSet fs = new FileSet();
71         fs.setFile(src);
72         addFileset(fs);
73     }
74
75     /**
76      * The directory which holds the files whose permissions must be changed.
77      * @param src the directory.
78      */

79     public void setDir(File JavaDoc src) {
80         defaultSet.setDir(src);
81     }
82
83     /**
84      * Set the new permissions.
85      * @param perm the new permissions.
86      */

87     public void setPerm(String JavaDoc perm) {
88         createArg().setValue(perm);
89         havePerm = true;
90     }
91
92     /**
93      * Add a name entry on the include list.
94      * @return a NameEntry to be configured.
95      */

96     public PatternSet.NameEntry createInclude() {
97         defaultSetDefined = true;
98         return defaultSet.createInclude();
99     }
100
101     /**
102      * Add a name entry on the exclude list.
103      * @return a nameentry to be configured.
104      */

105     public PatternSet.NameEntry createExclude() {
106         defaultSetDefined = true;
107         return defaultSet.createExclude();
108     }
109
110     /**
111      * Add a set of patterns.
112      * @return a patternset to be configured.
113      */

114     public PatternSet createPatternSet() {
115         defaultSetDefined = true;
116         return defaultSet.createPatternSet();
117     }
118
119     /**
120      * Sets the set of include patterns. Patterns may be separated by a comma
121      * or a space.
122      *
123      * @param includes the string containing the include patterns.
124      */

125     public void setIncludes(String JavaDoc includes) {
126         defaultSetDefined = true;
127         defaultSet.setIncludes(includes);
128     }
129
130     /**
131      * Sets the set of exclude patterns. Patterns may be separated by a comma
132      * or a space.
133      *
134      * @param excludes the string containing the exclude patterns.
135      */

136     public void setExcludes(String JavaDoc excludes) {
137         defaultSetDefined = true;
138         defaultSet.setExcludes(excludes);
139     }
140
141     /**
142      * Sets whether default exclusions should be used or not.
143      *
144      * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
145      * should be used, "false"|"off"|"no" when they
146      * shouldn't be used.
147      */

148     public void setDefaultexcludes(boolean useDefaultExcludes) {
149         defaultSetDefined = true;
150         defaultSet.setDefaultexcludes(useDefaultExcludes);
151     }
152
153     /**
154      * Check the attributes and nested elements.
155      */

156     protected void checkConfiguration() {
157         if (!havePerm) {
158             throw new BuildException("Required attribute perm not set in chmod",
159                                      getLocation());
160         }
161
162         if (defaultSetDefined && defaultSet.getDir(getProject()) != null) {
163             addFileset(defaultSet);
164         }
165         super.checkConfiguration();
166     }
167
168     /**
169      * Carry out the chmoding.
170      * @throws BuildException on error.
171      */

172     public void execute() throws BuildException {
173         /*
174          * In Ant 1.1, <chmod dir="foo" /> means, change the permissions
175          * of directory foo, not anything inside of it. This is the case the
176          * second branch of the if statement below catches for backwards
177          * compatibility.
178          */

179         if (defaultSetDefined || defaultSet.getDir(getProject()) == null) {
180             try {
181                 super.execute();
182             } finally {
183                 if (defaultSetDefined && defaultSet.getDir(getProject()) != null) {
184                     filesets.removeElement(defaultSet);
185                 }
186             }
187         } else if (isValidOs()) {
188             // we are chmodding the given directory
189
Execute execute = prepareExec();
190             Commandline cloned = (Commandline) cmdl.clone();
191             cloned.createArgument().setValue(defaultSet.getDir(getProject())
192                                              .getPath());
193             try {
194                 execute.setCommandline(cloned.getCommandline());
195                 runExecute(execute);
196             } catch (IOException JavaDoc e) {
197                 throw new BuildException("Execute failed: " + e, e, getLocation());
198             } finally {
199                 // close the output file if required
200
logFlush();
201             }
202         }
203     }
204
205     /**
206      * Set the executable.
207      * This is not allowed for Chmod.
208      * @param e ignored.
209      * @throws BuildException always.
210      * @ant.attribute ignore="true"
211      */

212     public void setExecutable(String JavaDoc e) {
213         throw new BuildException(getTaskType()
214             + " doesn\'t support the executable attribute", getLocation());
215     }
216
217     /**
218      * Set the command.
219      * This is not allowed for Chmod.
220      * @param cmdl ignored.
221      * @throws BuildException always.
222      * @ant.attribute ignore="true"
223      */

224     public void setCommand(Commandline cmdl) {
225         throw new BuildException(getTaskType()
226             + " doesn\'t support the command attribute", getLocation());
227     }
228
229     /**
230      * This is not allowed for Chmod.
231      * @param skip ignored.
232      * @throws BuildException always.
233      * @ant.attribute ignore="true"
234      */

235     public void setSkipEmptyFilesets(boolean skip) {
236         throw new BuildException(getTaskType()
237             + " doesn\'t support the skipemptyfileset attribute", getLocation());
238     }
239
240     /**
241      * This is not allowed for Chmod.
242      * @param b ignored.
243      * @throws BuildException always.
244      * @ant.attribute ignore="true"
245      */

246     public void setAddsourcefile(boolean b) {
247         throw new BuildException(getTaskType()
248             + " doesn\'t support the addsourcefile attribute", getLocation());
249     }
250
251     /**
252      * Check if the os is valid.
253      * Always include unix.
254      * @return true if the os is valid.
255      */

256     protected boolean isValidOs() {
257         return Os.isFamily(Os.FAMILY_UNIX) && super.isValidOs();
258     }
259 }
260
Popular Tags