1 18 19 package org.apache.tools.ant.taskdefs.optional.vss; 20 21 import org.apache.tools.ant.types.EnumeratedAttribute; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.text.DateFormat ; 25 import java.text.ParseException ; 26 import java.util.Calendar ; 27 import java.util.Date ; 28 import java.util.GregorianCalendar ; 29 30 import org.apache.tools.ant.BuildException; 31 import org.apache.tools.ant.Project; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.taskdefs.Execute; 34 import org.apache.tools.ant.taskdefs.LogStreamHandler; 35 import org.apache.tools.ant.types.Commandline; 36 import org.apache.tools.ant.util.FileUtils; 37 38 51 public abstract class MSVSS extends Task implements MSVSSConstants { 52 53 private String ssDir = null; 54 private String vssLogin = null; 55 private String vssPath = null; 56 private String serverPath = null; 57 58 59 private String version = null; 60 61 private String date = null; 62 63 private String label = null; 64 65 private String autoResponse = null; 66 67 private String localPath = null; 68 69 private String comment = null; 70 71 private String fromLabel = null; 72 73 private String toLabel = null; 74 75 private String outputFileName = null; 76 77 private String user = null; 78 79 private String fromDate = null; 80 81 private String toDate = null; 82 83 private String style = null; 84 85 private boolean quiet = false; 86 87 private boolean recursive = false; 88 89 private boolean writable = false; 90 91 private boolean failOnError = true; 92 93 private boolean getLocalCopy = true; 94 95 private int numDays = Integer.MIN_VALUE; 96 97 private DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); 98 99 private CurrentModUpdated timestamp = null; 100 101 private WritableFiles writableFiles = null; 102 103 109 abstract Commandline buildCmdLine(); 110 111 116 public final void setSsdir(String dir) { 117 this.ssDir = FileUtils.translatePath(dir); 118 } 119 120 127 public final void setLogin(final String vssLogin) { 128 this.vssLogin = vssLogin; 129 } 130 131 139 public final void setVsspath(final String vssPath) { 140 String projectPath; 141 if (vssPath.startsWith("vss://")) { projectPath = vssPath.substring(5); 143 } else { 144 projectPath = vssPath; 145 } 146 147 if (projectPath.startsWith(PROJECT_PREFIX)) { 148 this.vssPath = projectPath; 149 } else { 150 this.vssPath = PROJECT_PREFIX + projectPath; 151 } 152 } 153 154 158 public final void setServerpath(final String serverPath) { 159 this.serverPath = serverPath; 160 } 161 162 166 public final void setFailOnError(final boolean failOnError) { 167 this.failOnError = failOnError; 168 } 169 170 176 public void execute() throws BuildException { 177 int result = 0; 178 Commandline commandLine = buildCmdLine(); 179 result = run(commandLine); 180 if (Execute.isFailure(result) && getFailOnError()) { 181 String msg = "Failed executing: " + formatCommandLine(commandLine) 182 + " With a return code of " + result; 183 throw new BuildException(msg, getLocation()); 184 } 185 } 186 187 189 193 protected void setInternalComment(final String comment) { 194 this.comment = comment; 195 } 196 197 201 protected void setInternalAutoResponse(final String autoResponse) { 202 this.autoResponse = autoResponse; 203 } 204 205 209 protected void setInternalDate(final String date) { 210 this.date = date; 211 } 212 213 217 protected void setInternalDateFormat(final DateFormat dateFormat) { 218 this.dateFormat = dateFormat; 219 } 220 221 225 protected void setInternalFailOnError(final boolean failOnError) { 226 this.failOnError = failOnError; 227 } 228 229 233 protected void setInternalFromDate(final String fromDate) { 234 this.fromDate = fromDate; 235 } 236 237 241 protected void setInternalFromLabel(final String fromLabel) { 242 this.fromLabel = fromLabel; 243 } 244 245 249 protected void setInternalLabel(final String label) { 250 this.label = label; 251 } 252 253 257 protected void setInternalLocalPath(final String localPath) { 258 this.localPath = localPath; 259 } 260 261 265 protected void setInternalNumDays(final int numDays) { 266 this.numDays = numDays; 267 } 268 269 273 protected void setInternalOutputFilename(final String outputFileName) { 274 this.outputFileName = outputFileName; 275 } 276 277 281 protected void setInternalQuiet(final boolean quiet) { 282 this.quiet = quiet; 283 } 284 285 289 protected void setInternalRecursive(final boolean recursive) { 290 this.recursive = recursive; 291 } 292 293 297 protected void setInternalStyle(final String style) { 298 this.style = style; 299 } 300 301 305 protected void setInternalToDate(final String toDate) { 306 this.toDate = toDate; 307 } 308 309 313 protected void setInternalToLabel(final String toLabel) { 314 this.toLabel = toLabel; 315 } 316 317 321 protected void setInternalUser(final String user) { 322 this.user = user; 323 } 324 325 329 protected void setInternalVersion(final String version) { 330 this.version = version; 331 } 332 333 337 protected void setInternalWritable(final boolean writable) { 338 this.writable = writable; 339 } 340 341 345 protected void setInternalFileTimeStamp(final CurrentModUpdated timestamp) { 346 this.timestamp = timestamp; 347 } 348 349 353 protected void setInternalWritableFiles(final WritableFiles writableFiles) { 354 this.writableFiles = writableFiles; 355 } 356 357 361 protected void setInternalGetLocalCopy(final boolean getLocalCopy) { 362 this.getLocalCopy = getLocalCopy; 363 } 364 365 369 protected String getSSCommand() { 370 if (ssDir == null) { 371 return SS_EXE; 372 } 373 return ssDir.endsWith(File.separator) ? ssDir + SS_EXE : ssDir 374 + File.separator + SS_EXE; 375 } 376 377 381 protected String getVsspath() { 382 return vssPath; 383 } 384 385 389 protected String getQuiet() { 390 return quiet ? FLAG_QUIET : ""; 391 } 392 393 397 protected String getRecursive() { 398 return recursive ? FLAG_RECURSION : ""; 399 } 400 401 405 protected String getWritable() { 406 return writable ? FLAG_WRITABLE : ""; 407 } 408 409 414 protected String getLabel() { 415 String shortLabel = ""; 416 if (label != null && label.length() > 0) { 417 shortLabel = FLAG_LABEL + getShortLabel(); 418 } 419 return shortLabel; 420 } 421 426 private String getShortLabel() { 427 String shortLabel; 428 if (label != null && label.length() > 31) { 429 shortLabel = this.label.substring(0, 30); 430 log("Label is longer than 31 characters, truncated to: " + shortLabel, 431 Project.MSG_WARN); 432 } else { 433 shortLabel = label; 434 } 435 return shortLabel; 436 } 437 441 protected String getStyle() { 442 return style != null ? style : ""; 443 } 444 445 450 protected String getVersionDateLabel() { 451 String versionDateLabel = ""; 452 if (version != null) { 453 versionDateLabel = FLAG_VERSION + version; 454 } else if (date != null) { 455 versionDateLabel = FLAG_VERSION_DATE + date; 456 } else { 457 String shortLabel = getShortLabel(); 460 if (shortLabel != null && !shortLabel.equals("")) { 461 versionDateLabel = FLAG_VERSION_LABEL + shortLabel; 462 } 463 } 464 return versionDateLabel; 465 } 466 467 471 protected String getVersion() { 472 return version != null ? FLAG_VERSION + version : ""; 473 } 474 475 480 protected String getLocalpath() { 481 String lclPath = ""; if (localPath != null) { 483 File dir = getProject().resolveFile(localPath); 485 if (!dir.exists()) { 486 boolean done = dir.mkdirs(); 487 if (!done) { 488 String msg = "Directory " + localPath + " creation was not " 489 + "successful for an unknown reason"; 490 throw new BuildException(msg, getLocation()); 491 } 492 getProject().log("Created dir: " + dir.getAbsolutePath()); 493 } 494 lclPath = FLAG_OVERRIDE_WORKING_DIR + localPath; 495 } 496 return lclPath; 497 } 498 499 503 protected String getComment() { 504 return comment != null ? FLAG_COMMENT + comment : FLAG_COMMENT + "-"; 505 } 506 507 511 protected String getAutoresponse() { 512 if (autoResponse == null) { 513 return FLAG_AUTORESPONSE_DEF; 514 } else if (autoResponse.equalsIgnoreCase("Y")) { 515 return FLAG_AUTORESPONSE_YES; 516 } else if (autoResponse.equalsIgnoreCase("N")) { 517 return FLAG_AUTORESPONSE_NO; 518 } else { 519 return FLAG_AUTORESPONSE_DEF; 520 } 521 } 522 523 528 protected String getLogin() { 529 return vssLogin != null ? FLAG_LOGIN + vssLogin : ""; 530 } 531 532 536 protected String getOutput() { 537 return outputFileName != null ? FLAG_OUTPUT + outputFileName : ""; 538 } 539 540 544 protected String getUser() { 545 return user != null ? FLAG_USER + user : ""; 546 } 547 548 553 protected String getVersionLabel() { 554 if (fromLabel == null && toLabel == null) { 555 return ""; 556 } 557 if (fromLabel != null && toLabel != null) { 558 if (fromLabel.length() > 31) { 559 fromLabel = fromLabel.substring(0, 30); 560 log("FromLabel is longer than 31 characters, truncated to: " 561 + fromLabel, Project.MSG_WARN); 562 } 563 if (toLabel.length() > 31) { 564 toLabel = toLabel.substring(0, 30); 565 log("ToLabel is longer than 31 characters, truncated to: " 566 + toLabel, Project.MSG_WARN); 567 } 568 return FLAG_VERSION_LABEL + toLabel + VALUE_FROMLABEL + fromLabel; 569 } else if (fromLabel != null) { 570 if (fromLabel.length() > 31) { 571 fromLabel = fromLabel.substring(0, 30); 572 log("FromLabel is longer than 31 characters, truncated to: " 573 + fromLabel, Project.MSG_WARN); 574 } 575 return FLAG_VERSION + VALUE_FROMLABEL + fromLabel; 576 } else { 577 if (toLabel.length() > 31) { 578 toLabel = toLabel.substring(0, 30); 579 log("ToLabel is longer than 31 characters, truncated to: " 580 + toLabel, Project.MSG_WARN); 581 } 582 return FLAG_VERSION_LABEL + toLabel; 583 } 584 } 585 586 591 protected String getVersionDate() throws BuildException { 592 if (fromDate == null && toDate == null 593 && numDays == Integer.MIN_VALUE) { 594 return ""; 595 } 596 if (fromDate != null && toDate != null) { 597 return FLAG_VERSION_DATE + toDate + VALUE_FROMDATE + fromDate; 598 } else if (toDate != null && numDays != Integer.MIN_VALUE) { 599 try { 600 return FLAG_VERSION_DATE + toDate + VALUE_FROMDATE 601 + calcDate(toDate, numDays); 602 } catch (ParseException ex) { 603 String msg = "Error parsing date: " + toDate; 604 throw new BuildException(msg, getLocation()); 605 } 606 } else if (fromDate != null && numDays != Integer.MIN_VALUE) { 607 try { 608 return FLAG_VERSION_DATE + calcDate(fromDate, numDays) 609 + VALUE_FROMDATE + fromDate; 610 } catch (ParseException ex) { 611 String msg = "Error parsing date: " + fromDate; 612 throw new BuildException(msg, getLocation()); 613 } 614 } else { 615 return fromDate != null ? FLAG_VERSION + VALUE_FROMDATE 616 + fromDate : FLAG_VERSION_DATE + toDate; 617 } 618 } 619 620 624 protected String getGetLocalCopy() { 625 return (!getLocalCopy) ? FLAG_NO_GET : ""; 626 } 627 628 632 private boolean getFailOnError() { 633 return getWritableFiles().equals(WRITABLE_SKIP) ? false : failOnError; 634 } 635 636 637 646 public String getFileTimeStamp() { 647 if (timestamp == null) { 648 return ""; 649 } else if (timestamp.getValue().equals(TIME_MODIFIED)) { 650 return FLAG_FILETIME_MODIFIED; 651 } else if (timestamp.getValue().equals(TIME_UPDATED)) { 652 return FLAG_FILETIME_UPDATED; 653 } else { 654 return FLAG_FILETIME_DEF; 655 } 656 } 657 658 659 663 public String getWritableFiles() { 664 if (writableFiles == null) { 665 return ""; 666 } else if (writableFiles.getValue().equals(WRITABLE_REPLACE)) { 667 return FLAG_REPLACE_WRITABLE; 668 } else if (writableFiles.getValue().equals(WRITABLE_SKIP)) { 669 failOnError = false; 672 return FLAG_SKIP_WRITABLE; 673 } else { 674 return ""; 675 } 676 } 677 678 684 private int run(Commandline cmd) { 685 try { 686 Execute exe = new Execute(new LogStreamHandler(this, 687 Project.MSG_INFO, 688 Project.MSG_WARN)); 689 690 if (serverPath != null) { 693 String [] env = exe.getEnvironment(); 694 if (env == null) { 695 env = new String [0]; 696 } 697 String [] newEnv = new String [env.length + 1]; 698 System.arraycopy(env, 0, newEnv, 0, env.length); 699 newEnv[env.length] = "SSDIR=" + serverPath; 700 701 exe.setEnvironment(newEnv); 702 } 703 704 exe.setAntRun(getProject()); 705 exe.setWorkingDirectory(getProject().getBaseDir()); 706 exe.setCommandline(cmd.getCommandline()); 707 exe.setVMLauncher(false); 709 return exe.execute(); 710 } catch (IOException e) { 711 throw new BuildException(e, getLocation()); 712 } 713 } 714 715 724 private String calcDate(String startDate, int daysToAdd) throws ParseException { 725 Calendar calendar = new GregorianCalendar (); 726 Date currentDate = dateFormat.parse(startDate); 727 calendar.setTime(currentDate); 728 calendar.add(Calendar.DATE, daysToAdd); 729 return dateFormat.format(calendar.getTime()); 730 } 731 732 738 private String formatCommandLine(Commandline cmd) { 739 StringBuffer sBuff = new StringBuffer (cmd.toString()); 740 int indexUser = sBuff.substring(0).indexOf(FLAG_LOGIN); 741 if (indexUser > 0) { 742 int indexPass = sBuff.substring(0).indexOf(",", indexUser); 743 int indexAfterPass = sBuff.substring(0).indexOf(" ", indexPass); 744 745 for (int i = indexPass + 1; i < indexAfterPass; i++) { 746 sBuff.setCharAt(i, '*'); 747 } 748 } 749 return sBuff.toString(); 750 } 751 752 755 public static class CurrentModUpdated extends EnumeratedAttribute { 756 760 public String [] getValues() { 761 return new String [] {TIME_CURRENT, TIME_MODIFIED, TIME_UPDATED}; 762 } 763 } 764 765 768 public static class WritableFiles extends EnumeratedAttribute { 769 773 public String [] getValues() { 774 return new String [] {WRITABLE_REPLACE, WRITABLE_SKIP, WRITABLE_FAIL}; 775 } 776 } 777 } 778 | Popular Tags |