1 12 package org.eclipse.team.internal.ccvs.core.client; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.resources.IFile; 20 import org.eclipse.core.resources.IResource; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.team.internal.ccvs.core.*; 24 import org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener; 25 26 30 public abstract class Command extends Request { 31 32 public final static Add ADD = new Add(); 33 public final static Admin ADMIN = new Admin(); 34 public final static Annotate ANNOTATE = new Annotate(); 35 public final static Checkout CHECKOUT = new CheckoutWithOverwrite(); 36 public final static Commit COMMIT = new Commit(); 37 public final static Diff DIFF = new Diff(); 38 public final static RDiff RDIFF = new RDiff(); 39 public final static Editors EDITORS = new Editors(); 40 public final static Import IMPORT = new Import(); 41 public final static Log LOG = new Log(); 42 public final static Remove REMOVE = new Remove(); 43 public final static Status STATUS = new Status(); 44 public final static Tag TAG = new Tag(); 45 public final static Tag CUSTOM_TAG = new Tag(true); 48 public final static RTag RTAG = new RTag(); 49 public final static Update UPDATE = new Update(); 50 public final static Update REPLACE = new Replace(); 51 public final static SyncUpdate SYNCUPDATE = new SyncUpdate(); 52 public final static Version VERSION = new Version(); 53 public final static NOOPCommand NOOP = new NOOPCommand(); 54 55 public final static String [] NO_ARGUMENTS = new String [0]; 57 58 59 public static final GlobalOption[] NO_GLOBAL_OPTIONS = new GlobalOption[0]; 61 public static final GlobalOption DO_NOT_CHANGE = new GlobalOption("-n"); public static final GlobalOption DO_NOT_LOG = new GlobalOption("-l"); public static final GlobalOption MAKE_READ_ONLY = new GlobalOption("-r"); public static final GlobalOption TRACE_EXECUTION = new GlobalOption("-t"); 70 71 public static final QuietOption VERBOSE = new QuietOption(""); public static final QuietOption PARTLY_QUIET = new QuietOption("-q"); public static final QuietOption SILENT = new QuietOption("-Q"); 78 79 public static final LocalOption[] NO_LOCAL_OPTIONS = new LocalOption[0]; 81 public static final LocalOption RECURSE = new LocalOption("-R"); public static final LocalOption DO_NOT_RECURSE = new LocalOption("-l"); public static final LocalOption PRUNE_EMPTY_DIRECTORIES = new LocalOption("-P"); public static final LocalOption MESSAGE_OPTION = new LocalOption("-m"); 89 90 private static final Map ksubstOptionMap = new HashMap (); 92 public static final KSubstOption KSUBST_BINARY = new KSubstOption("-kb"); public static final KSubstOption KSUBST_TEXT = new KSubstOption("-ko"); public static final KSubstOption KSUBST_TEXT_EXPAND = new KSubstOption("-kkv"); public static final KSubstOption KSUBST_TEXT_EXPAND_LOCKER = new KSubstOption("-kkvl"); public static final KSubstOption KSUBST_TEXT_VALUES_ONLY = new KSubstOption("-kv"); public static final KSubstOption KSUBST_TEXT_KEYWORDS_ONLY = new KSubstOption("-kk"); 99 100 protected static final ICommandOutputListener DEFAULT_OUTPUT_LISTENER = new CommandOutputListener(); 101 102 105 protected Command() { } 106 107 113 protected ICommandOutputListener getDefaultCommandOutputListener() { 114 return DEFAULT_OUTPUT_LISTENER; 115 } 116 117 128 protected void sendArguments(Session session, String [] arguments) throws CVSException { 129 for (int i = 0; i < arguments.length; ++i) { 130 session.sendArgument(arguments[i]); 131 } 132 } 133 134 156 protected abstract ICVSResource[] sendLocalResourceState(Session session, GlobalOption[] globalOptions, 157 LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor) 158 throws CVSException; 159 160 178 protected IStatus commandFinished(Session session, GlobalOption[] globalOptions, 179 LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor, 180 IStatus status) throws CVSException { 181 return status; 182 } 183 184 195 protected void sendLocalWorkingDirectory(Session session) throws CVSException { 196 ICVSFolder localRoot = session.getLocalRoot(); 197 if (localRoot.isCVSFolder()) { 198 session.sendLocalRootDirectory(); 199 } else { 200 session.sendConstructedRootDirectory(); 201 } 202 } 203 204 217 protected ICVSResource[] computeWorkResources(Session session, 218 LocalOption[] localOptions, String [] arguments) throws CVSException { 219 ICVSFolder localRoot = session.getLocalRoot(); 220 221 if (arguments.length == 0) { 222 return new ICVSResource[] { localRoot }; 225 } else { 226 ICVSResource[] resources = new ICVSResource[arguments.length]; 229 for (int i = 0; i < arguments.length; i++) { 230 ICVSResource resource = localRoot.getChild(arguments[i]); 231 if(resource==null) { 235 if(localRoot.getName().length()==0) { 236 resource = localRoot.getFolder(arguments[i]); 239 } else { 240 resource = localRoot.getFile(arguments[i]); 241 } 242 } 243 resources[i] = resource; 244 } 245 return resources; 246 } 247 } 248 249 255 protected void sendFileStructure(Session session, ICVSResource[] resources, 256 LocalOption[] localOptions, boolean emptyFolders, IProgressMonitor monitor) throws CVSException { 257 checkResourcesManaged(session, resources); 258 259 new FileStructureVisitor(session, localOptions, emptyFolders, true).visit(session, resources, monitor); 260 } 261 262 269 protected void checkResourcesManaged(Session session, ICVSResource[] resources) throws CVSException { 270 for (int i = 0; i < resources.length; ++i) { 271 ICVSFolder folder; 272 if (resources[i].isFolder()) { 273 folder = (ICVSFolder) resources[i]; 274 } 275 else { 276 folder = resources[i].getParent(); 277 } 278 if (!folder.isCVSFolder() && folder.exists()) { 279 IStatus status = new CVSStatus(IStatus.ERROR,CVSStatus.ERROR,NLS.bind(CVSMessages.Command_argumentNotManaged, new String [] { folder.getName() }),session.getLocalRoot()); 280 throw new CVSException(status); 281 } 282 } 283 } 284 285 302 public final IStatus execute(final Session session, final GlobalOption[] globalOptions, 303 final LocalOption[] localOptions, final String [] arguments, final ICommandOutputListener listener, 304 IProgressMonitor pm) throws CVSException { 305 final IStatus[] status = new IStatus[1]; 306 ICVSRunnable job = new ICVSRunnable() { 307 public void run(IProgressMonitor monitor) throws CVSException { 308 GlobalOption[] gOptions = filterGlobalOptions(session, globalOptions); 310 LocalOption[] lOptions = filterLocalOptions(session, gOptions, localOptions); 311 312 if (session.isOutputToConsole() || Policy.isDebugProtocol()) { 314 IPath commandRootPath; 315 IResource resource = session.getLocalRoot().getIResource(); 316 if (resource == null) { 317 commandRootPath = Path.EMPTY; 318 } else { 319 commandRootPath = resource.getFullPath(); 320 } 321 String line = constructCommandInvocationString(commandRootPath, gOptions, lOptions, arguments); 322 ConsoleListeners.getInstance().commandInvoked(session, line); 323 if (Policy.isDebugProtocol()) Policy.printProtocolLine("CMD> " + line); } 325 326 try { 328 session.setCurrentCommand(Command.this); 329 status[0] = doExecute(session, gOptions, lOptions, arguments, listener, monitor); 330 notifyConsoleOnCompletion(session, status[0], null); 331 } catch (CVSException e) { 332 notifyConsoleOnCompletion(session, null, e); 333 throw e; 334 } catch (RuntimeException e) { 335 notifyConsoleOnCompletion(session, null, e); 336 throw e; 337 } 338 } 339 }; 340 if (isWorkspaceModification()) { 341 session.getLocalRoot().run(job, pm); 342 } else { 343 job.run(pm); 344 } 345 return status[0]; 346 } 347 348 356 protected boolean isWorkspaceModification() { 357 return true; 358 } 359 360 private void notifyConsoleOnCompletion(Session session, IStatus status, Exception exception) { 361 ConsoleListeners.getInstance().commandCompleted(session, status, exception); 362 if (Policy.isDebugProtocol()) { 363 if (status != null) Policy.printProtocolLine("RESULT> " + status.toString()); else Policy.printProtocolLine("RESULT> " + exception.toString()); } 366 } 367 368 protected IStatus doExecute(Session session, GlobalOption[] globalOptions, 369 LocalOption[] localOptions, String [] arguments, ICommandOutputListener listener, 370 IProgressMonitor monitor) throws CVSException { 371 ICVSResource[] resources = null; 372 373 monitor = Policy.monitorFor(monitor); 374 monitor.beginTask(null, 100); 375 Policy.checkCanceled(monitor); 376 try { 377 378 session.setNoLocalChanges(DO_NOT_CHANGE.isElementOf(globalOptions)); 380 session.setModTime(null); 381 382 383 for (int i = 0; i < globalOptions.length; i++) { 385 globalOptions[i].send(session); 386 } 387 Policy.checkCanceled(monitor); 388 for (int i = 0; i < localOptions.length; i++) { 390 localOptions[i].send(session); 391 } 392 Policy.checkCanceled(monitor); 393 resources = computeWorkResources(session, localOptions, arguments); 395 Policy.checkCanceled(monitor); 396 resources = sendLocalResourceState(session, globalOptions, localOptions, 398 resources, Policy.infiniteSubMonitorFor(monitor, 48)); 399 Policy.checkCanceled(monitor); 400 sendArguments(session, arguments); 402 sendLocalWorkingDirectory(session); 404 405 if (listener == null) listener = getDefaultCommandOutputListener(); 407 408 409 IStatus status = executeRequest(session, listener, Policy.subMonitorFor(monitor, 50)); 411 412 status = commandFinished(session, globalOptions, localOptions, resources, Policy.subMonitorFor(monitor, 2), 414 status); 415 return status; 416 } finally { 417 monitor.done(); 418 } 419 } 420 421 429 private String constructCommandInvocationString(IPath commandRootPath, GlobalOption[] globalOptions, 430 LocalOption[] localOptions, String [] arguments) { 431 StringBuffer commandLine = new StringBuffer ("cvs"); for (int i = 0; i < globalOptions.length; ++i) { 433 String option = globalOptions[i].toString(); 434 if (option.length() == 0) continue; 435 commandLine.append(' '); 436 commandLine.append(option); 437 } 438 commandLine.append(' '); 439 commandLine.append(getRequestId()); 440 for (int i = 0; i < localOptions.length; ++i) { 441 String option = localOptions[i].toString(); 442 if (option.length() == 0) continue; 443 commandLine.append(' '); 444 commandLine.append(option); 445 } 446 for (int i = 0; i < arguments.length; ++i) { 447 if (arguments[i].length() == 0) continue; 448 commandLine.append(" \""); IPath completePath = commandRootPath; 450 if (!arguments[i].equals(Session.CURRENT_LOCAL_FOLDER)) { 451 completePath = completePath.append(arguments[i]); 452 } 453 commandLine.append(completePath.toString()); 454 commandLine.append('"'); 455 } 456 return commandLine.toString(); 457 } 458 459 462 protected static abstract class Option { 463 protected String option, argument; 464 protected Option(String option, String argument) { 465 this.option = option; 466 this.argument = argument; 467 } 468 473 public boolean isElementOf(Option[] array) { 474 return findOption(array, option) != null; 475 } 476 479 String getOption() { 480 return option; 481 } 482 486 public boolean equals(Object other) { 487 if (this == other) return true; 488 if (other instanceof Option) { 489 Option otherOption = (Option) other; 490 return option.equals(otherOption.option); 491 } 492 return false; 493 } 494 498 public abstract void send(Session session) throws CVSException; 499 502 public String toString() { 503 if (argument != null && argument.length() != 0) { 504 return option + " \"" + argument + '"'; } else { 506 return option; 507 } 508 } 509 } 510 513 public static class GlobalOption extends Option { 514 protected GlobalOption(String option) { 515 super(option, null); 516 } 517 public void send(Session session) throws CVSException { 518 session.sendGlobalOption(option); 519 } 520 527 protected GlobalOption[] addToEnd(GlobalOption[] options) { 528 GlobalOption[] globalOptions = new GlobalOption[options.length + 1]; 529 System.arraycopy(options, 0, globalOptions, 0, options.length); 530 globalOptions[globalOptions.length - 1] = this; 531 return globalOptions; 532 } 533 } 534 537 public static final class QuietOption extends GlobalOption { 538 private QuietOption(String option) { 539 super(option); 540 } 541 public void send(Session session) throws CVSException { 542 if (option.length() != 0) super.send(session); 543 } 544 } 545 548 public static class LocalOption extends Option { 549 protected LocalOption(String option) { 550 super(option, null); 551 } 552 protected LocalOption(String option, String argument) { 553 super(option, argument); 554 } 555 public void send(Session session) throws CVSException { 556 session.sendArgument(option); 557 if (argument != null) session.sendArgument(argument); 558 } 559 public LocalOption[] addTo(LocalOption[] options) { 560 if (this.isElementOf(options)) { 561 return options; 562 } 563 LocalOption[] newOptions = new LocalOption[options.length + 1]; 564 System.arraycopy(options, 0, newOptions, 0, options.length); 565 newOptions[options.length] = this; 566 return newOptions; 567 } 568 public LocalOption[] removeFrom(LocalOption[] options) { 569 if (!this.isElementOf(options)) { 570 return options; 571 } 572 List result = new ArrayList (); 573 for (int i = 0; i < options.length; i++) { 574 Command.LocalOption option = options[i]; 575 if (!option.equals(this)) { 576 result.add(option); 577 } 578 } 579 return (LocalOption[]) result.toArray(new LocalOption[result.size()]); 580 } 581 } 582 585 public static class KSubstOption extends LocalOption { 586 private boolean isUnknownMode; 587 private KSubstOption(String option) { 588 this(option, false); 589 } 590 private KSubstOption(String option, boolean isUnknownMode) { 591 super(option); 592 this.isUnknownMode = isUnknownMode; 593 ksubstOptionMap.put(option, this); 594 } 595 601 public static KSubstOption fromMode(String mode) { 602 if (mode.length() == 0) mode = "-kkv"; KSubstOption option = (KSubstOption) ksubstOptionMap.get(mode); 604 if (option == null) option = new KSubstOption(mode, true); 605 return option; 606 } 607 613 public static KSubstOption fromFile(IFile file) { 614 if (CVSProviderPlugin.isText(file)) 615 return getDefaultTextMode(); 616 return KSUBST_BINARY; 617 } 618 621 public static KSubstOption[] getAllKSubstOptions() { 622 return (KSubstOption[]) ksubstOptionMap.values().toArray(new KSubstOption[ksubstOptionMap.size()]); 623 } 624 629 public String toEntryLineMode() { 630 if (KSUBST_TEXT_EXPAND.equals(this)) return ""; return getOption(); 632 } 633 634 637 public String toMode(){ 638 return getOption(); 639 } 640 641 645 public boolean isBinary() { 646 return KSUBST_BINARY.equals(this); 647 } 648 651 public String getShortDisplayText() { 652 if (isUnknownMode) 653 return NLS.bind(CVSMessages.KSubstOption_unknown_short, new String [] { option }); 654 if (option.equals("-kb")) return CVSMessages.KSubstOption__kb_short; 656 if (option.equals("-kkv")) return CVSMessages.KSubstOption__kkv_short; 658 if (option.equals("-ko")) return CVSMessages.KSubstOption__ko_short; 660 if (option.equals("-kk")) return CVSMessages.KSubstOption__kk_short; 662 if (option.equals("-kv")) return CVSMessages.KSubstOption__kv_short; 664 if (option.equals("-kkvl")) return CVSMessages.KSubstOption__kkvl_short; 666 return NLS.bind(CVSMessages.KSubstOption_unknown_short, new String [] { option }); 667 } 668 671 public String getLongDisplayText() { 672 if (isUnknownMode) 673 return NLS.bind(CVSMessages.KSubstOption_unknown_long, new String [] { option }); 674 if (option.equals("-kb")) return CVSMessages.KSubstOption__kb_long; 676 if (option.equals("-kkv")) return CVSMessages.KSubstOption__kkv_long; 678 if (option.equals("-ko")) return CVSMessages.KSubstOption__ko_long; 680 if (option.equals("-kk")) return CVSMessages.KSubstOption__kk_long; 682 if (option.equals("-kv")) return CVSMessages.KSubstOption__kv_long; 684 if (option.equals("-kkvl")) return CVSMessages.KSubstOption__kkvl_long; 686 return NLS.bind(CVSMessages.KSubstOption_unknown_long, new String [] { option }); 687 } 688 691 public static KSubstOption getDefaultTextMode() { 692 return CVSProviderPlugin.getPlugin().getDefaultTextKSubstOption(); 693 } 694 } 695 696 700 public static LocalOption makeArgumentOption(LocalOption option, String argument) { 701 if(argument == null) { 702 argument = ""; } 704 return new LocalOption(option.getOption(), argument); 705 } 706 707 711 public static LocalOption makeTagOption(CVSTag tag) { 712 int type = tag.getType(); 713 switch (type) { 714 case CVSTag.BRANCH: 715 case CVSTag.VERSION: 716 return new LocalOption("-r", tag.getName()); case CVSTag.DATE: 718 return new LocalOption("-D", tag.getName()); default: 720 throw new IllegalArgumentException (CVSMessages.Command_invalidTag); 722 } 723 } 724 725 731 public static Option findOption(Option[] array, String option) { 732 for (int i = 0; i < array.length; ++i) { 733 if (array[i].getOption().equals(option)) return array[i]; 734 } 735 return null; 736 } 737 738 744 protected static String [] collectOptionArguments(Option[] array, String option) { 745 List list = new ArrayList (); 746 for (int i = 0; i < array.length; ++i) { 747 if (array[i].getOption().equals(option)) { 748 list.add(array[i].argument); 749 } 750 } 751 return (String []) list.toArray(new String [list.size()]); 752 } 753 754 764 protected GlobalOption[] filterGlobalOptions(Session session, GlobalOption[] globalOptions) { 765 return session.filterGlobalOptions(globalOptions); 766 } 767 768 777 protected LocalOption[] filterLocalOptions(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions) { 778 return localOptions; 779 } 780 781 797 public final IStatus execute(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions, ICVSResource[] arguments, 798 ICommandOutputListener listener, IProgressMonitor pm) throws CVSException { 799 800 String [] stringArguments = convertArgumentsForOpenSession(arguments, session); 801 return execute(session, globalOptions, localOptions, stringArguments, listener, pm); 802 } 803 804 protected String [] convertArgumentsForOpenSession(ICVSResource[] arguments, Session openSession) throws CVSException { 805 List stringArguments = new ArrayList (arguments.length); 807 for (int i = 0; i < arguments.length; i++) { 808 stringArguments.add(arguments[i].getRelativePath(openSession.getLocalRoot())); 809 } 810 return (String []) stringArguments.toArray(new String [stringArguments.size()]); 811 } 812 813 819 protected IStatus mergeStatus(IStatus accumulatedStatus, IStatus newStatus) { 820 if (accumulatedStatus.isMultiStatus()) { 821 ((MultiStatus)accumulatedStatus).merge(newStatus); 822 return accumulatedStatus; 823 } 824 if (accumulatedStatus.isOK()) return newStatus; 825 if (newStatus.isOK()) return accumulatedStatus; 826 MultiStatus result = new MultiStatus(CVSProviderPlugin.ID, IStatus.INFO, 827 new IStatus[] {accumulatedStatus, newStatus}, 828 NLS.bind(CVSMessages.Command_warnings, new String [] { getDisplayText() }), null); 829 return result; 830 } 831 } 832 | Popular Tags |