KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > ManagedCommandline


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 /*
38  * The Apache Software License, Version 1.1
39  *
40  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
41  * reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright
48  * notice, this list of conditions and the following disclaimer.
49  *
50  * 2. Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in
52  * the documentation and/or other materials provided with the
53  * distribution.
54  *
55  * 3. The end-user documentation included with the redistribution, if
56  * any, must include the following acknowlegement:
57  * "This product includes software developed by the
58  * Apache Software Foundation (http://www.apache.org/)."
59  * Alternately, this acknowlegement may appear in the software itself,
60  * if and wherever such third-party acknowlegements normally appear.
61  *
62  * 4. The names "The Jakarta Project", "Ant", and "Apache Software
63  * Foundation" must not be used to endorse or promote products derived
64  * from this software without prior written permission. For written
65  * permission, please contact apache@apache.org.
66  *
67  * 5. Products derived from this software may not be called "Apache"
68  * nor may "Apache" appear in their names without prior written
69  * permission of the Apache Group.
70  *
71  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
72  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
74  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
75  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
76  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
77  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
78  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
79  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
80  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
81  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82  * SUCH DAMAGE.
83  * ====================================================================
84  *
85  * This software consists of voluntary contributions made by many
86  * individuals on behalf of the Apache Software Foundation. For more
87  * information on the Apache Software Foundation, please see
88  * <http://www.apache.org/>.
89  */

90 package net.sourceforge.cruisecontrol.util;
91
92 import java.io.BufferedReader JavaDoc;
93 import java.io.IOException JavaDoc;
94 import java.io.InputStreamReader JavaDoc;
95 import java.util.ArrayList JavaDoc;
96 import java.util.List JavaDoc;
97
98 import org.apache.log4j.Logger;
99
100 import net.sourceforge.cruisecontrol.CruiseControlException;
101
102 /**
103  * Extends <cod>EnvCommandline</code> by adding stdout and stderr
104  * stream handling as well as some assertions to check for proper
105  * execution of the command.
106  *
107  * @author <a HREF="mailto:rjmpsmith@hotmail.com">Robert J. Smith</a>
108  */

