001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.ccm;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.OutputStream;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028: import org.apache.tools.ant.taskdefs.Execute;
029: import org.apache.tools.ant.taskdefs.ExecuteStreamHandler;
030: import org.apache.tools.ant.types.Commandline;
031:
032: /**
033: * Creates new Continuus ccm task and sets it as the default.
034: *
035: * @ant.task name="ccmcreatetask" category="scm"
036: */
037: public class CCMCreateTask extends Continuus implements
038: ExecuteStreamHandler {
039:
040: private String comment = null;
041: private String platform = null;
042: private String resolver = null;
043: private String release = null;
044: private String subSystem = null;
045: private String task = null;
046:
047: /**
048: * Constructor for CCMCreateTask.
049: */
050: public CCMCreateTask() {
051: super ();
052: setCcmAction(COMMAND_CREATE_TASK);
053: }
054:
055: /**
056: * Executes the task.
057: * <p>
058: * Builds a command line to execute ccm and then calls Exec's run method
059: * to execute the command line.
060: * </p>
061: * @throws BuildException on error
062: */
063: public void execute() throws BuildException {
064: Commandline commandLine = new Commandline();
065: int result = 0;
066:
067: // build the command line from what we got the format
068: // as specified in the CCM.EXE help
069: commandLine.setExecutable(getCcmCommand());
070: commandLine.createArgument().setValue(getCcmAction());
071:
072: checkOptions(commandLine);
073:
074: result = run(commandLine, this );
075: if (Execute.isFailure(result)) {
076: String msg = "Failed executing: " + commandLine.toString();
077: throw new BuildException(msg, getLocation());
078: }
079:
080: //create task ok, set this task as the default one
081: Commandline commandLine2 = new Commandline();
082: commandLine2.setExecutable(getCcmCommand());
083: commandLine2.createArgument().setValue(COMMAND_DEFAULT_TASK);
084: commandLine2.createArgument().setValue(getTask());
085:
086: log(commandLine.describeCommand(), Project.MSG_DEBUG);
087:
088: result = run(commandLine2);
089: if (result != 0) {
090: String msg = "Failed executing: " + commandLine2.toString();
091: throw new BuildException(msg, getLocation());
092: }
093:
094: }
095:
096: /**
097: * Check the command line options.
098: */
099: private void checkOptions(Commandline cmd) {
100: if (getComment() != null) {
101: cmd.createArgument().setValue(FLAG_COMMENT);
102: cmd.createArgument().setValue("\"" + getComment() + "\"");
103: }
104:
105: if (getPlatform() != null) {
106: cmd.createArgument().setValue(FLAG_PLATFORM);
107: cmd.createArgument().setValue(getPlatform());
108: } // end of if ()
109:
110: if (getResolver() != null) {
111: cmd.createArgument().setValue(FLAG_RESOLVER);
112: cmd.createArgument().setValue(getResolver());
113: } // end of if ()
114:
115: if (getSubSystem() != null) {
116: cmd.createArgument().setValue(FLAG_SUBSYSTEM);
117: cmd.createArgument().setValue("\"" + getSubSystem() + "\"");
118: } // end of if ()
119:
120: if (getRelease() != null) {
121: cmd.createArgument().setValue(FLAG_RELEASE);
122: cmd.createArgument().setValue(getRelease());
123: } // end of if ()
124: }
125:
126: /**
127: * Get the value of comment.
128: * @return value of comment.
129: */
130: public String getComment() {
131: return comment;
132: }
133:
134: /**
135: * Specifies a comment.
136: *
137: * @param v Value to assign to comment.
138: */
139: public void setComment(String v) {
140: this .comment = v;
141: }
142:
143: /**
144: * Get the value of platform.
145: * @return value of platform.
146: */
147: public String getPlatform() {
148: return platform;
149: }
150:
151: /**
152: * Specifies the target platform.
153: *
154: * @param v Value to assign to platform.
155: */
156: public void setPlatform(String v) {
157: this .platform = v;
158: }
159:
160: /**
161: * Get the value of resolver.
162: * @return value of resolver.
163: */
164: public String getResolver() {
165: return resolver;
166: }
167:
168: /**
169: * Specifies the resolver.
170: *
171: * @param v Value to assign to resolver.
172: */
173: public void setResolver(String v) {
174: this .resolver = v;
175: }
176:
177: /**
178: * Get the value of release.
179: * @return value of release.
180: */
181: public String getRelease() {
182: return release;
183: }
184:
185: /**
186: * Specify the CCM release.
187: *
188: * @param v Value to assign to release.
189: */
190: public void setRelease(String v) {
191: this .release = v;
192: }
193:
194: /**
195: * Get the value of subSystem.
196: * @return value of subSystem.
197: */
198: public String getSubSystem() {
199: return subSystem;
200: }
201:
202: /**
203: * Specifies the subsystem.
204: *
205: * @param v Value to assign to subSystem.
206: */
207: public void setSubSystem(String v) {
208: this .subSystem = v;
209: }
210:
211: /**
212: * Get the value of task.
213: * @return value of task.
214: */
215: public String getTask() {
216: return task;
217: }
218:
219: /**
220: * Specifies the task number used to checkin
221: * the file (may use 'default').
222: *
223: * @param v Value to assign to task.
224: */
225: public void setTask(String v) {
226: this .task = v;
227: }
228:
229: /**
230: * /comment -- comments associated to the task
231: */
232: public static final String FLAG_COMMENT = "/synopsis";
233:
234: /**
235: * /platform flag -- target platform
236: */
237: public static final String FLAG_PLATFORM = "/plat";
238:
239: /**
240: * /resolver flag
241: */
242: public static final String FLAG_RESOLVER = "/resolver";
243:
244: /**
245: * /release flag
246: */
247: public static final String FLAG_RELEASE = "/release";
248:
249: /**
250: * /release flag
251: */
252: public static final String FLAG_SUBSYSTEM = "/subsystem";
253:
254: /**
255: * -task flag -- associate checkout task with task
256: */
257: public static final String FLAG_TASK = "/task";
258:
259: // implementation of org.apache.tools.ant.taskdefs.ExecuteStreamHandler interface
260:
261: /**
262: *
263: * @throws IOException on error
264: */
265: public void start() throws IOException {
266: }
267:
268: /**
269: *
270: */
271: public void stop() {
272: }
273:
274: /**
275: *
276: * @param param1 the output stream
277: * @exception java.io.IOException on error
278: */
279: public void setProcessInputStream(OutputStream param1)
280: throws IOException {
281: }
282:
283: /**
284: *
285: * @param is the input stream
286: * @exception java.io.IOException on error
287: */
288: public void setProcessErrorStream(InputStream is)
289: throws IOException {
290: BufferedReader reader = new BufferedReader(
291: new InputStreamReader(is));
292: String s = reader.readLine();
293: if (s != null) {
294: log("err " + s, Project.MSG_DEBUG);
295: } // end of if ()
296: }
297:
298: /**
299: * read the output stream to retrieve the new task number.
300: * @param is InputStream
301: * @throws IOException on error
302: */
303: public void setProcessOutputStream(InputStream is)
304: throws IOException {
305:
306: String buffer = "";
307: try {
308: BufferedReader reader = new BufferedReader(
309: new InputStreamReader(is));
310: buffer = reader.readLine();
311: if (buffer != null) {
312: log("buffer:" + buffer, Project.MSG_DEBUG);
313: String taskstring = buffer.substring(
314: buffer.indexOf(' ')).trim();
315: taskstring = taskstring.substring(0,
316: taskstring.lastIndexOf(' ')).trim();
317: setTask(taskstring);
318: log("task is " + getTask(), Project.MSG_DEBUG);
319: } // end of if ()
320: } catch (NullPointerException npe) {
321: log("error procession stream , null pointer exception",
322: Project.MSG_ERR);
323: npe.printStackTrace();
324: throw new BuildException(npe.getClass().getName());
325: } catch (Exception e) {
326: log("error procession stream " + e.getMessage(),
327: Project.MSG_ERR);
328: throw new BuildException(e.getMessage());
329: } // end of try-catch
330:
331: }
332:
333: }
|