You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/12/16 11:17:15 UTC

[5/5] mina-sshd git commit: Added some more SessionHolder markings to existing entities

Added some more SessionHolder markings to existing entities


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/68844229
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/68844229
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/68844229

Branch: refs/heads/master
Commit: 68844229a070ac3899a8f7df114677ef49011109
Parents: 914b570
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Wed Dec 16 12:16:51 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Wed Dec 16 12:16:51 2015 +0200

----------------------------------------------------------------------
 .../sshd/client/scp/AbstractScpClient.java      |   5 +
 .../org/apache/sshd/client/scp/ScpClient.java   |   4 +-
 .../common/forward/TcpipForwarderFactory.java   |  10 +-
 .../org/apache/sshd/common/scp/ScpHelper.java   | 199 ++++++++++++++-----
 .../session/AbstractConnectionService.java      |  56 +++---
 5 files changed, 188 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/68844229/sshd-core/src/main/java/org/apache/sshd/client/scp/AbstractScpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/scp/AbstractScpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/scp/AbstractScpClient.java
index 40030b6..c96b02c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/scp/AbstractScpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/scp/AbstractScpClient.java
@@ -56,6 +56,11 @@ public abstract class AbstractScpClient extends AbstractLoggingBean implements S
     }
 
     @Override
+    public final ClientSession getSession() {
+        return getClientSession();
+    }
+
+    @Override
     public void download(String remote, String local, Option... options) throws IOException {
         download(remote, local, GenericUtils.isEmpty(options) ? Collections.<Option>emptySet() : GenericUtils.of(options));
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/68844229/sshd-core/src/main/java/org/apache/sshd/client/scp/ScpClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/scp/ScpClient.java b/sshd-core/src/main/java/org/apache/sshd/client/scp/ScpClient.java
index 20ca54e..579f30f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/scp/ScpClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/scp/ScpClient.java
@@ -26,12 +26,14 @@ import java.nio.file.attribute.PosixFilePermission;
 import java.util.Collection;
 import java.util.concurrent.TimeUnit;
 
+import org.apache.sshd.client.session.ClientSession;
 import org.apache.sshd.client.session.ClientSessionHolder;
 import org.apache.sshd.common.scp.ScpTimestamp;
+import org.apache.sshd.common.session.SessionHolder;
 
 /**
  */
-public interface ScpClient extends ClientSessionHolder {
+public interface ScpClient extends SessionHolder<ClientSession>, ClientSessionHolder {
 
     enum Option {
         Recursive,

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/68844229/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipForwarderFactory.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipForwarderFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipForwarderFactory.java
index 4b19b89..beb64cd 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipForwarderFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipForwarderFactory.java
@@ -21,16 +21,16 @@ package org.apache.sshd.common.forward;
 import org.apache.sshd.common.session.ConnectionService;
 
 /**
- * A factory for creating TcpipForwarder objects for client Port forwarding
+ * A factory for creating forwarder objects for client port forwarding
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
 public interface TcpipForwarderFactory {
 
     /**
-     * Creates the TcpipForwarder to be used for TCP/IP port forwards for
-     * this ClientSession.
+     * Creates the forwarder to be used for TCP/IP port forwards for this session.
      *
-     * @param service the service the connections are forwarded through
-     * @return the TcpipForwarder that will listen for connections and set up forwarding
+     * @param service the {@link ConnectionService} the connections are forwarded through
+     * @return the {@link TcpipForwarder} that will listen for connections and set up forwarding
      */
     TcpipForwarder create(ConnectionService service);
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/68844229/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
index 988ef32..d665eec 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/scp/ScpHelper.java
@@ -45,8 +45,11 @@ import java.util.concurrent.TimeUnit;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.file.util.MockPath;
 import org.apache.sshd.common.scp.ScpTransferEventListener.FileOperation;
+import org.apache.sshd.common.session.Session;
+import org.apache.sshd.common.session.SessionHolder;
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.SelectorUtils;
+import org.apache.sshd.common.util.ValidateUtils;
 import org.apache.sshd.common.util.io.DirectoryScanner;
 import org.apache.sshd.common.util.io.IoUtils;
 import org.apache.sshd.common.util.io.LimitInputStream;
@@ -55,7 +58,7 @@ import org.apache.sshd.common.util.logging.AbstractLoggingBean;
 /**
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class ScpHelper extends AbstractLoggingBean {
+public class ScpHelper extends AbstractLoggingBean implements SessionHolder<Session> {
     /**
      * Command prefix used to identify SCP commands
      */
@@ -94,13 +97,21 @@ public class ScpHelper extends AbstractLoggingBean {
     protected final OutputStream out;
     protected final ScpTransferEventListener listener;
 
-    public ScpHelper(InputStream in, OutputStream out, FileSystem fileSystem, ScpTransferEventListener eventListener) {
-        this.in = in;
-        this.out = out;
+    private final Session session;
+
+    public ScpHelper(Session session, InputStream in, OutputStream out, FileSystem fileSystem, ScpTransferEventListener eventListener) {
+        this.session = ValidateUtils.checkNotNull(session, "No session");
+        this.in = ValidateUtils.checkNotNull(in, "No input stream");
+        this.out = ValidateUtils.checkNotNull(out, "No output stream");
         this.fileSystem = fileSystem;
         this.listener = (eventListener == null) ? ScpTransferEventListener.EMPTY : eventListener;
     }
 
+    @Override
+    public Session getSession() {
+        return session;
+    }
+
     public void receiveFileStream(final OutputStream local, final int bufferSize) throws IOException {
         receive(new ScpReceiveLineHandler() {
             @Override
@@ -115,7 +126,8 @@ public class ScpHelper extends AbstractLoggingBean {
                     @Override
                     public OutputStream resolveTargetStream(String name, long length, Set<PosixFilePermission> perms) throws IOException {
                         if (log.isDebugEnabled()) {
-                            log.debug("resolveTargetStream(" + name + ")[" + perms + "][len=" + length + "] started local stream download");
+                            log.debug("resolveTargetStream({}) name={}, perms={}, len={} - started local stream download",
+                                      ScpHelper.this, name, perms, length);
                         }
                         return local;
                     }
@@ -129,7 +141,8 @@ public class ScpHelper extends AbstractLoggingBean {
                     @SuppressWarnings("synthetic-access")
                     public void postProcessReceivedData(String name, boolean preserve, Set<PosixFilePermission> perms, ScpTimestamp time) throws IOException {
                         if (log.isDebugEnabled()) {
-                            log.debug("postProcessReceivedData(" + name + ")[" + perms + "][time=" + time + "] ended local stream download");
+                            log.debug("postProcessReceivedData({}) name={}, perms={}, preserve={} time={}",
+                                      ScpHelper.this, name, perms, preserve, time);
                         }
                     }
 
@@ -142,7 +155,8 @@ public class ScpHelper extends AbstractLoggingBean {
         });
     }
 
-    public void receive(final Path path, final boolean recursive, boolean shouldBeDir, final boolean preserve, final int bufferSize) throws IOException {
+    public void receive(Path local, final boolean recursive, boolean shouldBeDir, final boolean preserve, final int bufferSize) throws IOException {
+        final Path path = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         if (shouldBeDir) {
             LinkOption[] options = IoUtils.getLinkOptions(false);
             Boolean status = IoUtils.checkFileExists(path, options);
@@ -182,21 +196,29 @@ public class ScpHelper extends AbstractLoggingBean {
                 case 'D':
                     isDir = true;
                     line = String.valueOf((char) c) + readLine();
-                    log.debug("Received header: " + line);
+                    if (log.isDebugEnabled()) {
+                        log.debug("receive({}) - Received 'D' header: {}", this, line);
+                    }
                     break;
                 case 'C':
                     line = String.valueOf((char) c) + readLine();
-                    log.debug("Received header: " + line);
+                    if (log.isDebugEnabled()) {
+                        log.debug("receive({}) - Received 'C' header: {}", this, line);
+                    }
                     break;
                 case 'T':
                     line = String.valueOf((char) c) + readLine();
-                    log.debug("Received header: " + line);
+                    if (log.isDebugEnabled()) {
+                        log.debug("receive({}) - Received 'T' header: {}", this, line);
+                    }
                     time = ScpTimestamp.parseTime(line);
                     ack();
                     continue;
                 case 'E':
                     line = String.valueOf((char) c) + readLine();
-                    log.debug("Received header: " + line);
+                    if (log.isDebugEnabled()) {
+                        log.debug("receive({}) - Received 'E' header: {}", this, line);
+                    }
                     ack();
                     return;
                 default:
@@ -212,12 +234,14 @@ public class ScpHelper extends AbstractLoggingBean {
         }
     }
 
-    public void receiveDir(String header, Path path, ScpTimestamp time, boolean preserve, int bufferSize) throws IOException {
+    public void receiveDir(String header, Path local, ScpTimestamp time, boolean preserve, int bufferSize) throws IOException {
+        Path path = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         if (log.isDebugEnabled()) {
-            log.debug("Receiving directory {}", path);
+            log.debug("receiveDir({})[{}] Receiving directory {} - preserve={}, time={}, buffer-size={}",
+                      this, header, path, preserve, time, bufferSize);
         }
         if (!header.startsWith("D")) {
-            throw new IOException("Expected a D message but got '" + header + "'");
+            throw new IOException("Expected a 'D; message but got '" + header + "'");
         }
 
         Set<PosixFilePermission> perms = parseOctalPermissions(header.substring(1, 5));
@@ -276,7 +300,7 @@ public class ScpHelper extends AbstractLoggingBean {
             for (;;) {
                 header = readLine();
                 if (log.isDebugEnabled()) {
-                    log.debug("Received header: " + header);
+                    log.debug("receiveDir({})[{}] Received header: {}", this, file, header);
                 }
                 if (header.startsWith("C")) {
                     receiveFile(header, file, time, preserve, bufferSize);
@@ -300,9 +324,11 @@ public class ScpHelper extends AbstractLoggingBean {
         }
     }
 
-    public void receiveFile(String header, Path path, ScpTimestamp time, boolean preserve, int bufferSize) throws IOException {
+    public void receiveFile(String header, Path local, ScpTimestamp time, boolean preserve, int bufferSize) throws IOException {
+        Path path = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         if (log.isDebugEnabled()) {
-            log.debug("Receiving file {}", path);
+            log.debug("receiveFile({})[{}] Receiving file {} - preserve={}, time={}, buffer-size={}",
+                      this, header, path, preserve, time, bufferSize);
         }
 
         receiveStream(header, new LocalFileScpTargetStreamResolver(path), time, preserve, bufferSize);
@@ -321,14 +347,15 @@ public class ScpHelper extends AbstractLoggingBean {
         final long length = Long.parseLong(header.substring(6, header.indexOf(' ', 6)));
         String name = header.substring(header.indexOf(' ', 6) + 1);
         if (length < 0L) { // TODO consider throwing an exception...
-            log.warn("receiveStream(" + resolver + ") bad length in header: " + header);
+            log.warn("receiveStream({})[{}] bad length in header: {}", this, resolver, header);
         }
 
         // if file size is less than buffer size allocate only expected file size
         int bufSize;
         if (length == 0L) {
             if (log.isDebugEnabled()) {
-                log.debug("receiveStream(" + resolver + ") zero file size (perhaps special file) using copy buffer size=" + MIN_RECEIVE_BUFFER_SIZE);
+                log.debug("receiveStream({})[{}] zero file size (perhaps special file) using copy buffer size={}",
+                          this, resolver, MIN_RECEIVE_BUFFER_SIZE);
             }
             bufSize = MIN_RECEIVE_BUFFER_SIZE;
         } else {
@@ -336,7 +363,8 @@ public class ScpHelper extends AbstractLoggingBean {
         }
 
         if (bufSize < 0) { // TODO consider throwing an exception
-            log.warn("receiveFile(" + resolver + ") bad buffer size (" + bufSize + ") using default (" + MIN_RECEIVE_BUFFER_SIZE + ")");
+            log.warn("receiveStream({})[{}] bad buffer size ({}) using default ({})",
+                     this, resolver, bufSize, MIN_RECEIVE_BUFFER_SIZE);
             bufSize = MIN_RECEIVE_BUFFER_SIZE;
         }
 
@@ -361,12 +389,11 @@ public class ScpHelper extends AbstractLoggingBean {
 
         ack();
         readAck(false);
-
     }
 
     protected void updateFileProperties(Path file, Set<PosixFilePermission> perms, ScpTimestamp time) throws IOException {
         if (log.isTraceEnabled()) {
-            log.trace("updateFileProperties(" + file + ") permissions: " + perms);
+            log.trace("updateFileProperties({}) {} permissions={}, time={}", this, file, perms, time);
         }
         IoUtils.setPermissions(file, perms);
 
@@ -375,7 +402,7 @@ public class ScpHelper extends AbstractLoggingBean {
             FileTime lastModified = FileTime.from(time.getLastModifiedTime(), TimeUnit.MILLISECONDS);
             FileTime lastAccess = FileTime.from(time.getLastAccessTime(), TimeUnit.MILLISECONDS);
             if (log.isTraceEnabled()) {
-                log.trace("updateFileProperties(" + file + ") last-modified=" + lastModified + ", last-access=" + lastAccess);
+                log.trace("updateFileProperties({}) {} last-modified={}, last-access={}", this, file, lastModified, lastAccess);
             }
             view.setTimes(lastModified, lastAccess, null);
         }
@@ -386,18 +413,19 @@ public class ScpHelper extends AbstractLoggingBean {
     }
 
     public String readLine(boolean canEof) throws IOException {
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        for (;;) {
-            int c = in.read();
-            if (c == '\n') {
-                return baos.toString();
-            } else if (c == -1) {
-                if (!canEof) {
-                    throw new EOFException("EOF while await end of line");
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream(Byte.MAX_VALUE)) {
+            for (;;) {
+                int c = in.read();
+                if (c == '\n') {
+                    return baos.toString(StandardCharsets.UTF_8.name());
+                } else if (c == -1) {
+                    if (!canEof) {
+                        throw new EOFException("EOF while await end of line");
+                    }
+                    return null;
+                } else {
+                    baos.write(c);
                 }
-                return null;
-            } else {
-                baos.write(c);
             }
         }
     }
@@ -426,12 +454,18 @@ public class ScpHelper extends AbstractLoggingBean {
                         sendFile(file, preserve, bufferSize);
                     } else if (Files.isDirectory(file, options)) {
                         if (!recursive) {
+                            if (log.isDebugEnabled()) {
+                                log.debug("send({}) {}: not a regular file", this, path);
+                            }
                             out.write(ScpHelper.WARNING);
                             out.write((path.replace(File.separatorChar, '/') + " not a regular file\n").getBytes(StandardCharsets.UTF_8));
                         } else {
                             sendDir(file, preserve, bufferSize);
                         }
                     } else {
+                        if (log.isDebugEnabled()) {
+                            log.debug("send({}) {}: unknown file type", this, path);
+                        }
                         out.write(ScpHelper.WARNING);
                         out.write((path.replace(File.separatorChar, '/') + " unknown file type\n").getBytes(StandardCharsets.UTF_8));
                     }
@@ -451,7 +485,8 @@ public class ScpHelper extends AbstractLoggingBean {
         }
     }
 
-    protected void send(Path file, boolean recursive, boolean preserve, int bufferSize, LinkOption... options) throws IOException {
+    protected void send(Path local, boolean recursive, boolean preserve, int bufferSize, LinkOption... options) throws IOException {
+        Path file = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         Boolean status = IoUtils.checkFileExists(file, options);
         if (status == null) {
             throw new AccessDeniedException("Send file existence status cannot be determined: " + file);
@@ -493,15 +528,16 @@ public class ScpHelper extends AbstractLoggingBean {
         Path abs = lcl.isAbsolute() ? lcl : lcl.toAbsolutePath();
         Path p = abs.normalize();
         if (log.isTraceEnabled()) {
-            log.trace("resolveLocalPath({}) {}", commandPath, p);
+            log.trace("resolveLocalPath({}) {}: {}", this, commandPath, p);
         }
-        
+
         return p;
     }
 
-    public void sendFile(Path path, boolean preserve, int bufferSize) throws IOException {
+    public void sendFile(Path local, boolean preserve, int bufferSize) throws IOException {
+        Path path = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         if (log.isDebugEnabled()) {
-            log.debug("Sending file {}", path);
+            log.debug("sendFile({})[preserve={},buffer-size={}] Sending file {}", this, preserve, bufferSize, path);
         }
 
         sendStream(new LocalFileScpSourceStreamResolver(path), preserve, bufferSize);
@@ -517,7 +553,8 @@ public class ScpHelper extends AbstractLoggingBean {
         int bufSize;
         if (fileSize <= 0L) {
             if (log.isDebugEnabled()) {
-                log.debug("sendStream(" + resolver + ") unknown file size (" + fileSize + ")  perhaps special file - using copy buffer size=" + MIN_SEND_BUFFER_SIZE);
+                log.debug("sendStream({})[{}] unknown file size ({}) perhaps special file - using copy buffer size={}",
+                          this, resolver, fileSize, MIN_SEND_BUFFER_SIZE);
             }
             bufSize = MIN_SEND_BUFFER_SIZE;
         } else {
@@ -525,15 +562,20 @@ public class ScpHelper extends AbstractLoggingBean {
         }
 
         if (bufSize < 0) { // TODO consider throwing an exception
-            log.warn("sendStream(" + resolver + ") bad buffer size (" + bufSize + ") using default (" + MIN_SEND_BUFFER_SIZE + ")");
+            log.warn("sendStream({})[{}] bad buffer size ({}) using default ({})",
+                     this, resolver, bufSize, MIN_SEND_BUFFER_SIZE);
             bufSize = MIN_SEND_BUFFER_SIZE;
         }
 
         ScpTimestamp time = resolver.getTimestamp();
         if (preserve && (time != null)) {
             String cmd = "T" + TimeUnit.MILLISECONDS.toSeconds(time.getLastModifiedTime())
-                    + ' ' + '0' + ' ' + TimeUnit.MILLISECONDS.toSeconds(time.getLastAccessTime())
-                    + ' ' + '0' + '\n';
+                    + " " + "0" + " " + TimeUnit.MILLISECONDS.toSeconds(time.getLastAccessTime())
+                    + " " + "0" + "\n";
+            if (log.isDebugEnabled()) {
+                log.debug("sendStream({})[{}] send timestamp={} command: {}",
+                          this, resolver, time, cmd.substring(0, cmd.length() - 1));
+            }
             out.write(cmd.getBytes(StandardCharsets.UTF_8));
             out.flush();
             readAck(false);
@@ -542,7 +584,11 @@ public class ScpHelper extends AbstractLoggingBean {
         Set<PosixFilePermission> perms = EnumSet.copyOf(resolver.getPermissions());
         String octalPerms = preserve ? getOctalPermissions(perms) : "0644";
         String fileName = resolver.getFileName();
-        String cmd = "C" + octalPerms + ' ' + fileSize + ' ' + fileName + '\n';
+        String cmd = "C" + octalPerms + " " + fileSize + " " + fileName + "\n";
+        if (log.isDebugEnabled()) {
+            log.debug("sendStream({})[{}] send 'C' command: {}",
+                      this, resolver, cmd.substring(0, cmd.length() - 1));
+        }
         out.write(cmd.getBytes(StandardCharsets.UTF_8));
         out.flush();
         readAck(false);
@@ -562,24 +608,39 @@ public class ScpHelper extends AbstractLoggingBean {
         readAck(false);
     }
 
-    public void sendDir(Path path, boolean preserve, int bufferSize) throws IOException {
+    public void sendDir(Path local, boolean preserve, int bufferSize) throws IOException {
+        Path path = ValidateUtils.checkNotNull(local, "No local path").normalize().toAbsolutePath();
         if (log.isDebugEnabled()) {
-            log.debug("Sending directory {}", path);
+            log.debug("sendDir({}) Sending directory {} - preserve={}, buffer-size={}",
+                      this, path, preserve, bufferSize);
         }
 
         BasicFileAttributes basic = Files.getFileAttributeView(path, BasicFileAttributeView.class).readAttributes();
         if (preserve) {
-            out.write(("T" + basic.lastModifiedTime().to(TimeUnit.SECONDS) + " "
-                    + "0" + " " + basic.lastAccessTime().to(TimeUnit.SECONDS) + " "
-                    + "0" + "\n").getBytes(StandardCharsets.UTF_8));
+            FileTime lastModified = basic.lastModifiedTime();
+            FileTime lastAccess = basic.lastAccessTime();
+            String cmd = "T" + lastModified.to(TimeUnit.SECONDS) + " "
+                    + "0" + " " + lastAccess.to(TimeUnit.SECONDS) + " "
+                    + "0" + "\n";
+            if (log.isDebugEnabled()) {
+                log.debug("sendDir({})[{}] send last-modified={}, last-access={} command: {}",
+                          this, path, lastModified,  lastAccess, cmd.substring(0, cmd.length() - 1));
+            }
+
+            out.write(cmd.getBytes(StandardCharsets.UTF_8));
             out.flush();
             readAck(false);
         }
 
         LinkOption[] options = IoUtils.getLinkOptions(false);
         Set<PosixFilePermission> perms = IoUtils.getPermissions(path, options);
-        out.write(("D" + (preserve ? getOctalPermissions(perms) : "0755") + " "
-                + "0" + " " + path.getFileName().toString() + "\n").getBytes(StandardCharsets.UTF_8));
+        String cmd = "D" + (preserve ? getOctalPermissions(perms) : "0755") + " "
+                + "0" + " " + path.getFileName().toString() + "\n";
+        if (log.isDebugEnabled()) {
+            log.debug("sendDir({})[{}] send 'D' command: {}",
+                      this, path, cmd.substring(0, cmd.length() - 1));
+        }
+        out.write(cmd.getBytes(StandardCharsets.UTF_8));
         out.flush();
         readAck(false);
 
@@ -602,6 +663,9 @@ public class ScpHelper extends AbstractLoggingBean {
             }
         }
 
+        if (log.isDebugEnabled()) {
+            log.debug("sendDir({})[{}] send 'E' command", this, path);
+        }
         out.write("E\n".getBytes(StandardCharsets.UTF_8));
         out.flush();
         readAck(false);
@@ -699,20 +763,45 @@ public class ScpHelper extends AbstractLoggingBean {
         int c = in.read();
         switch (c) {
             case -1:
+                if (log.isDebugEnabled()) {
+                    log.debug("readAck({})[EOF={}] received EOF", this, canEof);
+                }
                 if (!canEof) {
                     throw new EOFException("readAck - EOF before ACK");
                 }
                 break;
             case OK:
+                if (log.isDebugEnabled()) {
+                    log.debug("readAck({})[EOF={}] read OK", this, canEof);
+                }
                 break;
-            case WARNING:
-                log.warn("Received warning: " + readLine());
+            case WARNING: {
+                if (log.isDebugEnabled()) {
+                    log.debug("readAck({})[EOF={}] read warning message", this, canEof);
+                }
+
+                String line = readLine();
+                log.warn("readAck({})[EOF={}] - Received warning: {}", this, canEof, line);
                 break;
-            case ERROR:
-                throw new IOException("Received nack: " + readLine());
+            }
+            case ERROR: {
+                if (log.isDebugEnabled()) {
+                    log.debug("readAck({})[EOF={}] read error message", this, canEof);
+                }
+                String line = readLine();
+                if (log.isDebugEnabled()) {
+                    log.debug("readAck({})[EOF={}] received error: {}", this, canEof, line);
+                }
+                throw new IOException("Received nack: " + line);
+            }
             default:
                 break;
         }
         return c;
     }
+
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + "[" + getSession() + "]";
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/68844229/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
index 179d67c..176c867 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractConnectionService.java
@@ -79,8 +79,6 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
      */
     protected final AtomicInteger nextChannelId = new AtomicInteger(0);
 
-    private final S session;
-
     /**
      * The tcpip forwarder
      */
@@ -89,17 +87,18 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
     protected final X11ForwardSupport x11Forward;
     private final AtomicBoolean allowMoreSessions = new AtomicBoolean(true);
 
+    private final S sessionInstance;
+
     protected AbstractConnectionService(S session) {
-        this.session = session;
-        FactoryManager manager = session.getFactoryManager();
+        sessionInstance = ValidateUtils.checkNotNull(session, "No session");
         agentForward = new AgentForwardSupport(this);
         x11Forward = new X11ForwardSupport(this);
 
-        TcpipForwarderFactory factory = ValidateUtils.checkNotNull(
-                manager.getTcpipForwarderFactory(),
-                "No forwarder factory",
-                GenericUtils.EMPTY_OBJECT_ARRAY);
-        tcpipForwarder = factory.create(this);
+        FactoryManager manager =
+                ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
+        TcpipForwarderFactory factory =
+                ValidateUtils.checkNotNull(manager.getTcpipForwarderFactory(), "No forwarder factory");
+        tcpipForwarder = ValidateUtils.checkNotNull(factory.create(this), "No forwarder created for %s", session);
     }
 
     public Collection<Channel> getChannels() {
@@ -108,7 +107,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
 
     @Override
     public S getSession() {
-        return session;
+        return sessionInstance;
     }
 
     @Override
@@ -124,7 +123,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
     @Override
     protected Closeable getInnerCloseable() {
         return builder()
-                .sequential(tcpipForwarder, agentForward, x11Forward)
+                .sequential(getTcpipForwarder(), agentForward, x11Forward)
                 .parallel(channels.values())
                 .build();
     }
@@ -135,6 +134,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
 
     @Override
     public int registerChannel(Channel channel) throws IOException {
+        final Session session = getSession();
         int maxChannels = PropertyResolverUtils.getIntProperty(session, MAX_CONCURRENT_CHANNELS_PROP, DEFAULT_MAX_CHANNELS);
         int curSize = channels.size();
         if (curSize > maxChannels) {
@@ -152,7 +152,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
         }
 
         if (log.isDebugEnabled()) {
-            log.debug("registerChannel(id={}) {}", Integer.valueOf(channelId), channel);
+            log.debug("registerChannel({})[id={}] {}", this, channelId, channel);
         }
         return channelId;
     }
@@ -386,6 +386,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
             return;
         }
 
+        final Session session = getSession();
         FactoryManager manager = ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
         final Channel channel = NamedFactory.Utils.create(manager.getChannelFactories(), type);
         if (channel == null) {
@@ -444,6 +445,7 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
                       this, sender, SshConstants.getOpenErrorCodeName(reasonCode), lang, message);
         }
 
+        final Session session = getSession();
         Buffer buf = session.prepareBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, BufferUtils.clear(buffer));
         buf.putInt(sender);
         buf.putInt(reasonCode);
@@ -462,20 +464,24 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
         String req = buffer.getString();
         boolean wantReply = buffer.getBoolean();
         if (log.isDebugEnabled()) {
-            log.debug("Received SSH_MSG_GLOBAL_REQUEST {} want-reply={}", req, Boolean.valueOf(wantReply));
+            log.debug("globalRequest({}) received SSH_MSG_GLOBAL_REQUEST {} want-reply={}",
+                      this, req, Boolean.valueOf(wantReply));
         }
 
-        List<RequestHandler<ConnectionService>> handlers = session.getFactoryManager().getGlobalRequestHandlers();
+        Session session = getSession();
+        FactoryManager manager =
+                ValidateUtils.checkNotNull(session.getFactoryManager(), "No factory manager");
+        List<RequestHandler<ConnectionService>> handlers = manager.getGlobalRequestHandlers();
         if (GenericUtils.size(handlers) > 0) {
             for (RequestHandler<ConnectionService> handler : handlers) {
                 RequestHandler.Result result;
                 try {
                     result = handler.process(this, req, wantReply, buffer);
                 } catch (Exception e) {
-                    log.warn("globalRequest({})[want-reply={}] failed ({}) to process: {}",
-                             req, wantReply, e.getClass().getSimpleName(), e.getMessage());
+                    log.warn("globalRequest({})[{}, want-reply={}] failed ({}) to process: {}",
+                             this, req, wantReply, e.getClass().getSimpleName(), e.getMessage());
                     if (log.isDebugEnabled()) {
-                        log.debug("globalRequest(" + req + ")[want-reply=" + wantReply + "] failure details", e);
+                        log.debug("globalRequest(" + this + ")[" + req + ", want-reply=" + wantReply + "] failure details", e);
                     }
                     result = RequestHandler.Result.ReplyFailure;
                 }
@@ -483,8 +489,8 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
                 // if Unsupported then check the next handler in line
                 if (RequestHandler.Result.Unsupported.equals(result)) {
                     if (log.isTraceEnabled()) {
-                        log.trace("{}#process({})[want-reply={}] : {}",
-                                  handler.getClass().getSimpleName(), req, wantReply, result);
+                        log.trace("globalRequest({}) {}#process({})[want-reply={}] : {}",
+                                  this, handler.getClass().getSimpleName(), req, wantReply, result);
                     }
                 } else {
                     sendResponse(buffer, req, result, wantReply);
@@ -493,13 +499,13 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
             }
         }
 
-        log.warn("Unknown global request: {}", req);
+        log.warn("globalRequest({}) unknown global request: {}", this, req);
         sendResponse(buffer, req, RequestHandler.Result.Unsupported, wantReply);
     }
 
     protected void sendResponse(Buffer buffer, String req, RequestHandler.Result result, boolean wantReply) throws IOException {
         if (log.isDebugEnabled()) {
-            log.debug("sendResponse({}) result={}, want-reply={}", req, result, Boolean.valueOf(wantReply));
+            log.debug("sendResponse({})[{}] result={}, want-reply={}", this, req, result, Boolean.valueOf(wantReply));
         }
 
         if (RequestHandler.Result.Replied.equals(result) || (!wantReply)) {
@@ -515,20 +521,20 @@ public abstract class AbstractConnectionService<S extends AbstractSession> exten
         buffer.rpos(5);
         buffer.wpos(5);
         buffer.putByte(cmd);
-        session.writePacket(buffer);
+        getSession().writePacket(buffer);
     }
 
     protected void requestSuccess(Buffer buffer) throws Exception {
-        session.requestSuccess(buffer);
+        getSession().requestSuccess(buffer);
     }
 
     protected void requestFailure(Buffer buffer) throws Exception {
-        session.requestFailure(buffer);
+        getSession().requestFailure(buffer);
     }
 
     @Override
     public String toString() {
-        return getClass().getSimpleName() + "[" + session + "]";
+        return getClass().getSimpleName() + "[" + getSession() + "]";
     }
 
 }