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 Lock 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>replace</td>
  39. * <td>Specifies replacing an existing lock</td>
  40. * <td>No</td>
  41. * <tr>
  42. * <tr>
  43. * <td>nusers</td>
  44. * <td>Specifies user(s) who can still modify the object/pname</td>
  45. * <td>No</td>
  46. * <tr>
  47. * <tr>
  48. * <td>obsolete</td>
  49. * <td>Specifies that the object/pname should be marked obsolete</td>
  50. * <td>No</td>
  51. * <tr>
  52. * <tr>
  53. * <td>comment</td>
  54. * <td>Specifies how to populate comments fields</td>
  55. * <td>No</td>
  56. * <tr>
  57. * <tr>
  58. * <td>pname</td>
  59. * <td>Specifies the pathname to be locked.</td>
  60. * <td>No</td>
  61. * <tr>
  62. * <td>objselect</td>
  63. * <td>This variable is obsolete. Should use <i>objsel</i> instead.</td>
  64. * <td>No</td>
  65. * <tr>
  66. * <tr>
  67. * <td>objsel</td>
  68. * <td>Specifies the object(s) to be unlocked.</td>
  69. * <td>No</td>
  70. * <tr>
  71. * <tr>
  72. * <td>failonerr</td>
  73. * <td>Throw an exception if the command fails. Default is true</td>
  74. * <td>No</td>
  75. * <tr>
  76. * </table>
  77. *
  78. */
  79. public class CCLock extends ClearCase {
  80. private boolean mReplace = false;
  81. private boolean mObsolete = false;
  82. private String mComment = null;
  83. private String mNusers = null;
  84. private String mPname = null;
  85. private String mObjselect = null;
  86. /**
  87. * Executes the task.
  88. * <p>
  89. * Builds a command line to execute cleartool and then calls Exec's run method
  90. * to execute the command line.
  91. * @throws BuildException if the command fails and failonerr is set to true
  92. */
  93. public void execute() throws BuildException {
  94. Commandline commandLine = new Commandline();
  95. Project aProj = getProject();
  96. int result = 0;
  97. // Default the viewpath to basedir if it is not specified
  98. if (getViewPath() == null) {
  99. setViewPath(aProj.getBaseDir().getPath());
  100. }
  101. // build the command line from what we got the format is
  102. // cleartool lock [options...]
  103. // as specified in the CLEARTOOL.EXE help
  104. commandLine.setExecutable(getClearToolCommand());
  105. commandLine.createArgument().setValue(COMMAND_LOCK);
  106. // Check the command line options
  107. checkOptions(commandLine);
  108. // For debugging
  109. // System.out.println(commandLine.toString());
  110. if (!getFailOnErr()) {
  111. getProject().log("Ignoring any errors that occur for: "
  112. + getOpType(), Project.MSG_VERBOSE);
  113. }
  114. result = run(commandLine);
  115. if (Execute.isFailure(result) && getFailOnErr()) {
  116. String msg = "Failed executing: " + commandLine.toString();
  117. throw new BuildException(msg, getLocation());
  118. }
  119. }
  120. /**
  121. * Check the command line options.
  122. */
  123. private void checkOptions(Commandline cmd) {
  124. // ClearCase items
  125. if (getReplace()) {
  126. // -replace
  127. cmd.createArgument().setValue(FLAG_REPLACE);
  128. }
  129. if (getObsolete()) {
  130. // -obsolete
  131. cmd.createArgument().setValue(FLAG_OBSOLETE);
  132. } else {
  133. getNusersCommand(cmd);
  134. }
  135. getCommentCommand(cmd);
  136. if (getObjselect() == null && getPname() == null) {
  137. throw new BuildException("Should select either an element "
  138. + "(pname) or an object (objselect)");
  139. }
  140. getPnameCommand(cmd);
  141. // object selector
  142. if (getObjselect() != null) {
  143. cmd.createArgument().setValue(getObjselect());
  144. }
  145. }
  146. /**
  147. * If true, replace an existing lock.
  148. *
  149. * @param replace the status to set the flag to
  150. */
  151. public void setReplace(boolean replace) {
  152. mReplace = replace;
  153. }
  154. /**
  155. * Get replace flag status
  156. *
  157. * @return boolean containing status of replace flag
  158. */
  159. public boolean getReplace() {
  160. return mReplace;
  161. }
  162. /**
  163. * If true, mark object as obsolete.
  164. *
  165. * @param obsolete the status to set the flag to
  166. */
  167. public void setObsolete(boolean obsolete) {
  168. mObsolete = obsolete;
  169. }
  170. /**
  171. * Get obsolete flag status
  172. *
  173. * @return boolean containing status of obsolete flag
  174. */
  175. public boolean getObsolete() {
  176. return mObsolete;
  177. }
  178. /**
  179. * Sets the users who may continue to
  180. * edit the object while it is locked.
  181. *
  182. * @param nusers users excluded from lock
  183. */
  184. public void setNusers(String nusers) {
  185. mNusers = nusers;
  186. }
  187. /**
  188. * Get nusers list
  189. *
  190. * @return String containing the list of users excluded from lock
  191. */
  192. public String getNusers() {
  193. return mNusers;
  194. }
  195. /**
  196. * Sets how comments should be written
  197. * for the event record(s)
  198. *
  199. * @param comment comment method to use
  200. */
  201. public void setComment(String comment) {
  202. mComment = comment;
  203. }
  204. /**
  205. * Get comment method
  206. *
  207. * @return String containing the desired comment method
  208. */
  209. public String getComment() {
  210. return mComment;
  211. }
  212. /**
  213. * Sets the pathname to be locked
  214. *
  215. * @param pname pathname to be locked
  216. */
  217. public void setPname(String pname) {
  218. mPname = pname;
  219. }
  220. /**
  221. * Get the pathname to be locked
  222. *
  223. * @return String containing the pathname to be locked
  224. */
  225. public String getPname() {
  226. return mPname;
  227. }
  228. /**
  229. * Sets the object(s) to be locked
  230. *
  231. * @param objsel objects to be locked
  232. * @since ant 1.6.1
  233. */
  234. public void setObjSel(String objsel) {
  235. mObjselect = objsel;
  236. }
  237. /**
  238. * Sets the object(s) to be locked
  239. *
  240. * @param objselect objects to be locked
  241. */
  242. public void setObjselect(String objselect) {
  243. mObjselect = objselect;
  244. }
  245. /**
  246. * Get list of objects to be locked
  247. *
  248. * @return String containing the objects to be locked
  249. */
  250. public String getObjselect() {
  251. return mObjselect;
  252. }
  253. /**
  254. * Get the 'nusers' command
  255. *
  256. * @param cmd containing the command line string with or
  257. * without the nusers flag and value appended
  258. */
  259. private void getNusersCommand(Commandline cmd) {
  260. if (getNusers() == 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_NUSERS);
  269. cmd.createArgument().setValue(getNusers());
  270. }
  271. }
  272. /**
  273. * Get the 'comment' command
  274. *
  275. * @param cmd containing the command line string with or without the
  276. * comment flag and value appended
  277. */
  278. private void getCommentCommand(Commandline cmd) {
  279. if (getComment() == null) {
  280. return;
  281. } else {
  282. /* Had to make two separate commands here because if a space is
  283. inserted between the flag and the value, it is treated as a
  284. Windows filename with a space and it is enclosed in double
  285. quotes ("). This breaks clearcase.
  286. */
  287. cmd.createArgument().setValue(FLAG_COMMENT);
  288. cmd.createArgument().setValue(getComment());
  289. }
  290. }
  291. /**
  292. * Get the 'pname' command
  293. *
  294. * @param cmd containing the command line string with or
  295. * without the pname flag and value appended
  296. */
  297. private void getPnameCommand(Commandline cmd) {
  298. if (getPname() == null) {
  299. return;
  300. } else {
  301. /* Had to make two separate commands here because if a space is
  302. inserted between the flag and the value, it is treated as a
  303. Windows filename with a space and it is enclosed in double
  304. quotes ("). This breaks clearcase.
  305. */
  306. cmd.createArgument().setValue(FLAG_PNAME);
  307. cmd.createArgument().setValue(getPname());
  308. }
  309. }
  310. /**
  311. * Return which object/pname is being operated on
  312. *
  313. * @return String containing the object/pname being worked on
  314. */
  315. private String getOpType() {
  316. if (getPname() != null) {
  317. return getPname();
  318. } else {
  319. return getObjselect();
  320. }
  321. }
  322. /**
  323. * -replace flag -- replace existing lock on object(s)
  324. */
  325. public static final String FLAG_REPLACE = "-replace";
  326. /**
  327. * -nusers flag -- list of users to exclude from lock
  328. */
  329. public static final String FLAG_NUSERS = "-nusers";
  330. /**
  331. * -obsolete flag -- mark locked object as obsolete
  332. */
  333. public static final String FLAG_OBSOLETE = "-obsolete";
  334. /**
  335. * -comment flag -- method to use for commenting events
  336. */
  337. public static final String FLAG_COMMENT = "-comment";
  338. /**
  339. * -pname flag -- pathname to lock
  340. */
  341. public static final String FLAG_PNAME = "-pname";
  342. }