int port = location.hasPort() ? location.getPort() : credentials.getPortNumber();
if (port < 0) {
port = 22;
}
if (!isUsePersistentConnection()) {
Connection connection = openConnection(location, credentials, port);
return new SSHConnectionInfo(null, "unpersistent", connection, false);
}
String key = credentials.getUserName() + ":" + location.getHost() + ":" + port;
String id = credentials.getUserName() + ":" + location.getHost() + ":" + port;
if (credentials.getPrivateKeyFile() != null) {
key += ":" + credentials.getPrivateKeyFile().getAbsolutePath();
}
if (credentials.getPassphrase() != null) {
key += ":" + credentials.getPassphrase();
}
if (credentials.getPassword() != null) {
key += ":" + credentials.getPassword();
}
SSHConnectionInfo connectionInfo = null;
LinkedList connectionsList = (LinkedList) ourConnectionsPool.get(key);
if (connectionsList == null) {
connectionsList = new LinkedList();
ourConnectionsPool.put(key, connectionsList);
}
SVNDebugLog.getDefaultLog().info(ourRequestor + ": EXISTING CONNECTIONS COUNT: " + connectionsList.size());
for (Iterator infos = connectionsList.iterator(); infos.hasNext();) {
SSHConnectionInfo info = (SSHConnectionInfo) infos.next();
// ping connection here. if it is stale - close connection and remove it from the pool.
try {
info.myConnection.ping();
} catch (IOException e) {
// ping failed, remove _this_ info only and close its connection.
// the we may check next available connection.
// all channels binded to the closed connection will be closed
// on the next attempt to access them.
SVNDebugLog.getDefaultLog().info(ourRequestor + ": ROTTEN CONNECTION DETECTED, WILL CLOSE IT: " + info);
infos.remove();
// to let it be closed even if it is the last one.
info.setPersistent(false);
SVNDebugLog.getDefaultLog().info(ourRequestor + ": ROTTEN CONNECTION MADE NOT PERSISTENT: " + info);
closeConnection(info);
continue;
}
if (info.getSessionCount() < MAX_SESSIONS_PER_CONNECTION) {
info.resetTimeout();
SVNDebugLog.getDefaultLog().info(ourRequestor + ": REUSING ONE WITH " + info.getSessionCount() + " SESSIONS: " + info.myConnection);
return info;
}
}
SVNDebugLog.getDefaultLog().info(ourRequestor + ": OPENING NEW CONNECTION");
Connection connection = openConnection(location, credentials, port);
connectionInfo = new SSHConnectionInfo(key, id, connection, true);
connectionsList.add(connectionInfo);
SVNDebugLog.getDefaultLog().info(ourRequestor + ": NEW CONNECTION OPENED, TOTAL: " + connectionsList.size());
return connectionInfo;
} finally {