001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.clearcase;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.taskdefs.Execute;
024: import org.apache.tools.ant.types.Commandline;
025:
026: /**
027: * Performs ClearCase checkout.
028: *
029: * <p>
030: * The following attributes are interpreted:
031: * <table border="1">
032: * <tr>
033: * <th>Attribute</th>
034: * <th>Values</th>
035: * <th>Required</th>
036: * </tr>
037: * <tr>
038: * <td>viewpath</td>
039: * <td>Path to the ClearCase view file or directory that the command will operate on</td>
040: * <td>No</td>
041: * <tr>
042: * <tr>
043: * <td>reserved</td>
044: * <td>Specifies whether to check out the file as reserved or not</td>
045: * <td>Yes</td>
046: * <tr>
047: * <tr>
048: * <td>out</td>
049: * <td>Creates a writable file under a different filename</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>nodata</td>
054: * <td>Checks out the file but does not create an editable file containing its data</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>branch</td>
059: * <td>Specify a branch to check out the file to</td>
060: * <td>No</td>
061: * <tr>
062: * <tr>
063: * <td>version</td>
064: * <td>Allows checkout of a version other than main latest</td>
065: * <td>No</td>
066: * <tr>
067: * <tr>
068: * <td>nowarn</td>
069: * <td>Suppress warning messages</td>
070: * <td>No</td>
071: * <tr>
072: * <tr>
073: * <td>comment</td>
074: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
075: * <td>No</td>
076: * <tr>
077: * <tr>
078: * <td>commentfile</td>
079: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
080: * <td>No</td>
081: * <tr>
082: * <tr>
083: * <td>notco</td>
084: * <td>Fail if it's already checked out to the current view. Set to false to ignore it.</td>
085: * <td>No</td>
086: * <tr>
087: * <tr>
088: * <td>failonerr</td>
089: * <td>Throw an exception if the command fails. Default is true</td>
090: * <td>No</td>
091: * <tr>
092: * </table>
093: *
094: */
095: public class CCCheckout extends ClearCase {
096: private boolean mReserved = true;
097: private String mOut = null;
098: private boolean mNdata = false;
099: private String mBranch = null;
100: private boolean mVersion = false;
101: private boolean mNwarn = false;
102: private String mComment = null;
103: private String mCfile = null;
104: private boolean mNotco = true;
105:
106: /**
107: * Executes the task.
108: * <p>
109: * Builds a command line to execute cleartool and then calls Exec's run method
110: * to execute the command line.
111: * @throws BuildException if the command fails and failonerr is set to true
112: */
113: public void execute() throws BuildException {
114: Commandline commandLine = new Commandline();
115: Project aProj = getProject();
116: int result = 0;
117:
118: // Default the viewpath to basedir if it is not specified
119: if (getViewPath() == null) {
120: setViewPath(aProj.getBaseDir().getPath());
121: }
122:
123: // build the command line from what we got the format is
124: // cleartool checkout [options...] [viewpath ...]
125: // as specified in the CLEARTOOL.EXE help
126: commandLine.setExecutable(getClearToolCommand());
127: commandLine.createArgument().setValue(COMMAND_CHECKOUT);
128:
129: checkOptions(commandLine);
130: /*
131: * If configured to not care about whether the element is
132: * already checked out to the current view.
133: * Then check to see if it is checked out.
134: */
135: if (!getNotco() && lsCheckout()) {
136: getProject().log(
137: "Already checked out in this view: "
138: + getViewPathBasename(),
139: Project.MSG_VERBOSE);
140: return;
141: }
142: if (!getFailOnErr()) {
143: getProject().log(
144: "Ignoring any errors that occur for: "
145: + getViewPathBasename(),
146: Project.MSG_VERBOSE);
147: }
148: result = run(commandLine);
149: if (Execute.isFailure(result) && getFailOnErr()) {
150: String msg = "Failed executing: " + commandLine.toString();
151: throw new BuildException(msg, getLocation());
152: }
153: }
154:
155: /**
156: * Check to see if the element is checked out in the current view.
157: */
158: private boolean lsCheckout() {
159: Commandline cmdl = new Commandline();
160: String result;
161:
162: // build the command line from what we got the format is
163: // cleartool lsco [options...] [viewpath ...]
164: // as specified in the CLEARTOOL.EXE help
165: cmdl.setExecutable(getClearToolCommand());
166: cmdl.createArgument().setValue(COMMAND_LSCO);
167: cmdl.createArgument().setValue("-cview");
168: cmdl.createArgument().setValue("-short");
169: cmdl.createArgument().setValue("-d");
170: // viewpath
171: cmdl.createArgument().setValue(getViewPath());
172:
173: result = runS(cmdl);
174:
175: // System.out.println( "lsCheckout: " + result );
176:
177: return (result != null && result.length() > 0) ? true : false;
178: }
179:
180: /**
181: * Check the command line options.
182: */
183: private void checkOptions(Commandline cmd) {
184: // ClearCase items
185: if (getReserved()) {
186: // -reserved
187: cmd.createArgument().setValue(FLAG_RESERVED);
188: } else {
189: // -unreserved
190: cmd.createArgument().setValue(FLAG_UNRESERVED);
191: }
192:
193: if (getOut() != null) {
194: // -out
195: getOutCommand(cmd);
196: } else {
197: if (getNoData()) {
198: // -ndata
199: cmd.createArgument().setValue(FLAG_NODATA);
200: }
201:
202: }
203:
204: if (getBranch() != null) {
205: // -branch
206: getBranchCommand(cmd);
207: } else {
208: if (getVersion()) {
209: // -version
210: cmd.createArgument().setValue(FLAG_VERSION);
211: }
212:
213: }
214:
215: if (getNoWarn()) {
216: // -nwarn
217: cmd.createArgument().setValue(FLAG_NOWARN);
218: }
219:
220: if (getComment() != null) {
221: // -c
222: getCommentCommand(cmd);
223: } else {
224: if (getCommentFile() != null) {
225: // -cfile
226: getCommentFileCommand(cmd);
227: } else {
228: cmd.createArgument().setValue(FLAG_NOCOMMENT);
229: }
230: }
231:
232: // viewpath
233: cmd.createArgument().setValue(getViewPath());
234:
235: // Print out info about the notco option
236: // System.out.println( "Notco: " + (getNotco() ? "yes" : "no") );
237: }
238:
239: /**
240: * If true, checks out the file as reserved.
241: *
242: * @param reserved the status to set the flag to
243: */
244: public void setReserved(boolean reserved) {
245: mReserved = reserved;
246: }
247:
248: /**
249: * Get reserved flag status
250: *
251: * @return boolean containing status of reserved flag
252: */
253: public boolean getReserved() {
254: return mReserved;
255: }
256:
257: /**
258: * If true, checkout fails if the element is already checked out to the current view.
259: *
260: * @param notco the status to set the flag to
261: * @since ant 1.6.1
262: */
263: public void setNotco(boolean notco) {
264: mNotco = notco;
265: }
266:
267: /**
268: * Get notco flag status
269: *
270: * @return boolean containing status of notco flag
271: * @since ant 1.6.1
272: */
273: public boolean getNotco() {
274: return mNotco;
275: }
276:
277: /**
278: * Creates a writable file under a different filename.
279: *
280: * @param outf the path to the out file
281: */
282: public void setOut(String outf) {
283: mOut = outf;
284: }
285:
286: /**
287: * Get out file
288: *
289: * @return String containing the path to the out file
290: */
291: public String getOut() {
292: return mOut;
293: }
294:
295: /**
296: * If true, checks out the file but does not create an
297: * editable file containing its data.
298: *
299: * @param ndata the status to set the flag to
300: */
301: public void setNoData(boolean ndata) {
302: mNdata = ndata;
303: }
304:
305: /**
306: * Get nodata flag status
307: *
308: * @return boolean containing status of ndata flag
309: */
310: public boolean getNoData() {
311: return mNdata;
312: }
313:
314: /**
315: * Specify a branch to check out the file to.
316: *
317: * @param branch the name of the branch
318: */
319: public void setBranch(String branch) {
320: mBranch = branch;
321: }
322:
323: /**
324: * Get branch name
325: *
326: * @return String containing the name of the branch
327: */
328: public String getBranch() {
329: return mBranch;
330: }
331:
332: /**
333: * If true, allows checkout of a version other than main latest.
334: *
335: * @param version the status to set the flag to
336: */
337: public void setVersion(boolean version) {
338: mVersion = version;
339: }
340:
341: /**
342: * Get version flag status
343: *
344: * @return boolean containing status of version flag
345: */
346: public boolean getVersion() {
347: return mVersion;
348: }
349:
350: /**
351: * If true, warning messages are suppressed.
352: *
353: * @param nwarn the status to set the flag to
354: */
355: public void setNoWarn(boolean nwarn) {
356: mNwarn = nwarn;
357: }
358:
359: /**
360: * Get nowarn flag status
361: *
362: * @return boolean containing status of nwarn flag
363: */
364: public boolean getNoWarn() {
365: return mNwarn;
366: }
367:
368: /**
369: * Sets the comment string.
370: *
371: * @param comment the comment string
372: */
373: public void setComment(String comment) {
374: mComment = comment;
375: }
376:
377: /**
378: * Get comment string
379: *
380: * @return String containing the comment
381: */
382: public String getComment() {
383: return mComment;
384: }
385:
386: /**
387: * Specifies a file containing a comment.
388: *
389: * @param cfile the path to the comment file
390: */
391: public void setCommentFile(String cfile) {
392: mCfile = cfile;
393: }
394:
395: /**
396: * Get comment file
397: *
398: * @return String containing the path to the comment file
399: */
400: public String getCommentFile() {
401: return mCfile;
402: }
403:
404: /**
405: * Get the 'out' command
406: *
407: * @param cmd containing the command line string with or
408: * without the out flag and path appended
409: */
410: private void getOutCommand(Commandline cmd) {
411: if (getOut() != null) {
412: /* Had to make two separate commands here because if a space is
413: inserted between the flag and the value, it is treated as a
414: Windows filename with a space and it is enclosed in double
415: quotes ("). This breaks clearcase.
416: */
417: cmd.createArgument().setValue(FLAG_OUT);
418: cmd.createArgument().setValue(getOut());
419: }
420: }
421:
422: /**
423: * Get the 'branch' command
424: *
425: * @param cmd containing the command line string with or
426: without the branch flag and name appended
427: */
428: private void getBranchCommand(Commandline cmd) {
429: if (getBranch() != null) {
430: /* Had to make two separate commands here because if a space is
431: inserted between the flag and the value, it is treated as a
432: Windows filename with a space and it is enclosed in double
433: quotes ("). This breaks clearcase.
434: */
435: cmd.createArgument().setValue(FLAG_BRANCH);
436: cmd.createArgument().setValue(getBranch());
437: }
438: }
439:
440: /**
441: * Get the 'comment' command
442: *
443: * @param cmd containing the command line string with or
444: * without the comment flag and string appended
445: */
446: private void getCommentCommand(Commandline cmd) {
447: if (getComment() != null) {
448: /* Had to make two separate commands here because if a space is
449: inserted between the flag and the value, it is treated as a
450: Windows filename with a space and it is enclosed in double
451: quotes ("). This breaks clearcase.
452: */
453: cmd.createArgument().setValue(FLAG_COMMENT);
454: cmd.createArgument().setValue(getComment());
455: }
456: }
457:
458: /**
459: * Get the 'cfile' command
460: *
461: * @param cmd containing the command line string with or
462: * without the cfile flag and file appended
463: */
464: private void getCommentFileCommand(Commandline cmd) {
465: if (getCommentFile() != null) {
466: /* Had to make two separate commands here because if a space is
467: inserted between the flag and the value, it is treated as a
468: Windows filename with a space and it is enclosed in double
469: quotes ("). This breaks clearcase.
470: */
471: cmd.createArgument().setValue(FLAG_COMMENTFILE);
472: cmd.createArgument().setValue(getCommentFile());
473: }
474: }
475:
476: /**
477: * -reserved flag -- check out the file as reserved
478: */
479: public static final String FLAG_RESERVED = "-reserved";
480: /**
481: * -reserved flag -- check out the file as unreserved
482: */
483: public static final String FLAG_UNRESERVED = "-unreserved";
484: /**
485: * -out flag -- create a writable file under a different filename
486: */
487: public static final String FLAG_OUT = "-out";
488: /**
489: * -ndata flag -- checks out the file but does not create an editable file containing its data
490: */
491: public static final String FLAG_NODATA = "-ndata";
492: /**
493: * -branch flag -- checks out the file on a specified branch
494: */
495: public static final String FLAG_BRANCH = "-branch";
496: /**
497: * -version flag -- allows checkout of a version that is not main latest
498: */
499: public static final String FLAG_VERSION = "-version";
500: /**
501: * -nwarn flag -- suppresses warning messages
502: */
503: public static final String FLAG_NOWARN = "-nwarn";
504: /**
505: * -c flag -- comment to attach to the file
506: */
507: public static final String FLAG_COMMENT = "-c";
508: /**
509: * -cfile flag -- file containing a comment to attach to the file
510: */
511: public static final String FLAG_COMMENTFILE = "-cfile";
512: /**
513: * -nc flag -- no comment is specified
514: */
515: public static final String FLAG_NOCOMMENT = "-nc";
516:
517: }
|