reduce reliance on implicit channel close; add close() method to SFTP classes; update examples

This commit is contained in:
Shikhar Bhushan
2010-07-28 23:53:26 +01:00
parent fb97ccb67c
commit 2882129211
11 changed files with 70 additions and 43 deletions

View File

@@ -30,6 +30,7 @@ import net.schmizz.sshj.connection.channel.forwarded.ForwardedChannelOpener;
import net.schmizz.sshj.transport.Transport;
import net.schmizz.sshj.transport.TransportException;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
@@ -248,9 +249,9 @@ public class ConnectionImpl
public void notifyDisconnect()
throws SSHException {
super.notifyDisconnect();
FutureUtils.alertAll(new ConnectionException("Disconnected."), globalReqFutures);
for (Channel chan : channels.values())
chan.finishOff();
final ConnectionException ex = new ConnectionException("Disconnected.");
FutureUtils.alertAll(ex, globalReqFutures);
ErrorNotifiable.Util.alertAll(ex, new HashSet<Channel>(channels.values()));
}
}

View File

@@ -302,8 +302,7 @@ public abstract class AbstractChannel
rwin.expand(howMuch);
}
@Override
public void finishOff() {
protected void finishOff() {
conn.forget(this);
close.set();
}

View File

@@ -117,9 +117,6 @@ public interface Channel
/** @return whether the channel is open. */
boolean isOpen();
/** Called when this channel's end-of-life has been reached. Subclasses may override but must call super. */
void finishOff();
/**
* Sends an EOF message to the server for this channel; indicating that no more data will be sent by us. The {@code
* OutputStream} for this channel will be closed and no longer usable.

View File

@@ -20,6 +20,7 @@ import net.schmizz.sshj.xfer.FilePermission;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
import java.util.Deque;
import java.util.EnumSet;
@@ -27,7 +28,8 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public class SFTPClient {
public class SFTPClient
implements Closeable {
/** Logger */
protected final Logger log = LoggerFactory.getLogger(getClass());
@@ -238,4 +240,10 @@ public class SFTPClient {
xfer.upload(source, dest);
}
@Override
public void close()
throws IOException {
sftp.close();
}
}

View File

@@ -21,6 +21,7 @@ import net.schmizz.sshj.connection.channel.direct.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Closeable;
import java.io.IOException;
import java.io.OutputStream;
import java.util.EnumSet;
@@ -30,7 +31,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
public class SFTPEngine
implements Requester {
implements Requester, Closeable {
/** Logger */
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -233,4 +234,10 @@ public class SFTPEngine
return timeout;
}
@Override
public void close()
throws IOException {
sub.close();
}
}

View File

@@ -421,8 +421,8 @@ public final class TransportImpl
.putInt(reason.toInt())
.putString(message)
.putString(""));
} catch (IOException logged) {
log.warn("Error writing packet: {}", logged);
} catch (IOException worthless) {
log.debug("Error writing packet: {}", worthless.toString());
}
}