obligatory ssh client close()

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

View file

@ -37,6 +37,7 @@ public class SFTPContext implements Closeable {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
sshClient.stop(); sshClient.stop();
sshClient.close();
fileSystem.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; package org.apache.sshd.fs.test;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.files.FileService; import org.xbib.files.FileService;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.logging.Logger; import java.util.logging.Logger;
public class FileServiceProviderTest { public class FileServiceProviderTest {
@Test @Test
public void testSFTP() throws IOException { public void testSFTP() throws Exception {
Map<String, ?> env = Map.of("username", "joerg"); Map<String, ?> env = Map.of("username", "joerg");
FileService fs = FileService.newInstance("sftp://alkmene:22", env); 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(); fileSystem.close();
// ... // ...
sshClient.stop(); sshClient.stop();
sshClient.close();
} }
} }

View file

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

View file

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

View file

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