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 mklbtype 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>typename</td>
  34. * <td>Name of the label type to create</td>
  35. * <td>Yes</td>
  36. * <tr>
  37. * <tr>
  38. * <td>vob</td>
  39. * <td>Name of the VOB</td>
  40. * <td>No</td>
  41. * <tr>
  42. * <tr>
  43. * <td>replace</td>
  44. * <td>Replace an existing label definition of the same type</td>
  45. * <td>No</td>
  46. * <tr>
  47. * <tr>
  48. * <td>global</td>
  49. * <td>Either global or ordinary can be specified, not both.
  50. * Creates a label type that is global to the VOB or to
  51. * VOBs that use this VOB</td>
  52. * <td>No</td>
  53. * <tr>
  54. * <tr>
  55. * <td>ordinary</td>
  56. * <td>Either global or ordinary can be specified, not both.
  57. * Creates a label type that can be used only in the current
  58. * VOB. <B>Default</B></td>
  59. * <td>No</td>
  60. * <tr>
  61. * <tr>
  62. * <td>pbranch</td>
  63. * <td>Allows the label type to be used once per branch in a given
  64. * element's version tree</td>
  65. * <td>No</td>
  66. * <tr>
  67. * <tr>
  68. * <td>shared</td>
  69. * <td>Sets the way mastership is checked by ClearCase. See ClearCase
  70. * documentation for details</td>
  71. * <td>No</td>
  72. * <tr>
  73. * <tr>
  74. * <td>comment</td>
  75. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  76. * <td>No</td>
  77. * <tr>
  78. * <tr>
  79. * <td>commentfile</td>
  80. * <td>Specify a file containing a comment. Only one of comment or
  81. * cfile may be used.</td>
  82. * <td>No</td>
  83. * <tr>
  84. * <tr>
  85. * <td>failonerr</td>
  86. * <td>Throw an exception if the command fails. Default is true</td>
  87. * <td>No</td>
  88. * <tr>
  89. * </table>
  90. *
  91. */
  92. public class CCMklbtype extends ClearCase {
  93. private String mTypeName = null;
  94. private String mVOB = null;
  95. private String mComment = null;
  96. private String mCfile = null;
  97. private boolean mReplace = false;
  98. private boolean mGlobal = false;
  99. private boolean mOrdinary = true;
  100. private boolean mPbranch = false;
  101. private boolean mShared = false;
  102. /**
  103. * Executes the task.
  104. * <p>
  105. * Builds a command line to execute cleartool and then calls Exec's run method
  106. * to execute the command line.
  107. * @throws BuildException if the command fails and failonerr is set to true
  108. */
  109. public void execute() throws BuildException {
  110. Commandline commandLine = new Commandline();
  111. Project aProj = getProject();
  112. int result = 0;
  113. // Check for required attributes
  114. if (getTypeName() == null) {
  115. throw new BuildException("Required attribute TypeName not specified");
  116. }
  117. // build the command line from what we got. the format is
  118. // cleartool mklbtype [options...] type-selector...
  119. // as specified in the CLEARTOOL help
  120. commandLine.setExecutable(getClearToolCommand());
  121. commandLine.createArgument().setValue(COMMAND_MKLBTYPE);
  122. checkOptions(commandLine);
  123. if (!getFailOnErr()) {
  124. getProject().log("Ignoring any errors that occur for: "
  125. + getTypeSpecifier(), Project.MSG_VERBOSE);
  126. }
  127. result = run(commandLine);
  128. if (Execute.isFailure(result) && getFailOnErr()) {
  129. String msg = "Failed executing: " + commandLine.toString();
  130. throw new BuildException(msg, getLocation());
  131. }
  132. }
  133. /**
  134. * Check the command line options.
  135. */
  136. private void checkOptions(Commandline cmd) {
  137. if (getReplace()) {
  138. // -replace
  139. cmd.createArgument().setValue(FLAG_REPLACE);
  140. }
  141. if (getOrdinary()) {
  142. // -ordinary
  143. cmd.createArgument().setValue(FLAG_ORDINARY);
  144. } else {
  145. if (getGlobal()) {
  146. // -global
  147. cmd.createArgument().setValue(FLAG_GLOBAL);
  148. }
  149. }
  150. if (getPbranch()) {
  151. // -pbranch
  152. cmd.createArgument().setValue(FLAG_PBRANCH);
  153. }
  154. if (getShared()) {
  155. // -shared
  156. cmd.createArgument().setValue(FLAG_SHARED);
  157. }
  158. if (getComment() != null) {
  159. // -c
  160. getCommentCommand(cmd);
  161. } else {
  162. if (getCommentFile() != null) {
  163. // -cfile
  164. getCommentFileCommand(cmd);
  165. } else {
  166. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  167. }
  168. }
  169. // type-name@vob
  170. cmd.createArgument().setValue(getTypeSpecifier());
  171. }
  172. /**
  173. * Set type-name string
  174. *
  175. * @param tn the type-name string
  176. */
  177. public void setTypeName(String tn) {
  178. mTypeName = tn;
  179. }
  180. /**
  181. * Get type-name string
  182. *
  183. * @return String containing the type-name
  184. */
  185. public String getTypeName() {
  186. return mTypeName;
  187. }
  188. /**
  189. * Set the VOB name
  190. *
  191. * @param vob the VOB name
  192. */
  193. public void setVOB(String vob) {
  194. mVOB = vob;
  195. }
  196. /**
  197. * Get VOB name
  198. *
  199. * @return String containing VOB name
  200. */
  201. public String getVOB() {
  202. return mVOB;
  203. }
  204. /**
  205. * Set the replace flag
  206. *
  207. * @param repl the status to set the flag to
  208. */
  209. public void setReplace(boolean repl) {
  210. mReplace = repl;
  211. }
  212. /**
  213. * Get replace flag status
  214. *
  215. * @return boolean containing status of replace flag
  216. */
  217. public boolean getReplace() {
  218. return mReplace;
  219. }
  220. /**
  221. * Set the global flag
  222. *
  223. * @param glob the status to set the flag to
  224. */
  225. public void setGlobal(boolean glob) {
  226. mGlobal = glob;
  227. }
  228. /**
  229. * Get global flag status
  230. *
  231. * @return boolean containing status of global flag
  232. */
  233. public boolean getGlobal() {
  234. return mGlobal;
  235. }
  236. /**
  237. * Set the ordinary flag
  238. *
  239. * @param ordinary the status to set the flag to
  240. */
  241. public void setOrdinary(boolean ordinary) {
  242. mOrdinary = ordinary;
  243. }
  244. /**
  245. * Get ordinary flag status
  246. *
  247. * @return boolean containing status of ordinary flag
  248. */
  249. public boolean getOrdinary() {
  250. return mOrdinary;
  251. }
  252. /**
  253. * Set the pbranch flag
  254. *
  255. * @param pbranch the status to set the flag to
  256. */
  257. public void setPbranch(boolean pbranch) {
  258. mPbranch = pbranch;
  259. }
  260. /**
  261. * Get pbranch flag status
  262. *
  263. * @return boolean containing status of pbranch flag
  264. */
  265. public boolean getPbranch() {
  266. return mPbranch;
  267. }
  268. /**
  269. * Set the shared flag
  270. *
  271. * @param shared the status to set the flag to
  272. */
  273. public void setShared(boolean shared) {
  274. mShared = shared;
  275. }
  276. /**
  277. * Get shared flag status
  278. *
  279. * @return boolean containing status of shared flag
  280. */
  281. public boolean getShared() {
  282. return mShared;
  283. }
  284. /**
  285. * Set comment string
  286. *
  287. * @param comment the comment string
  288. */
  289. public void setComment(String comment) {
  290. mComment = comment;
  291. }
  292. /**
  293. * Get comment string
  294. *
  295. * @return String containing the comment
  296. */
  297. public String getComment() {
  298. return mComment;
  299. }
  300. /**
  301. * Set comment file
  302. *
  303. * @param cfile the path to the comment file
  304. */
  305. public void setCommentFile(String cfile) {
  306. mCfile = cfile;
  307. }
  308. /**
  309. * Get comment file
  310. *
  311. * @return String containing the path to the comment file
  312. */
  313. public String getCommentFile() {
  314. return mCfile;
  315. }
  316. /**
  317. * Get the 'comment' command
  318. *
  319. * @param cmd containing the command line string with or
  320. * without the comment flag and string appended
  321. */
  322. private void getCommentCommand(Commandline cmd) {
  323. if (getComment() != null) {
  324. /* Had to make two separate commands here because if a space is
  325. inserted between the flag and the value, it is treated as a
  326. Windows filename with a space and it is enclosed in double
  327. quotes ("). This breaks clearcase.
  328. */
  329. cmd.createArgument().setValue(FLAG_COMMENT);
  330. cmd.createArgument().setValue(getComment());
  331. }
  332. }
  333. /**
  334. * Get the 'commentfile' command
  335. *
  336. * @param cmd containing the command line string with or
  337. * without the commentfile flag and file appended
  338. */
  339. private void getCommentFileCommand(Commandline cmd) {
  340. if (getCommentFile() != null) {
  341. /* Had to make two separate commands here because if a space is
  342. inserted between the flag and the value, it is treated as a
  343. Windows filename with a space and it is enclosed in double
  344. quotes ("). This breaks clearcase.
  345. */
  346. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  347. cmd.createArgument().setValue(getCommentFile());
  348. }
  349. }
  350. /**
  351. * Get the type-name specifier
  352. *
  353. * @return the 'type-name-specifier' command if the attribute was
  354. * specified, otherwise an empty string
  355. */
  356. private String getTypeSpecifier() {
  357. String typenm = null;
  358. typenm = getTypeName();
  359. if (getVOB() != null) {
  360. typenm += "@" + getVOB();
  361. }
  362. return typenm;
  363. }
  364. /**
  365. * -replace flag -- replace existing label definition of the same type
  366. */
  367. public static final String FLAG_REPLACE = "-replace";
  368. /**
  369. * -global flag -- creates a label type that is global to the VOB or to VOBs that use this VOB
  370. */
  371. public static final String FLAG_GLOBAL = "-global";
  372. /**
  373. * -ordinary flag -- creates a label type that can be used only in the current VOB
  374. */
  375. public static final String FLAG_ORDINARY = "-ordinary";
  376. /**
  377. * -pbranch flag -- allows label type to be used once per branch
  378. */
  379. public static final String FLAG_PBRANCH = "-pbranch";
  380. /**
  381. * -shared flag -- sets the way mastership is checked by ClearCase
  382. */
  383. public static final String FLAG_SHARED = "-shared";
  384. /**
  385. * -c flag -- comment to attach to the file
  386. */
  387. public static final String FLAG_COMMENT = "-c";
  388. /**
  389. * -cfile flag -- file containing a comment to attach to the file
  390. */
  391. public static final String FLAG_COMMENTFILE = "-cfile";
  392. /**
  393. * -nc flag -- no comment is specified
  394. */
  395. public static final String FLAG_NOCOMMENT = "-nc";
  396. }