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 a ClearCase Update command.
  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>graphical</td>
  40. * <td>Displays a graphical dialog during the update</td>
  41. * <td>No</td>
  42. * <tr>
  43. * <tr>
  44. * <td>log</td>
  45. * <td>Specifies a log file for ClearCase to write to</td>
  46. * <td>No</td>
  47. * <tr>
  48. * <tr>
  49. * <td>overwrite</td>
  50. * <td>Specifies whether to overwrite hijacked files or not</td>
  51. * <td>No</td>
  52. * <tr>
  53. * <tr>
  54. * <td>rename</td>
  55. * <td>Specifies that hijacked files should be renamed with a .keep extension</td>
  56. * <td>No</td>
  57. * <tr>
  58. * <tr>
  59. * <td>currenttime</td>
  60. * <td>Specifies that modification time should be written as the current
  61. * time. Either currenttime or preservetime can be specified.</td>
  62. * <td>No</td>
  63. * <tr>
  64. * <tr>
  65. * <td>preservetime</td>
  66. * <td>Specifies that modification time should preserved from the VOB
  67. * time. Either currenttime or preservetime can be specified.</td>
  68. * <td>No</td>
  69. * <tr>
  70. * <tr>
  71. * <td>failonerr</td>
  72. * <td>Throw an exception if the command fails. Default is true</td>
  73. * <td>No</td>
  74. * <tr>
  75. * </table>
  76. *
  77. */
  78. public class CCUpdate extends ClearCase {
  79. private boolean mGraphical = false;
  80. private boolean mOverwrite = false;
  81. private boolean mRename = false;
  82. private boolean mCtime = false;
  83. private boolean mPtime = false;
  84. private String mLog = null;
  85. /**
  86. * Executes the task.
  87. * <p>
  88. * Builds a command line to execute cleartool and then calls Exec's run method
  89. * to execute the command line.
  90. * @throws BuildException if the command fails and failonerr is set to true
  91. */
  92. public void execute() throws BuildException {
  93. Commandline commandLine = new Commandline();
  94. Project aProj = getProject();
  95. int result = 0;
  96. // Default the viewpath to basedir if it is not specified
  97. if (getViewPath() == null) {
  98. setViewPath(aProj.getBaseDir().getPath());
  99. }
  100. // build the command line from what we got the format is
  101. // cleartool update [options...] [viewpath ...]
  102. // as specified in the CLEARTOOL.EXE help
  103. commandLine.setExecutable(getClearToolCommand());
  104. commandLine.createArgument().setValue(COMMAND_UPDATE);
  105. // Check the command line options
  106. checkOptions(commandLine);
  107. // For debugging
  108. getProject().log(commandLine.toString(), Project.MSG_DEBUG);
  109. if (!getFailOnErr()) {
  110. getProject().log("Ignoring any errors that occur for: "
  111. + getViewPathBasename(), Project.MSG_VERBOSE);
  112. }
  113. result = run(commandLine);
  114. if (Execute.isFailure(result) && getFailOnErr()) {
  115. String msg = "Failed executing: " + commandLine.toString();
  116. throw new BuildException(msg, getLocation());
  117. }
  118. }
  119. /**
  120. * Check the command line options.
  121. */
  122. private void checkOptions(Commandline cmd) {
  123. // ClearCase items
  124. if (getGraphical()) {
  125. // -graphical
  126. cmd.createArgument().setValue(FLAG_GRAPHICAL);
  127. } else {
  128. if (getOverwrite()) {
  129. // -overwrite
  130. cmd.createArgument().setValue(FLAG_OVERWRITE);
  131. } else {
  132. if (getRename()) {
  133. // -rename
  134. cmd.createArgument().setValue(FLAG_RENAME);
  135. } else {
  136. // -noverwrite
  137. cmd.createArgument().setValue(FLAG_NOVERWRITE);
  138. }
  139. }
  140. if (getCurrentTime()) {
  141. // -ctime
  142. cmd.createArgument().setValue(FLAG_CURRENTTIME);
  143. } else {
  144. if (getPreserveTime()) {
  145. // -ptime
  146. cmd.createArgument().setValue(FLAG_PRESERVETIME);
  147. }
  148. }
  149. // -log logname
  150. getLogCommand(cmd);
  151. }
  152. // viewpath
  153. cmd.createArgument().setValue(getViewPath());
  154. }
  155. /**
  156. * If true, displays a graphical dialog during the update.
  157. *
  158. * @param graphical the status to set the flag to
  159. */
  160. public void setGraphical(boolean graphical) {
  161. mGraphical = graphical;
  162. }
  163. /**
  164. * Get graphical flag status
  165. *
  166. * @return boolean containing status of graphical flag
  167. */
  168. public boolean getGraphical() {
  169. return mGraphical;
  170. }
  171. /**
  172. * If true, overwrite hijacked files.
  173. *
  174. * @param ow the status to set the flag to
  175. */
  176. public void setOverwrite(boolean ow) {
  177. mOverwrite = ow;
  178. }
  179. /**
  180. * Get overwrite hijacked files status
  181. *
  182. * @return boolean containing status of overwrite flag
  183. */
  184. public boolean getOverwrite() {
  185. return mOverwrite;
  186. }
  187. /**
  188. * If true, hijacked files are renamed with a .keep extension.
  189. *
  190. * @param ren the status to set the flag to
  191. */
  192. public void setRename(boolean ren) {
  193. mRename = ren;
  194. }
  195. /**
  196. * Get rename hijacked files status
  197. *
  198. * @return boolean containing status of rename flag
  199. */
  200. public boolean getRename() {
  201. return mRename;
  202. }
  203. /**
  204. * If true, modification time should be written as the current time.
  205. * Either currenttime or preservetime can be specified.
  206. *
  207. * @param ct the status to set the flag to
  208. */
  209. public void setCurrentTime(boolean ct) {
  210. mCtime = ct;
  211. }
  212. /**
  213. * Get current time status
  214. *
  215. * @return boolean containing status of current time flag
  216. */
  217. public boolean getCurrentTime() {
  218. return mCtime;
  219. }
  220. /**
  221. * If true, modification time should be preserved from the VOB time.
  222. * Either currenttime or preservetime can be specified.
  223. *
  224. * @param pt the status to set the flag to
  225. */
  226. public void setPreserveTime(boolean pt) {
  227. mPtime = pt;
  228. }
  229. /**
  230. * Get preserve time status
  231. *
  232. * @return boolean containing status of preserve time flag
  233. */
  234. public boolean getPreserveTime() {
  235. return mPtime;
  236. }
  237. /**
  238. * Sets the log file where cleartool records
  239. * the status of the command.
  240. *
  241. * @param log the path to the log file
  242. */
  243. public void setLog(String log) {
  244. mLog = log;
  245. }
  246. /**
  247. * Get log file
  248. *
  249. * @return String containing the path to the log file
  250. */
  251. public String getLog() {
  252. return mLog;
  253. }
  254. /**
  255. * Get the 'log' command
  256. *
  257. * @param cmd containing the command line string with or without the log flag and path appended
  258. */
  259. private void getLogCommand(Commandline cmd) {
  260. if (getLog() == null) {
  261. return;
  262. } else {
  263. /* Had to make two separate commands here because if a space is
  264. inserted between the flag and the value, it is treated as a
  265. Windows filename with a space and it is enclosed in double
  266. quotes ("). This breaks clearcase.
  267. */
  268. cmd.createArgument().setValue(FLAG_LOG);
  269. cmd.createArgument().setValue(getLog());
  270. }
  271. }
  272. /**
  273. * -graphical flag -- display graphical dialog during update operation
  274. */
  275. public static final String FLAG_GRAPHICAL = "-graphical";
  276. /**
  277. * -log flag -- file to log status to
  278. */
  279. public static final String FLAG_LOG = "-log";
  280. /**
  281. * -overwrite flag -- overwrite hijacked files
  282. */
  283. public static final String FLAG_OVERWRITE = "-overwrite";
  284. /**
  285. * -noverwrite flag -- do not overwrite hijacked files
  286. */
  287. public static final String FLAG_NOVERWRITE = "-noverwrite";
  288. /**
  289. * -rename flag -- rename hijacked files with .keep extension
  290. */
  291. public static final String FLAG_RENAME = "-rename";
  292. /**
  293. * -ctime flag -- modified time is written as the current time
  294. */
  295. public static final String FLAG_CURRENTTIME = "-ctime";
  296. /**
  297. * -ptime flag -- modified time is written as the VOB time
  298. */
  299. public static final String FLAG_PRESERVETIME = "-ptime";
  300. }