}
int reconnect = 1;
while(true) {
SVNSSHAuthentication authentication = (SVNSSHAuthentication) authManager.getFirstAuthentication(ISVNAuthenticationManager.SSH, realm, repository.getLocation());
SSHConnectionInfo connection = null;
// lock SVNSSHSession to make sure connection opening and session creation is atomic.
SVNSSHSession.lock(Thread.currentThread());
try {
while (authentication != null) {
try {
connection = SVNSSHSession.getConnection(repository.getLocation(), authentication, authManager.getConnectTimeout(repository), myIsUseConnectionPing);
if (connection == null) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Cannot connect to ''{0}''", repository.getLocation().setPath("", false));
SVNErrorManager.error(err, SVNLogType.NETWORK);
}
authManager.acknowledgeAuthentication(true, ISVNAuthenticationManager.SSH, realm, null, authentication);
break;
} catch (SVNAuthenticationException e) {
SVNDebugLog.getDefaultLog().logFine(SVNLogType.NETWORK, e);
authManager.acknowledgeAuthentication(false, ISVNAuthenticationManager.SSH, realm, e.getErrorMessage(), authentication);
authentication = (SVNSSHAuthentication) authManager.getNextAuthentication(ISVNAuthenticationManager.SSH, realm, repository.getLocation());
connection = null;
}
}
if (authentication == null) {
SVNErrorManager.cancel("authentication cancelled", SVNLogType.NETWORK);
} else if (connection == null) {
SVNErrorManager.error(SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Can not establish connection to ''{0}''", realm), SVNLogType.NETWORK);
}
try {
mySession = connection.openSession();
SVNAuthentication author = authManager.getFirstAuthentication(ISVNAuthenticationManager.USERNAME, realm, repository.getLocation());
if (author == null) {
SVNErrorManager.cancel("authentication cancelled", SVNLogType.NETWORK);
}
String userName = author.getUserName();
if (userName == null || "".equals(userName.trim())) {
userName = authentication.getUserName();
}
if (author.getUserName() == null || author.getUserName().equals(authentication.getUserName()) ||
"".equals(author.getUserName())) {
repository.setExternalUserName("");
} else {
repository.setExternalUserName(author.getUserName());
}
author = new SVNUserNameAuthentication(userName, author.isStorageAllowed());
authManager.acknowledgeAuthentication(true, ISVNAuthenticationManager.USERNAME, realm, null, author);
if ("".equals(repository.getExternalUserName())) {
mySession.execCommand(SVNSERVE_COMMAND);
} else {
mySession.execCommand(SVNSERVE_COMMAND_WITH_USER_NAME + "\"" + repository.getExternalUserName() + "\"");
}
myOutputStream = mySession.getStdin();
myOutputStream = new BufferedOutputStream(myOutputStream, 16*1024);
myInputStream = mySession.getStdout();
myInputStream = new BufferedInputStream(myInputStream, 16*1024);
new StreamGobbler(mySession.getStderr());
myConnection = connection;
return;
} catch (SocketTimeoutException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_IO_ERROR, "timed out waiting for server", null, SVNErrorMessage.TYPE_ERROR, e);
SVNErrorManager.error(err, e, SVNLogType.NETWORK);
} catch (UnknownHostException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_IO_ERROR, "Unknown host " + e.getMessage(), null, SVNErrorMessage.TYPE_ERROR, e);
SVNErrorManager.error(err, e, SVNLogType.NETWORK);
} catch (ConnectException e) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_IO_ERROR, "connection refused by the server", null, SVNErrorMessage.TYPE_ERROR, e);
SVNErrorManager.error(err, e, SVNLogType.NETWORK);
} catch (IOException e) {
reconnect--;
if (reconnect >= 0) {
// try again, but close session first.
connection.closeSession(mySession);
continue;
}
repository.getDebugLog().logFine(SVNLogType.NETWORK, e);
close(repository);
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.RA_SVN_CONNECTION_CLOSED, "Cannot connect to ''{0}'': {1}", new Object[] {repository.getLocation().setPath("", false), e.getMessage()});