obligatory ssh client close()

This commit is contained in:
Jörg Prante 2024-05-14 22:04:36 +02:00
parent 0d0fbe167b
commit a1cd358787
7 changed files with 15 additions and 11 deletions

View file

@ -37,6 +37,7 @@ public class SFTPContext implements Closeable {
@Override
public void close() throws IOException {
sshClient.stop();
sshClient.close();
fileSystem.close();
}
}

View file

@ -472,8 +472,4 @@ public class SFTPFileService implements FileService {
}
}
}
private <T> T performWithoutClose(SFTPContext ctx, WithContext<T> action) throws IOException {
return action.perform(ctx);
}
}

View file

@ -1,18 +1,26 @@
package org.apache.sshd.fs.test;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.xbib.files.FileService;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;
public class FileServiceProviderTest {
@Test
public void testSFTP() throws IOException {
public void testSFTP() throws Exception {
Map<String, ?> env = Map.of("username", "joerg");
FileService fs = FileService.newInstance("sftp://alkmene:22", env);
fs.list(".").forEach(p -> Logger.getAnonymousLogger().info(p.toString()));
// close() is essential!
try (Stream<Path> list = fs.list(".")) {
list.forEach(p -> Logger.getAnonymousLogger().info(p.toString()));
}
// see if a second run still works
try (Stream<Path> list = fs.list(".")) {
list.forEach(p -> Logger.getAnonymousLogger().info(p.toString()));
}
}
}

View file

@ -37,5 +37,6 @@ public class SFTPFileSystemTest {
fileSystem.close();
// ...
sshClient.stop();
sshClient.close();
}
}

View file

@ -652,7 +652,7 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
protected SshFutureListener<IoConnectFuture> createConnectCompletionListener(
ConnectFuture connectFuture, String username, SocketAddress address,
KeyIdentityProvider identities, boolean useDefaultIdentities) {
return new SshFutureListener<IoConnectFuture>() {
return new SshFutureListener<>() {
@Override
@SuppressWarnings("synthetic-access")
public void operationComplete(IoConnectFuture future) {
@ -660,7 +660,6 @@ public class SshClient extends AbstractFactoryManager implements ClientFactoryMa
connectFuture.cancel();
return;
}
Throwable t = future.getException();
if (t != null) {
connectFuture.setException(t);

View file

@ -20,7 +20,6 @@ package org.apache.sshd.common;
import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.channels.Channel;
import java.time.Duration;
import org.apache.sshd.common.future.CloseFuture;

View file

@ -1,3 +1,3 @@
group = org.xbib
name = files
version = 4.4.0
version = 4.5.0