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 java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Project;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.taskdefs.ExecTask;
  23. import org.apache.tools.ant.taskdefs.Execute;
  24. import org.apache.tools.ant.taskdefs.LogStreamHandler;
  25. import org.apache.tools.ant.types.Commandline;
  26. /**
  27. * A base class for creating tasks for executing commands on ClearCase.
  28. * <p>
  29. * The class extends the 'exec' task as it operates by executing the cleartool program
  30. * supplied with ClearCase. By default the task expects the cleartool executable to be
  31. * in the path, * you can override this be specifying the cleartooldir attribute.
  32. * </p>
  33. * <p>
  34. * This class provides set and get methods for the 'viewpath' and 'objselect'
  35. * attribute. It also contains constants for the flags that can be passed to
  36. * cleartool.
  37. * </p>
  38. *
  39. */
  40. public abstract class ClearCase extends Task {
  41. private String mClearToolDir = "";
  42. private String mviewPath = null;
  43. private String mobjSelect = null;
  44. private static int pcnt = 0;
  45. private boolean mFailonerr = true;
  46. /**
  47. * Set the directory where the cleartool executable is located.
  48. *
  49. * @param dir the directory containing the cleartool executable
  50. */
  51. public final void setClearToolDir(String dir) {
  52. mClearToolDir = getProject().translatePath(dir);
  53. }
  54. /**
  55. * Builds and returns the command string to execute cleartool
  56. *
  57. * @return String containing path to the executable
  58. */
  59. protected final String getClearToolCommand() {
  60. String toReturn = mClearToolDir;
  61. if (!toReturn.equals("") && !toReturn.endsWith("/")) {
  62. toReturn += "/";
  63. }
  64. toReturn += CLEARTOOL_EXE;
  65. return toReturn;
  66. }
  67. /**
  68. * Set the path to the item in a ClearCase view to operate on.
  69. *
  70. * @param viewPath Path to the view directory or file
  71. */
  72. public final void setViewPath(String viewPath) {
  73. mviewPath = viewPath;
  74. }
  75. /**
  76. * Get the path to the item in a clearcase view
  77. *
  78. * @return mviewPath
  79. */
  80. public String getViewPath() {
  81. return mviewPath;
  82. }
  83. /**
  84. * Get the basename path of the item in a clearcase view
  85. *
  86. * @return basename
  87. */
  88. public String getViewPathBasename() {
  89. return (new File(mviewPath)).getName();
  90. }
  91. /**
  92. * Set the object to operate on.
  93. *
  94. * @param objSelect object to operate on
  95. */
  96. public final void setObjSelect(String objSelect) {
  97. mobjSelect = objSelect;
  98. }
  99. /**
  100. * Get the object to operate on
  101. *
  102. * @return mobjSelect
  103. */
  104. public String getObjSelect() {
  105. return mobjSelect;
  106. }
  107. /**
  108. * Execute the given command are return success or failure
  109. * @param cmd command line to execute
  110. * @return the exit status of the subprocess or <code>INVALID</code>
  111. */
  112. protected int run(Commandline cmd) {
  113. try {
  114. Project aProj = getProject();
  115. Execute exe
  116. = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN));
  117. exe.setAntRun(aProj);
  118. exe.setWorkingDirectory(aProj.getBaseDir());
  119. exe.setCommandline(cmd.getCommandline());
  120. return exe.execute();
  121. } catch (java.io.IOException e) {
  122. throw new BuildException(e, getLocation());
  123. }
  124. }
  125. /**
  126. * Execute the given command, and return it's output
  127. * @param cmdline command line to execute
  128. * @return output of the command line
  129. */
  130. protected String runS(Commandline cmdline) {
  131. String outV = "opts.cc.runS.output" + pcnt++;
  132. Project aProj = getProject();
  133. ExecTask exe = (ExecTask) aProj.createTask("exec");
  134. Commandline.Argument arg = exe.createArg();
  135. exe.setExecutable(cmdline.getExecutable());
  136. arg.setLine(cmdline.toString(cmdline.getArguments()));
  137. exe.setOutputproperty(outV);
  138. exe.execute();
  139. // System.out.println( "runS: " + outV + " : " + aProj.getProperty( outV ));
  140. return aProj.getProperty(outV);
  141. }
  142. /**
  143. * If true, command will throw an exception on failure.
  144. *
  145. * @param failonerr the status to set the flag to
  146. * @since ant 1.6.1
  147. */
  148. public void setFailOnErr(boolean failonerr) {
  149. mFailonerr = failonerr;
  150. }
  151. /**
  152. * Get failonerr flag status
  153. *
  154. * @return boolean containing status of failonerr flag
  155. * @since ant 1.6.1
  156. */
  157. public boolean getFailOnErr() {
  158. return mFailonerr;
  159. }
  160. /**
  161. * Constant for the thing to execute
  162. */
  163. private static final String CLEARTOOL_EXE = "cleartool";
  164. /**
  165. * The 'Update' command
  166. */
  167. public static final String COMMAND_UPDATE = "update";
  168. /**
  169. * The 'Checkout' command
  170. */
  171. public static final String COMMAND_CHECKOUT = "checkout";
  172. /**
  173. * The 'Checkin' command
  174. */
  175. public static final String COMMAND_CHECKIN = "checkin";
  176. /**
  177. * The 'UndoCheckout' command
  178. */
  179. public static final String COMMAND_UNCHECKOUT = "uncheckout";
  180. /**
  181. * The 'Lock' command
  182. */
  183. public static final String COMMAND_LOCK = "lock";
  184. /**
  185. * The 'Unlock' command
  186. */
  187. public static final String COMMAND_UNLOCK = "unlock";
  188. /**
  189. * The 'Mkbl' command
  190. */
  191. public static final String COMMAND_MKBL = "mkbl";
  192. /**
  193. * The 'Mklabel' command
  194. */
  195. public static final String COMMAND_MKLABEL = "mklabel";
  196. /**
  197. * The 'Mklbtype' command
  198. */
  199. public static final String COMMAND_MKLBTYPE = "mklbtype";
  200. /**
  201. * The 'Rmtype' command
  202. */
  203. public static final String COMMAND_RMTYPE = "rmtype";
  204. /**
  205. * The 'LsCheckout' command
  206. */
  207. public static final String COMMAND_LSCO = "lsco";
  208. /**
  209. * The 'Mkelem' command
  210. */
  211. public static final String COMMAND_MKELEM = "mkelem";
  212. /**
  213. * The 'Mkattr' command
  214. */
  215. public static final String COMMAND_MKATTR = "mkattr";
  216. /**
  217. * The 'Mkdir' command
  218. */
  219. public static final String COMMAND_MKDIR = "mkdir";
  220. }