KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
40 import java.io.File JavaDoc;
41
42 import net.sourceforge.cruisecontrol.Builder;
43 import net.sourceforge.cruisecontrol.CruiseControlException;
44 import net.sourceforge.cruisecontrol.util.ValidationHelper;
45 import net.sourceforge.cruisecontrol.util.DateUtil;
46
47 import org.apache.log4j.Logger;
48 import org.jdom.CDATA;
49 import org.jdom.Element;
50
51 /**
52  * Exec builder class.
53  *
54  * Executes a command line as a builder and determines whether the command
55  * was succesful or not. A string can be supplied to additionally check for
56  * certain error messages in the output
57  *
58  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
59  */

60 public class ExecBuilder extends Builder {
61
62     private static final Logger LOG = Logger.getLogger(ExecBuilder.class);
63
64     private String JavaDoc command;
65     private String JavaDoc args;
66     private String JavaDoc workingDir;
67     private String JavaDoc errorStr;
68     private long timeout = ScriptRunner.NO_TIMEOUT;
69     private Element buildLogElement = null; // global log to produce
70

71     /*
72      * validate the attributes for the plugin
73      */

74     public void validate() throws CruiseControlException {
75         super.validate();
76
77         // need at least an command argument
78
ValidationHelper.assertIsSet(command, "command", this.getClass());
79         
80         // has the working directory been specified?
81
if (this.workingDir == null) {
82             // NO: use the Java temp directory
83
this.workingDir = System.getProperty("java.io.tmpdir");
84         }
85                      
86     } // validate
87

88     /**
89      * execute the command and return the results as XML
90      */

91     public Element build(Map JavaDoc buildProperties) throws CruiseControlException {
92
93         // time the command started
94
long startTime = System.currentTimeMillis();
95
96         buildLogElement = new Element("build");
97
98         // setup script handler
99
ExecScript script = new ExecScript();
100         script.setExecCommand(this.command);
101         script.setExecArgs(this.args);
102         script.setErrorStr(this.errorStr);
103         //script.setBuildProperties(buildProperties); - currently ignored
104

105         // mimic Ant target/task logging
106
Element task = script.setBuildLogHeader(buildLogElement);
107         script.setBuildLogElement(task);
108         
109         // execute the command
110
ScriptRunner scriptRunner = new ScriptRunner();
111         boolean scriptCompleted = false;
112         boolean scriptIOError = false;
113         try {
114             scriptCompleted = scriptRunner.runScript(new File JavaDoc(this.workingDir), script, timeout);
115         } catch (CruiseControlException ex) {
116           scriptIOError = true;
117         }
118
119         // set the time it took to exec command
120
long endTime = System.currentTimeMillis();
121         buildLogElement.setAttribute("time", DateUtil.getDurationAsString((endTime - startTime)));
122         
123         // did the exec fail in anyway?
124
if (scriptIOError) {
125             // YES: could fin or execute command
126
LOG.warn("Could not execute command: " + command + " " + args);
127             synchronized (buildLogElement) {
128                 buildLogElement.setAttribute("error", "exec error");
129                 Element msg = new Element("message");
130                 msg.addContent(new CDATA("Could not execute command: " + command + " " + args));
131                 msg.setAttribute("priority", "error");
132                 task.addContent(msg);
133             }
134         } else if (script.wasError()) {
135             // YES: detected the error string in the command output
136
synchronized (buildLogElement) {
137                 LOG.warn("Detected error string string in build output");
138                 buildLogElement.setAttribute("error", "error string found");
139                 Element msg = new Element("message");
140                 msg.addContent(new CDATA("Detected error string: " + errorStr));
141                 msg.setAttribute("priority", "error");
142                 task.addContent(msg);
143             }
144         } else if (!scriptCompleted) {
145             // YES: timeout was exceeded
146
LOG.warn("Build timeout timer of " + timeout + " seconds has expired");
147             synchronized (buildLogElement) {
148                 buildLogElement.setAttribute("error", "build timeout");
149             }
150         } else if (script.getExitCode() != 0) {
151             // YES: the command returned non-zero value
152
LOG.warn("Exec return code is " + script.getExitCode());
153             synchronized (buildLogElement) {
154                 buildLogElement.setAttribute("error", "return code is " + script.getExitCode());
155             }
156         }
157             
158         script.flushCurrentElement();
159         return buildLogElement;
160     } // build
161

162
163     public Element buildWithTarget(Map JavaDoc properties, String JavaDoc target) throws CruiseControlException {
164         String JavaDoc origArgs = args;
165         try {
166             args = target;
167             return build(properties);
168         } finally {
169             args = origArgs;
170         }
171     }
172     
173     /**
174      * Sets build timeout in seconds.
175      * @param timeout long build timeout
176      */

177     public void setTimeout(long timeout) {
178         this.timeout = timeout;
179     } // setTimeout
180

181     /**
182      * Sets the command to execute
183      * @param cmd the command to execute
184      */

185     public void setCommand(String JavaDoc cmd) {
186         this.command = cmd;
187     } // setCommand
188

189     /**
190      * Sets the arguments for the command to execute
191      * @param args arguments for the command to execute
192      */

193     public void setArgs(String JavaDoc args) {
194         this.args = args;
195     } // setArgs
196

197     /**
198      * Sets the error string to search for in the command output
199      * @param errStr the error string to search for in the command output
200      */

201     public void setErrorStr(String JavaDoc errStr) {
202         this.errorStr = errStr;
203     } // setErrorStr
204

205     /**
206      * Sets the working directory where the command is to be executed
207      * @param dir the directory where the command is to be executed
208      */

209     public void setWorkingDir(String JavaDoc dir) {
210         this.workingDir = dir;
211     } // setWorkingDir
212

213     /**
214      * Get whether there was n error written to the build log
215      * @return the error string otherwise null
216      */

217     public String JavaDoc getBuildError() {
218         if (this.buildLogElement.getAttribute("error") != null) {
219             return this.buildLogElement.getAttribute("error").getValue();
220         } else {
221             return "none";
222         }
223     } // getBuildError
224

225 } // ExecBuilder
226
Popular Tags