1. /*
  2. * Copyright 2000-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 checkout.
  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>reserved</td>
  40. * <td>Specifies whether to check out the file as reserved or not</td>
  41. * <td>Yes</td>
  42. * <tr>
  43. * <tr>
  44. * <td>out</td>
  45. * <td>Creates a writable file under a different filename</td>
  46. * <td>No</td>
  47. * <tr>
  48. * <tr>
  49. * <td>nodata</td>
  50. * <td>Checks out the file but does not create an editable file containing its data</td>
  51. * <td>No</td>
  52. * <tr>
  53. * <tr>
  54. * <td>branch</td>
  55. * <td>Specify a branch to check out the file to</td>
  56. * <td>No</td>
  57. * <tr>
  58. * <tr>
  59. * <td>version</td>
  60. * <td>Allows checkout of a version other than main latest</td>
  61. * <td>No</td>
  62. * <tr>
  63. * <tr>
  64. * <td>nowarn</td>
  65. * <td>Suppress warning messages</td>
  66. * <td>No</td>
  67. * <tr>
  68. * <tr>
  69. * <td>comment</td>
  70. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  71. * <td>No</td>
  72. * <tr>
  73. * <tr>
  74. * <td>commentfile</td>
  75. * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
  76. * <td>No</td>
  77. * <tr>
  78. * <tr>
  79. * <td>notco</td>
  80. * <td>Fail if it's already checked out to the current view. Set to false to ignore it.</td>
  81. * <td>No</td>
  82. * <tr>
  83. * <tr>
  84. * <td>failonerr</td>
  85. * <td>Throw an exception if the command fails. Default is true</td>
  86. * <td>No</td>
  87. * <tr>
  88. * </table>
  89. *
  90. */
  91. public class CCCheckout extends ClearCase {
  92. private boolean mReserved = true;
  93. private String mOut = null;
  94. private boolean mNdata = false;
  95. private String mBranch = null;
  96. private boolean mVersion = false;
  97. private boolean mNwarn = false;
  98. private String mComment = null;
  99. private String mCfile = null;
  100. private boolean mNotco = true;
  101. /**
  102. * Executes the task.
  103. * <p>
  104. * Builds a command line to execute cleartool and then calls Exec's run method
  105. * to execute the command line.
  106. * @throws BuildException if the command fails and failonerr is set to true
  107. */
  108. public void execute() throws BuildException {
  109. Commandline commandLine = new Commandline();
  110. Project aProj = getProject();
  111. int result = 0;
  112. // Default the viewpath to basedir if it is not specified
  113. if (getViewPath() == null) {
  114. setViewPath(aProj.getBaseDir().getPath());
  115. }
  116. // build the command line from what we got the format is
  117. // cleartool checkout [options...] [viewpath ...]
  118. // as specified in the CLEARTOOL.EXE help
  119. commandLine.setExecutable(getClearToolCommand());
  120. commandLine.createArgument().setValue(COMMAND_CHECKOUT);
  121. checkOptions(commandLine);
  122. /*
  123. * If configured to not care about whether the element is
  124. * already checked out to the current view.
  125. * Then check to see if it is checked out.
  126. */
  127. if (!getNotco() && lsCheckout()) {
  128. getProject().log("Already checked out in this view: "
  129. + getViewPathBasename(), Project.MSG_VERBOSE);
  130. return;
  131. }
  132. if (!getFailOnErr()) {
  133. getProject().log("Ignoring any errors that occur for: "
  134. + getViewPathBasename(), Project.MSG_VERBOSE);
  135. }
  136. result = run(commandLine);
  137. if (Execute.isFailure(result) && getFailOnErr()) {
  138. String msg = "Failed executing: " + commandLine.toString();
  139. throw new BuildException(msg, getLocation());
  140. }
  141. }
  142. /**
  143. * Check to see if the element is checked out in the current view.
  144. */
  145. private boolean lsCheckout() {
  146. Commandline cmdl = new Commandline();
  147. String result;
  148. // build the command line from what we got the format is
  149. // cleartool lsco [options...] [viewpath ...]
  150. // as specified in the CLEARTOOL.EXE help
  151. cmdl.setExecutable(getClearToolCommand());
  152. cmdl.createArgument().setValue(COMMAND_LSCO);
  153. cmdl.createArgument().setValue("-cview");
  154. cmdl.createArgument().setValue("-short");
  155. cmdl.createArgument().setValue("-d");
  156. // viewpath
  157. cmdl.createArgument().setValue(getViewPath());
  158. result = runS(cmdl);
  159. // System.out.println( "lsCheckout: " + result );
  160. return (result != null && result.length() > 0) ? true : false;
  161. }
  162. /**
  163. * Check the command line options.
  164. */
  165. private void checkOptions(Commandline cmd) {
  166. // ClearCase items
  167. if (getReserved()) {
  168. // -reserved
  169. cmd.createArgument().setValue(FLAG_RESERVED);
  170. } else {
  171. // -unreserved
  172. cmd.createArgument().setValue(FLAG_UNRESERVED);
  173. }
  174. if (getOut() != null) {
  175. // -out
  176. getOutCommand(cmd);
  177. } else {
  178. if (getNoData()) {
  179. // -ndata
  180. cmd.createArgument().setValue(FLAG_NODATA);
  181. }
  182. }
  183. if (getBranch() != null) {
  184. // -branch
  185. getBranchCommand(cmd);
  186. } else {
  187. if (getVersion()) {
  188. // -version
  189. cmd.createArgument().setValue(FLAG_VERSION);
  190. }
  191. }
  192. if (getNoWarn()) {
  193. // -nwarn
  194. cmd.createArgument().setValue(FLAG_NOWARN);
  195. }
  196. if (getComment() != null) {
  197. // -c
  198. getCommentCommand(cmd);
  199. } else {
  200. if (getCommentFile() != null) {
  201. // -cfile
  202. getCommentFileCommand(cmd);
  203. } else {
  204. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  205. }
  206. }
  207. // viewpath
  208. cmd.createArgument().setValue(getViewPath());
  209. // Print out info about the notco option
  210. // System.out.println( "Notco: " + (getNotco() ? "yes" : "no") );
  211. }
  212. /**
  213. * If true, checks out the file as reserved.
  214. *
  215. * @param reserved the status to set the flag to
  216. */
  217. public void setReserved(boolean reserved) {
  218. mReserved = reserved;
  219. }
  220. /**
  221. * Get reserved flag status
  222. *
  223. * @return boolean containing status of reserved flag
  224. */
  225. public boolean getReserved() {
  226. return mReserved;
  227. }
  228. /**
  229. * If true, checkout fails if the element is already checked out to the current view.
  230. *
  231. * @param notco the status to set the flag to
  232. * @since ant 1.6.1
  233. */
  234. public void setNotco(boolean notco) {
  235. mNotco = notco;
  236. }
  237. /**
  238. * Get notco flag status
  239. *
  240. * @return boolean containing status of notco flag
  241. * @since ant 1.6.1
  242. */
  243. public boolean getNotco() {
  244. return mNotco;
  245. }
  246. /**
  247. * Creates a writable file under a different filename.
  248. *
  249. * @param outf the path to the out file
  250. */
  251. public void setOut(String outf) {
  252. mOut = outf;
  253. }
  254. /**
  255. * Get out file
  256. *
  257. * @return String containing the path to the out file
  258. */
  259. public String getOut() {
  260. return mOut;
  261. }
  262. /**
  263. * If true, checks out the file but does not create an
  264. * editable file containing its data.
  265. *
  266. * @param ndata the status to set the flag to
  267. */
  268. public void setNoData(boolean ndata) {
  269. mNdata = ndata;
  270. }
  271. /**
  272. * Get nodata flag status
  273. *
  274. * @return boolean containing status of ndata flag
  275. */
  276. public boolean getNoData() {
  277. return mNdata;
  278. }
  279. /**
  280. * Specify a branch to check out the file to.
  281. *
  282. * @param branch the name of the branch
  283. */
  284. public void setBranch(String branch) {
  285. mBranch = branch;
  286. }
  287. /**
  288. * Get branch name
  289. *
  290. * @return String containing the name of the branch
  291. */
  292. public String getBranch() {
  293. return mBranch;
  294. }
  295. /**
  296. * If true, allows checkout of a version other than main latest.
  297. *
  298. * @param version the status to set the flag to
  299. */
  300. public void setVersion(boolean version) {
  301. mVersion = version;
  302. }
  303. /**
  304. * Get version flag status
  305. *
  306. * @return boolean containing status of version flag
  307. */
  308. public boolean getVersion() {
  309. return mVersion;
  310. }
  311. /**
  312. * If true, warning messages are suppressed.
  313. *
  314. * @param nwarn the status to set the flag to
  315. */
  316. public void setNoWarn(boolean nwarn) {
  317. mNwarn = nwarn;
  318. }
  319. /**
  320. * Get nowarn flag status
  321. *
  322. * @return boolean containing status of nwarn flag
  323. */
  324. public boolean getNoWarn() {
  325. return mNwarn;
  326. }
  327. /**
  328. * Sets the comment string.
  329. *
  330. * @param comment the comment string
  331. */
  332. public void setComment(String comment) {
  333. mComment = comment;
  334. }
  335. /**
  336. * Get comment string
  337. *
  338. * @return String containing the comment
  339. */
  340. public String getComment() {
  341. return mComment;
  342. }
  343. /**
  344. * Specifies a file containing a comment.
  345. *
  346. * @param cfile the path to the comment file
  347. */
  348. public void setCommentFile(String cfile) {
  349. mCfile = cfile;
  350. }
  351. /**
  352. * Get comment file
  353. *
  354. * @return String containing the path to the comment file
  355. */
  356. public String getCommentFile() {
  357. return mCfile;
  358. }
  359. /**
  360. * Get the 'out' command
  361. *
  362. * @param cmd containing the command line string with or
  363. * without the out flag and path appended
  364. */
  365. private void getOutCommand(Commandline cmd) {
  366. if (getOut() != null) {
  367. /* Had to make two separate commands here because if a space is
  368. inserted between the flag and the value, it is treated as a
  369. Windows filename with a space and it is enclosed in double
  370. quotes ("). This breaks clearcase.
  371. */
  372. cmd.createArgument().setValue(FLAG_OUT);
  373. cmd.createArgument().setValue(getOut());
  374. }
  375. }
  376. /**
  377. * Get the 'branch' command
  378. *
  379. * @param cmd containing the command line string with or
  380. without the branch flag and name appended
  381. */
  382. private void getBranchCommand(Commandline cmd) {
  383. if (getBranch() != null) {
  384. /* Had to make two separate commands here because if a space is
  385. inserted between the flag and the value, it is treated as a
  386. Windows filename with a space and it is enclosed in double
  387. quotes ("). This breaks clearcase.
  388. */
  389. cmd.createArgument().setValue(FLAG_BRANCH);
  390. cmd.createArgument().setValue(getBranch());
  391. }
  392. }
  393. /**
  394. * Get the 'comment' command
  395. *
  396. * @param cmd containing the command line string with or
  397. * without the comment flag and string appended
  398. */
  399. private void getCommentCommand(Commandline cmd) {
  400. if (getComment() != null) {
  401. /* Had to make two separate commands here because if a space is
  402. inserted between the flag and the value, it is treated as a
  403. Windows filename with a space and it is enclosed in double
  404. quotes ("). This breaks clearcase.
  405. */
  406. cmd.createArgument().setValue(FLAG_COMMENT);
  407. cmd.createArgument().setValue(getComment());
  408. }
  409. }
  410. /**
  411. * Get the 'cfile' command
  412. *
  413. * @param cmd containing the command line string with or
  414. * without the cfile flag and file appended
  415. */
  416. private void getCommentFileCommand(Commandline cmd) {
  417. if (getCommentFile() != null) {
  418. /* Had to make two separate commands here because if a space is
  419. inserted between the flag and the value, it is treated as a
  420. Windows filename with a space and it is enclosed in double
  421. quotes ("). This breaks clearcase.
  422. */
  423. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  424. cmd.createArgument().setValue(getCommentFile());
  425. }
  426. }
  427. /**
  428. * -reserved flag -- check out the file as reserved
  429. */
  430. public static final String FLAG_RESERVED = "-reserved";
  431. /**
  432. * -reserved flag -- check out the file as unreserved
  433. */
  434. public static final String FLAG_UNRESERVED = "-unreserved";
  435. /**
  436. * -out flag -- create a writable file under a different filename
  437. */
  438. public static final String FLAG_OUT = "-out";
  439. /**
  440. * -ndata flag -- checks out the file but does not create an editable file containing its data
  441. */
  442. public static final String FLAG_NODATA = "-ndata";
  443. /**
  444. * -branch flag -- checks out the file on a specified branch
  445. */
  446. public static final String FLAG_BRANCH = "-branch";
  447. /**
  448. * -version flag -- allows checkout of a version that is not main latest
  449. */
  450. public static final String FLAG_VERSION = "-version";
  451. /**
  452. * -nwarn flag -- suppresses warning messages
  453. */
  454. public static final String FLAG_NOWARN = "-nwarn";
  455. /**
  456. * -c flag -- comment to attach to the file
  457. */
  458. public static final String FLAG_COMMENT = "-c";
  459. /**
  460. * -cfile flag -- file containing a comment to attach to the file
  461. */
  462. public static final String FLAG_COMMENTFILE = "-cfile";
  463. /**
  464. * -nc flag -- no comment is specified
  465. */
  466. public static final String FLAG_NOCOMMENT = "-nc";
  467. }