KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
28  * Performs ClearCase checkout.
29  *
30  * <p>
31  * The following attributes are interpreted:
32  * <table border="1">
33  * <tr>
34  * <th>Attribute</th>
35  * <th>Values</th>
36  * <th>Required</th>
37  * </tr>
38  * <tr>
39  * <td>viewpath</td>
40  * <td>Path to the ClearCase view file or directory that the command will operate on</td>
41  * <td>No</td>
42  * <tr>
43  * <tr>
44  * <td>reserved</td>
45  * <td>Specifies whether to check out the file as reserved or not</td>
46  * <td>Yes</td>
47  * <tr>
48  * <tr>
49  * <td>out</td>
50  * <td>Creates a writable file under a different filename</td>
51  * <td>No</td>
52  * <tr>
53  * <tr>
54  * <td>nodata</td>
55  * <td>Checks out the file but does not create an editable file containing its data</td>
56  * <td>No</td>
57  * <tr>
58  * <tr>
59  * <td>branch</td>
60  * <td>Specify a branch to check out the file to</td>
61  * <td>No</td>
62  * <tr>
63  * <tr>
64  * <td>version</td>
65  * <td>Allows checkout of a version other than main latest</td>
66  * <td>No</td>
67  * <tr>
68  * <tr>
69  * <td>nowarn</td>
70  * <td>Suppress warning messages</td>
71  * <td>No</td>
72  * <tr>
73  * <tr>
74  * <td>comment</td>
75  * <td>Specify a comment. Only one of comment or cfile may be used.</td>
76  * <td>No</td>
77  * <tr>
78  * <tr>
79  * <td>commentfile</td>
80  * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
81  * <td>No</td>
82  * <tr>
83  * <tr>
84  * <td>notco</td>
85  * <td>Fail if it's already checked out to the current view. Set to false to ignore it.</td>
86  * <td>No</td>
87  * <tr>
88  * <tr>
89  * <td>failonerr</td>
90  * <td>Throw an exception if the command fails. Default is true</td>
91  * <td>No</td>
92  * <tr>
93  * </table>
94  *
95  */

