private void copyAgentJar ( TaskListener listener , String workingDirectory ) throws IOException , InterruptedException { String fileName = workingDirectory + SLASH_AGENT_JAR ; listener . getLogger ( ) . println ( Messages . SSHLauncher_StartingSFTPClient ( getTimestamp ( ) ) ) ; SFTPClient sftpClient = null ; try { sftpClient = new SFTPClient ( connection ) ; try { SFTPv3FileAttributes fileAttributes = sftpClient . _stat ( workingDirectory ) ; if ( fileAttributes == null ) { listener . getLogger ( ) . println ( Messages . SSHLauncher_RemoteFSDoesNotExist ( getTimestamp ( ) , workingDirectory ) ) ; sftpClient . mkdirs ( workingDirectory , 0700 ) ; } else if ( fileAttributes . isRegularFile ( ) ) { throw new IOException ( Messages . SSHLauncher_RemoteFSIsAFile ( workingDirectory ) ) ; } listener . getLogger ( ) . println ( Messages . SSHLauncher_CopyingAgentJar ( getTimestamp ( ) ) ) ; byte [ ] agentJar = new Slave . JnlpJar ( AGENT_JAR ) . readFully ( ) ; // If the agent jar already exists see if it needs to be updated boolean overwrite = true ; if ( sftpClient . exists ( fileName ) ) { String sourceAgentHash = getMd5Hash ( agentJar ) ; String existingAgentHash = getMd5Hash ( readInputStreamIntoByteArrayAndClose ( sftpClient . read ( fileName ) ) ) ; listener . getLogger ( ) . println ( MessageFormat . format ( "Source agent hash is {0}. " + "Installed agent hash is {1}" , sourceAgentHash , existingAgentHash ) ) ; overwrite = ! sourceAgentHash . equals ( existingAgentHash ) ; } if ( overwrite ) { try { // try to delete the file in case the agent we are copying is shorter than the agent // that is already there sftpClient . rm ( fileName ) ; } catch ( IOException e ) { // the file did not exist... so no need to delete it! } try ( OutputStream os = sftpClient . writeToFile ( fileName ) ) { os . write ( agentJar ) ; listener . getLogger ( ) . println ( Messages . SSHLauncher_CopiedXXXBytes ( getTimestamp ( ) , agentJar . length ) ) ; } catch ( Error error ) { throw error ; } catch ( Throwable e ) { throw new IOException ( Messages . SSHLauncher_ErrorCopyingAgentJarTo ( fileName ) , e ) ; } } else { listener . getLogger ( ) . println ( "Verified agent jar. No update is necessary." ) ; } } catch ( Error error ) { throw error ; } catch ( Throwable e ) { throw new IOException ( Messages . SSHLauncher_ErrorCopyingAgentJarInto ( workingDirectory ) , e ) ; } } catch ( IOException e ) { if ( sftpClient == null ) { e . printStackTrace ( listener . error ( Messages . SSHLauncher_StartingSCPClient ( getTimestamp ( ) ) ) ) ; // lets try to recover if the agent doesn't have an SFTP service copySlaveJarUsingSCP ( listener , workingDirectory ) ; } else { throw e ; } } finally { if ( sftpClient != null ) { sftpClient . close ( ) ; } } }
Method copies the agent jar to the remote system .