1 37 46 package net.sourceforge.cruisecontrol.sourcecontrols.accurev; 47 48 import net.sourceforge.cruisecontrol.CruiseControlException; 49 import net.sourceforge.cruisecontrol.sourcecontrols.Accurev; 50 import net.sourceforge.cruisecontrol.util.EnvCommandline; 51 import net.sourceforge.cruisecontrol.util.StreamPumper; 52 import org.apache.log4j.Logger; 53 54 import java.io.BufferedReader ; 55 import java.io.File ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.InputStreamReader ; 59 60 66 public class AccurevCommandline extends EnvCommandline implements AccurevInputParser, Runner { 67 private static final Logger LOG = Logger.getLogger(Accurev.class); 68 private boolean verbose; 69 private int runCount; 70 private int returnCode; 71 private AccurevCommand command; 72 private AccurevInputParser inputParser; 73 private static final int SUCCESS = 0; 74 private boolean syntaxError; 75 private Runner runner; 76 77 82 public AccurevCommandline(AccurevCommand command) { 83 super("accurev"); 84 this.inputParser = this; 85 this.runner = this; 86 this.command = command; 87 createArgument().setValue(command.toString()); 88 } 89 90 95 public void setStream(String stream) { 96 addOption("-s", stream); 97 } 98 99 104 public void setDepot(String depot) { 105 addOption("-d", depot); 106 } 107 108 113 public void setComment(String comment) { 114 addOption("-c", comment); 115 } 116 117 120 public void setInfoOnly() { 121 addArgument("-i"); 122 } 123 124 129 public void setTransactionRange(Timespec time) { 130 addOption("-t", time.toString()); 131 } 132 133 136 public void setTransactionRange(Timespec begin, Timespec end) { 137 StringBuffer buf = new StringBuffer (); 138 if (begin != null) { 139 buf.append(begin); 140 } 141 buf.append("-"); 142 if (end != null) { 143 buf.append(end); 144 } 145 addOption("-t", buf.toString()); 146 } 147 148 151 public void setFormatExpanded(char format) throws CruiseControlException { 152 if ("evstx".indexOf(format) < 0) { 153 throw new CruiseControlException( 154 "Invalid format specifier (use one of 'e' 'v' 's' 't' 'x') " + format); 155 } 156 addOption("-f", new String (new char[]{format})); 157 } 158 159 164 public void addArgument(String argument) { 165 createArgument().setValue(argument); 166 } 167 168 171 public void setInputParser(AccurevInputParser inputParser) { 172 this.inputParser = inputParser; 173 } 174 175 181 public void addOption(String option, String optionArgument) { 182 createArgument().setValue(option); 183 createArgument().setValue(optionArgument); 184 } 185 186 189 public void selectModified() { 190 addArgument("-m"); 191 } 192 193 198 public void setFileList(String filelistName) { 199 addOption("-l", filelistName); 200 } 201 202 208 public void setWorkspaceLocalPath(File workspace) throws CruiseControlException { 209 this.setWorkingDirectory(workspace.getAbsolutePath()); 210 } 211 212 218 public void setWorkspaceLocalPath(String workspace) throws CruiseControlException { 219 this.setWorkingDirectory(workspace); 220 } 221 222 225 public void run() { 226 if (verbose) { 227 LOG.info("Accurev: Executing '" + toString() + "'"); 228 } 229 this.syntaxError = runner.execute(inputParser); 230 this.returnCode = runner.getReturnCode(); 231 runCount++; 232 } 233 234 239 public boolean execute(AccurevInputParser inputParser) { 240 Process proc; 241 boolean error = false; 242 try { 243 proc = super.execute(); 244 StreamPumper errorPumper = new StreamPumper(proc.getErrorStream()); 245 new Thread (errorPumper).start(); 246 InputStream input = proc.getInputStream(); 247 try { 248 try { 249 try { 250 if (inputParser != null) { 251 error = !inputParser.parseStream(input); 252 } 253 returnCode = proc.waitFor(); 254 } finally { 255 proc.getInputStream().close(); 256 } 257 } finally { 258 proc.getOutputStream().close(); 259 } 260 } finally { 261 proc.getErrorStream().close(); 262 } 263 } catch (IOException e) { 264 LOG.error(e); 265 throw new RuntimeException (e.getMessage()); 266 } catch (CruiseControlException e) { 267 LOG.error(e); 268 throw new RuntimeException (e.getMessage()); 269 } catch (InterruptedException e) { 270 LOG.error(e); 271 throw new RuntimeException (e.getMessage()); 272 } 273 return error; 274 } 275 276 protected String [] buildCommandLine() { 277 return null; 278 } 279 280 285 public boolean parseStream(InputStream iStream) throws CruiseControlException { 286 BufferedReader reader = new BufferedReader (new InputStreamReader (iStream)); 287 boolean badSyntax = false; 288 try { 289 while (true) { 290 String line = reader.readLine(); 291 if (line == null) { 292 break; 293 } 294 if (line.startsWith("AccuRev was unable to understand your command.")) { 295 badSyntax = true; 296 } 297 if (verbose) { 298 LOG.info(line); 299 } 300 } 301 } catch (IOException ex) { 302 throw new CruiseControlException("Error reading input"); 303 } 304 return !badSyntax; 305 } 306 307 312 public int getReturnCode() { 313 return returnCode; 314 } 315 316 323 public AccurevCommand getCommand() { 324 return command; 325 } 326 327 332 public void setVerbose(boolean verbose) { 333 this.verbose = verbose; 334 } 335 336 341 public boolean isVerbose() { 342 return verbose; 343 } 344 345 351 public boolean isSuccess() { 352 return (runCount > 0) && (!syntaxError) && (returnCode == SUCCESS); 353 } 354 355 360 public void assertSuccess() throws CruiseControlException { 361 if (!isSuccess()) { 362 throw new CruiseControlException("Error running " + toString()); 363 } 364 } 365 366 372 public void setRunner(Runner runner) { 373 this.runner = runner; 374 } 375 } 376 | Popular Tags |