96 public class CCCheckout extends ClearCase {
97     private boolean mReserved = true;
98     private String JavaDoc mOut = null;
99     private boolean mNdata = false;
100     private String JavaDoc mBranch = null;
101     private boolean mVersion = false;
102     private boolean mNwarn = false;
103     private String JavaDoc mComment = null;
104     private String JavaDoc mCfile = null;
105     private boolean mNotco = true;
106
107     /**
108      * Executes the task.
109      * <p>
110      * Builds a command line to execute cleartool and then calls Exec's run method
111      * to execute the command line.
112      * @throws BuildException if the command fails and failonerr is set to true
113      */

114     public void execute() throws BuildException {
115         Commandline commandLine = new Commandline();
116         Project aProj = getProject();
117         int result = 0;
118
119         // Default the viewpath to basedir if it is not specified
120
if (getViewPath() == null) {
121             setViewPath(aProj.getBaseDir().getPath());
122         }
123
124         // build the command line from what we got the format is
125
// cleartool checkout [options...] [viewpath ...]
126
// as specified in the CLEARTOOL.EXE help
127
commandLine.setExecutable(getClearToolCommand());
128         commandLine.createArgument().setValue(COMMAND_CHECKOUT);
129
130         checkOptions(commandLine);
131         /*
132          * If configured to not care about whether the element is
133          * already checked out to the current view.
134          * Then check to see if it is checked out.
135          */

136         if (!getNotco() && lsCheckout()) {
137             getProject().log("Already checked out in this view: "
138                     + getViewPathBasename(), Project.MSG_VERBOSE);
139             return;
140         }
141         if (!getFailOnErr()) {
142             getProject().log("Ignoring any errors that occur for: "
143                     + getViewPathBasename(), Project.MSG_VERBOSE);
144         }
145         result = run(commandLine);
146         if (Execute.isFailure(result) && getFailOnErr()) {
147             String JavaDoc msg = "Failed executing: " + commandLine.toString();
148             throw new BuildException(msg, getLocation());
149         }
150     }
151
152     /**
153      * Check to see if the element is checked out in the current view.
154      */

155     private boolean lsCheckout() {
156         Commandline cmdl = new Commandline();
157         String JavaDoc result;
158
159         // build the command line from what we got the format is
160
// cleartool lsco [options...] [viewpath ...]
161
// as specified in the CLEARTOOL.EXE help
162
cmdl.setExecutable(getClearToolCommand());
163         cmdl.createArgument().setValue(COMMAND_LSCO);
164         cmdl.createArgument().setValue("-cview");
165         cmdl.createArgument().setValue("-short");
166         cmdl.createArgument().setValue("-d");
167         // viewpath
168
cmdl.createArgument().setValue(getViewPath());
169
170         result = runS(cmdl);
171
172         // System.out.println( "lsCheckout: " + result );
173

174         return (result != null && result.length() > 0) ? true : false;
175     }
176     /**
177      * Check the command line options.
178      */

179     private void checkOptions(Commandline cmd) {
180         // ClearCase items
181
if (getReserved()) {
182             // -reserved
183
cmd.createArgument().setValue(FLAG_RESERVED);
184         } else {
185             // -unreserved
186
cmd.createArgument().setValue(FLAG_UNRESERVED);
187         }
188
189         if (getOut() != null) {
190             // -out
191
getOutCommand(cmd);
192         } else {
193             if (getNoData()) {
194                 // -ndata
195
cmd.createArgument().setValue(FLAG_NODATA);
196             }
197
198         }
199
200         if (getBranch() != null) {
201             // -branch
202
getBranchCommand(cmd);
203         } else {
204             if (getVersion()) {
205                 // -version
206
cmd.createArgument().setValue(FLAG_VERSION);
207             }
208
209         }
210
211         if (getNoWarn()) {
212             // -nwarn
213
cmd.createArgument().setValue(FLAG_NOWARN);
214         }
215
216         if (getComment() != null) {
217             // -c
218
getCommentCommand(cmd);
219         } else {
220             if (getCommentFile() != null) {
221                 // -cfile
222
getCommentFileCommand(cmd);
223             } else {
224                 cmd.createArgument().setValue(FLAG_NOCOMMENT);
225             }
226         }
227
228         // viewpath
229
cmd.createArgument().setValue(getViewPath());
230
231         // Print out info about the notco option
232
// System.out.println( "Notco: " + (getNotco() ? "yes" : "no") );
233
}
234
235     /**
236      * If true, checks out the file as reserved.
237      *
238      * @param reserved the status to set the flag to
239      */

240     public void setReserved(boolean reserved) {
241         mReserved = reserved;
242     }
243
244     /**
245      * Get reserved flag status
246      *
247      * @return boolean containing status of reserved flag
248      */

249     public boolean getReserved() {
250         return mReserved;
251     }
252
253     /**
254      * If true, checkout fails if the element is already checked out to the current view.
255      *
256      * @param notco the status to set the flag to
257      * @since ant 1.6.1
258      */

259     public void setNotco(boolean notco) {
260         mNotco = notco;
261     }
262
263     /**
264      * Get notco flag status
265      *
266      * @return boolean containing status of notco flag
267      * @since ant 1.6.1
268      */

269     public boolean getNotco() {
270         return mNotco;
271     }
272
273
274     /**
275      * Creates a writable file under a different filename.
276      *
277      * @param outf the path to the out file
278      */

279     public void setOut(String JavaDoc outf) {
280         mOut = outf;
281     }
282
283     /**
284      * Get out file
285      *
286      * @return String containing the path to the out file
287      */

288     public String JavaDoc getOut() {
289         return mOut;
290     }
291
292     /**
293      * If true, checks out the file but does not create an
294      * editable file containing its data.
295      *
296      * @param ndata the status to set the flag to
297      */

298     public void setNoData(boolean ndata) {
299         mNdata = ndata;
300     }
301
302     /**
303      * Get nodata flag status
304      *
305      * @return boolean containing status of ndata flag
306      */

307     public boolean getNoData() {
308         return mNdata;
309     }
310
311     /**
312      * Specify a branch to check out the file to.
313      *
314      * @param branch the name of the branch
315      */

316     public void setBranch(String JavaDoc branch) {
317         mBranch = branch;
318     }
319
320     /**
321      * Get branch name
322      *
323      * @return String containing the name of the branch
324      */

325     public String JavaDoc getBranch() {
326         return mBranch;
327     }
328
329     /**
330      * If true, allows checkout of a version other than main latest.
331      *
332      * @param version the status to set the flag to
333      */

334     public void setVersion(boolean version) {
335         mVersion = version;
336     }
337
338     /**
339      * Get version flag status
340      *
341      * @return boolean containing status of version flag
342      */

343     public boolean getVersion() {
344         return mVersion;
345     }
346
347     /**
348      * If true, warning messages are suppressed.
349      *
350      * @param nwarn the status to set the flag to
351      */

352     public void setNoWarn(boolean nwarn) {
353         mNwarn = nwarn;
354     }
355
356     /**
357      * Get nowarn flag status
358      *
359      * @return boolean containing status of nwarn flag
360      */

361     public boolean getNoWarn() {
362         return mNwarn;
363     }
364
365     /**
366      * Sets the comment string.
367      *
368      * @param comment the comment string
369      */

370     public void setComment(String JavaDoc comment) {
371         mComment = comment;
372     }
373
374     /**
375      * Get comment string
376      *
377      * @return String containing the comment
378      */

379     public String JavaDoc getComment() {
380         return mComment;
381     }
382
383     /**
384      * Specifies a file containing a comment.
385      *
386      * @param cfile the path to the comment file
387      */

388     public void setCommentFile(String JavaDoc cfile) {
389         mCfile = cfile;
390     }
391
392     /**
393      * Get comment file
394      *
395      * @return String containing the path to the comment file
396      */

397     public String JavaDoc getCommentFile() {
398         return mCfile;
399     }
400
401     /**
402      * Get the 'out' command
403      *
404      * @param cmd containing the command line string with or
405      * without the out flag and path appended
406      */

407     private void getOutCommand(Commandline cmd) {
408         if (getOut() != null) {
409             /* Had to make two separate commands here because if a space is
410                inserted between the flag and the value, it is treated as a
411                Windows filename with a space and it is enclosed in double
412                quotes ("). This breaks clearcase.
413             */

414             cmd.createArgument().setValue(FLAG_OUT);
415             cmd.createArgument().setValue(getOut());
416         }
417     }
418
419     /**
420      * Get the 'branch' command
421      *
422      * @param cmd containing the command line string with or
423                           without the branch flag and name appended
424      */

425     private void getBranchCommand(Commandline cmd) {
426         if (getBranch() != null) {
427             /* Had to make two separate commands here because if a space is
428                inserted between the flag and the value, it is treated as a
429                Windows filename with a space and it is enclosed in double
430                quotes ("). This breaks clearcase.
431             */

432             cmd.createArgument().setValue(FLAG_BRANCH);
433             cmd.createArgument().setValue(getBranch());
434         }
435     }
436
437
438     /**
439      * Get the 'comment' command
440      *
441      * @param cmd containing the command line string with or
442      * without the comment flag and string appended
443      */

444     private void getCommentCommand(Commandline cmd) {
445         if (getComment() != null) {
446             /* Had to make two separate commands here because if a space is
447                inserted between the flag and the value, it is treated as a
448                Windows filename with a space and it is enclosed in double
449                quotes ("). This breaks clearcase.
450             */

451             cmd.createArgument().setValue(FLAG_COMMENT);
452             cmd.createArgument().setValue(getComment());
453         }
454     }
455
456     /**
457      * Get the 'cfile' command
458      *
459      * @param cmd containing the command line string with or
460      * without the cfile flag and file appended
461      */

462     private void getCommentFileCommand(Commandline cmd) {
463         if (getCommentFile() != null) {
464             /* Had to make two separate commands here because if a space is
465                inserted between the flag and the value, it is treated as a
466                Windows filename with a space and it is enclosed in double
467                quotes ("). This breaks clearcase.
468             */

469             cmd.createArgument().setValue(FLAG_COMMENTFILE);
470             cmd.createArgument().setValue(getCommentFile());
471         }
472     }
473
474         /**
475      * -reserved flag -- check out the file as reserved
476      */

477     public static final String JavaDoc FLAG_RESERVED = "-reserved";
478         /**
479      * -reserved flag -- check out the file as unreserved
480      */

481     public static final String JavaDoc FLAG_UNRESERVED = "-unreserved";
482         /**
483      * -out flag -- create a writable file under a different filename
484      */

485     public static final String JavaDoc FLAG_OUT = "-out";
486         /**
487      * -ndata flag -- checks out the file but does not create an editable file containing its data
488      */

489     public static final String JavaDoc FLAG_NODATA = "-ndata";
490         /**
491      * -branch flag -- checks out the file on a specified branch
492      */

493     public static final String JavaDoc FLAG_BRANCH = "-branch";
494         /**
495      * -version flag -- allows checkout of a version that is not main latest
496      */

497     public static final String JavaDoc FLAG_VERSION = "-version";
498         /**
499      * -nwarn flag -- suppresses warning messages
500      */

501     public static final String JavaDoc FLAG_NOWARN = "-nwarn";
502         /**
503      * -c flag -- comment to attach to the file
504      */

505     public static final String JavaDoc FLAG_COMMENT = "-c";
506         /**
507      * -cfile flag -- file containing a comment to attach to the file
508      */

509     public static final String JavaDoc FLAG_COMMENTFILE = "-cfile";
510         /**
511      * -nc flag -- no comment is specified
512      */

513     public static final String JavaDoc FLAG_NOCOMMENT = "-nc";
514
515 }
516
517
Popular Tags