private SshConnection connect ( ) { if ( sshConnection != null && sshConnection . isConnected ( ) ) { return sshConnection ; } while ( ! shutdownInProgress ) { SshConnection ssh = null ; try { logger . debug ( "Connecting..." ) ; ssh = SshConnectionFactory . getConnection ( gerritHostName , gerritSshPort , gerritProxy , authentication , authenticationUpdater ) ; gerritVersion = formatVersion ( ssh . executeCommand ( "gerrit version" ) ) ; logger . debug ( "connection seems ok, returning it." ) ; return ssh ; } catch ( SshConnectException sshConEx ) { logger . error ( "Could not connect to Gerrit server! " + "Host: {} Port: {}" , gerritHostName , gerritSshPort ) ; logger . error ( " Proxy: {}" , gerritProxy ) ; logger . error ( " User: {} KeyFile: {}" , authentication . getUsername ( ) , authentication . getPrivateKeyFile ( ) ) ; logger . error ( "ConnectionException: " , sshConEx ) ; } catch ( SshAuthenticationException sshAuthEx ) { logger . error ( "Could not authenticate to Gerrit server!" + "\n\tUsername: {}\n\tKeyFile: {}\n\tPassword: {}" , new Object [ ] { authentication . getUsername ( ) , authentication . getPrivateKeyFile ( ) , authentication . getPrivateKeyFilePassword ( ) , } ) ; logger . error ( "AuthenticationException: " , sshAuthEx ) ; } catch ( IOException ex ) { logger . error ( "Could not connect to Gerrit server! " + "Host: {} Port: {}" , gerritHostName , gerritSshPort ) ; logger . error ( " Proxy: {}" , gerritProxy ) ; logger . error ( " User: {} KeyFile: {}" , authentication . getUsername ( ) , authentication . getPrivateKeyFile ( ) ) ; logger . error ( "IOException: " , ex ) ; } if ( ssh != null ) { logger . trace ( "Disconnecting bad connection." ) ; try { //The ssh lib used is starting at least one thread for each connection. //The thread isn't shutdown properly when the connection goes down, //so we need to close it "manually" ssh . disconnect ( ) ; } catch ( Exception ex ) { logger . warn ( "Error when disconnecting bad connection." , ex ) ; } finally { ssh = null ; } } if ( ! shutdownInProgress ) { //If we end up here, sleep for a while and then go back up in the loop. logger . trace ( "Sleeping for a bit." ) ; try { Thread . sleep ( CONNECT_SLEEP ) ; } catch ( InterruptedException ex ) { logger . warn ( "Got interrupted while sleeping." , ex ) ; } } } return null ; }
Connects to the Gerrit server and authenticates as the specified user .