KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > clearcase > CCMklabel


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.optional.clearcase;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.taskdefs.Execute;
24 import org.apache.tools.ant.types.Commandline;
25
26 /**
27  * Task to perform mklabel command to ClearCase.
28  * <p>
29  * The following attributes are interpreted:
30  * <table border="1">
31  * <tr>
32  * <th>Attribute</th>
33  * <th>Values</th>
34  * <th>Required</th>
35  * </tr>
36  * <tr>
37  * <td>viewpath</td>
38  * <td>Path to the ClearCase view file or directory that the command will operate on</td>
39  * <td>No</td>
40  * <tr>
41  * <tr>
42  * <td>replace</td>
43  * <td>Replace a label of the same type on the same branch</td>
44  * <td>No</td>
45  * <tr>
46  * <tr>
47  * <td>recurse</td>
48  * <td>Process each subdirectory under viewpath</td>
49  * <td>No</td>
50  * <tr>
51  * <tr>
52  * <td>version</td>
53  * <td>Identify a specific version to attach the label to</td>
54  * <td>No</td>
55  * <tr>
56  * <tr>
57  * <td>typename</td>
58  * <td>Name of the label type</td>
59  * <td>Yes</td>
60  * <tr>
61  * <tr>
62  * <td>vob</td>
63  * <td>Name of the VOB</td>
64  * <td>No</td>
65  * <tr>
66  * <tr>
67  * <td>comment</td>
68  * <td>Specify a comment. Only one of comment or cfile may be used.</td>
69  * <td>No</td>
70  * <tr>
71  * <tr>
72  * <td>commentfile</td>
73  * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
74  * <td>No</td>
75  * <tr>
76  * <tr>
77  * <td>failonerr</td>
78  * <td>Throw an exception if the command fails. Default is true</td>
79  * <td>No</td>
80  * <tr>
81  * </table>
82  *
83  */

