1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.util.Map ; 40 import java.io.File ; 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 60 public class ExecBuilder extends Builder { 61 62 private static final Logger LOG = Logger.getLogger(ExecBuilder.class); 63 64 private String command; 65 private String args; 66 private String workingDir; 67 private String errorStr; 68 private long timeout = ScriptRunner.NO_TIMEOUT; 69 private Element buildLogElement = null; 71 74 public void validate() throws CruiseControlException { 75 super.validate(); 76 77 ValidationHelper.assertIsSet(command, "command", this.getClass()); 79 80 if (this.workingDir == null) { 82 this.workingDir = System.getProperty("java.io.tmpdir"); 84 } 85 86 } 88 91 public Element build(Map buildProperties) throws CruiseControlException { 92 93 long startTime = System.currentTimeMillis(); 95 96 buildLogElement = new Element("build"); 97 98 ExecScript script = new ExecScript(); 100 script.setExecCommand(this.command); 101 script.setExecArgs(this.args); 102 script.setErrorStr(this.errorStr); 103 105 Element task = script.setBuildLogHeader(buildLogElement); 107 script.setBuildLogElement(task); 108 109 ScriptRunner scriptRunner = new ScriptRunner(); 111 boolean scriptCompleted = false; 112 boolean scriptIOError = false; 113 try { 114 scriptCompleted = scriptRunner.runScript(new File (this.workingDir), script, timeout); 115 } catch (CruiseControlException ex) { 116 scriptIOError = true; 117 } 118 119 long endTime = System.currentTimeMillis(); 121 buildLogElement.setAttribute("time", DateUtil.getDurationAsString((endTime - startTime))); 122 123 if (scriptIOError) { 125 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 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 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 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 } 162 163 public Element buildWithTarget(Map properties, String target) throws CruiseControlException { 164 String origArgs = args; 165 try { 166 args = target; 167 return build(properties); 168 } finally { 169 args = origArgs; 170 } 171 } 172 173 177 public void setTimeout(long timeout) { 178 this.timeout = timeout; 179 } 181 185 public void setCommand(String cmd) { 186 this.command = cmd; 187 } 189 193 public void setArgs(String args) { 194 this.args = args; 195 } 197 201 public void setErrorStr(String errStr) { 202 this.errorStr = errStr; 203 } 205 209 public void setWorkingDir(String dir) { 210 this.workingDir = dir; 211 } 213 217 public String getBuildError() { 218 if (this.buildLogElement.getAttribute("error") != null) { 219 return this.buildLogElement.getAttribute("error").getValue(); 220 } else { 221 return "none"; 222 } 223 } 225 } | Popular Tags |