1 18 package org.apache.tools.ant.taskdefs.optional.scm; 19 20 import com.starbase.starteam.Folder; 21 import com.starbase.starteam.Item; 22 import com.starbase.starteam.Property; 23 import com.starbase.starteam.Server; 24 import com.starbase.starteam.StarTeamFinder; 25 import com.starbase.starteam.Type; 26 import com.starbase.starteam.View; 27 import com.starbase.util.Platform; 28 import java.util.StringTokenizer ; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.DirectoryScanner; 31 import org.apache.tools.ant.Project; 32 33 74 public class AntStarTeamCheckOut extends org.apache.tools.ant.Task { 75 76 83 public static final String DEFAULT_INCLUDESETTING = "*"; 84 85 93 public static final String DEFAULT_EXCLUDESETTING = null; 94 95 103 public static final String DEFAULT_FOLDERSETTING = null; 104 105 109 private Folder prevFolder = null; 110 111 112 private int checkedOut = 0; 113 114 116 117 private String serverName = null; 118 119 120 private int serverPort = -1; 121 122 123 private String projectName = null; 124 125 129 private String folderName = DEFAULT_FOLDERSETTING; 130 131 132 private String viewName = null; 133 134 135 private String username = null; 136 137 138 private String password = null; 139 140 144 private String targetFolder = null; 145 146 150 private boolean force = false; 151 152 156 private boolean verbose = false; 157 158 162 private boolean recursion = true; 163 164 166 167 private String includes = DEFAULT_INCLUDESETTING; 168 169 170 private String excludes = DEFAULT_EXCLUDESETTING; 171 172 173 private String delim = Platform.getFilePathDelim(); 174 175 180 private boolean targetFolderAbsolute = false; 181 182 183 184 private static void assertTrue(boolean value, String msg) throws BuildException { 185 if (!value) { 186 throw new BuildException(msg); 187 } 188 } 189 190 194 protected void checkParameters() throws BuildException { 195 assertTrue(getServerName() != null, "ServerName must be set."); 197 assertTrue(getServerPort() != -1, "ServerPort must be set."); 198 assertTrue(getProjectName() != null, "ProjectName must be set."); 199 assertTrue(getViewName() != null, "ViewName must be set."); 200 assertTrue(getUsername() != null, "Username must be set."); 201 assertTrue(getPassword() != null, "Password must be set."); 202 assertTrue(getTargetFolder() != null, "TargetFolder must be set."); 203 204 if ((getTargetFolder().endsWith("/") 208 || getTargetFolder().endsWith("\\")) 209 && getTargetFolder().length() > 1) { 210 setTargetFolder(getTargetFolder().substring(0, getTargetFolder().length() - 1)); 211 } 212 213 java.io.File dirExist = new java.io.File (getTargetFolder()); 215 216 if (dirExist.isDirectory() && !getForce()) { 217 throw new BuildException("Target directory exists. Set \"force\" " 218 + "to \"true\" to continue anyway."); 219 } 220 } 221 222 223 228 public void execute() throws BuildException { 229 log("DEPRECATED - The starteam task is deprecated. Use stcheckout instead.", 230 Project.MSG_WARN); 231 232 Server s = getServer(); 234 235 try { 236 runServer(s); 238 } finally { 239 s.disconnect(); 241 } 242 246 log(checkedOut + " files checked out."); 248 } 249 250 251 256 protected Server getServer() { 257 Server s = new Server(getServerName(), getServerPort()); 259 260 s.connect(); 262 263 s.logOn(getUsername(), getPassword()); 265 266 return s; 267 } 268 269 270 275 protected void runServer(Server s) { 276 com.starbase.starteam.Project[] projects = s.getProjects(); 277 278 for (int i = 0; i < projects.length; i++) { 279 com.starbase.starteam.Project p = projects[i]; 280 281 if (p.getName().equals(getProjectName())) { 282 if (getVerbose()) { 283 log("Found " + getProjectName() + delim); 284 } 285 runProject(s, p); 286 break; 287 } 288 } 289 } 290 291 292 298 protected void runProject(Server s, com.starbase.starteam.Project p) { 299 View[] views = p.getViews(); 300 301 for (int i = 0; i < views.length; i++) { 302 View v = views[i]; 303 304 if (v.getName().equals(getViewName())) { 305 if (getVerbose()) { 306 log("Found " + getProjectName() + delim + getViewName() + delim); 307 } 308 runType(s, p, v, s.typeForName(s.getTypeNames().FILE)); 309 break; 310 } 311 } 312 } 313 314 315 323 protected void runType(Server s, com.starbase.starteam.Project p, View v, Type t) { 324 Folder f = v.getRootFolder(); 326 327 if (getFolderName() != null) { 328 if (getFolderName().equals("\\") || getFolderName().equals("/")) { 329 setFolderName(null); 330 } else { 331 f = StarTeamFinder.findFolder(v.getRootFolder(), getFolderName()); 332 assertTrue(null != f, "ERROR: " + getProjectName() + delim 333 + getViewName() + delim + v.getRootFolder() + delim 334 + getFolderName() + delim 335 + " does not exist."); 336 } 337 } 338 339 if (getVerbose() && getFolderName() != null) { 340 log("Found " + getProjectName() + delim + getViewName() 341 + delim + getFolderName() + delim + "\n"); 342 } 343 344 347 int nProperties = 2; 349 350 Property p1 = getPrimaryDescriptor(t); 352 353 Property p2 = getSecondaryDescriptor(t); 356 357 if (p2 != null) { 358 nProperties++; 359 } 360 361 String [] strNames = new String [nProperties]; 363 int iProperty = 0; 364 365 strNames[iProperty++] = s.getPropertyNames().OBJECT_ID; 366 strNames[iProperty++] = p1.getName(); 367 if (p2 != null) { 368 strNames[iProperty++] = p2.getName(); 369 } 370 371 f.populateNow(t.getName(), strNames, -1); 373 374 runFolder(s, p, v, t, f, calcTargetFolder(v, f)); 376 377 f.discardItems(t.getName(), -1); 379 } 380 381 382 395 private java.io.File calcTargetFolder(View v, Folder rootSourceFolder) { 396 java.io.File root = new java.io.File (getTargetFolder()); 397 398 if (!getTargetFolderAbsolute()) { 399 String defaultPath = v.getDefaultPath(); 403 404 defaultPath = defaultPath.replace('/', java.io.File.separatorChar); 406 defaultPath = defaultPath.replace('\\', java.io.File.separatorChar); 407 408 java.io.File dir = new java.io.File (defaultPath); 409 String dirName = dir.getName(); 410 411 if (dirName.endsWith(delim)) { 413 dirName = dirName.substring(0, dirName.length() - 1); 414 } 415 416 StringTokenizer pathTokenizer 420 = new StringTokenizer (rootSourceFolder.getFolderHierarchy(), delim); 421 String currentToken = null; 422 boolean foundRoot = false; 423 424 while (pathTokenizer.hasMoreTokens()) { 425 currentToken = pathTokenizer.nextToken(); 426 if (currentToken.equals(getProjectName()) && !foundRoot) { 427 currentToken = dirName; 428 foundRoot = true; } 430 root = new java.io.File (root, currentToken); 431 } 432 } 433 434 return root; 435 } 436 437 438 449 protected void runFolder(Server s, 450 com.starbase.starteam.Project p, 451 View v, 452 Type t, 453 Folder f, 454 java.io.File tgt) { 455 Item[] items = f.getItems(t.getName()); 457 458 for (int i = 0; i < items.length; i++) { 459 runItem(s, p, v, t, f, items[i], tgt); 460 } 461 462 if (getRecursion()) { 464 Folder[] subfolders = f.getSubFolders(); 465 466 for (int i = 0; i < subfolders.length; i++) { 467 runFolder(s, p, v, t, subfolders[i], 468 new java.io.File (tgt, subfolders[i].getName())); 469 } 470 } 471 } 472 473 474 486 protected void runItem(Server s, 487 com.starbase.starteam.Project p, 488 View v, 489 Type t, 490 Folder f, 491 Item item, 492 java.io.File tgt) { 493 Property p1 = getPrimaryDescriptor(t); 495 Property p2 = getSecondaryDescriptor(t); 496 497 String pName = (String ) item.get(p1.getName()); 498 499 if (!shouldCheckout(pName)) { 500 return; 501 } 502 503 if (getVerbose()) { 505 boolean bShowHeader = (f != prevFolder); 507 508 if (bShowHeader) { 509 String strFolder = f.getFolderHierarchy(); 514 int i = strFolder.indexOf(delim); 515 516 if (i >= 0) { 517 strFolder = strFolder.substring(i + 1); 518 } 519 log(" Folder: \"" + strFolder + "\""); 520 prevFolder = f; 521 522 StringBuffer header = new StringBuffer (" Item"); 525 526 header.append(",\t").append(p1.getDisplayName()); 527 if (p2 != null) { 528 header.append(",\t").append(p2.getDisplayName()); 529 } 530 log(header.toString()); 531 } 532 533 StringBuffer itemLine = new StringBuffer (" "); 536 537 itemLine.append(item.getItemID()); 538 539 itemLine.append(",\t").append(formatForDisplay(p1, item.get(p1.getName()))); 542 543 if (p2 != null) { 546 itemLine.append(",\t").append(formatForDisplay(p2, item.get(p2.getName()))); 547 } 548 549 int locker = item.getLocker(); 551 552 if (locker > -1) { 553 itemLine.append(",\tLocked by ").append(locker); 554 } else { 555 itemLine.append(",\tNot locked"); 556 } 557 log(itemLine.toString()); 558 } 559 561 563 com.starbase.starteam.File remote = (com.starbase.starteam.File) item; 565 566 java.io.File local = new java.io.File (tgt, (String ) item.get(p1.getName())); 569 570 try { 571 remote.checkoutTo(local, Item.LockType.UNCHANGED, false, true, true); 572 checkedOut++; 573 } catch (Exception e) { 574 throw new BuildException("Failed to checkout '" + local + "'", e); 575 } 576 } 577 578 579 586 protected boolean shouldCheckout(String pName) { 587 boolean includeIt = matchPatterns(getIncludes(), pName); 588 boolean excludeIt = matchPatterns(getExcludes(), pName); 589 590 return (includeIt && !excludeIt); 591 } 592 593 594 602 protected boolean matchPatterns(String patterns, String pName) { 603 if (patterns == null) { 604 return false; 605 } 606 StringTokenizer exStr = new StringTokenizer (patterns, " "); 607 608 while (exStr.hasMoreTokens()) { 609 if (DirectoryScanner.match(exStr.nextToken(), pName)) { 610 return true; 611 } 612 } 613 return false; 614 } 615 616 617 624 protected Property getPrimaryDescriptor(Type t) { 625 Property[] properties = t.getProperties(); 626 627 for (int i = 0; i < properties.length; i++) { 628 Property p = properties[i]; 629 630 if (p.isPrimaryDescriptor()) { 631 return p; 632 } 633 } 634 return null; 635 } 636 637 638 646 protected Property getSecondaryDescriptor(Type t) { 647 Property[] properties = t.getProperties(); 648 649 for (int i = 0; i < properties.length; i++) { 650 Property p = properties[i]; 651 652 if (p.isDescriptor() && !p.isPrimaryDescriptor()) { 653 return p; 654 } 655 } 656 return null; 657 } 658 659 660 668 protected String formatForDisplay(Property p, Object value) { 669 if (p.getTypeCode() == Property.Types.TEXT) { 670 String str = value.toString(); 671 672 if (str.length() > 35) { 673 str = str.substring(0, 32) + "..."; 674 } 675 return "\"" + str + "\""; 676 } else { 677 if (p.getTypeCode() == Property.Types.ENUMERATED) { 678 return "\"" + p.getEnumDisplayName(((Integer ) value).intValue()) + "\""; 679 } else { 680 return value.toString(); 681 } 682 } 683 } 684 685 687 693 public void setServerName(String serverName) { 694 this.serverName = serverName; 695 } 696 697 698 704 public String getServerName() { 705 return serverName; 706 } 707 708 709 717 public void setServerPort(int serverPort) { 718 this.serverPort = serverPort; 719 } 720 721 722 728 public int getServerPort() { 729 return serverPort; 730 } 731 732 733 739 public void setProjectName(String projectName) { 740 this.projectName = projectName; 741 } 742 743 744 750 public String getProjectName() { 751 return projectName; 752 } 753 754 755 761 public void setViewName(String viewName) { 762 this.viewName = viewName; 763 } 764 765 766 772 public String getViewName() { 773 return viewName; 774 } 775 776 777 785 public void setFolderName(String folderName) { 786 this.folderName = folderName; 787 } 788 789 790 797 public String getFolderName() { 798 return folderName; 799 } 800 801 802 808 public void setUsername(String username) { 809 this.username = username; 810 } 811 812 813 819 public String getUsername() { 820 return username; 821 } 822 823 824 830 public void setPassword(String password) { 831 this.password = password; 832 } 833 834 835 841 public String getPassword() { 842 return password; 843 } 844 845 846 852 public void setTargetFolder(String targetFolder) { 853 this.targetFolder = targetFolder; 854 } 855 856 857 863 public String getTargetFolder() { 864 return targetFolder; 865 } 866 867 868 876 public void setForce(boolean force) { 877 this.force = force; 878 } 879 880 881 887 public boolean getForce() { 888 return force; 889 } 890 891 892 900 public void setRecursion(boolean recursion) { 901 this.recursion = recursion; 902 } 903 904 905 913 public boolean getRecursion() { 914 return recursion; 915 } 916 917 918 926 public void setVerbose(boolean verbose) { 927 this.verbose = verbose; 928 } 929 930 931 937 public boolean getVerbose() { 938 return verbose; 939 } 940 941 943 979 public void setIncludes(String includes) { 980 this.includes = includes; 981 } 982 983 984 993 public String getIncludes() { 994 return includes; 995 } 996 997 998 1032 public void setExcludes(String excludes) { 1033 this.excludes = excludes; 1034 } 1035 1036 1037 1046 public String getExcludes() { 1047 return excludes; 1048 } 1049 1050 1051 1060 public boolean getTargetFolderAbsolute() { 1061 return this.targetFolderAbsolute; 1062 } 1063 1064 1065 1076 public void setTargetFolderAbsolute(boolean targetFolderAbsolute) { 1077 this.targetFolderAbsolute = targetFolderAbsolute; 1078 } 1079} 1080 1081 | Popular Tags |