KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > ExecScript


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 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 package net.sourceforge.cruisecontrol.builders;
38
39 import java.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42
43 import org.apache.log4j.Logger;
44 import org.jdom.CDATA;
45 import org.jdom.Element;
46
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48 import net.sourceforge.cruisecontrol.util.Commandline;
49 import net.sourceforge.cruisecontrol.util.StreamConsumer;
50
51
52 /**
53  * Exec script class.
54  *
55  * Script support to execute a command and logs the results.
56  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
57  */

58 public class ExecScript implements Script, StreamConsumer {
59     private static final Logger LOG = Logger.getLogger(ExecScript.class);
60      
61     private String JavaDoc execCommand;
62     private String JavaDoc execArgs;
63     private String JavaDoc errorStr;
64     private int exitCode;
65     private boolean foundError = false;
66     private Element buildLogElement;
67     private Element currentElement = null;
68
69     /**
70      * construct the command that we're going to execute.
71      * @return Commandline holding command to be executed
72      * @throws CruiseControlException
73      */

74     public Commandline buildCommandline() throws CruiseControlException {
75         Commandline cmdLine = new Commandline();
76
77         // make sure we have a command
78
if (execCommand != null) {
79             cmdLine.setExecutable(execCommand);
80         } else {
81             throw new CruiseControlException("no command to be executed");
82         }
83
84         // add the arguments if necessary
85
if (execArgs != null) {
86             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(execArgs, " \t\r\n");
87             while (stok.hasMoreTokens()) {
88                 cmdLine.createArgument().setValue(stok.nextToken());
89             }
90         }
91
92         // log the command if debug is enabled
93
if (LOG.isDebugEnabled()) {
94             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95             sb.append("Executing Command: ");
96             String JavaDoc[] args = cmdLine.getCommandline();
97             for (int i = 0; i < args.length; i++) {
98                 String JavaDoc arg = args[i];
99                 sb.append(arg);
100                 sb.append(" ");
101             }
102             LOG.debug(sb.toString());
103         }
104         return cmdLine;
105     } // buildCommandLine
106

107     /**
108      * Ugly parsing of Exec output into some Elements.
109      * Gets called from StreamPumper.
110      * @param line the line of output to parse
111      */

112     public synchronized void consumeLine(String JavaDoc line) {
113         if (line == null || line.length() == 0 || buildLogElement == null) {
114             return;
115         }
116
117         synchronized (buildLogElement) {
118             // check if the output contains the error string
119
if (errorStr != null) {
120                 // YES: set error flag
121
if (line.indexOf(errorStr) >= 0) {
122                     foundError = true;
123                 }
124             } else {
125                 // NO: just write the ouput to the log
126
Element msg = new Element("message");
127                 msg.addContent(new CDATA(line));
128                 msg.setAttribute("priority", "info");
129                 if (currentElement == null) {
130                     buildLogElement.addContent(msg);
131                 } else {
132                     currentElement.addContent(msg);
133                 }
134             }
135         }
136     } // consumeLine
137

138     /**
139      * flush the current log element
140      */

141     protected void flushCurrentElement() {
142         if (buildLogElement == null) {
143             return;
144         }
145         synchronized (buildLogElement) {
146             if (currentElement != null) {
147                 if (buildLogElement.getAttribute("error") != null) {
148                     // All the messages of the last (failed) goal should be
149
// switched to priority error
150
List JavaDoc lst = currentElement.getChildren("message");
151                     if (lst != null) {
152                         Iterator JavaDoc it = lst.iterator();
153                         while (it.hasNext()) {
154                             Element msg = (Element) it.next();
155                             msg.setAttribute("priority", "error");
156                         }
157                     }
158                 }
159                 buildLogElement.addContent(currentElement);
160             }
161             currentElement = null;
162         }
163     } // flushCurrentElement
164

165     /**
166      * set the "header" for this part of the build log.
167      * turns it into an Ant target/task style element for reporting purposes
168      * @param buildLogElement the element of the build log
169      * @return updated element
170      */

171     public Element setBuildLogHeader(Element buildLogElement) {
172         Element target = new Element("target");
173         target.setAttribute("name", "exec");
174         buildLogElement.addContent(target);
175         Element task = new Element("task");
176         task.setAttribute("name", this.execCommand);
177         target.addContent(task);
178         return task;
179     } // setBuildLogHeader
180

181     
182     /**
183      * @param execArgs The execArgs to set.
184      */

185     public void setExecArgs(String JavaDoc execArgs) {
186         this.execArgs = execArgs;
187     } // setExecArgs
188

189     /**
190      * @param execCommand The execCommand to set.
191      */

192     public void setExecCommand(String JavaDoc execCommand) {
193         this.execCommand = execCommand;
194     } // setExecCommand
195

196     /**
197      * @return returns the exitcode of the command
198      */

199     public int getExitCode() {
200         return exitCode;
201     } // getExitCode
202

203     /**
204      * @param exitCode the exit code value to set.
205      */

206     public void setExitCode(int exitCode) {
207         this.exitCode = exitCode;
208     } // setExitCode
209

210     /**
211      * @param errStr the error string to search for
212      */

213     public void setErrorStr(String JavaDoc errStr) {
214         this.errorStr = errStr;
215     } // setErrorStr
216

217     /**
218      * @param buildLogElement The buildLogElement to set.
219      */

220     public void setBuildLogElement(Element buildLogElement) {
221         this.buildLogElement = buildLogElement;
222     } // setBuildLogElement
223

224     /**
225      * @return true if error occured, else false
226      */

227     public boolean wasError() {
228         return this.foundError;
229     } // wasError
230

231 } // ExecScript
232
Popular Tags