GH-9272: Close ClientSession from SftpSession

Fixes: #9272

* close ClientSession when closing SftpSession
* fix whitespace issues
* stop the SshClient on bean destruction
* use convenient assertions

(cherry picked from commit a3fb68a831)
This commit is contained in:
darrylsmithUGA
2024-07-01 13:02:35 -04:00
committed by Spring Builds
parent 46c8589a49
commit 587bad90be
10 changed files with 137 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,6 +49,7 @@ import org.apache.sshd.sftp.client.SftpVersionSelector;
import org.apache.sshd.sftp.client.impl.AbstractSftpClient;
import org.apache.sshd.sftp.client.impl.DefaultSftpClient;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.core.io.Resource;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.file.remote.session.SessionFactory;
@@ -74,10 +75,11 @@ import org.springframework.util.Assert;
* @author Auke Zaaiman
* @author Christian Tzolov
* @author Adama Sorho
* @author Darryl Smith
*
* @since 2.0
*/
public class DefaultSftpSessionFactory implements SessionFactory<SftpClient.DirEntry>, SharedSessionCapable {
public class DefaultSftpSessionFactory implements SessionFactory<SftpClient.DirEntry>, SharedSessionCapable, DisposableBean {
private final Lock lock = new ReentrantLock();
@@ -421,6 +423,13 @@ public class DefaultSftpSessionFactory implements SessionFactory<SftpClient.DirE
return new ConcurrentSftpClient(clientSession, initialVersionSelector, errorDataHandler);
}
@Override
public void destroy() throws Exception {
if (this.isInnerClient && this.sshClient != null && this.sshClient.isStarted()) {
this.sshClient.stop();
}
}
/**
* The {@link DefaultSftpClient} extension to lock the {@link #send(int, Buffer)}
* for concurrent interaction.

View File

@@ -25,6 +25,7 @@ import java.time.Duration;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.sftp.SftpModuleProperties;
import org.apache.sshd.sftp.client.SftpClient;
@@ -47,6 +48,7 @@ import org.springframework.util.StringUtils;
* @author Gary Russell
* @author Artem Bilan
* @author Christian Tzolov
* @author Darryl Smith
* @since 2.0
*/
public class SftpSession implements Session<SftpClient.DirEntry> {
@@ -156,6 +158,16 @@ public class SftpSession implements Session<SftpClient.DirEntry> {
catch (IOException ex) {
throw new UncheckedIOException("failed to close an SFTP client", ex);
}
try {
ClientSession session = this.sftpClient.getSession();
if (session != null && session.isOpen()) {
session.close();
}
}
catch (IOException ex) {
throw new UncheckedIOException("failed to close an SFTP client (session)", ex);
}
}
@Override