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 checkin.
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>comment</td>
044: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>commentfile</td>
049: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>nowarn</td>
054: * <td>Suppress warning messages</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>preservetime</td>
059: * <td>Preserve the modification time</td>
060: * <td>No</td>
061: * <tr>
062: * <tr>
063: * <td>keepcopy</td>
064: * <td>Keeps a copy of the file with a .keep extension</td>
065: * <td>No</td>
066: * <tr>
067: * <tr>
068: * <td>identical</td>
069: * <td>Allows the file to be checked in even if it is identical to the original</td>
070: * <td>No</td>
071: * <tr>
072: * <tr>
073: * <td>failonerr</td>
074: * <td>Throw an exception if the command fails. Default is true</td>
075: * <td>No</td>
076: * <tr>
077: * </table>
078: *
079: */
080: public class CCCheckin extends ClearCase {
081: private String mComment = null;
082: private String mCfile = null;
083: private boolean mNwarn = false;
084: private boolean mPtime = false;
085: private boolean mKeep = false;
086: private boolean mIdentical = true;
087:
088: /**
089: * Executes the task.
090: * <p>
091: * Builds a command line to execute cleartool and then calls Exec's run method
092: * to execute the command line.
093: * @throws BuildException if the command fails and failonerr is set to true
094: */
095: public void execute() throws BuildException {
096: Commandline commandLine = new Commandline();
097: Project aProj = getProject();
098: int result = 0;
099:
100: // Default the viewpath to basedir if it is not specified
101: if (getViewPath() == null) {
102: setViewPath(aProj.getBaseDir().getPath());
103: }
104:
105: // build the command line from what we got. the format is
106: // cleartool checkin [options...] [viewpath ...]
107: // as specified in the CLEARTOOL.EXE help
108: commandLine.setExecutable(getClearToolCommand());
109: commandLine.createArgument().setValue(COMMAND_CHECKIN);
110:
111: checkOptions(commandLine);
112:
113: if (!getFailOnErr()) {
114: getProject().log(
115: "Ignoring any errors that occur for: "
116: + getViewPathBasename(),
117: Project.MSG_VERBOSE);
118: }
119: result = run(commandLine);
120: if (Execute.isFailure(result) && getFailOnErr()) {
121: String msg = "Failed executing: " + commandLine.toString();
122: throw new BuildException(msg, getLocation());
123: }
124: }
125:
126: /**
127: * Check the command line options.
128: */
129: private void checkOptions(Commandline cmd) {
130: if (getComment() != null) {
131: // -c
132: getCommentCommand(cmd);
133: } else {
134: if (getCommentFile() != null) {
135: // -cfile
136: getCommentFileCommand(cmd);
137: } else {
138: cmd.createArgument().setValue(FLAG_NOCOMMENT);
139: }
140: }
141:
142: if (getNoWarn()) {
143: // -nwarn
144: cmd.createArgument().setValue(FLAG_NOWARN);
145: }
146:
147: if (getPreserveTime()) {
148: // -ptime
149: cmd.createArgument().setValue(FLAG_PRESERVETIME);
150: }
151:
152: if (getKeepCopy()) {
153: // -keep
154: cmd.createArgument().setValue(FLAG_KEEPCOPY);
155: }
156:
157: if (getIdentical()) {
158: // -identical
159: cmd.createArgument().setValue(FLAG_IDENTICAL);
160: }
161:
162: // viewpath
163: cmd.createArgument().setValue(getViewPath());
164: }
165:
166: /**
167: * Sets the comment string.
168: *
169: * @param comment the comment string
170: */
171: public void setComment(String comment) {
172: mComment = comment;
173: }
174:
175: /**
176: * Get comment string
177: *
178: * @return String containing the comment
179: */
180: public String getComment() {
181: return mComment;
182: }
183:
184: /**
185: * Specifies a file containing a comment.
186: *
187: * @param cfile the path to the comment file
188: */
189: public void setCommentFile(String cfile) {
190: mCfile = cfile;
191: }
192:
193: /**
194: * Get comment file
195: *
196: * @return String containing the path to the comment file
197: */
198: public String getCommentFile() {
199: return mCfile;
200: }
201:
202: /**
203: * If true, suppress warning messages.
204: *
205: * @param nwarn the status to set the flag to
206: */
207: public void setNoWarn(boolean nwarn) {
208: mNwarn = nwarn;
209: }
210:
211: /**
212: * Get nowarn flag status
213: *
214: * @return boolean containing status of nwarn flag
215: */
216: public boolean getNoWarn() {
217: return mNwarn;
218: }
219:
220: /**
221: * If true, preserve the modification time.
222: *
223: * @param ptime the status to set the flag to
224: */
225: public void setPreserveTime(boolean ptime) {
226: mPtime = ptime;
227: }
228:
229: /**
230: * Get preservetime flag status
231: *
232: * @return boolean containing status of preservetime flag
233: */
234: public boolean getPreserveTime() {
235: return mPtime;
236: }
237:
238: /**
239: * If true, keeps a copy of the file with a .keep extension.
240: *
241: * @param keep the status to set the flag to
242: */
243: public void setKeepCopy(boolean keep) {
244: mKeep = keep;
245: }
246:
247: /**
248: * Get keepcopy flag status
249: *
250: * @return boolean containing status of keepcopy flag
251: */
252: public boolean getKeepCopy() {
253: return mKeep;
254: }
255:
256: /**
257: * If true, allows the file to be checked in even
258: * if it is identical to the original.
259: *
260: * @param identical the status to set the flag to
261: */
262: public void setIdentical(boolean identical) {
263: mIdentical = identical;
264: }
265:
266: /**
267: * Get identical flag status
268: *
269: * @return boolean containing status of identical flag
270: */
271: public boolean getIdentical() {
272: return mIdentical;
273: }
274:
275: /**
276: * Get the 'comment' command
277: *
278: * @param cmd containing the command line string with or
279: * without the comment flag and string appended
280: */
281: private void getCommentCommand(Commandline cmd) {
282: if (getComment() != null) {
283: /* Had to make two separate commands here because if a space is
284: inserted between the flag and the value, it is treated as a
285: Windows filename with a space and it is enclosed in double
286: quotes ("). This breaks clearcase.
287: */
288: cmd.createArgument().setValue(FLAG_COMMENT);
289: cmd.createArgument().setValue(getComment());
290: }
291: }
292:
293: /**
294: * Get the 'commentfile' command
295: *
296: * @param cmd containing the command line string with or
297: * without the commentfile flag and file appended
298: */
299: private void getCommentFileCommand(Commandline cmd) {
300: if (getCommentFile() != null) {
301: /* Had to make two separate commands here because if a space is
302: inserted between the flag and the value, it is treated as a
303: Windows filename with a space and it is enclosed in double
304: quotes ("). This breaks clearcase.
305: */
306: cmd.createArgument().setValue(FLAG_COMMENTFILE);
307: cmd.createArgument().setValue(getCommentFile());
308: }
309: }
310:
311: /**
312: * -c flag -- comment to attach to the file
313: */
314: public static final String FLAG_COMMENT = "-c";
315: /**
316: * -cfile flag -- file containing a comment to attach to the file
317: */
318: public static final String FLAG_COMMENTFILE = "-cfile";
319: /**
320: * -nc flag -- no comment is specified
321: */
322: public static final String FLAG_NOCOMMENT = "-nc";
323: /**
324: * -nwarn flag -- suppresses warning messages
325: */
326: public static final String FLAG_NOWARN = "-nwarn";
327: /**
328: * -ptime flag -- preserves the modification time
329: */
330: public static final String FLAG_PRESERVETIME = "-ptime";
331: /**
332: * -keep flag -- keeps a copy of the file with a .keep extension
333: */
334: public static final String FLAG_KEEPCOPY = "-keep";
335: /**
336: * -identical flag -- allows the file to be checked in even if it is identical to the original
337: */
338: public static final String FLAG_IDENTICAL = "-identical";
339:
340: }
|