1 11 package org.eclipse.team.internal.ccvs.core; 12 13 import java.io.*; 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.List ; 19 import java.util.Map ; 20 21 import org.eclipse.core.resources.*; 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.core.runtime.preferences.InstanceScope; 24 import org.eclipse.jsch.core.IJSchService; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.team.core.Team; 27 import org.eclipse.team.core.TeamException; 28 import org.eclipse.team.internal.ccvs.core.client.Command; 29 import org.eclipse.team.internal.ccvs.core.client.ConsoleListeners; 30 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption; 31 import org.eclipse.team.internal.ccvs.core.client.Command.QuietOption; 32 import org.eclipse.team.internal.ccvs.core.client.listeners.IConsoleListener; 33 import org.eclipse.team.internal.ccvs.core.mapping.CVSActiveChangeSetCollector; 34 import org.eclipse.team.internal.ccvs.core.resources.FileModificationManager; 35 import org.eclipse.team.internal.ccvs.core.util.*; 36 import org.eclipse.team.internal.core.subscribers.ActiveChangeSetManager; 37 import org.osgi.framework.BundleContext; 38 import org.osgi.util.tracker.ServiceTracker; 39 40 public class CVSProviderPlugin extends Plugin { 41 42 public static final String READ_ONLY = "cvs.read.only"; public static final String ENABLE_WATCH_ON_EDIT = "cvs.watch.on.edit"; 46 public static final String DEFAULT_CVS_RSH = "ssh"; public static final String DEFAULT_CVS_RSH_PARAMETERS = "-l {user} {host}"; public static final String DEFAULT_CVS_SERVER = "cvs"; public static final boolean DEFAULT_PRUNE = true; 54 public static final boolean DEFAULT_CONFIRM_MOVE_TAG = true; 56 public static final boolean DEFAULT_FETCH = true; 58 public static final int DEFAULT_TIMEOUT = 60; 60 public static final int DEFAULT_COMPRESSION_LEVEL = 0; 62 public static final KSubstOption DEFAULT_TEXT_KSUBST_OPTION = Command.KSUBST_TEXT_EXPAND; 64 65 public static final String ID = "org.eclipse.team.cvs.core"; 68 public static final QualifiedName CVS_WORKSPACE_SUBSCRIBER_ID = new QualifiedName("org.eclipse.team.cvs.ui.cvsworkspace-participant", "syncparticipant"); public static final String PT_AUTHENTICATOR = "authenticator"; public static final String PT_CONNECTIONMETHODS = "connectionmethods"; public static final String PT_FILE_MODIFICATION_VALIDATOR = "filemodificationvalidator"; 73 private QuietOption quietness; 74 private int compressionLevel = DEFAULT_COMPRESSION_LEVEL; 75 private KSubstOption defaultTextKSubstOption = DEFAULT_TEXT_KSUBST_OPTION; 76 private boolean usePlatformLineend = true; 77 private int communicationsTimeout = DEFAULT_TIMEOUT; 78 private boolean pruneEmptyDirectories = DEFAULT_PRUNE; 79 private boolean fetchAbsentDirectories = DEFAULT_FETCH; 80 private boolean replaceUnmanaged = true; 81 private boolean repositoriesAreBinary = false; 82 private String cvsRshCommand = DEFAULT_CVS_RSH; 83 private String cvsRshParameters = DEFAULT_CVS_RSH_PARAMETERS; 84 private String cvsServer = DEFAULT_CVS_SERVER; 85 private boolean determineVersionEnabled = true; 86 87 private static volatile CVSProviderPlugin instance; 88 89 private BuildCleanupListener addDeleteMoveListener; 91 private FileModificationManager fileModificationManager; 92 private SyncFileChangeListener metaFileSyncListener; 93 94 private static final String REPOSITORIES_STATE_FILE = ".cvsProviderState"; private static final int REPOSITORIES_STATE_FILE_VERSION_2 = -1; 97 private static List decoratorEnablementListeners = new ArrayList (); 98 99 private CVSWorkspaceSubscriber cvsWorkspaceSubscriber; 100 101 109 private static final String NATURE_ID = ID + ".cvsnature"; 111 private static final String CRASH_INDICATION_FILE = ".running"; private boolean crash; 114 115 private boolean autoShareOnImport; 116 private boolean useProxy; 117 118 public static final String PROXY_TYPE_HTTP = "HTTP"; public static final String PROXY_TYPE_SOCKS5 = "SOCKS5"; public static final String HTTP_DEFAULT_PORT = "80"; public static final String SOCKS5_DEFAULT_PORT = "1080"; 123 private String proxyType; 124 private String proxyHost; 125 private String proxyPort; 126 private boolean useProxyAuth; 127 128 private CVSActiveChangeSetCollector changeSetManager; 129 private ServiceTracker tracker; 130 131 private static final String INFO_PROXY_USER = "org.eclipse.team.cvs.core.proxy.user"; private static final String INFO_PROXY_PASS = "org.eclipse.team.cvs.core.proxy.pass"; 134 private static final URL FAKE_URL; 135 static { 136 URL temp = null; 137 try { 138 temp = new URL ("http://org.eclipse.team.cvs.proxy.auth"); } catch (MalformedURLException e) { 140 } 142 FAKE_URL = temp; 143 } 144 145 146 public synchronized CVSWorkspaceSubscriber getCVSWorkspaceSubscriber() { 147 if (cvsWorkspaceSubscriber == null) { 148 cvsWorkspaceSubscriber = new CVSWorkspaceSubscriber( 149 CVS_WORKSPACE_SUBSCRIBER_ID, 150 CVSMessages.CVSProviderPlugin_20); 151 } 152 return cvsWorkspaceSubscriber; 153 } 154 155 159 public CVSProviderPlugin() { 160 super(); 161 instance = this; 162 } 163 164 167 public static void log(CoreException e) { 168 log(e.getStatus().getSeverity(), e.getMessage(), e); 169 } 170 171 174 public static void log(int severity, String message, Throwable e) { 175 log(new Status(severity, ID, 0, message, e)); 176 } 177 178 182 public static void log(IStatus status) { 183 getPlugin().getLog().log(status); 184 } 185 186 191 public static CVSProviderPlugin getPlugin() { 192 return instance; 193 } 194 195 198 public static String getTypeId() { 199 return NATURE_ID; 200 } 201 202 206 public void setCompressionLevel(int level) { 207 compressionLevel = level; 208 } 209 210 213 public int getCompressionLevel() { 214 return compressionLevel; 215 } 216 217 220 public void setDefaultTextKSubstOption(KSubstOption ksubst) { 221 defaultTextKSubstOption = ksubst; 222 } 223 224 225 228 public KSubstOption getDefaultTextKSubstOption() { 229 return defaultTextKSubstOption; 230 } 231 232 235 public boolean getPruneEmptyDirectories() { 236 return pruneEmptyDirectories; 237 } 238 239 242 public void setPruneEmptyDirectories(boolean prune) { 243 pruneEmptyDirectories = prune; 244 } 245 246 249 public int getTimeout() { 250 return communicationsTimeout; 251 } 252 253 257 public void setTimeout(int timeout) { 258 this.communicationsTimeout = Math.max(0, timeout); 259 } 260 261 265 public void setQuietness(QuietOption option) { 266 this.quietness = option; 267 } 268 269 272 public QuietOption getQuietness() { 273 return quietness; 274 } 275 276 280 public void setConsoleListener(IConsoleListener consoleListener) { 281 ConsoleListeners.getInstance().addListener(consoleListener); 282 } 283 284 287 public void start(BundleContext context) throws Exception { 288 super.start(context); 289 290 loadOldState(); 292 crash = createCrashFile(); 293 294 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 296 addDeleteMoveListener = new BuildCleanupListener(); 297 fileModificationManager = new FileModificationManager(); 298 metaFileSyncListener = new SyncFileChangeListener(); 299 workspace.addResourceChangeListener(addDeleteMoveListener, IResourceChangeEvent.POST_BUILD); 300 workspace.addResourceChangeListener(metaFileSyncListener, IResourceChangeEvent.POST_CHANGE); 301 workspace.addResourceChangeListener(fileModificationManager, IResourceChangeEvent.POST_CHANGE); 302 303 getCVSWorkspaceSubscriber(); 304 305 getChangeSetManager(); 307 308 tracker = new ServiceTracker(getBundle().getBundleContext(), IJSchService.class.getName(), null); 309 tracker.open(); 310 } 311 312 315 public void stop(BundleContext context) throws Exception { 316 try { 317 savePluginPreferences(); 318 319 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 321 workspace.removeResourceChangeListener(metaFileSyncListener); 322 workspace.removeResourceChangeListener(fileModificationManager); 323 workspace.removeResourceChangeListener(addDeleteMoveListener); 324 325 workspace.removeSaveParticipant(this); 328 329 getChangeSetManager().dispose(); 330 331 tracker.close(); 332 333 deleteCrashFile(); 334 } finally { 335 super.stop(context); 336 } 337 } 338 339 342 protected void initializeDefaultPluginPreferences(){ 343 Preferences store = getPluginPreferences(); 344 store.setDefault(READ_ONLY, false); 345 store.setDefault(ENABLE_WATCH_ON_EDIT, false); 346 } 347 348 352 public String getCvsRshCommand() { 353 return cvsRshCommand; 354 } 355 356 360 public void setCvsRshCommand(String cvsRshCommand) { 361 this.cvsRshCommand = cvsRshCommand; 362 } 363 364 368 public String getCvsRshParameters() { 369 return cvsRshParameters; 370 } 371 372 376 public void setCvsRshParameters(String cvsRshParameters) { 377 this.cvsRshParameters = cvsRshParameters; 378 } 379 380 384 public String getCvsServer() { 385 return cvsServer; 386 } 387 388 392 public void setCvsServer(String cvsServer) { 393 this.cvsServer = cvsServer; 394 } 395 396 400 public boolean getFetchAbsentDirectories() { 401 return fetchAbsentDirectories; 402 } 403 404 public boolean getRepositoriesAreBinary() { 405 return repositoriesAreBinary; 406 } 407 408 412 public void setFetchAbsentDirectories(boolean fetchAbsentDirectories) { 413 this.fetchAbsentDirectories = fetchAbsentDirectories; 414 } 415 416 public void setRepositoriesAreBinary(boolean binary) { 417 repositoriesAreBinary = binary; 418 } 419 420 public static void broadcastDecoratorEnablementChanged(final boolean enabled) { 421 ICVSDecoratorEnablementListener[] listeners; 422 synchronized(decoratorEnablementListeners) { 423 listeners = (ICVSDecoratorEnablementListener[]) decoratorEnablementListeners.toArray(new ICVSDecoratorEnablementListener[decoratorEnablementListeners.size()]); 424 } 425 for (int i = 0; i < listeners.length; i++) { 426 final ICVSDecoratorEnablementListener listener = listeners[i]; 427 ISafeRunnable code = new ISafeRunnable() { 428 public void run() throws Exception { 429 listener.decoratorEnablementChanged(enabled); 430 } 431 public void handleException(Throwable e) { 432 } 434 }; 435 Platform.run(code); 436 } 437 } 438 439 443 public boolean isReplaceUnmanaged() { 444 return replaceUnmanaged; 445 } 446 447 451 public void setReplaceUnmanaged(boolean replaceUnmanaged) { 452 this.replaceUnmanaged = replaceUnmanaged; 453 } 454 455 456 459 public void addRepositoryListener(ICVSListener listener) { 460 KnownRepositories.getInstance().addRepositoryListener(listener); 461 } 462 463 468 public void addDecoratorEnablementListener(ICVSDecoratorEnablementListener listener) { 469 synchronized(decoratorEnablementListeners) { 470 decoratorEnablementListeners.add(listener); 471 } 472 } 473 474 477 public void removeRepositoryListener(ICVSListener listener) { 478 KnownRepositories.getInstance().removeRepositoryListener(listener); 479 } 480 481 484 public void removeDecoratorEnablementListener(ICVSDecoratorEnablementListener listener) { 485 synchronized(decoratorEnablementListeners) { 486 decoratorEnablementListeners.remove(listener); 487 } 488 } 489 490 494 public ICVSRepositoryLocation[] getKnownRepositories() { 495 return KnownRepositories.getInstance().getRepositories(); 496 } 497 498 private void loadOldState() { 499 try { 500 IPath pluginStateLocation = CVSProviderPlugin.getPlugin().getStateLocation().append(REPOSITORIES_STATE_FILE); 501 File file = pluginStateLocation.toFile(); 502 if (file.exists()) { 503 try { 504 DataInputStream dis = new DataInputStream(new FileInputStream(file)); 505 readOldState(dis); 506 dis.close(); 507 file.delete(); 510 } catch (IOException e) { 511 throw new TeamException(new Status(IStatus.ERROR, CVSProviderPlugin.ID, TeamException.UNABLE, CVSMessages.CVSProvider_ioException, e)); 512 } 513 } 514 } catch (TeamException e) { 515 Util.logError(CVSMessages.CVSProvider_errorLoading, e); 516 } 517 } 518 519 private void readOldState(DataInputStream dis) throws IOException, CVSException { 520 KnownRepositories instance = KnownRepositories.getInstance(); 521 int count = dis.readInt(); 522 if (count >= 0) { 523 for (int i = 0; i < count; i++) { 525 ICVSRepositoryLocation location = instance.getRepository(dis.readUTF()); 526 instance.addRepository(location, false ); 527 } 528 } else if (count == REPOSITORIES_STATE_FILE_VERSION_2) { 529 count = dis.readInt(); 530 for (int i = 0; i < count; i++) { 531 ICVSRepositoryLocation location = instance.getRepository(dis.readUTF()); 532 instance.addRepository(location, false ); 533 dis.readUTF(); 535 } 536 } else { 537 Util.logError(NLS.bind(CVSMessages.CVSProviderPlugin_unknownStateFileVersion, new String [] { new Integer (count).toString() }), null); 538 } 539 } 540 541 public static boolean isText(IFile file) { 542 if (CVSProviderPlugin.getPlugin().getRepositoriesAreBinary()) return false; 543 return Team.getFileContentManager().getType(file) == Team.TEXT; 544 } 545 546 550 public boolean isDetermineVersionEnabled() { 551 return determineVersionEnabled; 552 } 553 554 558 public void setDetermineVersionEnabled(boolean determineVersionEnabled) { 559 this.determineVersionEnabled = determineVersionEnabled; 560 } 561 562 566 public FileModificationManager getFileModificationManager() { 567 return fileModificationManager; 568 } 569 570 573 public boolean isWatchEditEnabled() { 574 return getPluginPreferences().getBoolean(CVSProviderPlugin.READ_ONLY); 575 } 576 577 public void setDebugProtocol(boolean value) { 578 Policy.DEBUG_CVS_PROTOCOL = value; 579 } 580 581 public boolean isDebugProtocol() { 582 return Policy.DEBUG_CVS_PROTOCOL; 583 } 584 585 590 private boolean createCrashFile() { 591 IPath pluginStateLocation = CVSProviderPlugin.getPlugin().getStateLocation(); 592 File crashFile = pluginStateLocation.append(CRASH_INDICATION_FILE).toFile(); 593 if (crashFile.exists()) { 594 return true; 595 } 596 try { 597 crashFile.createNewFile(); 598 } catch (IOException e) { 599 CVSProviderPlugin.log(IStatus.ERROR, e.getMessage(), e); 600 } 601 return false; 602 } 603 604 private void deleteCrashFile() { 605 IPath pluginStateLocation = CVSProviderPlugin.getPlugin().getStateLocation(); 606 File crashFile = pluginStateLocation.append(CRASH_INDICATION_FILE).toFile(); 607 crashFile.delete(); 608 } 609 610 public boolean crashOnLastRun() { 611 return crash; 612 } 613 614 617 public org.osgi.service.prefs.Preferences getInstancePreferences() { 618 return new InstanceScope().getNode(getBundle().getSymbolicName()); 619 } 620 621 624 public boolean isUsePlatformLineend() { 625 return usePlatformLineend; 626 } 627 630 public void setUsePlatformLineend(boolean usePlatformLineend) { 631 this.usePlatformLineend = usePlatformLineend; 632 } 633 634 public void setAutoshareOnImport(boolean autoShareOnImport) { 635 this.autoShareOnImport = autoShareOnImport; 636 } 637 638 public boolean isAutoshareOnImport() { 639 return autoShareOnImport; 640 } 641 642 645 public boolean isWatchOnEdit() { 646 return getPluginPreferences().getBoolean(CVSProviderPlugin.ENABLE_WATCH_ON_EDIT); 647 } 648 649 651 public void setUseProxy(boolean useProxy) { 652 this.useProxy = useProxy; 653 } 654 655 public boolean isUseProxy() { 656 return this.useProxy; 657 } 658 659 public void setProxyType(String proxyType) { 660 this.proxyType = proxyType; 661 } 662 663 public String getProxyType() { 664 return this.proxyType; 665 } 666 667 public void setProxyHost(String proxyHost) { 668 this.proxyHost = proxyHost; 669 } 670 671 public String getProxyHost() { 672 return this.proxyHost; 673 } 674 675 public void setProxyPort(String proxyPort) { 676 this.proxyPort = proxyPort; 677 } 678 679 public String getProxyPort() { 680 return this.proxyPort; 681 } 682 683 public void setUseProxyAuth(boolean useProxyAuth) { 684 this.useProxyAuth = useProxyAuth; 685 } 686 687 public boolean isUseProxyAuth() { 688 return this.useProxyAuth; 689 } 690 691 public String getProxyUser() { 692 Object user = getAuthInfo().get(INFO_PROXY_USER); 693 return user==null ? "" : (String ) user; } 695 696 public String getProxyPassword() { 697 Object pass = getAuthInfo().get(INFO_PROXY_PASS); 698 return pass==null ? "" : (String ) pass; } 700 701 private Map getAuthInfo() { 702 Map authInfo = Platform.getAuthorizationInfo(FAKE_URL, "proxy", ""); return authInfo!=null ? authInfo : Collections.EMPTY_MAP; 705 } 706 707 public void setProxyAuth(String proxyUser, String proxyPass) { 708 Map authInfo = getAuthInfo(); 709 if (authInfo.size()==0) { 710 authInfo = new java.util.HashMap (4); 711 } 712 if (proxyUser != null) { 713 authInfo.put(INFO_PROXY_USER, proxyUser); 714 } 715 if (proxyPass != null) { 716 authInfo.put(INFO_PROXY_PASS, proxyPass); 717 } 718 try { 719 Platform.addAuthorizationInfo(FAKE_URL, "proxy", "", authInfo); } catch (CoreException e) { 721 CVSProviderPlugin.log(e); 723 } 724 } 725 726 public synchronized ActiveChangeSetManager getChangeSetManager() { 727 if (changeSetManager == null) { 728 changeSetManager = new CVSActiveChangeSetCollector(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber()); 729 } 730 return changeSetManager; 731 } 732 733 public IJSchService getJSchService() { 734 return (IJSchService)tracker.getService(); 735 } 736 737 } 738 | Popular Tags |