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 UnCheckout 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>keepcopy</td>
  40. * <td>Specifies whether to keep a copy of the file with a .keep extension or not</td>
  41. * <td>No</td>
  42. * <tr>
  43. * <tr>
  44. * <td>failonerr</td>
  45. * <td>Throw an exception if the command fails. Default is true</td>
  46. * <td>No</td>
  47. * <tr>
  48. * </table>
  49. *
  50. */
  51. public class CCUnCheckout extends ClearCase {
  52. private boolean mKeep = false;
  53. /**
  54. * Executes the task.
  55. * <p>
  56. * Builds a command line to execute cleartool and then calls Exec's run method
  57. * to execute the command line.
  58. * @throws BuildException if the command fails and failonerr is set to true
  59. */
  60. public void execute() throws BuildException {
  61. Commandline commandLine = new Commandline();
  62. Project aProj = getProject();
  63. int result = 0;
  64. // Default the viewpath to basedir if it is not specified
  65. if (getViewPath() == null) {
  66. setViewPath(aProj.getBaseDir().getPath());
  67. }
  68. // build the command line from what we got the format is
  69. // cleartool uncheckout [options...] [viewpath ...]
  70. // as specified in the CLEARTOOL.EXE help
  71. commandLine.setExecutable(getClearToolCommand());
  72. commandLine.createArgument().setValue(COMMAND_UNCHECKOUT);
  73. checkOptions(commandLine);
  74. if (!getFailOnErr()) {
  75. getProject().log("Ignoring any errors that occur for: "
  76. + getViewPathBasename(), Project.MSG_VERBOSE);
  77. }
  78. result = run(commandLine);
  79. if (Execute.isFailure(result) && getFailOnErr()) {
  80. String msg = "Failed executing: " + commandLine.toString();
  81. throw new BuildException(msg, getLocation());
  82. }
  83. }
  84. /**
  85. * Check the command line options.
  86. */
  87. private void checkOptions(Commandline cmd) {
  88. // ClearCase items
  89. if (getKeepCopy()) {
  90. // -keep
  91. cmd.createArgument().setValue(FLAG_KEEPCOPY);
  92. } else {
  93. // -rm
  94. cmd.createArgument().setValue(FLAG_RM);
  95. }
  96. // viewpath
  97. cmd.createArgument().setValue(getViewPath());
  98. }
  99. /**
  100. * If true, keep a copy of the file with a .keep extension.
  101. *
  102. * @param keep the status to set the flag to
  103. */
  104. public void setKeepCopy(boolean keep) {
  105. mKeep = keep;
  106. }
  107. /**
  108. * Get keepcopy flag status
  109. *
  110. * @return boolean containing status of keep flag
  111. */
  112. public boolean getKeepCopy() {
  113. return mKeep;
  114. }
  115. /**
  116. * -keep flag -- keep a copy of the file with .keep extension
  117. */
  118. public static final String FLAG_KEEPCOPY = "-keep";
  119. /**
  120. * -rm flag -- remove the copy of the file
  121. */
  122. public static final String FLAG_RM = "-rm";
  123. }