1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.StringTokenizer ; 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 58 public class ExecScript implements Script, StreamConsumer { 59 private static final Logger LOG = Logger.getLogger(ExecScript.class); 60 61 private String execCommand; 62 private String execArgs; 63 private String errorStr; 64 private int exitCode; 65 private boolean foundError = false; 66 private Element buildLogElement; 67 private Element currentElement = null; 68 69 74 public Commandline buildCommandline() throws CruiseControlException { 75 Commandline cmdLine = new Commandline(); 76 77 if (execCommand != null) { 79 cmdLine.setExecutable(execCommand); 80 } else { 81 throw new CruiseControlException("no command to be executed"); 82 } 83 84 if (execArgs != null) { 86 StringTokenizer stok = new StringTokenizer (execArgs, " \t\r\n"); 87 while (stok.hasMoreTokens()) { 88 cmdLine.createArgument().setValue(stok.nextToken()); 89 } 90 } 91 92 if (LOG.isDebugEnabled()) { 94 StringBuffer sb = new StringBuffer (); 95 sb.append("Executing Command: "); 96 String [] args = cmdLine.getCommandline(); 97 for (int i = 0; i < args.length; i++) { 98 String arg = args[i]; 99 sb.append(arg); 100 sb.append(" "); 101 } 102 LOG.debug(sb.toString()); 103 } 104 return cmdLine; 105 } 107 112 public synchronized void consumeLine(String line) { 113 if (line == null || line.length() == 0 || buildLogElement == null) { 114 return; 115 } 116 117 synchronized (buildLogElement) { 118 if (errorStr != null) { 120 if (line.indexOf(errorStr) >= 0) { 122 foundError = true; 123 } 124 } else { 125 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 } 138 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 List lst = currentElement.getChildren("message"); 151 if (lst != null) { 152 Iterator 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 } 165 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 } 181 182 185 public void setExecArgs(String execArgs) { 186 this.execArgs = execArgs; 187 } 189 192 public void setExecCommand(String execCommand) { 193 this.execCommand = execCommand; 194 } 196 199 public int getExitCode() { 200 return exitCode; 201 } 203 206 public void setExitCode(int exitCode) { 207 this.exitCode = exitCode; 208 } 210 213 public void setErrorStr(String errStr) { 214 this.errorStr = errStr; 215 } 217 220 public void setBuildLogElement(Element buildLogElement) { 221 this.buildLogElement = buildLogElement; 222 } 224 227 public boolean wasError() { 228 return this.foundError; 229 } 231 } | Popular Tags |