1 22 package org.netbeans.lib.cvsclient.command.checkout; 23 24 import java.io.*; 25 import java.util.*; 26 27 import org.netbeans.lib.cvsclient.*; 28 import org.netbeans.lib.cvsclient.command.*; 29 import org.netbeans.lib.cvsclient.command.update.*; 30 import org.netbeans.lib.cvsclient.connection.*; 31 import org.netbeans.lib.cvsclient.event.*; 32 import org.netbeans.lib.cvsclient.request.*; 33 34 41 public class CheckoutCommand extends BasicCommand 42 implements TemporaryFileCreator { 43 44 private static final String UPDATING = ": Updating "; 50 private final Set emptyDirectories = new HashSet(); 51 52 56 private final List modules = new LinkedList(); 57 58 61 private final List expandedModules = new LinkedList(); 62 63 66 private boolean showModules; 67 68 71 private boolean showModulesWithStatus; 72 73 76 private boolean pipeToOutput; 77 78 82 private boolean pruneDirectories; 83 84 87 private boolean resetStickyOnes; 88 89 93 private boolean useHeadIfNotFound; 94 95 98 private boolean notShortenPaths; 99 100 103 private boolean isNotShortenSet; 104 105 108 private String checkoutByDate; 109 110 113 private String checkoutByRevision; 114 115 118 private String checkoutDirectory; 119 120 124 private KeywordSubstitutionOptions keywordSubst; 125 126 129 private boolean notRunModuleProgram; 130 131 132 private ClientServices client; 133 134 139 public CheckoutCommand(boolean recursive, String [] modules) { 140 resetCVSCommand(); 141 setRecursive(recursive); 142 setModules(modules); 143 } 144 145 150 public CheckoutCommand(boolean recursive, String module) { 151 resetCVSCommand(); 152 setRecursive(recursive); 153 setModule(module); 154 } 155 156 159 public CheckoutCommand() { 160 resetCVSCommand(); 161 setRecursive(true); 162 } 163 164 168 public void setModule(String module) { 169 modules.add(module); 170 } 171 172 175 176 public void clearModules() { 177 this.modules.clear(); 178 } 179 180 184 public void setModules(String [] modules) { 185 clearModules(); 186 for (int i = 0; i < modules.length; i++) { 187 String module = modules[i]; 188 this.modules.add(module); 189 } 190 } 191 192 public String [] getModules() { 193 String [] mods = new String [modules.size()]; 194 mods = (String [])modules.toArray(mods); 195 return mods; 196 } 197 198 203 private void processExistingModules(String localPath) { 204 if (expandedModules.size() == 0) { 205 return; 206 } 207 208 List list = new ArrayList(expandedModules.size()); 209 for (Iterator it = expandedModules.iterator(); it.hasNext();) { 210 String moduleName = (String )it.next(); 211 if (moduleName.equals(".")) { list.add(new File(localPath)); 213 break; 214 } 215 File moduleDir = null; 216 final File moduleFile = new File(localPath, moduleName); 217 if (moduleFile.isFile()) { 218 moduleDir = moduleFile.getParentFile(); 219 } 220 else { 221 moduleDir = moduleFile; 222 } 223 final File moduleCVSDir = new File(moduleDir, "CVS/Repository"); if (moduleCVSDir.exists()) { 225 list.add(moduleFile); 226 } 227 } 228 File[] directories = new File[list.size()]; 229 directories = (File[])list.toArray(directories); 230 setFiles(directories); 231 } 232 233 239 public void execute(ClientServices client, EventManager em) 240 throws CommandException, AuthenticationException { 241 242 client.ensureConnection(); 243 this.client = client; 244 245 try { 246 requests = new LinkedList(); 247 if (client.isFirstCommand()) { 248 requests.add(new RootRequest(client.getRepository())); 249 } 250 251 if (showModules || showModulesWithStatus) { 252 if (builder == null && !isBuilderSet()) { 256 builder = createBuilder(em); 257 } 258 259 if (showModules) { 261 requests.add(new ArgumentRequest("-c")); } 263 if (showModulesWithStatus) { 264 requests.add(new ArgumentRequest("-s")); } 266 requests.add(CommandRequest.CHECKOUT); 267 try { 268 client.processRequests(requests); 269 requests.clear(); 270 } 271 catch (CommandException ex) { 272 throw ex; 273 } 274 catch (EOFException ex) { 275 throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); } 277 catch (Exception ex) { 278 throw new CommandException(ex, ex.getLocalizedMessage()); 279 } 280 return; 281 } 282 283 for (Iterator it = modules.iterator(); it.hasNext();) { 284 String module = (String )it.next(); 285 requests.add(new ArgumentRequest(module)); 286 } 287 288 expandedModules.clear(); 289 requests.add(new DirectoryRequest(".", client.getRepository())); requests.add(new RootRequest(client.getRepository())); 291 requests.add(new ExpandModulesRequest()); 292 try { 293 client.processRequests(requests); 294 } 295 catch (CommandException ex) { 296 throw ex; 297 } 298 catch (Exception ex) { 299 throw new CommandException(ex, ex.getLocalizedMessage()); 300 } 301 requests.clear(); 302 postExpansionExecute(client, em); 303 } finally { 304 this.client = null; 305 } 306 } 307 308 317 protected boolean assumeLocalPathWhenUnspecified() { 318 return false; 319 } 320 321 325 public void moduleExpanded(ModuleExpansionEvent e) { 326 expandedModules.add(e.getModule()); 327 } 328 329 335 private void postExpansionExecute(ClientServices client, EventManager em) 336 throws CommandException, AuthenticationException { 337 processExistingModules(client.getLocalPath()); 341 super.execute(client, em); 344 345 int index = requests.size(); 349 final int FIRST_INDEX = 0; 350 final int SECOND_INDEX = 1; 351 if (!isRecursive()) { 352 requests.add(FIRST_INDEX, new ArgumentRequest("-l")); } 354 if (pipeToOutput) { 355 requests.add(FIRST_INDEX, new ArgumentRequest("-p")); } 357 if (resetStickyOnes) { 358 requests.add(FIRST_INDEX, new ArgumentRequest("-A")); } 360 if (useHeadIfNotFound) { 361 requests.add(FIRST_INDEX, new ArgumentRequest("-f")); } 363 if (isNotShortenPaths()) { 364 requests.add(FIRST_INDEX, new ArgumentRequest("-N")); } 366 if (notRunModuleProgram) { 367 requests.add(FIRST_INDEX, new ArgumentRequest("-n")); } 369 if (checkoutByDate != null && checkoutByDate.length() > 0) { 370 requests.add(FIRST_INDEX, new ArgumentRequest("-D")); requests.add(SECOND_INDEX, new ArgumentRequest(getCheckoutByDate())); 372 } 373 if (checkoutByRevision != null && checkoutByRevision.length() > 0) { 374 requests.add(FIRST_INDEX, new ArgumentRequest("-r")); requests.add(SECOND_INDEX, new ArgumentRequest(getCheckoutByRevision())); 376 } 377 if (checkoutDirectory != null && (!checkoutDirectory.equals(""))) { 378 requests.add(FIRST_INDEX, new ArgumentRequest("-d")); requests.add(SECOND_INDEX, new ArgumentRequest(getCheckoutDirectory())); 380 } 381 if (getKeywordSubst() != null) { 382 requests.add(FIRST_INDEX, new ArgumentRequest("-k" + getKeywordSubst())); } 384 385 index = requests.size() - index; requests.add(index++, new ArgumentRequest("--")); 389 for (Iterator it = modules.iterator(); it.hasNext();) { 395 String module = (String )it.next(); 396 requests.add(index++, new ArgumentRequest(module)); 397 } 398 399 requests.add(new DirectoryRequest(".", client.getRepository())); requests.add(CommandRequest.CHECKOUT); 401 try { 402 client.processRequests(requests); 403 if (pruneDirectories) { 404 pruneEmptyDirectories(); 405 } 406 requests.clear(); 407 408 } 409 catch (CommandException ex) { 410 throw ex; 411 } 412 catch (Exception ex) { 413 throw new CommandException(ex, ex.getLocalizedMessage()); 414 } 415 } 416 417 421 public boolean isShowModules() { 422 return showModules; 423 } 424 425 429 public void setShowModules(boolean showModules) { 430 this.showModules = showModules; 431 } 432 433 437 public boolean isShowModulesWithStatus() { 438 return showModulesWithStatus; 439 } 440 441 445 public void setShowModulesWithStatus(boolean showModulesWithStatus) { 446 this.showModulesWithStatus = showModulesWithStatus; 447 } 448 449 453 public void setPruneDirectories(boolean pruneDirectories) { 454 this.pruneDirectories = pruneDirectories; 455 } 456 457 462 public boolean getPruneDirectories() { 463 return pruneDirectories; 464 } 465 466 470 public boolean isPipeToOutput() { 471 return pipeToOutput; 472 } 473 474 478 public void setPipeToOutput(boolean pipeToOutput) { 479 this.pipeToOutput = pipeToOutput; 480 } 481 482 486 public boolean isResetStickyOnes() { 487 return resetStickyOnes; 488 } 489 490 494 public void setResetStickyOnes(boolean resetStickyOnes) { 495 this.resetStickyOnes = resetStickyOnes; 496 } 497 498 502 public boolean isUseHeadIfNotFound() { 503 return useHeadIfNotFound; 504 } 505 506 510 public void setUseHeadIfNotFound(boolean useHeadIfNotFound) { 511 this.useHeadIfNotFound = useHeadIfNotFound; 512 } 513 514 518 public boolean isNotShortenPaths() { 519 return notShortenPaths || (!isNotShortenSet && checkoutDirectory == null); 521 } 522 523 527 public void setNotShortenPaths(boolean notShortenPaths) { 528 this.notShortenPaths = notShortenPaths; 529 isNotShortenSet = true; 530 } 531 532 536 public boolean isNotRunModuleProgram() { 537 return notRunModuleProgram; 538 } 539 540 544 public void setNotRunModuleProgram(boolean notRunModuleProgram) { 545 this.notRunModuleProgram = notRunModuleProgram; 546 } 547 548 552 public String getCheckoutByDate() { 553 return checkoutByDate; 554 } 555 556 560 public void setCheckoutByDate(String checkoutByDate) { 561 this.checkoutByDate = checkoutByDate; 562 } 563 564 568 public String getCheckoutByRevision() { 569 return checkoutByRevision; 570 } 571 572 576 public void setCheckoutByRevision(String checkoutByRevision) { 577 this.checkoutByRevision = checkoutByRevision; 578 } 579 580 583 public String getCheckoutDirectory() { 584 return this.checkoutDirectory; 585 } 586 587 590 public void setCheckoutDirectory(String checkoutDirectory) { 591 this.checkoutDirectory = checkoutDirectory; 592 } 593 594 598 public KeywordSubstitutionOptions getKeywordSubst() { 599 return keywordSubst; 600 } 601 602 606 public void setKeywordSubst(KeywordSubstitutionOptions keywordSubst) { 607 this.keywordSubst = keywordSubst; 608 } 609 610 public Builder createBuilder(EventManager eventMan) { 611 if (isShowModules() || isShowModulesWithStatus()) { 612 return new ModuleListBuilder(eventMan, this); 613 } 614 if (isPipeToOutput()) { 615 return new PipedFilesBuilder(eventMan, this, this); 616 } 617 return new UpdateBuilder(eventMan, getLocalDirectory()); 618 } 619 620 public File createTempFile(String filename) throws IOException { 621 File temp = File.createTempFile("cvs", ".dff", getGlobalOptions().getTempDir()); return temp; 623 } 624 625 630 public String getCVSCommand() { 631 StringBuffer toReturn = new StringBuffer ("checkout "); toReturn.append(getCVSArguments()); 633 if (!isShowModules() && !isShowModulesWithStatus()) { 634 for (Iterator it = modules.iterator(); it.hasNext();) { 635 String module = (String )it.next(); 636 toReturn.append(module); 637 toReturn.append(' '); 638 } 639 } 640 return toReturn.toString(); 641 } 642 643 648 public boolean setCVSCommand(char opt, String optArg) { 649 if (opt == 'c') { 650 setShowModules(true); 651 } 652 else if (opt == 's') { 653 setShowModulesWithStatus(true); 654 } 655 else if (opt == 'p') { 656 setPipeToOutput(true); 657 } 658 else if (opt == 'R') { 659 setRecursive(true); 660 } 661 else if (opt == 'l') { 662 setRecursive(false); 663 } 664 else if (opt == 'A') { 665 setResetStickyOnes(true); 666 } 667 else if (opt == 'f') { 668 setUseHeadIfNotFound(true); 669 } 670 else if (opt == 'P') { 671 setPruneDirectories(true); 672 } 673 else if (opt == 'D') { 674 setCheckoutByDate(optArg.trim()); 675 } 676 else if (opt == 'r') { 677 setCheckoutByRevision(optArg.trim()); 678 } 679 else if (opt == 'd') { 680 setCheckoutDirectory(optArg); 681 } 682 else if (opt == 'N') { 683 setNotShortenPaths(true); 684 } 685 else if (opt == 'n') { 686 setNotRunModuleProgram(true); 687 } 688 else if (opt == 'k') { 689 KeywordSubstitutionOptions keywordSubst = 690 KeywordSubstitutionOptions.findKeywordSubstOption(optArg); 691 setKeywordSubst(keywordSubst); 692 } 693 else { 694 return false; 695 } 696 return true; 697 } 698 699 702 public String getOptString() { 703 return "cnpslNPRAD:r:fk:d:"; } 705 706 711 public void resetCVSCommand() { 712 setShowModules(false); 713 setShowModulesWithStatus(false); 714 setPipeToOutput(false); 715 setRecursive(true); 716 setResetStickyOnes(false); 717 setUseHeadIfNotFound(false); 718 setCheckoutByDate(null); 719 setCheckoutByRevision(null); 720 setKeywordSubst(null); 721 setPruneDirectories(false); 722 setNotShortenPaths(false); 723 isNotShortenSet = false; 724 setNotRunModuleProgram(false); 725 setCheckoutDirectory(null); 726 } 727 728 732 public String getCVSArguments() { 733 StringBuffer toReturn = new StringBuffer (""); if (isShowModules()) { 735 toReturn.append("-c "); } 737 if (isShowModulesWithStatus()) { 738 toReturn.append("-s "); } 740 if (isPipeToOutput()) { 741 toReturn.append("-p "); } 743 if (!isRecursive()) { 744 toReturn.append("-l "); } 746 if (isResetStickyOnes()) { 747 toReturn.append("-A "); } 749 if (isUseHeadIfNotFound()) { 750 toReturn.append("-f "); } 752 if (getPruneDirectories()) { 753 toReturn.append("-P "); } 755 if (isNotShortenPaths()) { 756 toReturn.append("-N "); } 758 if (isNotRunModuleProgram()) { 759 toReturn.append("-n "); } 761 if (getKeywordSubst() != null) { 762 toReturn.append("-k"); toReturn.append(getKeywordSubst()); 764 toReturn.append(' '); 765 } 766 if (getCheckoutByRevision() != null && getCheckoutByRevision().length() > 0) { 767 toReturn.append("-r "); toReturn.append(getCheckoutByRevision()); 769 toReturn.append(' '); 770 } 771 if (getCheckoutByDate() != null && getCheckoutByDate().length() > 0) { 772 toReturn.append("-D "); toReturn.append(getCheckoutByDate()); 774 toReturn.append(' '); 775 } 776 if (getCheckoutDirectory() != null) { 777 toReturn.append("-d "); toReturn.append(getCheckoutDirectory()); 779 toReturn.append(" "); } 781 return toReturn.toString(); 782 } 783 784 790 public void messageSent(MessageEvent e) { 791 super.messageSent(e); 792 if (pruneDirectories && 795 e.getMessage().indexOf(UPDATING) > 0) { 796 File file = new File(getLocalDirectory(), e.getMessage().substring(e.getMessage().indexOf(UPDATING) + UPDATING.length())); 797 emptyDirectories.add(file); 798 } 799 } 800 801 805 private boolean pruneEmptyDirectory(File directory) throws IOException { 806 boolean empty = true; 807 808 final File[] contents = directory.listFiles(); 809 810 if (contents != null) { 812 for (int i = 0; i < contents.length; i++) { 813 if (contents[i].isFile()) { 814 empty = false; 815 } 816 else { 817 if (!contents[i].getName().equals("CVS")) { empty = pruneEmptyDirectory(contents[i]); 819 } 820 } 821 822 if (!empty) { 823 break; 824 } 825 } 826 827 if (empty) { 828 final File entriesFile = new File(directory, "CVS/Entries"); if (entriesFile.exists()) { 832 final File adminDir = new File(directory, "CVS"); final File[] adminFiles = adminDir.listFiles(); 834 for (int i = 0; i < adminFiles.length; i++) { 835 adminFiles[i].delete(); 836 } 837 adminDir.delete(); 838 directory.delete(); 839 client.removeEntry(directory); 840 } 841 } 842 } 843 844 return empty; 845 } 846 847 850 private void pruneEmptyDirectories() throws IOException { 851 final Iterator it = emptyDirectories.iterator(); 852 while (it.hasNext()) { 853 final File dir = (File)it.next(); 854 if (dir.exists()) { 857 pruneEmptyDirectory(dir); 858 } 859 } 860 emptyDirectories.clear(); 861 } 862 863 } 864 | Popular Tags |