Better version (i hope) of using ssh & sftp client objects

This commit is contained in:
2025-02-19 20:17:01 -06:00
parent dcf7218abf
commit 2502d102d9

View File

@ -62,7 +62,43 @@ public class AbstractSFTPAction extends AbstractBaseFilesystemAction<SFTPDirEntr
{ {
private static final QLogger LOG = QLogger.getLogger(AbstractSFTPAction.class); private static final QLogger LOG = QLogger.getLogger(AbstractSFTPAction.class);
private SshClient sshClient; /***************************************************************************
** singleton implementing Initialization-on-Demand Holder idiom
** to help ensure only a single SshClient object exists in a server.
***************************************************************************/
private static class SshClientManager
{
/***************************************************************************
**
***************************************************************************/
private static class Holder
{
private static final SshClient INSTANCE = SshClient.setUpDefaultClient();
static
{
INSTANCE.start();
}
}
/***************************************************************************
**
***************************************************************************/
public static SshClient getInstance()
{
return Holder.INSTANCE;
}
}
////////////////////////////////////////////////////////////////
// open clientSessionFirst, then sftpClient //
// and close them in reverse (sftpClient, then clientSession) //
////////////////////////////////////////////////////////////////
private ClientSession clientSession; private ClientSession clientSession;
private SftpClient sftpClient; private SftpClient sftpClient;
@ -152,9 +188,8 @@ public class AbstractSFTPAction extends AbstractBaseFilesystemAction<SFTPDirEntr
} }
}; };
closer.accept(sshClient);
closer.accept(clientSession);
closer.accept(sftpClient); closer.accept(sftpClient);
closer.accept(clientSession);
} }
@ -164,10 +199,7 @@ public class AbstractSFTPAction extends AbstractBaseFilesystemAction<SFTPDirEntr
***************************************************************************/ ***************************************************************************/
protected SftpClient makeConnection(String username, String hostName, Integer port, String password) throws IOException protected SftpClient makeConnection(String username, String hostName, Integer port, String password) throws IOException
{ {
this.sshClient = SshClient.setUpDefaultClient(); this.clientSession = SshClientManager.getInstance().connect(username, hostName, port).verify().getSession();
sshClient.start();
this.clientSession = sshClient.connect(username, hostName, port).verify().getSession();
clientSession.addPasswordIdentity(password); clientSession.addPasswordIdentity(password);
clientSession.auth().verify(); clientSession.auth().verify();