1 18 package org.apache.tools.ant.taskdefs.optional.starteam; 19 20 import com.starbase.starteam.Folder; 21 import com.starbase.starteam.Label; 22 import com.starbase.starteam.PropertyNames; 23 import com.starbase.starteam.StarTeamFinder; 24 import com.starbase.starteam.View; 25 import com.starbase.starteam.ViewConfiguration; 26 import com.starbase.util.OLEDate; 27 import java.text.ParseException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.Hashtable ; 31 import java.util.StringTokenizer ; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.util.DateUtils; 34 import org.apache.tools.ant.DirectoryScanner; 35 import org.apache.tools.ant.Project; 36 37 53 54 public abstract class TreeBasedTask extends StarTeamTask { 55 56 57 67 public static final String DEFAULT_INCLUDESETTING = "*"; 68 69 77 public static final String DEFAULT_EXCLUDESETTING = null; 78 79 81 84 private String rootStarteamFolder = "/"; 85 86 90 private String rootLocalFolder = null; 91 92 95 private String includes = DEFAULT_INCLUDESETTING; 96 97 100 private String excludes = DEFAULT_EXCLUDESETTING; 101 102 105 private String label = null; 106 107 111 private boolean recursive = true; 112 113 117 private boolean preloadFileInformation = true; 118 119 126 private boolean forced = false; 127 128 private Label labelInUse = null; 129 130 133 private String asOfDate = null; 134 135 138 private String asOfDateFormat = null; 139 140 141 142 147 152 public void setRootStarteamFolder(String rootStarteamFolder) { 153 this.rootStarteamFolder = rootStarteamFolder; 154 } 155 156 161 public String getRootStarteamFolder() { 162 return this.rootStarteamFolder; 163 } 164 165 175 public void setRootLocalFolder(String rootLocalFolder) { 176 this.rootLocalFolder = rootLocalFolder; 177 } 178 179 180 181 188 public String getRootLocalFolder() { 189 return this.rootLocalFolder; 190 } 191 192 193 201 public void setIncludes(String includes) { 202 this.includes = includes; 203 } 204 205 215 public String getIncludes() { 216 return includes; 217 } 218 219 222 protected void logIncludes() { 223 if (DEFAULT_INCLUDESETTING != this.includes) { 224 log(" Includes specified: " + this.includes); 225 } 226 } 227 228 262 public void setExcludes(String excludes) { 263 this.excludes = excludes; 264 } 265 266 276 public String getExcludes() { 277 return excludes; 278 } 279 280 283 protected void logExcludes() { 284 if (DEFAULT_EXCLUDESETTING != this.excludes) { 285 log(" Excludes specified: " + this.excludes); 286 } 287 } 288 289 291 297 protected void _setLabel(String label) { 298 if (null != label) { 299 label = label.trim(); 300 if (label.length() > 0) { 301 this.label = label; 302 } 303 } 304 } 305 306 314 protected void _setAsOfDate(String asOfDate) { 315 if (asOfDate != null && asOfDate.length() > 0) { 316 this.asOfDate = asOfDate; 317 } 318 } 319 320 328 protected void _setAsOfDateFormat(String asOfDateFormat) { 329 if (asOfDateFormat != null && asOfDateFormat.length() > 0) { 330 this.asOfDateFormat = asOfDateFormat; 331 } 332 } 333 334 336 337 344 protected String getAsOfDate() { 345 return this.asOfDate; 346 } 347 348 365 protected View getViewConfiguredByDate(View raw) throws BuildException { 366 if (this.asOfDate == null) { 367 return null; 368 } 369 Date asOfDate = null; 370 SimpleDateFormat fmt = null; 371 if (this.asOfDateFormat != null) { 372 fmt = new SimpleDateFormat (this.asOfDateFormat); 373 try { 374 asOfDate = fmt.parse(this.asOfDate); 375 } catch (ParseException px) { 376 throw new BuildException("AsOfDate " 377 + this.asOfDate 378 + " not parsable by supplied format " 379 + this.asOfDateFormat); 380 } 381 } else { 382 try { 383 asOfDate = DateUtils.parseIso8601DateTimeOrDate( 384 this.asOfDate); 385 } catch (ParseException px) { 386 throw new BuildException("AsOfDate " 387 + this.asOfDate 388 + " not parsable by default" 389 + " ISO8601 formats"); 390 } 391 } 392 return new View(raw, ViewConfiguration.createFromTime( 393 new OLEDate(asOfDate))); 394 } 395 396 401 protected String getLabel() { 402 return this.label; 403 } 404 405 409 public boolean isRecursive() { 410 return this.recursive; 411 } 412 413 418 public void setRecursive(boolean v) { 419 this.recursive = v; 420 } 421 422 426 public boolean isPreloadFileInformation() { 427 return this.preloadFileInformation; 428 } 429 430 437 public void setPreloadFileInformation(boolean v) { 438 this.preloadFileInformation = v; 439 } 440 441 445 public boolean isForced() { 446 return this.forced; 447 } 448 449 457 public void setForced(boolean v) { 458 this.forced = v; 459 } 460 461 466 protected boolean isUsingViewLabel() { 467 return null != this.labelInUse && this.labelInUse.isViewLabel(); 468 } 469 470 475 protected boolean isUsingRevisionLabel() { 476 return null != this.labelInUse && this.labelInUse.isRevisionLabel(); 477 } 478 479 484 protected Label getLabelInUse() { 485 return this.labelInUse; 486 } 487 488 491 protected void logLabel() { 492 if (this.isUsingViewLabel()) { 493 log(" Using view label " + getLabel()); 494 } else if (this.isUsingRevisionLabel()) { 495 log(" Using revision label " + getLabel()); 496 } 497 } 498 499 503 protected void logAsOfDate() { 504 if (null != this.asOfDate) { 505 log(" Using view as of date " + getAsOfDate()); 506 } 507 } 508 509 513 522 protected boolean shouldProcess(String pName) { 523 boolean includeIt = matchPatterns(getIncludes(), pName); 524 boolean excludeIt = matchPatterns(getExcludes(), pName); 525 return (includeIt && !excludeIt); 526 } 527 528 535 protected boolean matchPatterns(String patterns, String pName) { 536 if (patterns == null) { 537 return false; 538 } 539 StringTokenizer exStr = new StringTokenizer (patterns, ","); 540 while (exStr.hasMoreTokens()) { 541 if (DirectoryScanner.match(exStr.nextToken(), pName)) { 542 return true; 543 } 544 } 545 return false; 546 } 547 548 556 private Folder configureRootStarteamFolder() 557 throws BuildException { 558 Folder starteamrootfolder = null; 559 try { 560 View snapshot = openView(); 562 563 566 starteamrootfolder = 567 StarTeamFinder.findFolder(snapshot.getRootFolder(), 568 this.rootStarteamFolder); 569 570 if (this.isPreloadFileInformation()) { 571 PropertyNames pn = getServer().getPropertyNames(); 572 String [] props = new String [] {pn.FILE_NAME, pn.FILE_PATH, 573 pn.FILE_STATUS, pn.MODIFIED_TIME, 574 pn.FILE_FILE_TIME_AT_CHECKIN, 575 pn.MODIFIED_USER_ID, pn.FILE_SIZE, 576 pn.FILE_ENCODING}; 577 578 int depth = this.isRecursive() ? -1 : 0; 579 starteamrootfolder.populateNow(getServer().getTypeNames().FILE, 580 props, depth); 581 } 582 583 584 } catch (BuildException e) { 585 throw e; 586 } catch (Exception e) { 587 StringBuffer msg = new StringBuffer ("Unable to find root folder ") 588 .append(this.rootStarteamFolder) 589 .append(" in repository at ") 590 .append(getURL()); 591 if (this.label != null) { 592 msg.append(" using specified label ").append(this.label); 593 } 594 if (this.asOfDate != null) { 595 msg.append(" as of specified date ") 596 .append(this.asOfDate); 597 } 598 throw new BuildException(msg.toString(), e); 599 600 } 601 602 if (null == starteamrootfolder) { 603 throw new BuildException("Unable to find root folder " 604 + this.rootStarteamFolder + " in repository at " + getURL()); 605 } 606 607 return starteamrootfolder; 608 } 609 610 624 private java.io.File getLocalRootMapping(Folder starteamrootfolder) { 625 String localrootfolder; 627 if (null != this.rootLocalFolder) { 628 localrootfolder = rootLocalFolder; 629 } else { 630 localrootfolder = starteamrootfolder.getPathFragment(); 633 } 634 635 return new java.io.File (localrootfolder); 636 637 } 638 639 648 protected abstract void logOperationDescription( 649 Folder starteamrootFolder, java.io.File targetrootFolder); 650 651 661 662 public final void execute() throws BuildException { 663 try { 664 665 Folder starteamrootfolder = configureRootStarteamFolder(); 666 667 java.io.File localrootfolder = 669 getLocalRootMapping(starteamrootfolder); 670 671 testPreconditions(); 672 673 logOperationDescription(starteamrootfolder, localrootfolder); 675 676 visit(starteamrootfolder, localrootfolder); 678 679 } catch (Exception e) { 680 throw new BuildException(e); 681 } finally { 682 disconnectFromServer(); 683 } 684 } 685 686 private void findLabel(View v) throws BuildException { 687 Label[] allLabels = v.getLabels(); 688 for (int i = 0; i < allLabels.length; i++) { 689 Label stLabel = allLabels[i]; 690 log("checking label " + stLabel.getName(), Project.MSG_DEBUG); 691 if (stLabel != null && !stLabel.isDeleted() && stLabel.getName().equals(this.label)) { 692 if (!stLabel.isRevisionLabel() && !stLabel.isViewLabel()) { 693 throw new BuildException("Unexpected label type."); 694 } 695 log("using label " + stLabel.getName(), Project.MSG_VERBOSE); 696 this.labelInUse = stLabel; 697 return; 698 } 699 } 700 throw new BuildException("Error: label " 701 + this.label 702 + " does not exist in view " 703 + v.getFullName()); 704 705 } 706 707 716 protected int getLabelID(View v) throws BuildException { 717 if (null != this.label) { 718 findLabel(v); 719 return this.labelInUse.getID(); 720 } 721 return -1; 722 } 723 724 729 protected int getIDofLabelInUse() { 730 if (null != this.labelInUse) { 731 return this.labelInUse.getID(); 732 } 733 return -1; 734 } 735 736 747 protected abstract void visit(Folder rootStarteamFolder, 748 java.io.File rootLocalFolder) 749 throws BuildException; 750 751 761 protected abstract void testPreconditions() throws BuildException; 762 763 771 public static String getFullRepositoryPath( 772 com.starbase.starteam.File remotefile) { 773 StringBuffer sb = new StringBuffer (); 774 sb.append(remotefile.getParentFolderHierarchy()) 775 .append(remotefile.getName()); 776 return sb.toString(); 777 } 778 779 795 protected abstract class UnmatchedFileMap extends Hashtable { 796 797 808 UnmatchedFileMap init(java.io.File localFolder, Folder remoteFolder) { 809 if (!localFolder.exists()) { 810 return this; 811 } 812 813 String [] localFiles = localFolder.list(); 814 815 for (int i = 0; i < localFiles.length; i++) { 816 String fn = localFiles[i]; 817 java.io.File localFile = 818 new java.io.File (localFolder, localFiles[i]).getAbsoluteFile(); 819 820 log("adding " + localFile + " to UnmatchedFileMap", 821 Project.MSG_DEBUG); 822 823 if (localFile.isDirectory()) { 824 this.put(localFile, new Folder(remoteFolder, fn, fn)); 825 } else { 826 com.starbase.starteam.File remoteFile = 827 new com.starbase.starteam.File(remoteFolder); 828 remoteFile.setName(fn); 829 this.put(localFile, remoteFile); 830 } 831 } 832 return this; 833 } 834 835 840 void removeControlledItem(java.io.File localFile) { 841 if (isActive()) { 842 log("removing processed " + localFile.getAbsoluteFile() 843 + " from UnmatchedFileMap", Project.MSG_DEBUG); 844 this.remove(localFile.getAbsoluteFile()); 845 } 846 } 847 854 abstract void processUncontrolledItems() throws BuildException; 855 856 863 protected abstract boolean isActive(); 864 865 } 866 867 } 868 | Popular Tags |