1. /*
  2. * Copyright 2003-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. * TODO:
  24. * comment field doesn't include all options yet
  25. */
  26. /**
  27. * Performs a ClearCase Unlock command.
  28. *
  29. * <p>
  30. * The following attributes are interpreted:
  31. * <table border="1">
  32. * <tr>
  33. * <th>Attribute</th>
  34. * <th>Values</th>
  35. * <th>Required</th>
  36. * </tr>
  37. * <tr>
  38. * <td>comment</td>
  39. * <td>Specifies how to populate comments fields</td>
  40. * <td>No</td>
  41. * <tr>
  42. * <tr>
  43. * <td>pname</td>
  44. * <td>Specifies the object pathname to be unlocked.</td>
  45. * <td>No</td>
  46. * <tr>
  47. * <td>objselect</td>
  48. * <td>This variable is obsolete. Should use <i>objsel</i> instead.</td>
  49. * <td>no</td>
  50. * <tr>
  51. * <tr>
  52. * <td>objsel</td>
  53. * <td>Specifies the object(s) to be unlocked.</td>
  54. * <td>No</td>
  55. * <tr>
  56. * <tr>
  57. * <td>failonerr</td>
  58. * <td>Throw an exception if the command fails. Default is true</td>
  59. * <td>No</td>
  60. * <tr>
  61. *
  62. * </table>
  63. *
  64. */
  65. public class CCUnlock extends ClearCase {
  66. private String mComment = null;
  67. private String mPname = null;
  68. private String mObjselect = null;
  69. /**
  70. * Executes the task.
  71. * <p>
  72. * Builds a command line to execute cleartool and then calls Exec's run method
  73. * to execute the command line.
  74. * @throws BuildException if the command fails and failonerr is set to true
  75. */
  76. public void execute() throws BuildException {
  77. Commandline commandLine = new Commandline();
  78. Project aProj = getProject();
  79. int result = 0;
  80. // Default the viewpath to basedir if it is not specified
  81. if (getViewPath() == null) {
  82. setViewPath(aProj.getBaseDir().getPath());
  83. }
  84. // build the command line from what we got the format is
  85. // cleartool lock [options...]
  86. // as specified in the CLEARTOOL.EXE help
  87. commandLine.setExecutable(getClearToolCommand());
  88. commandLine.createArgument().setValue(COMMAND_UNLOCK);
  89. // Check the command line options
  90. checkOptions(commandLine);
  91. // For debugging
  92. // System.out.println(commandLine.toString());
  93. if (!getFailOnErr()) {
  94. getProject().log("Ignoring any errors that occur for: "
  95. + getOpType(), Project.MSG_VERBOSE);
  96. }
  97. result = run(commandLine);
  98. if (Execute.isFailure(result) && getFailOnErr()) {
  99. String msg = "Failed executing: " + commandLine.toString();
  100. throw new BuildException(msg, getLocation());
  101. }
  102. }
  103. /**
  104. * Check the command line options.
  105. */
  106. private void checkOptions(Commandline cmd) {
  107. // ClearCase items
  108. getCommentCommand(cmd);
  109. if (getObjselect() == null && getPname() == null) {
  110. throw new BuildException("Should select either an element "
  111. + "(pname) or an object (objselect)");
  112. }
  113. getPnameCommand(cmd);
  114. // object selector
  115. if (getObjselect() != null) {
  116. cmd.createArgument().setValue(getObjselect());
  117. }
  118. }
  119. /**
  120. * Sets how comments should be written
  121. * for the event record(s)
  122. *
  123. * @param comment comment method to use
  124. */
  125. public void setComment(String comment) {
  126. mComment = comment;
  127. }
  128. /**
  129. * Get comment method
  130. *
  131. * @return String containing the desired comment method
  132. */
  133. public String getComment() {
  134. return mComment;
  135. }
  136. /**
  137. * Sets the pathname to be locked
  138. *
  139. * @param pname pathname to be locked
  140. */
  141. public void setPname(String pname) {
  142. mPname = pname;
  143. }
  144. /**
  145. * Get the pathname to be locked
  146. *
  147. * @return String containing the pathname to be locked
  148. */
  149. public String getPname() {
  150. return mPname;
  151. }
  152. /**
  153. * Sets the object(s) to be locked
  154. *
  155. * @param objselect objects to be locked
  156. */
  157. public void setObjselect(String objselect) {
  158. mObjselect = objselect;
  159. }
  160. /**
  161. * Sets the object(s) to be locked
  162. *
  163. * @param objsel objects to be locked
  164. * @since ant 1.6.1
  165. */
  166. public void setObjSel(String objsel) {
  167. mObjselect = objsel;
  168. }
  169. /**
  170. * Get list of objects to be locked
  171. *
  172. * @return String containing the objects to be locked
  173. */
  174. public String getObjselect() {
  175. return mObjselect;
  176. }
  177. /**
  178. * Get the 'comment' command
  179. *
  180. * @param cmd containing the command line string with or without the
  181. * comment flag and value appended
  182. */
  183. private void getCommentCommand(Commandline cmd) {
  184. if (getComment() == null) {
  185. return;
  186. } else {
  187. /* Had to make two separate commands here because if a space is
  188. inserted between the flag and the value, it is treated as a
  189. Windows filename with a space and it is enclosed in double
  190. quotes ("). This breaks clearcase.
  191. */
  192. cmd.createArgument().setValue(FLAG_COMMENT);
  193. cmd.createArgument().setValue(getComment());
  194. }
  195. }
  196. /**
  197. * Get the 'pname' command
  198. *
  199. * @param cmd containing the command line string with or without the
  200. * pname flag and value appended
  201. */
  202. private void getPnameCommand(Commandline cmd) {
  203. if (getPname() == null) {
  204. return;
  205. } else {
  206. /* Had to make two separate commands here because if a space is
  207. inserted between the flag and the value, it is treated as a
  208. Windows filename with a space and it is enclosed in double
  209. quotes ("). This breaks clearcase.
  210. */
  211. cmd.createArgument().setValue(FLAG_PNAME);
  212. cmd.createArgument().setValue(getPname());
  213. }
  214. }
  215. /**
  216. * Return which object/pname is being operated on
  217. *
  218. * @return String containing the object/pname being worked on
  219. */
  220. private String getOpType() {
  221. if (getPname() != null) {
  222. return getPname();
  223. } else {
  224. return getObjselect();
  225. }
  226. }
  227. /**
  228. * -comment flag -- method to use for commenting events
  229. */
  230. public static final String FLAG_COMMENT = "-comment";
  231. /**
  232. * -pname flag -- pathname to lock
  233. */
  234. public static final String FLAG_PNAME = "-pname";
  235. }