1. /*
  2. * Copyright 2000,2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.clearcase;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.taskdefs.Execute;
  21. import org.apache.tools.ant.types.Commandline;
  22. /**
  23. * Performs ClearCase checkin.
  24. *
  25. * <p>
  26. * The following attributes are interpreted:
  27. * <table border="1">
  28. * <tr>
  29. * <th>Attribute</th>
  30. * <th>Values</th>
  31. * <th>Required</th>
  32. * </tr>
  33. * <tr>
  34. * <td>viewpath</td>
  35. * <td>Path to the ClearCase view file or directory that the command will operate on</td>
  36. * <td>No</td>
  37. * <tr>
  38. * <tr>
  39. * <td>comment</td>
  40. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  41. * <td>No</td>
  42. * <tr>
  43. * <tr>
  44. * <td>commentfile</td>
  45. * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
  46. * <td>No</td>
  47. * <tr>
  48. * <tr>
  49. * <td>nowarn</td>
  50. * <td>Suppress warning messages</td>
  51. * <td>No</td>
  52. * <tr>
  53. * <tr>
  54. * <td>preservetime</td>
  55. * <td>Preserve the modification time</td>
  56. * <td>No</td>
  57. * <tr>
  58. * <tr>
  59. * <td>keepcopy</td>
  60. * <td>Keeps a copy of the file with a .keep extension</td>
  61. * <td>No</td>
  62. * <tr>
  63. * <tr>
  64. * <td>identical</td>
  65. * <td>Allows the file to be checked in even if it is identical to the original</td>
  66. * <td>No</td>
  67. * <tr>
  68. * <tr>
  69. * <td>failonerr</td>
  70. * <td>Throw an exception if the command fails. Default is true</td>
  71. * <td>No</td>
  72. * <tr>
  73. * </table>
  74. *
  75. */
  76. public class CCCheckin extends ClearCase {
  77. private String mComment = null;
  78. private String mCfile = null;
  79. private boolean mNwarn = false;
  80. private boolean mPtime = false;
  81. private boolean mKeep = false;
  82. private boolean mIdentical = true;
  83. /**
  84. * Executes the task.
  85. * <p>
  86. * Builds a command line to execute cleartool and then calls Exec's run method
  87. * to execute the command line.
  88. * @throws BuildException if the command fails and failonerr is set to true
  89. */
  90. public void execute() throws BuildException {
  91. Commandline commandLine = new Commandline();
  92. Project aProj = getProject();
  93. int result = 0;
  94. // Default the viewpath to basedir if it is not specified
  95. if (getViewPath() == null) {
  96. setViewPath(aProj.getBaseDir().getPath());
  97. }
  98. // build the command line from what we got. the format is
  99. // cleartool checkin [options...] [viewpath ...]
  100. // as specified in the CLEARTOOL.EXE help
  101. commandLine.setExecutable(getClearToolCommand());
  102. commandLine.createArgument().setValue(COMMAND_CHECKIN);
  103. checkOptions(commandLine);
  104. if (!getFailOnErr()) {
  105. getProject().log("Ignoring any errors that occur for: "
  106. + getViewPathBasename(), Project.MSG_VERBOSE);
  107. }
  108. result = run(commandLine);
  109. if (Execute.isFailure(result) && getFailOnErr()) {
  110. String msg = "Failed executing: " + commandLine.toString();
  111. throw new BuildException(msg, getLocation());
  112. }
  113. }
  114. /**
  115. * Check the command line options.
  116. */
  117. private void checkOptions(Commandline cmd) {
  118. if (getComment() != null) {
  119. // -c
  120. getCommentCommand(cmd);
  121. } else {
  122. if (getCommentFile() != null) {
  123. // -cfile
  124. getCommentFileCommand(cmd);
  125. } else {
  126. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  127. }
  128. }
  129. if (getNoWarn()) {
  130. // -nwarn
  131. cmd.createArgument().setValue(FLAG_NOWARN);
  132. }
  133. if (getPreserveTime()) {
  134. // -ptime
  135. cmd.createArgument().setValue(FLAG_PRESERVETIME);
  136. }
  137. if (getKeepCopy()) {
  138. // -keep
  139. cmd.createArgument().setValue(FLAG_KEEPCOPY);
  140. }
  141. if (getIdentical()) {
  142. // -identical
  143. cmd.createArgument().setValue(FLAG_IDENTICAL);
  144. }
  145. // viewpath
  146. cmd.createArgument().setValue(getViewPath());
  147. }
  148. /**
  149. * Sets the comment string.
  150. *
  151. * @param comment the comment string
  152. */
  153. public void setComment(String comment) {
  154. mComment = comment;
  155. }
  156. /**
  157. * Get comment string
  158. *
  159. * @return String containing the comment
  160. */
  161. public String getComment() {
  162. return mComment;
  163. }
  164. /**
  165. * Specifies a file containing a comment.
  166. *
  167. * @param cfile the path to the comment file
  168. */
  169. public void setCommentFile(String cfile) {
  170. mCfile = cfile;
  171. }
  172. /**
  173. * Get comment file
  174. *
  175. * @return String containing the path to the comment file
  176. */
  177. public String getCommentFile() {
  178. return mCfile;
  179. }
  180. /**
  181. * If true, suppress warning messages.
  182. *
  183. * @param nwarn the status to set the flag to
  184. */
  185. public void setNoWarn(boolean nwarn) {
  186. mNwarn = nwarn;
  187. }
  188. /**
  189. * Get nowarn flag status
  190. *
  191. * @return boolean containing status of nwarn flag
  192. */
  193. public boolean getNoWarn() {
  194. return mNwarn;
  195. }
  196. /**
  197. * If true, preserve the modification time.
  198. *
  199. * @param ptime the status to set the flag to
  200. */
  201. public void setPreserveTime(boolean ptime) {
  202. mPtime = ptime;
  203. }
  204. /**
  205. * Get preservetime flag status
  206. *
  207. * @return boolean containing status of preservetime flag
  208. */
  209. public boolean getPreserveTime() {
  210. return mPtime;
  211. }
  212. /**
  213. * If true, keeps a copy of the file with a .keep extension.
  214. *
  215. * @param keep the status to set the flag to
  216. */
  217. public void setKeepCopy(boolean keep) {
  218. mKeep = keep;
  219. }
  220. /**
  221. * Get keepcopy flag status
  222. *
  223. * @return boolean containing status of keepcopy flag
  224. */
  225. public boolean getKeepCopy() {
  226. return mKeep;
  227. }
  228. /**
  229. * If true, allows the file to be checked in even
  230. * if it is identical to the original.
  231. *
  232. * @param identical the status to set the flag to
  233. */
  234. public void setIdentical(boolean identical) {
  235. mIdentical = identical;
  236. }
  237. /**
  238. * Get identical flag status
  239. *
  240. * @return boolean containing status of identical flag
  241. */
  242. public boolean getIdentical() {
  243. return mIdentical;
  244. }
  245. /**
  246. * Get the 'comment' command
  247. *
  248. * @param cmd containing the command line string with or
  249. * without the comment flag and string appended
  250. */
  251. private void getCommentCommand(Commandline cmd) {
  252. if (getComment() != null) {
  253. /* Had to make two separate commands here because if a space is
  254. inserted between the flag and the value, it is treated as a
  255. Windows filename with a space and it is enclosed in double
  256. quotes ("). This breaks clearcase.
  257. */
  258. cmd.createArgument().setValue(FLAG_COMMENT);
  259. cmd.createArgument().setValue(getComment());
  260. }
  261. }
  262. /**
  263. * Get the 'commentfile' command
  264. *
  265. * @param cmd containing the command line string with or
  266. * without the commentfile flag and file appended
  267. */
  268. private void getCommentFileCommand(Commandline cmd) {
  269. if (getCommentFile() != null) {
  270. /* Had to make two separate commands here because if a space is
  271. inserted between the flag and the value, it is treated as a
  272. Windows filename with a space and it is enclosed in double
  273. quotes ("). This breaks clearcase.
  274. */
  275. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  276. cmd.createArgument().setValue(getCommentFile());
  277. }
  278. }
  279. /**
  280. * -c flag -- comment to attach to the file
  281. */
  282. public static final String FLAG_COMMENT = "-c";
  283. /**
  284. * -cfile flag -- file containing a comment to attach to the file
  285. */
  286. public static final String FLAG_COMMENTFILE = "-cfile";
  287. /**
  288. * -nc flag -- no comment is specified
  289. */
  290. public static final String FLAG_NOCOMMENT = "-nc";
  291. /**
  292. * -nwarn flag -- suppresses warning messages
  293. */
  294. public static final String FLAG_NOWARN = "-nwarn";
  295. /**
  296. * -ptime flag -- preserves the modification time
  297. */
  298. public static final String FLAG_PRESERVETIME = "-ptime";
  299. /**
  300. * -keep flag -- keeps a copy of the file with a .keep extension
  301. */
  302. public static final String FLAG_KEEPCOPY = "-keep";
  303. /**
  304. * -identical flag -- allows the file to be checked in even if it is identical to the original
  305. */
  306. public static final String FLAG_IDENTICAL = "-identical";
  307. }