1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.BufferedOutputStream ; 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.io.PrintStream ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.types.Commandline; 32 import org.apache.tools.ant.types.Environment; 33 import org.apache.tools.ant.util.StringUtils; 34 import org.apache.tools.ant.util.FileUtils; 35 36 45 public abstract class AbstractCvsTask extends Task { 46 50 public static final int DEFAULT_COMPRESSION_LEVEL = 3; 51 private static final int MAXIMUM_COMRESSION_LEVEL = 9; 52 53 private Commandline cmd = new Commandline(); 54 55 56 private Vector vecCommandlines = new Vector (); 57 58 61 private String cvsRoot; 62 63 66 private String cvsRsh; 67 68 71 private String cvsPackage; 72 75 private String tag; 76 79 private static final String DEFAULT_COMMAND = "checkout"; 80 83 private String command = null; 84 85 88 private boolean quiet = false; 89 90 93 private boolean reallyquiet = false; 94 95 98 private int compression = 0; 99 100 103 private boolean noexec = false; 104 105 108 private int port = 0; 109 110 113 private File passFile = null; 114 115 118 private File dest; 119 120 121 private boolean append = false; 122 123 126 private File output; 127 128 131 private File error; 132 133 137 private boolean failOnError = false; 138 139 143 private ExecuteStreamHandler executeStreamHandler; 144 private OutputStream outputStream; 145 private OutputStream errorStream; 146 147 148 public AbstractCvsTask() { 149 super(); 150 } 151 152 156 public void setExecuteStreamHandler(ExecuteStreamHandler handler) { 157 this.executeStreamHandler = handler; 158 } 159 160 164 protected ExecuteStreamHandler getExecuteStreamHandler() { 165 166 if (this.executeStreamHandler == null) { 167 setExecuteStreamHandler(new PumpStreamHandler(getOutputStream(), 168 getErrorStream())); 169 } 170 171 return this.executeStreamHandler; 172 } 173 174 178 protected void setOutputStream(OutputStream outputStream) { 179 180 this.outputStream = outputStream; 181 } 182 183 191 protected OutputStream getOutputStream() { 192 193 if (this.outputStream == null) { 194 195 if (output != null) { 196 try { 197 setOutputStream(new PrintStream ( 198 new BufferedOutputStream ( 199 new FileOutputStream (output 200 .getPath(), 201 append)))); 202 } catch (IOException e) { 203 throw new BuildException(e, getLocation()); 204 } 205 } else { 206 setOutputStream(new LogOutputStream(this, Project.MSG_INFO)); 207 } 208 } 209 210 return this.outputStream; 211 } 212 213 217 protected void setErrorStream(OutputStream errorStream) { 218 219 this.errorStream = errorStream; 220 } 221 222 230 protected OutputStream getErrorStream() { 231 232 if (this.errorStream == null) { 233 234 if (error != null) { 235 236 try { 237 setErrorStream(new PrintStream ( 238 new BufferedOutputStream ( 239 new FileOutputStream (error.getPath(), 240 append)))); 241 } catch (IOException e) { 242 throw new BuildException(e, getLocation()); 243 } 244 } else { 245 setErrorStream(new LogOutputStream(this, Project.MSG_WARN)); 246 } 247 } 248 249 return this.errorStream; 250 } 251 252 257 protected void runCommand(Commandline toExecute) throws BuildException { 258 262 265 Environment env = new Environment(); 266 267 if (port > 0) { 268 Environment.Variable var = new Environment.Variable(); 269 var.setKey("CVS_CLIENT_PORT"); 270 var.setValue(String.valueOf(port)); 271 env.addVariable(var); 272 } 273 274 278 if (passFile == null) { 279 280 File defaultPassFile = new File ( 281 System.getProperty("cygwin.user.home", 282 System.getProperty("user.home")) 283 + File.separatorChar + ".cvspass"); 284 285 if (defaultPassFile.exists()) { 286 this.setPassfile(defaultPassFile); 287 } 288 } 289 290 if (passFile != null) { 291 if (passFile.isFile() && passFile.canRead()) { 292 Environment.Variable var = new Environment.Variable(); 293 var.setKey("CVS_PASSFILE"); 294 var.setValue(String.valueOf(passFile)); 295 env.addVariable(var); 296 log("Using cvs passfile: " + String.valueOf(passFile), 297 Project.MSG_VERBOSE); 298 } else if (!passFile.canRead()) { 299 log("cvs passfile: " + String.valueOf(passFile) 300 + " ignored as it is not readable", 301 Project.MSG_WARN); 302 } else { 303 log("cvs passfile: " + String.valueOf(passFile) 304 + " ignored as it is not a file", 305 Project.MSG_WARN); 306 } 307 } 308 309 if (cvsRsh != null) { 310 Environment.Variable var = new Environment.Variable(); 311 var.setKey("CVS_RSH"); 312 var.setValue(String.valueOf(cvsRsh)); 313 env.addVariable(var); 314 } 315 316 Execute exe = new Execute(getExecuteStreamHandler(), null); 321 322 exe.setAntRun(getProject()); 323 if (dest == null) { 324 dest = getProject().getBaseDir(); 325 } 326 327 if (!dest.exists()) { 328 dest.mkdirs(); 329 } 330 331 exe.setWorkingDirectory(dest); 332 exe.setCommandline(toExecute.getCommandline()); 333 exe.setEnvironment(env.getVariables()); 334 335 try { 336 String actualCommandLine = executeToString(exe); 337 log(actualCommandLine, Project.MSG_VERBOSE); 338 int retCode = exe.execute(); 339 log("retCode=" + retCode, Project.MSG_DEBUG); 340 341 if (failOnError && Execute.isFailure(retCode)) { 342 throw new BuildException("cvs exited with error code " 343 + retCode 344 + StringUtils.LINE_SEP 345 + "Command line was [" 346 + actualCommandLine + "]", 347 getLocation()); 348 } 349 } catch (IOException e) { 350 if (failOnError) { 351 throw new BuildException(e, getLocation()); 352 } 353 log("Caught exception: " + e.getMessage(), Project.MSG_WARN); 354 } catch (BuildException e) { 355 if (failOnError) { 356 throw(e); 357 } 358 Throwable t = e.getException(); 359 if (t == null) { 360 t = e; 361 } 362 log("Caught exception: " + t.getMessage(), Project.MSG_WARN); 363 } catch (Exception e) { 364 if (failOnError) { 365 throw new BuildException(e, getLocation()); 366 } 367 log("Caught exception: " + e.getMessage(), Project.MSG_WARN); 368 } 369 } 370 371 376 public void execute() throws BuildException { 377 378 String savedCommand = getCommand(); 379 380 if (this.getCommand() == null && vecCommandlines.size() == 0) { 381 this.setCommand(AbstractCvsTask.DEFAULT_COMMAND); 383 } 384 385 String c = this.getCommand(); 386 Commandline cloned = null; 387 if (c != null) { 388 cloned = (Commandline) cmd.clone(); 389 cloned.createArgument(true).setLine(c); 390 this.addConfiguredCommandline(cloned, true); 391 } 392 393 try { 394 for (int i = 0; i < vecCommandlines.size(); i++) { 395 this.runCommand((Commandline) vecCommandlines.elementAt(i)); 396 } 397 } finally { 398 if (cloned != null) { 399 removeCommandline(cloned); 400 } 401 setCommand(savedCommand); 402 FileUtils.close(outputStream); 403 FileUtils.close(errorStream); 404 } 405 } 406 407 private String executeToString(Execute execute) { 408 409 StringBuffer stringBuffer = 410 new StringBuffer (Commandline.describeCommand(execute 411 .getCommandline())); 412 413 String newLine = StringUtils.LINE_SEP; 414 String [] variableArray = execute.getEnvironment(); 415 416 if (variableArray != null) { 417 stringBuffer.append(newLine); 418 stringBuffer.append(newLine); 419 stringBuffer.append("environment:"); 420 stringBuffer.append(newLine); 421 for (int z = 0; z < variableArray.length; z++) { 422 stringBuffer.append(newLine); 423 stringBuffer.append("\t"); 424 stringBuffer.append(variableArray[z]); 425 } 426 } 427 428 return stringBuffer.toString(); 429 } 430 431 436 public void setCvsRoot(String root) { 437 438 if (root != null) { 440 if (root.trim().equals("")) { 441 root = null; 442 } 443 } 444 445 this.cvsRoot = root; 446 } 447 448 452 public String getCvsRoot() { 453 454 return this.cvsRoot; 455 } 456 457 462 public void setCvsRsh(String rsh) { 463 if (rsh != null) { 465 if (rsh.trim().equals("")) { 466 rsh = null; 467 } 468 } 469 470 this.cvsRsh = rsh; 471 } 472 473 477 public String getCvsRsh() { 478 479 return this.cvsRsh; 480 } 481 482 487 public void setPort(int port) { 488 this.port = port; 489 } 490 491 495 public int getPort() { 496 497 return this.port; 498 } 499 500 505 public void setPassfile(File passFile) { 506 this.passFile = passFile; 507 } 508 509 513 public File getPassFile() { 514 515 return this.passFile; 516 } 517 518 527 public void setDest(File dest) { 528 this.dest = dest; 529 } 530 531 536 public File getDest() { 537 538 return this.dest; 539 } 540 541 546 public void setPackage(String p) { 547 this.cvsPackage = p; 548 } 549 550 555 public String getPackage() { 556 557 return this.cvsPackage; 558 } 559 564 public String getTag() { 565 return tag; 566 } 567 568 572 public void setTag(String p) { 573 if (p != null && p.trim().length() > 0) { 575 tag = p; 576 addCommandArgument("-r" + p); 577 } 578 } 579 580 585 public void addCommandArgument(String arg) { 586 this.addCommandArgument(cmd, arg); 587 } 588 589 599 public void addCommandArgument(Commandline c, String arg) { 600 c.createArgument().setValue(arg); 601 } 602 603 604 609 public void setDate(String p) { 610 if (p != null && p.trim().length() > 0) { 611 addCommandArgument("-D"); 612 addCommandArgument(p); 613 } 614 } 615 616 624 public void setCommand(String c) { 625 this.command = c; 626 } 627 635 public String getCommand() { 636 return this.command; 637 } 638 639 643 public void setQuiet(boolean q) { 644 quiet = q; 645 } 646 647 652 public void setReallyquiet(boolean q) { 653 reallyquiet = q; 654 } 655 656 657 662 public void setNoexec(boolean ne) { 663 noexec = ne; 664 } 665 666 670 public void setOutput(File output) { 671 this.output = output; 672 } 673 674 679 public void setError(File error) { 680 this.error = error; 681 } 682 683 687 public void setAppend(boolean value) { 688 this.append = value; 689 } 690 691 698 public void setFailOnError(boolean failOnError) { 699 this.failOnError = failOnError; 700 } 701 702 721 protected void configureCommandline(Commandline c) { 722 if (c == null) { 723 return; 724 } 725 c.setExecutable("cvs"); 726 if (cvsPackage != null) { 727 c.createArgument().setLine(cvsPackage); 728 } 729 if (this.compression > 0 730 && this.compression <= MAXIMUM_COMRESSION_LEVEL) { 731 c.createArgument(true).setValue("-z" + this.compression); 732 } 733 if (quiet && !reallyquiet) { 734 c.createArgument(true).setValue("-q"); 735 } 736 if (reallyquiet) { 737 c.createArgument(true).setValue("-Q"); 738 } 739 if (noexec) { 740 c.createArgument(true).setValue("-n"); 741 } 742 if (cvsRoot != null) { 743 c.createArgument(true).setLine("-d" + cvsRoot); 744 } 745 } 746 747 751 protected void removeCommandline(Commandline c) { 752 vecCommandlines.removeElement(c); 753 } 754 755 759 public void addConfiguredCommandline(Commandline c) { 760 this.addConfiguredCommandline(c, false); 761 } 762 763 769 public void addConfiguredCommandline(Commandline c, 770 boolean insertAtStart) { 771 if (c == null) { 772 return; 773 } 774 this.configureCommandline(c); 775 if (insertAtStart) { 776 vecCommandlines.insertElementAt(c, 0); 777 } else { 778 vecCommandlines.addElement(c); 779 } 780 } 781 782 787 public void setCompressionLevel(int level) { 788 this.compression = level; 789 } 790 791 797 public void setCompression(boolean usecomp) { 798 setCompressionLevel(usecomp 799 ? AbstractCvsTask.DEFAULT_COMPRESSION_LEVEL : 0); 800 } 801 802 } 803 | Popular Tags |