1. /*
  2. * Copyright 2001-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.ccm;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.OutputStream;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. import org.apache.tools.ant.taskdefs.Execute;
  26. import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
  27. import org.apache.tools.ant.types.Commandline;
  28. /**
  29. * Creates new Continuus ccm task and sets it as the default.
  30. *
  31. *
  32. * @ant.task name="ccmcreatetask" category="scm"
  33. */
  34. public class CCMCreateTask extends Continuus implements ExecuteStreamHandler {
  35. private String comment = null;
  36. private String platform = null;
  37. private String resolver = null;
  38. private String release = null;
  39. private String subSystem = null;
  40. private String task = null;
  41. public CCMCreateTask() {
  42. super();
  43. setCcmAction(COMMAND_CREATE_TASK);
  44. }
  45. /**
  46. * Executes the task.
  47. * <p>
  48. * Builds a command line to execute ccm and then calls Exec's run method
  49. * to execute the command line.
  50. * </p>
  51. */
  52. public void execute() throws BuildException {
  53. Commandline commandLine = new Commandline();
  54. int result = 0;
  55. // build the command line from what we got the format
  56. // as specified in the CCM.EXE help
  57. commandLine.setExecutable(getCcmCommand());
  58. commandLine.createArgument().setValue(getCcmAction());
  59. checkOptions(commandLine);
  60. result = run(commandLine, this);
  61. if (Execute.isFailure(result)) {
  62. String msg = "Failed executing: " + commandLine.toString();
  63. throw new BuildException(msg, getLocation());
  64. }
  65. //create task ok, set this task as the default one
  66. Commandline commandLine2 = new Commandline();
  67. commandLine2.setExecutable(getCcmCommand());
  68. commandLine2.createArgument().setValue(COMMAND_DEFAULT_TASK);
  69. commandLine2.createArgument().setValue(getTask());
  70. log(commandLine.describeCommand(), Project.MSG_DEBUG);
  71. result = run(commandLine2);
  72. if (result != 0) {
  73. String msg = "Failed executing: " + commandLine2.toString();
  74. throw new BuildException(msg, getLocation());
  75. }
  76. }
  77. /**
  78. * Check the command line options.
  79. */
  80. private void checkOptions(Commandline cmd) {
  81. if (getComment() != null) {
  82. cmd.createArgument().setValue(FLAG_COMMENT);
  83. cmd.createArgument().setValue("\"" + getComment() + "\"");
  84. }
  85. if (getPlatform() != null) {
  86. cmd.createArgument().setValue(FLAG_PLATFORM);
  87. cmd.createArgument().setValue(getPlatform());
  88. } // end of if ()
  89. if (getResolver() != null) {
  90. cmd.createArgument().setValue(FLAG_RESOLVER);
  91. cmd.createArgument().setValue(getResolver());
  92. } // end of if ()
  93. if (getSubSystem() != null) {
  94. cmd.createArgument().setValue(FLAG_SUBSYSTEM);
  95. cmd.createArgument().setValue("\"" + getSubSystem() + "\"");
  96. } // end of if ()
  97. if (getRelease() != null) {
  98. cmd.createArgument().setValue(FLAG_RELEASE);
  99. cmd.createArgument().setValue(getRelease());
  100. } // end of if ()
  101. }
  102. /**
  103. * Get the value of comment.
  104. * @return value of comment.
  105. */
  106. public String getComment() {
  107. return comment;
  108. }
  109. /**
  110. * Specifies a comment.
  111. *
  112. * @param v Value to assign to comment.
  113. */
  114. public void setComment(String v) {
  115. this.comment = v;
  116. }
  117. /**
  118. * Get the value of platform.
  119. * @return value of platform.
  120. */
  121. public String getPlatform() {
  122. return platform;
  123. }
  124. /**
  125. * Specifies the target platform.
  126. *
  127. * @param v Value to assign to platform.
  128. */
  129. public void setPlatform(String v) {
  130. this.platform = v;
  131. }
  132. /**
  133. * Get the value of resolver.
  134. * @return value of resolver.
  135. */
  136. public String getResolver() {
  137. return resolver;
  138. }
  139. /**
  140. * Specifies the resolver.
  141. *
  142. * @param v Value to assign to resolver.
  143. */
  144. public void setResolver(String v) {
  145. this.resolver = v;
  146. }
  147. /**
  148. * Get the value of release.
  149. * @return value of release.
  150. */
  151. public String getRelease() {
  152. return release;
  153. }
  154. /**
  155. * Specify the CCM release.
  156. *
  157. * @param v Value to assign to release.
  158. */
  159. public void setRelease(String v) {
  160. this.release = v;
  161. }
  162. /**
  163. * Get the value of subSystem.
  164. * @return value of subSystem.
  165. */
  166. public String getSubSystem() {
  167. return subSystem;
  168. }
  169. /**
  170. * Specifies the subsystem.
  171. *
  172. * @param v Value to assign to subSystem.
  173. */
  174. public void setSubSystem(String v) {
  175. this.subSystem = v;
  176. }
  177. /**
  178. * Get the value of task.
  179. * @return value of task.
  180. */
  181. public String getTask() {
  182. return task;
  183. }
  184. /**
  185. * Specifies the task number used to checkin
  186. * the file (may use 'default').
  187. *
  188. * @param v Value to assign to task.
  189. */
  190. public void setTask(String v) {
  191. this.task = v;
  192. }
  193. /**
  194. * /comment -- comments associated to the task
  195. */
  196. public static final String FLAG_COMMENT = "/synopsis";
  197. /**
  198. * /platform flag -- target platform
  199. */
  200. public static final String FLAG_PLATFORM = "/plat";
  201. /**
  202. * /resolver flag
  203. */
  204. public static final String FLAG_RESOLVER = "/resolver";
  205. /**
  206. * /release flag
  207. */
  208. public static final String FLAG_RELEASE = "/release";
  209. /**
  210. * /release flag
  211. */
  212. public static final String FLAG_SUBSYSTEM = "/subsystem";
  213. /**
  214. * -task flag -- associate checkout task with task
  215. */
  216. public static final String FLAG_TASK = "/task";
  217. // implementation of org.apache.tools.ant.taskdefs.ExecuteStreamHandler interface
  218. /**
  219. *
  220. * @exception java.io.IOException
  221. */
  222. public void start() throws IOException {
  223. }
  224. /**
  225. *
  226. */
  227. public void stop() {
  228. }
  229. /**
  230. *
  231. * @param param1
  232. * @exception java.io.IOException
  233. */
  234. public void setProcessInputStream(OutputStream param1) throws IOException {
  235. }
  236. /**
  237. *
  238. * @param is
  239. * @exception java.io.IOException
  240. */
  241. public void setProcessErrorStream(InputStream is) throws IOException {
  242. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  243. String s = reader.readLine();
  244. if (s != null) {
  245. log("err " + s, Project.MSG_DEBUG);
  246. } // end of if ()
  247. }
  248. /**
  249. * read the output stream to retrieve the new task number.
  250. * @param is InputStream
  251. * @exception java.io.IOException
  252. */
  253. public void setProcessOutputStream(InputStream is) throws IOException {
  254. String buffer = "";
  255. try {
  256. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  257. buffer = reader.readLine();
  258. if (buffer != null) {
  259. log("buffer:" + buffer, Project.MSG_DEBUG);
  260. String taskstring = buffer.substring(buffer.indexOf(' ')).trim();
  261. taskstring = taskstring.substring(0, taskstring.lastIndexOf(' ')).trim();
  262. setTask(taskstring);
  263. log("task is " + getTask(), Project.MSG_DEBUG);
  264. } // end of if ()
  265. } catch (NullPointerException npe) {
  266. log("error procession stream , null pointer exception", Project.MSG_ERR);
  267. npe.printStackTrace();
  268. throw new BuildException(npe.getClass().getName());
  269. } catch (Exception e) {
  270. log("error procession stream " + e.getMessage(), Project.MSG_ERR);
  271. throw new BuildException(e.getMessage());
  272. } // end of try-catch
  273. }
  274. }