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. * Task to perform rmtype command to ClearCase.
  24. * <p>
  25. * The following attributes are interpreted:
  26. * <table border="1">
  27. * <tr>
  28. * <th>Attribute</th>
  29. * <th>Values</th>
  30. * <th>Required</th>
  31. * </tr>
  32. * <tr>
  33. * <td>typekind</td>
  34. * <td>The kind of type to create. Valid types are:<br>
  35. * attype attribute type<br>
  36. * brtype branch type<br>
  37. * eltype element type<br>
  38. * hltype hyperlink type<br>
  39. * lbtype label type<br>
  40. * trtype trigger type<br>
  41. * </td>
  42. * <td>Yes</td>
  43. * <tr>
  44. * <tr>
  45. * <td>typename</td>
  46. * <td>The name of the type to remove</td>
  47. * <td>Yes</td>
  48. * <tr>
  49. * <tr>
  50. * <td>vob</td>
  51. * <td>Name of the VOB</td>
  52. * <td>No</td>
  53. * <tr>
  54. * <tr>
  55. * <td>ignore</td>
  56. * <td>Used with trigger types only. Forces removal of trigger type
  57. * even if a pre-operation trigger would prevent its removal</td>
  58. * <td>No</td>
  59. * <tr>
  60. * <tr>
  61. * <td>rmall</td>
  62. * <td>Removes all instances of a type and the type object itself</td>
  63. * <td>No</td>
  64. * <tr>
  65. * <tr>
  66. * <td>comment</td>
  67. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  68. * <td>No</td>
  69. * <tr>
  70. * <tr>
  71. * <td>commentfile</td>
  72. * <td>Specify a file containing a comment. Only one of comment or cfile
  73. * may be used.</td>
  74. * <td>No</td>
  75. * <tr>
  76. * <tr>
  77. * <td>failonerr</td>
  78. * <td>Throw an exception if the command fails. Default is true</td>
  79. * <td>No</td>
  80. * <tr>
  81. * </table>
  82. *
  83. */
  84. public class CCRmtype extends ClearCase {
  85. private String mTypeKind = null;
  86. private String mTypeName = null;
  87. private String mVOB = null;
  88. private String mComment = null;
  89. private String mCfile = null;
  90. private boolean mRmall = false;
  91. private boolean mIgnore = false;
  92. /**
  93. * Executes the task.
  94. * <p>
  95. * Builds a command line to execute cleartool and then calls Exec's run method
  96. * to execute the command line.
  97. * @throws BuildException if the command fails and failonerr is set to true
  98. */
  99. public void execute() throws BuildException {
  100. Commandline commandLine = new Commandline();
  101. Project aProj = getProject();
  102. int result = 0;
  103. // Check for required attributes
  104. if (getTypeKind() == null) {
  105. throw new BuildException("Required attribute TypeKind not specified");
  106. }
  107. if (getTypeName() == null) {
  108. throw new BuildException("Required attribute TypeName not specified");
  109. }
  110. // build the command line from what we got. the format is
  111. // cleartool rmtype [options...] type-selector...
  112. // as specified in the CLEARTOOL help
  113. commandLine.setExecutable(getClearToolCommand());
  114. commandLine.createArgument().setValue(COMMAND_RMTYPE);
  115. checkOptions(commandLine);
  116. if (!getFailOnErr()) {
  117. getProject().log("Ignoring any errors that occur for: "
  118. + getTypeSpecifier(), Project.MSG_VERBOSE);
  119. }
  120. result = run(commandLine);
  121. if (Execute.isFailure(result) && getFailOnErr()) {
  122. String msg = "Failed executing: " + commandLine.toString();
  123. throw new BuildException(msg, getLocation());
  124. }
  125. }
  126. /**
  127. * Check the command line options.
  128. */
  129. private void checkOptions(Commandline cmd) {
  130. if (getIgnore()) {
  131. // -ignore
  132. cmd.createArgument().setValue(FLAG_IGNORE);
  133. }
  134. if (getRmAll()) {
  135. // -rmall -force
  136. cmd.createArgument().setValue(FLAG_RMALL);
  137. cmd.createArgument().setValue(FLAG_FORCE);
  138. }
  139. if (getComment() != null) {
  140. // -c
  141. getCommentCommand(cmd);
  142. } else {
  143. if (getCommentFile() != null) {
  144. // -cfile
  145. getCommentFileCommand(cmd);
  146. } else {
  147. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  148. }
  149. }
  150. // type-kind:type-name
  151. cmd.createArgument().setValue(getTypeSpecifier());
  152. }
  153. /**
  154. * Set the ignore flag
  155. *
  156. * @param ignore the status to set the flag to
  157. */
  158. public void setIgnore(boolean ignore) {
  159. mIgnore = ignore;
  160. }
  161. /**
  162. * Get ignore flag status
  163. *
  164. * @return boolean containing status of ignore flag
  165. */
  166. public boolean getIgnore() {
  167. return mIgnore;
  168. }
  169. /**
  170. * Set rmall flag
  171. *
  172. * @param rmall the status to set the flag to
  173. */
  174. public void setRmAll(boolean rmall) {
  175. mRmall = rmall;
  176. }
  177. /**
  178. * Get rmall flag status
  179. *
  180. * @return boolean containing status of rmall flag
  181. */
  182. public boolean getRmAll() {
  183. return mRmall;
  184. }
  185. /**
  186. * Set comment string
  187. *
  188. * @param comment the comment string
  189. */
  190. public void setComment(String comment) {
  191. mComment = comment;
  192. }
  193. /**
  194. * Get comment string
  195. *
  196. * @return String containing the comment
  197. */
  198. public String getComment() {
  199. return mComment;
  200. }
  201. /**
  202. * Set comment file
  203. *
  204. * @param cfile the path to the comment file
  205. */
  206. public void setCommentFile(String cfile) {
  207. mCfile = cfile;
  208. }
  209. /**
  210. * Get comment file
  211. *
  212. * @return String containing the path to the comment file
  213. */
  214. public String getCommentFile() {
  215. return mCfile;
  216. }
  217. /**
  218. * Set type-kind string
  219. *
  220. * @param tk the type-kind string
  221. */
  222. public void setTypeKind(String tk) {
  223. mTypeKind = tk;
  224. }
  225. /**
  226. * Get type-kind string
  227. *
  228. * @return String containing the type-kind
  229. */
  230. public String getTypeKind() {
  231. return mTypeKind;
  232. }
  233. /**
  234. * Set type-name string
  235. *
  236. * @param tn the type-name string
  237. */
  238. public void setTypeName(String tn) {
  239. mTypeName = tn;
  240. }
  241. /**
  242. * Get type-name string
  243. *
  244. * @return String containing the type-name
  245. */
  246. public String getTypeName() {
  247. return mTypeName;
  248. }
  249. /**
  250. * Set the VOB name
  251. *
  252. * @param vob the VOB name
  253. */
  254. public void setVOB(String vob) {
  255. mVOB = vob;
  256. }
  257. /**
  258. * Get VOB name
  259. *
  260. * @return String containing VOB name
  261. */
  262. public String getVOB() {
  263. return mVOB;
  264. }
  265. /**
  266. * Get the 'type-specifier' string
  267. *
  268. * @return the 'type-kind:type-name@vob' specifier
  269. *
  270. */
  271. private String getTypeSpecifier() {
  272. String tkind = getTypeKind();
  273. String tname = getTypeName();
  274. String typeSpec = null;
  275. // Return the type-selector
  276. typeSpec = tkind + ":" + tname;
  277. if (getVOB() != null) {
  278. typeSpec += "@" + getVOB();
  279. }
  280. return typeSpec;
  281. }
  282. /**
  283. * Get the 'comment' command
  284. *
  285. * @param cmd containing the command line string with or
  286. * without the comment flag and string appended
  287. */
  288. private void getCommentCommand(Commandline cmd) {
  289. if (getComment() != null) {
  290. /* Had to make two separate commands here because if a space is
  291. inserted between the flag and the value, it is treated as a
  292. Windows filename with a space and it is enclosed in double
  293. quotes ("). This breaks clearcase.
  294. */
  295. cmd.createArgument().setValue(FLAG_COMMENT);
  296. cmd.createArgument().setValue(getComment());
  297. }
  298. }
  299. /**
  300. * Get the 'commentfile' command
  301. *
  302. * @param cmd containing the command line string with or
  303. * without the commentfile flag and file appended
  304. */
  305. private void getCommentFileCommand(Commandline cmd) {
  306. if (getCommentFile() != null) {
  307. /* Had to make two separate commands here because if a space is
  308. inserted between the flag and the value, it is treated as a
  309. Windows filename with a space and it is enclosed in double
  310. quotes ("). This breaks clearcase.
  311. */
  312. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  313. cmd.createArgument().setValue(getCommentFile());
  314. }
  315. }
  316. /**
  317. * -ignore flag -- ignore pre-trigger operations when removing a trigger type
  318. */
  319. public static final String FLAG_IGNORE = "-ignore";
  320. /**
  321. * -rmall flag -- removes all instances of a type and the type object itself
  322. */
  323. public static final String FLAG_RMALL = "-rmall";
  324. /**
  325. * -force flag -- suppresses confirmation prompts
  326. */
  327. public static final String FLAG_FORCE = "-force";
  328. /**
  329. * -c flag -- comment to attach to the file
  330. */
  331. public static final String FLAG_COMMENT = "-c";
  332. /**
  333. * -cfile flag -- file containing a comment to attach to the file
  334. */
  335. public static final String FLAG_COMMENTFILE = "-cfile";
  336. /**
  337. * -nc flag -- no comment is specified
  338. */
  339. public static final String FLAG_NOCOMMENT = "-nc";
  340. }