109 public class ManagedCommandline extends EnvCommandline {
110
111     private static final Logger LOG = Logger.getLogger(ManagedCommandline.class);
112
113     /**
114      * Holds the exit code from the command
115      */

116     private int exitCode;
117
118     /**
119      * The stdout from the command as a string
120      */

121     private String JavaDoc stdout;
122
123     /**
124      * The stdout from the command as a List of output lines
125      */

126     private List JavaDoc stdoutLines = new ArrayList JavaDoc();
127
128     /**
129      * The stderr from the command as a string
130      */

131     private String JavaDoc stderr;
132
133     /**
134      * The stderr from the command as a List of output lines
135      */

136     private List JavaDoc stderrLines = new ArrayList JavaDoc();
137
138     /**
139      * Constructor which takes a command line string and attempts
140      * to parse it into it's various components.
141      *
142      * @param command The command
143      */

144     public ManagedCommandline(String JavaDoc command) {
145         super(command);
146     }
147
148     /**
149      * Default constructor
150      */

151     public ManagedCommandline() {
152         super();
153     }
154
155     /**
156      * Returns the exit code of the command as reported by the OS.
157      *
158      * @return The exit code of the command
159      */

160     public int getExitCode() {
161         return exitCode;
162     }
163
164     /**
165      * Returns the stdout from the command as a String
166      *
167      * @return The standard output of the command as a <code>String</code>
168      */

169     public String JavaDoc getStdoutAsString() {
170         return stdout;
171     }
172
173     /**
174      * Returns the stdout from the command as a List of Strings where each
175      * String is one line of the output.
176      *
177      * @return The standard output of the command as a <code>List</code> of
178      * output lines.
179      */

180     public List JavaDoc getStdoutAsList() {
181         return stdoutLines;
182     }
183
184     /**
185      * Returns the stderr from the command as a String
186      *
187      * @return The standard error of the command as a <code>String</code>
188      */

189     public String JavaDoc getStderrAsString() {
190         return stderr;
191     }
192
193     /**
194      * Returns the stderr from the command as a List of Strings where each
195      * String is one line of the error.
196      *
197      * @return The standard error of the command as a <code>List</code> of
198      * output lines.
199      */

200     public List JavaDoc getStderrAsList() {
201         return stderrLines;
202     }
203
204     /**
205      * Clear out the whole command line.
206      */

207     public void clear() {
208         super.clear();
209         clearArgs();
210     }
211
212     /**
213      * Clear out the arguments and stored command output, but leave the
214      * executable in place for another operation.
215      */

216     public void clearArgs() {
217         exitCode = -1;
218         stdout = "";
219         stderr = "";
220         stdoutLines.clear();
221         stderrLines.clear();
222         super.clearArgs();
223     }
224
225     /**
226      * Asserts that the stdout of the command does not contain a
227      * given <code>String</code>. Throws a
228      * <code>CruiseControlException</code> if it does.
229      *
230      * @param string
231      * The forbidden <code>String</code>
232      *
233      * @throws CruiseControlException
234      */

235     public void assertStdoutDoesNotContain(String JavaDoc string) throws CruiseControlException {
236         if (stdout.indexOf(string) > -1) {
237             throw new CruiseControlException(
238                 "The command \""
239                     + this.toString()
240                     + "\" returned the forbidden string \""
241                     + string
242                     + "\". \n"
243                     + "Stdout: "
244                     + stdout
245                     + "Stderr: "
246                     + stderr);
247         }
248     }
249
250     /**
251      * Asserts that the stdout of the command contains a
252      * given <code>String</code>. Throws a
253      * <code>CruiseControlException</code> if it does not.
254      *
255      * @param string
256      * The required <code>String</code>
257      *
258      * @throws CruiseControlException
259      */

260     public void assertStdoutContains(String JavaDoc string) throws CruiseControlException {
261         if (stdout.indexOf(string) < 0) {
262             throw new CruiseControlException(
263                 "The stdout of the command \""
264                     + this.toString()
265                     + "\" did not contain the required string \""
266                     + string
267                     + "\". \n"
268                     + "Stdout: "
269                     + stdout
270                     + "Stderr: "
271                     + stderr);
272         }
273     }
274
275     /**
276      * Asserts that the stderr of the command does not contain a
277      * given <code>String</code>. Throws a
278      * <code>CruiseControlException</code> if it does.
279      *
280      * @param string
281      * The forbidden <code>String</code>
282      *
283      * @throws CruiseControlException
284      */

285     public void assertStderrDoesNotContain(String JavaDoc string) throws CruiseControlException {
286         if (stderr.indexOf(string) > -1) {
287             throw new CruiseControlException(
288                 "The command \""
289                     + this.toString()
290                     + "\" returned the forbidden string \""
291                     + string
292                     + "\". \n"
293                     + "Stdout: "
294                     + stdout
295                     + "Stderr: "
296                     + stderr);
297         }
298     }
299
300     /**
301      * Asserts that the exit code of the command matches an expected value.
302      * Throws a <code>CruiseControlException</code> if it does not.
303      *
304      * @param code
305      * The expected exit code of the command
306      *
307      * @throws CruiseControlException
308      */

309     public void assertExitCode(int code) throws CruiseControlException {
310         if (exitCode != code) {
311             throw new CruiseControlException(
312                 "The command \""
313                     + this.toString()
314                     + "\" returned exit code \""
315                     + exitCode
316                     + "\" when \""
317                     + code
318                     + "\" was expected.\n"
319                     + "Stdout: "
320                     + stdout
321                     + "Stderr: "
322                     + stderr);
323         }
324     }
325
326     /**
327      * Asserts that the exit code of the command is not a given value. Throws a
328      * <code>CruiseControlException</code> if it is.
329      *
330      * @param code
331      * The expected exit code of the command
332      *
333      * @throws CruiseControlException
334      */

335     public void assertExitCodeNot(int code) throws CruiseControlException {
336         if (exitCode == code) {
337             throw new CruiseControlException(
338                 "The command \""
339                     + this.toString()
340                     + "\" returned exit code \""
341                     + exitCode
342                     + "\".\n"
343                     + "Stdout: "
344                     + stdout
345                     + "Stderr: "
346                     + stderr);
347         }
348     }
349
350     /**
351      * Asserts that the exit code of the command is greater than a given value.
352      * Throws a <code>CruiseControlException</code> if it is not.
353      *
354      * @param code
355      * The expected exit code of the command
356      *
357      * @throws CruiseControlException
358      */

359     public void assertExitCodeGreaterThan(int code)
360         throws CruiseControlException {
361         if (exitCode <= code) {
362             throw new CruiseControlException(
363                 "The command \""
364                     + this.toString()
365                     + "\" returned exit code \""
366                     + exitCode
367                     + "\" when a value greater than \""
368                     + code
369                     + "\" was expected.\n"
370                     + "Stdout: "
371                     + stdout
372                     + "Stderr: "
373                     + stderr);
374         }
375     }
376
377     /**
378      * Asserts that the exit code of the command is less than a given value.
379      * Throws a <code>CruiseControlException</code> if it is not.
380      *
381      * @param code
382      * The expected exit code of the command
383      *
384      * @throws CruiseControlException
385      */

386     public void assertExitCodeLessThan(int code)
387         throws CruiseControlException {
388         if (exitCode >= code) {
389             throw new CruiseControlException(
390                 "The command \""
391                     + this.toString()
392                     + "\" returned exit code \""
393                     + exitCode
394                     + "\" when a value less than \""
395                     + code
396                     + "\" was expected.\n"
397                     + "Stdout: "
398                     + stdout
399                     + "Stderr: "
400                     + stderr);
401         }
402     }
403
404     /**
405      * Executes the command.
406      */

407     public Process JavaDoc execute() throws IOException JavaDoc {
408
409         // Execute the command using the specified environment
410
Process JavaDoc proc = super.execute();
411
412         // Capture the output of the command
413
BufferedReader JavaDoc stdoutStream = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
414                 proc.getInputStream()));
415         BufferedReader JavaDoc stderrStream = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
416                 proc.getErrorStream()));
417
418         // Parse the stdout of the command
419
String JavaDoc line;
420         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
421         while ((line = stdoutStream.readLine()) != null) {
422             stdoutLines.add(line);
423             buff.append(line).append('\n');
424         }
425         stdout = buff.toString();
426
427         // Parse the stderr of the command
428
buff.setLength(0);
429         while ((line = stderrStream.readLine()) != null) {
430             stderrLines.add(line);
431             buff.append(line).append('\n');
432         }
433         stderr = buff.toString();
434
435         // Wait for the command to complete
436
try {
437             proc.waitFor();
438         } catch (InterruptedException JavaDoc e) {
439             LOG.error("Thread was interrupted while executing command \""
440                     + this.toString() + "\".", e);
441         }
442
443         // Close down our streams
444
stdoutStream.close();
445         stderrStream.close();
446
447         // Set the exit code
448
exitCode = proc.exitValue();
449
450         // Just to be compatible
451
return proc;
452     }
453 }
454
Popular Tags