1 11 package org.eclipse.team.internal.ccvs.core.connection; 12 13 14 import java.io.IOException ; 15 import java.net.MalformedURLException ; 16 import java.net.URL ; 17 import java.util.*; 18 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.core.runtime.jobs.ILock; 23 import org.eclipse.core.runtime.jobs.Job; 24 import org.eclipse.core.runtime.preferences.DefaultScope; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.team.core.TeamException; 27 import org.eclipse.team.internal.ccvs.core.*; 28 import org.eclipse.team.internal.ccvs.core.client.*; 29 import org.eclipse.team.internal.ccvs.core.resources.*; 30 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 31 import org.osgi.service.prefs.BackingStoreException; 32 import org.osgi.service.prefs.Preferences; 33 34 53 public class CVSRepositoryLocation extends PlatformObject implements ICVSRepositoryLocation, IUserInfo { 54 55 59 public static final String PREF_REPOSITORIES_NODE = "repositories"; 61 65 private static final String DEFAULT_REPOSITORY_SETTINGS_NODE = "default_repository_settings"; 67 public static final String PREF_LOCATION = "location"; public static final String PREF_SERVER_ENCODING = "encoding"; 71 public static final int UNDETERMINED_PLATFORM = 0; 73 public static final int CVS_SERVER = 1; 74 public static final int CVSNT_SERVER = 2; 75 public static final int UNSUPPORTED_SERVER = 3; 76 public static final int UNKNOWN_SERVER = 4; 77 78 private static IUserAuthenticator authenticator; 80 private static IConnectionMethod[] pluggedInConnectionMethods = null; 81 82 private static Map hostLocks = new HashMap(); 85 86 private IConnectionMethod method; 87 private String user; 88 private String password; 89 private String host; 90 private int port; 91 private String root; 92 private boolean userFixed; 93 private boolean passwordFixed; 94 private boolean allowCaching; 95 96 private int serverPlatform = UNDETERMINED_PLATFORM; 97 98 public static final char COLON = ':'; 99 public static final char SEMICOLON = ';'; 100 public static final char HOST_SEPARATOR = '@'; 101 public static final char PORT_SEPARATOR = '#'; 102 public static final boolean STANDALONE_MODE = (System.getProperty("eclipse.cvs.standalone")==null) ? false :(Boolean.valueOf(System.getProperty("eclipse.cvs.standalone")).booleanValue()); 105 private static final String INVOKE_SVR_CMD = "server"; 108 public static final String INFO_PASSWORD = "org.eclipse.team.cvs.core.password"; public static final String INFO_USERNAME = "org.eclipse.team.cvs.core.username"; public static final String AUTH_SCHEME = ""; public static final URL FAKE_URL; 113 114 117 public static final String USER_VARIABLE = "{user}"; public static final String PASSWORD_VARIABLE = "{password}"; public static final String HOST_VARIABLE = "{host}"; public static final String PORT_VARIABLE = "{port}"; 122 126 private static String extProxy; 127 128 133 private boolean previousAuthenticationFailed = false; 134 135 static { 136 URL temp = null; 137 try { 138 temp = new URL ("http://org.eclipse.team.cvs.core"); } catch (MalformedURLException e) { 140 } 142 FAKE_URL = temp; 143 } 144 145 149 public static Preferences getParentPreferences() { 150 return CVSProviderPlugin.getPlugin().getInstancePreferences().node(PREF_REPOSITORIES_NODE); 151 } 152 153 158 public static Preferences getDefaultPreferences() { 159 Preferences defaults = new DefaultScope().getNode(CVSProviderPlugin.ID).node(DEFAULT_REPOSITORY_SETTINGS_NODE); 160 defaults.put(PREF_SERVER_ENCODING, getDefaultEncoding()); 161 return defaults; 162 } 163 164 private static String getDefaultEncoding() { 165 return System.getProperty("file.encoding", "UTF-8"); } 167 168 175 public static void setExtConnectionMethodProxy(String string) { 176 extProxy = string; 177 } 178 179 191 public static CVSRepositoryLocation fromProperties(Properties configuration) throws CVSException { 192 String connection = configuration.getProperty("connection"); if (connection == null) 195 connection = "pserver"; IConnectionMethod method = getPluggedInConnectionMethod(connection); 197 if (method == null) 198 throw new CVSException(new Status(IStatus.ERROR, CVSProviderPlugin.ID, TeamException.UNABLE, NLS.bind(CVSMessages.CVSRepositoryLocation_methods, (new Object [] {getPluggedInConnectionMethodNames()})), null)); String user = configuration.getProperty("user"); if (user.length() == 0) 201 user = null; 202 String password = configuration.getProperty("password"); if (user == null) 204 password = null; 205 String host = configuration.getProperty("host"); if (host == null) 207 throw new CVSException(new Status(IStatus.ERROR, CVSProviderPlugin.ID, TeamException.UNABLE, CVSMessages.CVSRepositoryLocation_hostRequired, null)); String portString = configuration.getProperty("port"); int port; 210 if (portString == null) 211 port = ICVSRepositoryLocation.USE_DEFAULT_PORT; 212 else 213 port = Integer.parseInt(portString); 214 String root = configuration.getProperty("root"); if (root == null) 216 throw new CVSException(new Status(IStatus.ERROR, CVSProviderPlugin.ID, TeamException.UNABLE, CVSMessages.CVSRepositoryLocation_rootRequired, null)); 218 String encoding = configuration.getProperty("encoding"); 220 return new CVSRepositoryLocation(method, user, password, host, port, root, encoding, user != null, false); 221 } 222 223 231 public static CVSRepositoryLocation fromString(String location) throws CVSException { 232 try { 233 return fromString(location, false); 234 } catch (CVSException e) { 235 MultiStatus error = new MultiStatus(CVSProviderPlugin.ID, IStatus.ERROR, NLS.bind(CVSMessages.CVSRepositoryLocation_invalidFormat, (new Object [] {location})), null); error.merge(new CVSStatus(IStatus.ERROR, CVSMessages.CVSRepositoryLocation_locationForm)); error.merge(e.getStatus()); 240 throw new CVSException(error); 241 } 242 } 243 244 272 public static CVSRepositoryLocation fromString(String location, boolean validateOnly) throws CVSException { 273 String errorMessage = null; 274 try { 275 errorMessage = CVSMessages.CVSRepositoryLocation_parsingMethod; 277 int start = location.indexOf(COLON); 278 String methodName; 279 int end; 280 int optionStart = location.indexOf(SEMICOLON); 282 HashMap hmOptions = new HashMap(); 283 284 if (start == 0) { 285 end = location.indexOf(COLON, start + 1); 286 287 if (optionStart != -1) { 289 methodName = location.substring(start + 1, optionStart); 291 StringTokenizer stOpt = new StringTokenizer( 293 location.substring(optionStart+1, end), 294 "=;" ); 296 while (stOpt.hasMoreTokens()) { 297 hmOptions.put(stOpt.nextToken(), stOpt.nextToken()); 298 } 299 start = end + 1; 300 } else { 301 methodName = location.substring(start + 1, end); 302 start = end + 1; 303 } 304 } else { 305 methodName = "ext"; start = 0; 308 } 309 310 IConnectionMethod method = getPluggedInConnectionMethod(methodName); 311 if (method == null) 312 throw new CVSException(new CVSStatus(IStatus.ERROR, NLS.bind(CVSMessages.CVSRepositoryLocation_methods, (new Object [] {getPluggedInConnectionMethodNames()})))); 314 errorMessage = CVSMessages.CVSRepositoryLocation_parsingUser; 316 317 end = location.indexOf(HOST_SEPARATOR, start); 318 String user = null; 319 String password = null; 320 if (end != -1) { 323 user = location.substring(start, end); 325 start = user.indexOf(COLON); 327 if (start != -1) { 328 errorMessage = CVSMessages.CVSRepositoryLocation_parsingPassword; 329 password = user.substring(start+1); 330 user = user.substring(0, start); 331 } 332 start = end + 1; 334 } else if (optionStart != -1) { 335 if (hmOptions.containsKey("username")) user = hmOptions.get("username").toString(); if (hmOptions.containsKey("password")) password = hmOptions.get("password").toString(); } 341 342 errorMessage = CVSMessages.CVSRepositoryLocation_parsingHost; 344 end= location.indexOf(COLON, start); 345 int hostEnd = end; 346 if (end == -1) { 347 end = location.indexOf('/', start); 349 hostEnd = end; 350 if (end != -1) end--; 352 } 353 String host = (optionStart != -1) ? hmOptions.get("hostname").toString() : location.substring(start, hostEnd); int port = USE_DEFAULT_PORT; 355 boolean havePort = false; 356 if (hmOptions.containsKey("port")) { port = Integer.parseInt(hmOptions.get("port").toString()); havePort = true; 359 } 360 start = host.indexOf(PORT_SEPARATOR); 362 if (start != -1) { 363 try { 364 errorMessage = CVSMessages.CVSRepositoryLocation_parsingPort; 366 port = Integer.parseInt(host.substring(start+1)); 367 host = host.substring(0, start); 368 havePort = true; 369 } catch (NumberFormatException e) { 370 } 372 } 373 if (!havePort) { 374 errorMessage = CVSMessages.CVSRepositoryLocation_parsingPort; 376 int index = end; 377 char c = location.charAt(++index); 378 String portString = new String (); 379 while (Character.isDigit(c)) { 380 portString += c; 381 c = location.charAt(++index); 382 } 383 if (portString.length() > 0) { 384 end = index - 1; 385 port = Integer.parseInt(portString); 386 } 387 } 388 389 errorMessage = CVSMessages.CVSRepositoryLocation_parsingRoot; 391 start = end + 1; 392 String root = location.substring(start); 393 394 if (validateOnly) 395 throw new CVSException(new CVSStatus(IStatus.OK, CVSMessages.ok)); return new CVSRepositoryLocation(method, user, password, host, port, root, null , (user != null), (password != null)); 397 } 398 catch (IndexOutOfBoundsException e) { 399 IStatus status = new CVSStatus(IStatus.ERROR, errorMessage); 401 throw new CVSException(status); 402 } 403 catch (NumberFormatException e) { 404 IStatus status = new CVSStatus(IStatus.ERROR, errorMessage); 405 throw new CVSException(status); 407 } 408 } 409 410 414 public static IUserAuthenticator getAuthenticator() { 415 if (authenticator == null) { 416 authenticator = getPluggedInAuthenticator(); 417 } 418 return authenticator; 419 } 420 421 425 public static IConnectionMethod[] getPluggedInConnectionMethods() { 426 if(pluggedInConnectionMethods==null) { 427 List connectionMethods = new ArrayList(); 428 429 if (STANDALONE_MODE) { 430 connectionMethods.add(new PServerConnectionMethod()); 431 } else { 432 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(CVSProviderPlugin.ID, CVSProviderPlugin.PT_CONNECTIONMETHODS).getExtensions(); 433 for(int i=0; i<extensions.length; i++) { 434 IExtension extension = extensions[i]; 435 IConfigurationElement[] configs = extension.getConfigurationElements(); 436 if (configs.length == 0) { 437 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("Connection method {0} is missing required fields", new Object [] {extension.getUniqueIdentifier()}), null); continue; 439 } 440 try { 441 IConfigurationElement config = configs[0]; 442 connectionMethods.add(config.createExecutableExtension("run")); } catch (CoreException ex) { 444 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("Could not instantiate connection method for {0}", new Object [] {extension.getUniqueIdentifier()}), ex); } 446 } 447 } 448 pluggedInConnectionMethods = (IConnectionMethod[])connectionMethods.toArray(new IConnectionMethod[0]); 449 } 450 return pluggedInConnectionMethods; 451 } 452 453 457 private static IConnectionMethod getPluggedInConnectionMethod(String methodName) { 458 Assert.isNotNull(methodName); 459 IConnectionMethod[] methods = getPluggedInConnectionMethods(); 460 for(int i=0; i<methods.length; i++) { 461 if(methodName.equals(methods[i].getName())) 462 return methods[i]; 463 } 464 return null; 465 } 466 467 471 private static String getPluggedInConnectionMethodNames() { 472 IConnectionMethod[] methods = getPluggedInConnectionMethods(); 473 StringBuffer methodNames = new StringBuffer (); 474 for(int i=0; i<methods.length; i++) { 475 String name = methods[i].getName(); 476 if (i>0) 477 methodNames.append(", "); methodNames.append(name); 479 } 480 return methodNames.toString(); 481 } 482 483 486 private static IUserAuthenticator getPluggedInAuthenticator() { 487 IExtension[] extensions = Platform.getExtensionRegistry().getExtensionPoint(CVSProviderPlugin.ID, CVSProviderPlugin.PT_AUTHENTICATOR).getExtensions(); 488 if (extensions.length == 0) 489 return null; 490 IExtension extension = extensions[0]; 491 IConfigurationElement[] configs = extension.getConfigurationElements(); 492 if (configs.length == 0) { 493 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("User autheticator {0} is missing required fields", (new Object [] {extension.getUniqueIdentifier()})), null); return null; 495 } 496 try { 497 IConfigurationElement config = configs[0]; 498 return (IUserAuthenticator) config.createExecutableExtension("run"); } catch (CoreException ex) { 500 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind("Unable to instantiate user authenticator {0}", (new Object [] {extension.getUniqueIdentifier()})), ex); return null; 502 } 503 } 504 505 508 private CVSRepositoryLocation(IConnectionMethod method, String user, String password, String host, int port, String root, String encoding, boolean userFixed, boolean passwordFixed) { 509 this.method = method; 510 this.user = user; 511 this.password = password; 512 this.host = host; 513 this.port = port; 514 this.root = root; 515 if (userFixed && (user != null)) 517 this.userFixed = true; 518 if (userFixed && passwordFixed && (password != null)) 520 this.passwordFixed = true; 521 if (encoding != null) { 522 setEncoding(encoding); 523 } 524 } 525 526 531 private Connection createConnection(String password, IProgressMonitor monitor) throws CVSException { 532 IConnectionMethod methodToUse = method; 533 if (method.getName().equals("ext") && extProxy != null && !extProxy.equals(method.getName())) { methodToUse = getPluggedInConnectionMethod(extProxy); 535 } 536 Connection connection = new Connection(this, methodToUse.createConnection(this, password)); 537 connection.open(monitor); 538 return connection; 539 } 540 541 546 public void dispose() { 547 flushCache(); 548 try { 549 if (hasPreferences()) { 550 internalGetPreferences().removeNode(); 551 getParentPreferences().flush(); 552 } 553 } catch (BackingStoreException e) { 554 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind(CVSMessages.CVSRepositoryLocation_73, new String [] { getLocation(true) }), e); 555 } 556 } 557 558 561 private void flushCache() { 562 try { 563 Platform.flushAuthorizationInfo(FAKE_URL, getLocation(), AUTH_SCHEME); 564 } catch (CoreException e) { 565 CVSProviderPlugin.log(e); 569 } 570 } 571 572 575 public String getHost() { 576 return host; 577 } 578 579 586 public String getLocation() { 587 return getLocation(false); 588 } 589 590 public String getLocation(boolean forDisplay) { 591 return COLON + method.getName() + COLON + 592 (userFixed?(user + 593 ((passwordFixed && !forDisplay)?(COLON + password):"") + HOST_SEPARATOR):"") + host + COLON + 596 ((port == USE_DEFAULT_PORT)?"":(new Integer (port).toString())) + root; 598 } 599 600 603 public IConnectionMethod getMethod() { 604 return method; 605 } 606 607 610 public int getPort() { 611 return port; 612 } 613 614 617 public String getEncoding() { 618 if (hasPreferences()) { 619 return internalGetPreferences().get(PREF_SERVER_ENCODING, getDefaultEncoding()); 620 } else { 621 return getDefaultEncoding(); 622 } 623 } 624 625 628 public void setEncoding(String encoding) { 629 if (encoding == null || encoding == getDefaultEncoding()) { 630 if (hasPreferences()) { 631 internalGetPreferences().remove(PREF_SERVER_ENCODING); 632 } 633 } else { 634 ensurePreferencesStored(); 635 internalGetPreferences().put(PREF_SERVER_ENCODING, encoding); 636 flushPreferences(); 637 } 638 } 639 640 643 public ICVSRemoteResource[] members(CVSTag tag, boolean modules, IProgressMonitor progress) throws CVSException { 644 try { 645 if (modules) { 646 return RemoteModule.getRemoteModules(this, tag, progress); 647 } else { 648 RemoteFolder root = new RemoteFolder(null, this, ICVSRemoteFolder.REPOSITORY_ROOT_FOLDER_NAME, tag); 649 ICVSRemoteResource[] resources = root.members(progress); 650 List folders = new ArrayList(resources.length); 653 for (int i = 0; i < resources.length; i++) { 654 ICVSRemoteResource remoteResource = resources[i]; 655 if (remoteResource.isContainer()) { 656 folders.add(remoteResource); 657 } 658 } 659 return (ICVSRemoteResource[]) folders.toArray(new ICVSRemoteResource[folders.size()]); 660 } 661 } catch (CVSException e){ 662 throw e; 664 } catch(TeamException e1) { 665 throw new CVSException(e1.getStatus()); 666 } 667 } 668 669 672 public ICVSRemoteFolder getRemoteFolder(String remotePath, CVSTag tag) { 673 return new RemoteFolder(null, this, remotePath, tag); 674 } 675 676 679 public ICVSRemoteFile getRemoteFile(String remotePath, CVSTag tag) { 680 IPath path = new Path(null, remotePath); 681 RemoteFolderTree remoteFolder = new RemoteFolderTree(null, this, path.removeLastSegments(1).toString(), tag); 682 RemoteFile remoteFile = new RemoteFile(remoteFolder, Update.STATE_ADDED_LOCAL, path.lastSegment(), null, null, tag); 683 remoteFolder.setChildren(new ICVSRemoteResource[] { remoteFile }); 684 return remoteFile; 685 } 686 687 690 public String getRootDirectory() { 691 return root; 692 } 693 694 700 public int getTimeout() { 701 return CVSProviderPlugin.getPlugin().getTimeout(); 702 } 703 704 707 public IUserInfo getUserInfo(boolean makeUsernameMutable) { 708 return new UserInfo(getUsername(), password, makeUsernameMutable ? true : isUsernameMutable()); 709 } 710 711 715 public String getUsername() { 716 if (user == null && isUsernameMutable()) { 718 retrievePassword(); 719 } 720 return user == null ? "" : user; } 722 723 726 public boolean isUsernameMutable() { 727 return !userFixed; 728 } 729 730 744 public Connection openConnection(IProgressMonitor monitor) throws CVSException { 745 Policy.checkCanceled(monitor); 747 ILock hostLock; 748 synchronized(hostLocks) { 749 hostLock = (ILock)hostLocks.get(getHost()); 750 if (hostLock == null) { 751 hostLock = Job.getJobManager().newLock(); 752 hostLocks.put(getHost(), hostLock); 753 } 754 } 755 try { 756 hostLock.acquire(); 757 monitor.beginTask(NLS.bind(CVSMessages.CVSRepositoryLocation_openingConnection, new String [] { getHost() }), 2); 759 ensureLocationCached(); 760 boolean cacheNeedsUpdate = false; 761 if (previousAuthenticationFailed) { 763 promptForUserInfo(null); 764 cacheNeedsUpdate = true; 766 } 767 while (true) { 768 try { 769 String password = this.password; 771 if (password == null) { 772 password = retrievePassword(); 774 } 775 if (user == null) { 776 throw new CVSAuthenticationException(CVSMessages.CVSRepositoryLocation_usernameRequired, CVSAuthenticationException.RETRY, this, null); 778 } 779 Connection connection = createConnection(password, monitor); 782 if (cacheNeedsUpdate) 783 updateCachedLocation(); 784 previousAuthenticationFailed = false; 785 return connection; 786 } catch (CVSAuthenticationException ex) { 787 previousAuthenticationFailed = true; 788 if (ex.getRetryStatus() == CVSAuthenticationException.RETRY) { 789 String message = ex.getMessage(); 790 promptForUserInfo(message); 791 cacheNeedsUpdate = true; 793 } else { 794 throw ex; 795 } 796 } 797 } 798 } finally { 799 hostLock.release(); 800 monitor.done(); 801 } 802 } 803 804 807 private void promptForUserInfo(String message) throws CVSException { 808 IUserAuthenticator authenticator = getAuthenticator(); 809 if (authenticator == null) { 810 throw new CVSAuthenticationException(CVSMessages.CVSRepositoryLocation_noAuthenticator, CVSAuthenticationException.NO_RETRY,this); } 812 authenticator.promptForUserInfo(this, this, message); 813 } 814 815 820 private void ensureLocationCached() { 821 String location = getLocation(); 822 KnownRepositories repositories = KnownRepositories.getInstance(); 823 if (repositories.isKnownRepository(location)) { 824 try { 825 setAuthenticationInformation((CVSRepositoryLocation)repositories.getRepository(location)); 829 } catch (CVSException e) { 830 CVSProviderPlugin.log(e); 832 } 833 } else { 834 repositories.addRepository(this, true ); 837 } 838 } 839 840 844 private void setAuthenticationInformation(CVSRepositoryLocation other) { 845 if (other != this) { 846 if (other.getUserInfoCached()) { 848 this.allowCaching = true; 852 if (!userFixed) this.user = null; 853 if (!passwordFixed) this.password = null; 854 } else { 855 setAllowCaching(false); 858 if (!other.userFixed) 862 this.user = other.user; 863 if (!other.passwordFixed) 864 this.password = other.password; 865 } 866 } 867 } 868 869 874 private void updateCachedLocation() { 875 try { 876 CVSRepositoryLocation known = (CVSRepositoryLocation)KnownRepositories.getInstance().getRepository(getLocation()); 877 known.setAuthenticationInformation(this); 878 } catch (CVSException e) { 879 CVSProviderPlugin.log(e); 881 } 882 } 883 884 887 public String toString() { 888 return getLocation(true); 889 } 890 891 public boolean equals(Object o) { 892 if (this == o) return true; 893 if (!(o instanceof CVSRepositoryLocation)) return false; 894 return getLocation().equals(((CVSRepositoryLocation)o).getLocation()); 895 } 896 public int hashCode() { 897 return getLocation().hashCode(); 898 } 899 900 904 private String retrievePassword() { 905 Map map = Platform.getAuthorizationInfo(FAKE_URL, getLocation(), AUTH_SCHEME); 906 if (map != null) { 907 String username = (String ) map.get(INFO_USERNAME); 908 if (username != null && isUsernameMutable()) 909 setUsername(username); 910 String password = (String ) map.get(INFO_PASSWORD); 911 if (password != null) { 912 return password; 913 } 914 } 915 return null; 916 } 917 920 public void setPassword(String password) { 921 if (passwordFixed) 922 throw new UnsupportedOperationException (); 923 this.password = password; 926 } 927 928 931 public void setUsername(String user) { 932 if (userFixed) 933 throw new UnsupportedOperationException (); 934 this.user = user; 935 } 936 937 public void setUserMuteable(boolean muteable) { 938 userFixed = !muteable; 939 } 940 941 public void setAllowCaching(boolean value) { 942 allowCaching = value; 943 if (allowCaching) 944 updateCache(); 945 else 946 flushCache(); 947 } 948 949 public void updateCache() { 950 if (passwordFixed || ! allowCaching) return; 952 if (password == null && userFixed) return; 954 if (updateCache(user, password)) { 955 password = null; 958 } 959 ensurePreferencesStored(); 960 } 961 962 966 private boolean updateCache(String username, String password) { 967 Map map = Platform.getAuthorizationInfo(FAKE_URL, getLocation(), AUTH_SCHEME); 969 if (map == null) { 970 map = new java.util.HashMap (10); 971 } 972 if (username != null) 973 map.put(INFO_USERNAME, username); 974 if (password != null) 975 map.put(INFO_PASSWORD, password); 976 try { 977 Platform.addAuthorizationInfo(FAKE_URL, getLocation(), AUTH_SCHEME, map); 978 } catch (CoreException e) { 979 CVSProviderPlugin.log(e); 981 return false; 982 } 983 return true; 984 } 985 986 992 public void validateConnection(IProgressMonitor monitor) throws CVSException { 993 try { 994 monitor = Policy.monitorFor(monitor); 995 monitor.beginTask(null, 100); 996 ICVSFolder root = CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()); 997 Session session = new Session(this, root, false ); 998 session.open(Policy.subMonitorFor(monitor, 50), false ); 999 try { 1000 IStatus status = Command.VERSION.execute(session, this, Policy.subMonitorFor(monitor, 50)); 1001 if (! status.isOK()) { 1003 CVSProviderPlugin.log(status); 1004 } 1005 } finally { 1006 session.close(); 1007 monitor.done(); 1008 } 1009 } catch (CVSException e) { 1010 dispose(); 1012 throw e; 1013 } 1014 } 1015 1016 1024 public int getServerPlatform() { 1025 return serverPlatform; 1026 } 1027 1028 1031 public void setServerPlaform(int serverType) { 1032 switch (serverType) { 1034 case CVS_SERVER: 1035 case CVSNT_SERVER: 1036 case UNKNOWN_SERVER: 1037 case UNSUPPORTED_SERVER: 1038 serverPlatform = serverType; 1039 break; 1040 default: 1041 serverPlatform = UNDETERMINED_PLATFORM; 1044 } 1045 } 1046 1047 1050 public void flushUserInfo() { 1051 flushCache(); 1052 } 1053 1054 1057 String [] getExtCommand(String password) throws IOException { 1058 String CVS_RSH = CVSProviderPlugin.getPlugin().getCvsRshCommand(); 1060 String CVS_RSH_PARAMETERS = CVSProviderPlugin.getPlugin().getCvsRshParameters(); 1061 String CVS_SERVER = CVSProviderPlugin.getPlugin().getCvsServer(); 1062 if(CVS_RSH == null || CVS_SERVER == null) { 1063 throw new IOException (CVSMessages.EXTServerConnection_varsNotSet); 1064 } 1065 1066 if (CVS_RSH_PARAMETERS == null || CVS_RSH_PARAMETERS.length() == 0) { 1068 if (port != USE_DEFAULT_PORT) 1069 throw new IOException (CVSMessages.EXTServerConnection_invalidPort); 1070 return new String [] {CVS_RSH, host, "-l", user, CVS_SERVER, INVOKE_SVR_CMD}; } 1072 1073 CVS_RSH_PARAMETERS = stringReplace(CVS_RSH_PARAMETERS, USER_VARIABLE, user); 1075 CVS_RSH_PARAMETERS = stringReplace(CVS_RSH_PARAMETERS, PASSWORD_VARIABLE, password); 1076 CVS_RSH_PARAMETERS = stringReplace(CVS_RSH_PARAMETERS, HOST_VARIABLE, host); 1077 CVS_RSH_PARAMETERS = stringReplace(CVS_RSH_PARAMETERS, PORT_VARIABLE, new Integer (port).toString()); 1078 1079 List commands = new ArrayList(); 1081 commands.add(CVS_RSH); 1082 StringTokenizer tokenizer = new StringTokenizer(CVS_RSH_PARAMETERS); 1083 while (tokenizer.hasMoreTokens()) { 1084 String next = tokenizer.nextToken(); 1085 commands.add(next); 1086 } 1087 commands.add(CVS_SERVER); 1088 commands.add(INVOKE_SVR_CMD); 1089 return (String []) commands.toArray(new String [commands.size()]); 1090 } 1091 1092 1095 private String stringReplace(String string, String oldString, String newString) { 1096 int index = string.toLowerCase().indexOf(oldString); 1097 if (index == -1) return string; 1098 return stringReplace( 1099 string.substring(0, index) + newString + string.substring(index + oldString.length()), 1100 oldString, newString); 1101 } 1102 1103 1110 public String getServerMessageWithoutPrefix(String errorLine, String prefix) { 1111 String message = errorLine; 1112 int firstSpace = message.indexOf(' '); 1113 if(firstSpace != -1) { 1114 message = message.substring(firstSpace + 1); 1116 if (prefix.startsWith("[")) { int closingBracket = message.indexOf("]: "); if (closingBracket == -1) return null; 1122 String realPrefix = message.substring(1, closingBracket); 1124 int space = realPrefix.indexOf(' '); 1126 if (space == -1) return null; 1127 if (realPrefix.indexOf(' ', space +1) != -1) return null; 1128 if (!realPrefix.substring(space +1).equals("aborted")) return null; message = message.substring(closingBracket + 2); 1131 if (message.charAt(0) == ' ') { 1132 message = message.substring(1); 1133 } 1134 return message; 1135 } else { 1136 int colon = message.indexOf(": "); if (colon == -1) return null; 1140 String realPrefix = message.substring(0, colon); 1142 if (realPrefix.indexOf(' ') != -1) return null; 1144 message = message.substring(colon + 1); 1145 if (message.charAt(0) == ' ') { 1146 message = message.substring(1); 1147 } 1148 return message; 1149 } 1150 } 1151 return null; 1153 } 1154 1155 1158 public IUserAuthenticator getUserAuthenticator() { 1159 return getAuthenticator(); 1160 } 1161 1162 1165 public void setUserAuthenticator(IUserAuthenticator authenticator) { 1166 CVSRepositoryLocation.authenticator = authenticator; 1167 } 1168 1169 1172 public Preferences getPreferences() { 1173 if (!hasPreferences()) { 1174 ensurePreferencesStored(); 1175 } 1176 return internalGetPreferences(); 1177 } 1178 1179 private Preferences internalGetPreferences() { 1180 return getParentPreferences().node(getPreferenceName()); 1181 } 1182 1183 private boolean hasPreferences() { 1184 try { 1185 return getParentPreferences().nodeExists(getPreferenceName()); 1186 } catch (BackingStoreException e) { 1187 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind(CVSMessages.CVSRepositoryLocation_74, new String [] { getLocation(true) }), e); 1188 return false; 1189 } 1190 } 1191 1192 1198 private String getPreferenceName() { 1199 return getLocation().replace('/', '%').replace(':', '%'); 1200 } 1201 1202 public void storePreferences() { 1203 Preferences prefs = internalGetPreferences(); 1204 prefs.put(PREF_LOCATION, getLocation()); 1206 flushPreferences(); 1207 } 1208 1209 private void flushPreferences() { 1210 try { 1211 internalGetPreferences().flush(); 1212 } catch (BackingStoreException e) { 1213 CVSProviderPlugin.log(IStatus.ERROR, NLS.bind(CVSMessages.CVSRepositoryLocation_75, new String [] { getLocation(true) }), e); 1214 } 1215 } 1216 1217 private void ensurePreferencesStored() { 1218 if (!hasPreferences()) { 1219 storePreferences(); 1220 } 1221 } 1222 1223 1226 public boolean getUserInfoCached() { 1227 Map map = Platform.getAuthorizationInfo(FAKE_URL, getLocation(), AUTH_SCHEME); 1228 if (map != null) { 1229 String password = (String ) map.get(INFO_PASSWORD); 1230 return (password != null); 1231 } 1232 return false; 1233 } 1234} 1235 | Popular Tags |