84 public class CCMklabel extends ClearCase {
85     private boolean mReplace = false;
86     private boolean mRecurse = false;
87     private String JavaDoc mVersion = null;
88     private String JavaDoc mTypeName = null;
89     private String JavaDoc mVOB = null;
90     private String JavaDoc mComment = null;
91     private String JavaDoc mCfile = null;
92
93     /**
94      * Executes the task.
95      * <p>
96      * Builds a command line to execute cleartool and then calls Exec's run method
97      * to execute the command line.
98      * @throws BuildException if the command fails and failonerr is set to true
99      */

100     public void execute() throws BuildException {
101         Commandline commandLine = new Commandline();
102         Project aProj = getProject();
103         int result = 0;
104
105         // Check for required attributes
106
if (getTypeName() == null) {
107             throw new BuildException("Required attribute TypeName not specified");
108         }
109
110         // Default the viewpath to basedir if it is not specified
111
if (getViewPath() == null) {
112             setViewPath(aProj.getBaseDir().getPath());
113         }
114
115         // build the command line from what we got. the format is
116
// cleartool mklabel [options...] [viewpath ...]
117
// as specified in the CLEARTOOL help
118
commandLine.setExecutable(getClearToolCommand());
119         commandLine.createArgument().setValue(COMMAND_MKLABEL);
120
121         checkOptions(commandLine);
122
123         if (!getFailOnErr()) {
124             getProject().log("Ignoring any errors that occur for: "
125                     + getViewPathBasename(), Project.MSG_VERBOSE);
126         }
127         result = run(commandLine);
128         if (Execute.isFailure(result) && getFailOnErr()) {
129             String JavaDoc msg = "Failed executing: " + commandLine.toString();
130             throw new BuildException(msg, getLocation());
131         }
132     }
133
134
135     /**
136      * Check the command line options.
137      */

138     private void checkOptions(Commandline cmd) {
139         if (getReplace()) {
140             // -replace
141
cmd.createArgument().setValue(FLAG_REPLACE);
142         }
143
144         if (getRecurse()) {
145             // -recurse
146
cmd.createArgument().setValue(FLAG_RECURSE);
147         }
148
149         if (getVersion() != null) {
150             // -version
151
getVersionCommand(cmd);
152         }
153
154         if (getComment() != null) {
155             // -c
156
getCommentCommand(cmd);
157         } else {
158             if (getCommentFile() != null) {
159                 // -cfile
160
getCommentFileCommand(cmd);
161             } else {
162                 cmd.createArgument().setValue(FLAG_NOCOMMENT);
163             }
164         }
165
166         if (getTypeName() != null) {
167             // type
168
getTypeCommand(cmd);
169         }
170
171         // viewpath
172
cmd.createArgument().setValue(getViewPath());
173     }
174
175
176     /**
177      * Set the replace flag
178      *
179      * @param replace the status to set the flag to
180      */

181     public void setReplace(boolean replace) {
182         mReplace = replace;
183     }
184
185     /**
186      * Get replace flag status
187      *
188      * @return boolean containing status of replace flag
189      */

190     public boolean getReplace() {
191         return mReplace;
192     }
193
194     /**
195      * Set recurse flag
196      *
197      * @param recurse the status to set the flag to
198      */

199     public void setRecurse(boolean recurse) {
200         mRecurse = recurse;
201     }
202
203     /**
204      * Get recurse flag status
205      *
206      * @return boolean containing status of recurse flag
207      */

208     public boolean getRecurse() {
209         return mRecurse;
210     }
211
212     /**
213      * Set the version flag
214      *
215      * @param version the status to set the flag to
216      */

217     public void setVersion(String JavaDoc version) {
218         mVersion = version;
219     }
220
221     /**
222      * Get version flag status
223      *
224      * @return boolean containing status of version flag
225      */

226     public String JavaDoc getVersion() {
227         return mVersion;
228     }
229
230     /**
231      * Set comment string
232      *
233      * @param comment the comment string
234      */

235     public void setComment(String JavaDoc comment) {
236         mComment = comment;
237     }
238
239     /**
240      * Get comment string
241      *
242      * @return String containing the comment
243      */

244     public String JavaDoc getComment() {
245         return mComment;
246     }
247
248     /**
249      * Set comment file
250      *
251      * @param cfile the path to the comment file
252      */

253     public void setCommentFile(String JavaDoc cfile) {
254         mCfile = cfile;
255     }
256
257     /**
258      * Get comment file
259      *
260      * @return String containing the path to the comment file
261      */

262     public String JavaDoc getCommentFile() {
263         return mCfile;
264     }
265
266     /**
267      * Set the type-name
268      *
269      * @param tn the type name
270      */

271     public void setTypeName(String JavaDoc tn) {
272         mTypeName = tn;
273     }
274
275     /**
276      * Get type-name
277      *
278      * @return String containing type name
279      */

280     public String JavaDoc getTypeName() {
281         return mTypeName;
282     }
283
284     /**
285      * Set the VOB name
286      *
287      * @param vob the VOB name
288      */

289     public void setVOB(String JavaDoc vob) {
290         mVOB = vob;
291     }
292
293     /**
294      * Get VOB name
295      *
296      * @return String containing VOB name
297      */

298     public String JavaDoc getVOB() {
299         return mVOB;
300     }
301
302
303     /**
304      * Get the 'version' command
305      *
306      * @param cmd CommandLine containing the command line string with or
307      * without the version flag and string appended
308      */

309     private void getVersionCommand(Commandline cmd) {
310         if (getVersion() != null) {
311             /* Had to make two separate commands here because if a space is
312                inserted between the flag and the value, it is treated as a
313                Windows filename with a space and it is enclosed in double
314                quotes ("). This breaks clearcase.
315             */

316             cmd.createArgument().setValue(FLAG_VERSION);
317             cmd.createArgument().setValue(getVersion());
318         }
319     }
320
321     /**
322      * Get the 'comment' command
323      *
324      * @param cmd containing the command line string with or
325      * without the comment flag and string appended
326      */

327     private void getCommentCommand(Commandline cmd) {
328         if (getComment() != null) {
329             /* Had to make two separate commands here because if a space is
330                inserted between the flag and the value, it is treated as a
331                Windows filename with a space and it is enclosed in double
332                quotes ("). This breaks clearcase.
333             */

334             cmd.createArgument().setValue(FLAG_COMMENT);
335             cmd.createArgument().setValue(getComment());
336         }
337     }
338
339     /**
340      * Get the 'commentfile' command
341      *
342      * @param cmd containing the command line string with or
343      * without the commentfile flag and file appended
344      */

345     private void getCommentFileCommand(Commandline cmd) {
346         if (getCommentFile() != null) {
347             /* Had to make two separate commands here because if a space is
348                inserted between the flag and the value, it is treated as a
349                Windows filename with a space and it is enclosed in double
350                quotes ("). This breaks clearcase.
351             */

352             cmd.createArgument().setValue(FLAG_COMMENTFILE);
353             cmd.createArgument().setValue(getCommentFile());
354         }
355     }
356
357     /**
358      * Get the type-name
359      *
360      * @param cmd containing the command line string with or
361      * without the type-name
362      */

363     private void getTypeCommand(Commandline cmd) {
364         String JavaDoc typenm = null;
365
366         if (getTypeName() != null) {
367             typenm = getTypeName();
368             if (getVOB() != null) {
369                 typenm += "@" + getVOB();
370             }
371             cmd.createArgument().setValue(typenm);
372         }
373     }
374
375
376     /**
377      * -replace flag -- replace another label of the same type
378      */

379     public static final String JavaDoc FLAG_REPLACE = "-replace";
380     /**
381      * -recurse flag -- process all subdirectories
382      */

383     public static final String JavaDoc FLAG_RECURSE = "-recurse";
384     /**
385      * -version flag -- attach label to specified version
386      */

387     public static final String JavaDoc FLAG_VERSION = "-version";
388     /**
389      * -c flag -- comment to attach to the file
390      */

391     public static final String JavaDoc FLAG_COMMENT = "-c";
392     /**
393      * -cfile flag -- file containing a comment to attach to the file
394      */

395     public static final String JavaDoc FLAG_COMMENTFILE = "-cfile";
396     /**
397      * -nc flag -- no comment is specified
398      */

399     public static final String JavaDoc FLAG_NOCOMMENT = "-nc";
400
401 }
402
403
Popular Tags