You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gn...@apache.org on 2014/02/06 15:10:15 UTC

[1/7] git commit: Improve tests to log method / duration to System.out

Updated Branches:
  refs/heads/master 4f9049980 -> 9014ebd27


Improve tests to log method / duration to System.out

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

Branch: refs/heads/master
Commit: 52d3da906c42d3478db6f41a23ac8eec54206155
Parents: 4f90499
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Feb 3 20:25:50 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Feb 3 20:25:50 2014 +0100

----------------------------------------------------------------------
 pom.xml                                         |  2 +-
 .../org/apache/sshd/AbstractSessionTest.java    |  4 +-
 .../test/java/org/apache/sshd/AgentTest.java    |  2 +-
 .../org/apache/sshd/AuthenticationTest.java     |  3 +-
 .../test/java/org/apache/sshd/CipherTest.java   |  3 +-
 .../test/java/org/apache/sshd/ClientTest.java   |  2 +-
 .../java/org/apache/sshd/CompressionTest.java   |  3 +-
 .../java/org/apache/sshd/KeepAliveTest.java     |  2 +-
 .../src/test/java/org/apache/sshd/KexTest.java  |  3 +-
 .../java/org/apache/sshd/KeyReExchangeTest.java |  3 +-
 .../src/test/java/org/apache/sshd/LoadTest.java |  3 +-
 .../org/apache/sshd/PortForwardingLoadTest.java |  3 +-
 .../org/apache/sshd/PortForwardingTest.java     |  3 +-
 .../test/java/org/apache/sshd/RandomTest.java   |  3 +-
 .../src/test/java/org/apache/sshd/ScpTest.java  |  3 +-
 .../test/java/org/apache/sshd/ServerTest.java   |  3 +-
 .../src/test/java/org/apache/sshd/SftpTest.java |  3 +-
 .../java/org/apache/sshd/SpringConfigTest.java  |  3 +-
 .../java/org/apache/sshd/SshServerTest.java     |  3 +-
 .../java/org/apache/sshd/WelcomeBannerTest.java |  3 +-
 .../channel/ChannelPipedInputStreamTest.java    |  3 +-
 .../common/file/nativefs/NativeSshFileTest.java |  3 +-
 .../file/virtualfs/VirtualFileSystemTest.java   |  3 +-
 .../apache/sshd/common/future/FutureTest.java   |  3 +-
 .../org/apache/sshd/common/util/BufferTest.java |  3 +-
 .../sshd/common/util/SttySupportTest.java       |  4 +-
 .../jaas/JaasPasswordAuthenticatorTest.java     |  3 +-
 .../PEMGeneratorHostKeyProviderTest.java        |  3 +-
 .../SimpleGeneratorHostKeyProviderTest.java     |  3 +-
 .../server/shell/InvertedShellWrapperTest.java  |  3 +-
 .../java/org/apache/sshd/util/BaseTest.java     | 48 ++++++++++++++++++++
 31 files changed, 105 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 18a5f0e..07f4cda 100644
--- a/pom.xml
+++ b/pom.xml
@@ -163,7 +163,7 @@
             <dependency>
                 <groupId>junit</groupId>
                 <artifactId>junit</artifactId>
-                <version>4.4</version>
+                <version>4.11</version>
             </dependency>
             <dependency>
                 <groupId>net.sf.jpam</groupId>

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
index 8943be5..230dcc5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
@@ -25,6 +25,8 @@ import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.common.util.Buffer;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
+
+import org.apache.sshd.util.BaseTest;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -33,7 +35,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class AbstractSessionTest {
+public class AbstractSessionTest extends BaseTest {
 
     private MySession session;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
index 0e2b39e..bcdb5fc 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
@@ -48,7 +48,7 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assume.assumeThat;
 
-public class AgentTest {
+public class AgentTest extends BaseTest {
 
     @Test
     public void testAgent() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
index 1d429c3..85ecb10 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
@@ -28,6 +28,7 @@ import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.server.ServerFactoryManager;
 import org.apache.sshd.server.session.ServerSession;
 import org.apache.sshd.server.session.SessionFactory;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.BogusPublickeyAuthenticator;
 import org.apache.sshd.util.Utils;
@@ -39,7 +40,7 @@ import static junit.framework.Assert.assertEquals;
 import static junit.framework.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-public class AuthenticationTest {
+public class AuthenticationTest extends BaseTest {
 
     private static final String WELCOME = "Welcome to SSHD";
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/CipherTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/CipherTest.java b/sshd-core/src/test/java/org/apache/sshd/CipherTest.java
index 0b7e2d1..28e3b68 100644
--- a/sshd-core/src/test/java/org/apache/sshd/CipherTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/CipherTest.java
@@ -33,6 +33,7 @@ import org.apache.sshd.common.cipher.BlowfishCBC;
 import org.apache.sshd.common.cipher.TripleDESCBC;
 import org.apache.sshd.common.random.BouncyCastleRandom;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.JSchLogger;
@@ -48,7 +49,7 @@ import static org.junit.Assert.assertEquals;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class CipherTest {
+public class CipherTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
index 6e0de51..290115b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -62,7 +62,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class ClientTest {
+public class ClientTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
index 881efae..6fc8c5f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
@@ -30,6 +30,7 @@ import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.compression.CompressionDelayedZlib;
 import org.apache.sshd.common.compression.CompressionNone;
 import org.apache.sshd.common.compression.CompressionZlib;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.JSchLogger;
@@ -45,7 +46,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class CompressionTest {
+public class CompressionTest extends BaseTest {
 
     private SshServer sshd;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
index ad83bd5..1e11901 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
@@ -39,7 +39,7 @@ import static org.junit.Assert.assertTrue;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class KeepAliveTest {
+public class KeepAliveTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/KexTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KexTest.java b/sshd-core/src/test/java/org/apache/sshd/KexTest.java
index dd6680c..107a78f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KexTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KexTest.java
@@ -34,6 +34,7 @@ import org.apache.sshd.client.kex.ECDHP521;
 import org.apache.sshd.common.KeyExchange;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.TeeOutputStream;
@@ -50,7 +51,7 @@ import static org.junit.Assert.assertTrue;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class KexTest {
+public class KexTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
index b4170fa..590e3ba 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KeyReExchangeTest.java
@@ -30,6 +30,7 @@ import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.common.Session;
 import org.apache.sshd.common.SessionListener;
 import org.apache.sshd.server.ServerFactoryManager;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.JSchLogger;
@@ -48,7 +49,7 @@ import static org.junit.Assert.assertTrue;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class KeyReExchangeTest {
+public class KeyReExchangeTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
index 5590c0d..3a0886a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
@@ -37,6 +37,7 @@ import org.apache.sshd.common.Cipher;
 import org.apache.sshd.common.KeyExchange;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.cipher.BlowfishCBC;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.TeeOutputStream;
@@ -47,7 +48,7 @@ import static org.junit.Assert.assertArrayEquals;
 import org.junit.Before;
 import org.junit.Test;
 
-public class LoadTest {
+public class LoadTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
index 3ec5629..f27b3ad 100644
--- a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
@@ -48,6 +48,7 @@ import org.apache.mina.core.service.IoHandlerAdapter;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
 import org.apache.sshd.SshServer;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusForwardingFilter;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
@@ -67,7 +68,7 @@ import static org.junit.Assert.assertTrue;
 /**
  * Port forwarding tests
  */
-public class PortForwardingLoadTest {
+public class PortForwardingLoadTest extends BaseTest {
 
     private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java b/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
index 9077951..63db60b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
@@ -36,6 +36,7 @@ import org.apache.sshd.client.channel.ChannelDirectTcpip;
 import org.apache.sshd.client.future.AuthFuture;
 import org.apache.sshd.client.future.ConnectFuture;
 import org.apache.sshd.common.SshdSocketAddress;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusForwardingFilter;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
@@ -54,7 +55,7 @@ import static org.junit.Assert.assertEquals;
 /**
  * Port forwarding tests
  */
-public class PortForwardingTest {
+public class PortForwardingTest extends BaseTest {
 
     private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/RandomTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/RandomTest.java b/sshd-core/src/test/java/org/apache/sshd/RandomTest.java
index 7a17927..2d82ef1 100644
--- a/sshd-core/src/test/java/org/apache/sshd/RandomTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/RandomTest.java
@@ -21,6 +21,7 @@ package org.apache.sshd;
 import org.apache.sshd.common.Random;
 import org.apache.sshd.common.random.BouncyCastleRandom;
 import org.apache.sshd.common.random.JceRandom;
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
 /**
@@ -28,7 +29,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class RandomTest {
+public class RandomTest extends BaseTest {
 
     @Test
     public void testJce() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
index 3ebd7c9..9202c9d 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
@@ -37,6 +37,7 @@ import com.jcraft.jsch.Logger;
 import com.jcraft.jsch.UserInfo;
 import org.apache.sshd.client.ScpClient;
 import org.apache.sshd.server.command.ScpCommandFactory;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.JSchLogger;
@@ -57,7 +58,7 @@ import static org.junit.Assert.fail;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class ScpTest {
+public class ScpTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
index 6cd2a08..2489537 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
@@ -38,6 +38,7 @@ import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.server.sftp.SftpSubsystem;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.Utils;
@@ -54,7 +55,7 @@ import static org.junit.Assert.assertTrue;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class ServerTest {
+public class ServerTest extends BaseTest {
 
     private SshServer sshd;
     private SshClient client;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index 0d15d4e..01744f8 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -37,6 +37,7 @@ import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.server.sftp.SftpSubsystem;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.JSchLogger;
@@ -54,7 +55,7 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
-public class SftpTest {
+public class SftpTest extends BaseTest {
 
     private SshServer sshd;
     private int port;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java b/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
index 4593426..84cf547 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
@@ -24,6 +24,7 @@ import com.jcraft.jsch.Channel;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.Logger;
 import com.jcraft.jsch.UserInfo;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.JSchLogger;
 import org.apache.sshd.util.SimpleUserInfo;
 import org.junit.After;
@@ -36,7 +37,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SpringConfigTest {
+public class SpringConfigTest extends BaseTest {
 
     private ClassPathXmlApplicationContext context;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
index e6461af..3b730f8 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sshd;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.Utils;
@@ -33,7 +34,7 @@ import static org.junit.Assert.assertTrue;
  * @author Kohsuke Kawaguchi
  * @author Michael Heemskerk
  */
-public class SshServerTest {
+public class SshServerTest extends BaseTest {
 
     @Test
     public void stopMethodShouldBeIdempotent() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java b/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
index df57270..788bced 100644
--- a/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
@@ -19,6 +19,7 @@
 package org.apache.sshd;
 
 import org.apache.sshd.client.UserInteraction;
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.BogusPublickeyAuthenticator;
 import org.apache.sshd.util.Utils;
@@ -31,7 +32,7 @@ import java.util.concurrent.atomic.AtomicReference;
 
 import static junit.framework.Assert.assertEquals;
 
-public class WelcomeBannerTest {
+public class WelcomeBannerTest extends BaseTest {
 
     private static final String WELCOME = "Welcome to SSHD";
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java b/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
index dc086f3..d1138a9 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/channel/ChannelPipedInputStreamTest.java
@@ -20,6 +20,7 @@ package org.apache.sshd.common.channel;
 
 import java.util.Arrays;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusChannel;
 import org.junit.Test;
 
@@ -27,7 +28,7 @@ import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
 
-public class ChannelPipedInputStreamTest {
+public class ChannelPipedInputStreamTest extends BaseTest {
 
     @Test
     public void testAvailable() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/file/nativefs/NativeSshFileTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/nativefs/NativeSshFileTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/nativefs/NativeSshFileTest.java
index a941d85..5781df3 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/nativefs/NativeSshFileTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/file/nativefs/NativeSshFileTest.java
@@ -18,11 +18,12 @@
  */
 package org.apache.sshd.common.file.nativefs;
 
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class NativeSshFileTest {
+public class NativeSshFileTest extends BaseTest {
 
     @Test
     public void testResolve() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
index c9e887e..9b806b4 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
@@ -21,6 +21,7 @@ package org.apache.sshd.common.file.virtualfs;
 import java.io.File;
 import java.io.IOException;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.AbstractFactoryManager;
 import org.apache.sshd.common.file.FileSystemView;
@@ -33,7 +34,7 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 
-public class VirtualFileSystemTest {
+public class VirtualFileSystemTest extends BaseTest {
 
     @Test
     public void testNativeFileSystem() throws IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java b/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
index f6b5a8b..145088f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/future/FutureTest.java
@@ -18,13 +18,14 @@
  */
 package org.apache.sshd.common.future;
 
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
 import static junit.framework.Assert.assertNotNull;
 
 /**
  */
-public class FutureTest {
+public class FutureTest extends BaseTest {
 
     @Test
     public void testAwaitUnint() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
index edf2766..355e0cc 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
@@ -23,9 +23,10 @@ import static org.junit.Assert.assertEquals;
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
-public class BufferTest {
+public class BufferTest extends BaseTest {
 
     @Test
     public void testGetLong() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
index 48b29fa..97857f2 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
@@ -18,8 +18,8 @@
  */
 package org.apache.sshd.common.util;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.common.PtyMode;
-import org.apache.sshd.common.util.SttySupport;
 import org.junit.Test;
 
 import java.io.InputStreamReader;
@@ -31,7 +31,7 @@ import java.util.Map;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SttySupportTest {
+public class SttySupportTest extends BaseTest {
 
     @Test
     public void parseOutput1() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java b/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
index 8feffb2..43e98b5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
@@ -33,6 +33,7 @@ import javax.security.auth.callback.NameCallback;
 import javax.security.auth.callback.PasswordCallback;
 import javax.security.auth.callback.UnsupportedCallbackException;
 
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 import org.junit.Before;
 import org.junit.After;
@@ -44,7 +45,7 @@ import static org.junit.Assert.*;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class JaasPasswordAuthenticatorTest {
+public class JaasPasswordAuthenticatorTest extends BaseTest {
 
     @Before
     public void setUp() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
index a85973b..a6355c7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
@@ -25,6 +25,7 @@ import java.io.File;
 import java.security.KeyPair;
 import java.security.spec.ECGenParameterSpec;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.common.KeyPairProvider;
 import org.apache.sshd.common.util.SecurityUtils;
 import org.junit.Test;
@@ -34,7 +35,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class PEMGeneratorHostKeyProviderTest {
+public class PEMGeneratorHostKeyProviderTest extends BaseTest {
 
     @Test
     public void testDSA() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
index 3d7057a..f3d7d94 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertNotNull;
 import java.io.File;
 import java.security.spec.ECGenParameterSpec;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.common.KeyPairProvider;
 import org.apache.sshd.common.util.SecurityUtils;
 import org.junit.Test;
@@ -33,7 +34,7 @@ import org.junit.Test;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public class SimpleGeneratorHostKeyProviderTest {
+public class SimpleGeneratorHostKeyProviderTest extends BaseTest {
 
     @Test
     public void testDSA() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java b/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
index 09594e8..6deb7b8 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
@@ -18,6 +18,7 @@
  */
 package org.apache.sshd.server.shell;
 
+import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusEnvironment;
 import org.apache.sshd.util.BogusExitCallback;
 import org.apache.sshd.util.BogusInvertedShell;
@@ -28,7 +29,7 @@ import java.io.ByteArrayOutputStream;
 
 import static junit.framework.Assert.assertEquals;
 
-public class InvertedShellWrapperTest {
+public class InvertedShellWrapperTest extends BaseTest {
 
     @Test
     public void testStreamsAreFlushedBeforeClosing() throws Exception {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/52d3da90/sshd-core/src/test/java/org/apache/sshd/util/BaseTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/util/BaseTest.java b/sshd-core/src/test/java/org/apache/sshd/util/BaseTest.java
new file mode 100644
index 0000000..d7b38c4
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/util/BaseTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.util;
+
+import org.junit.Rule;
+import org.junit.rules.TestWatcher;
+import org.junit.runner.Description;
+
+/**
+ * TODO Add javadoc
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public abstract class BaseTest extends TestWatcher {
+
+    @Rule
+    public TestWatcher rule = this;
+
+    private long startTime;
+
+    @Override
+    protected void starting(Description description) {
+        System.out.println("\nStarting " + description.getClassName() + ":" + description.getMethodName() + "...\n");
+        startTime = System.currentTimeMillis();
+    }
+
+    @Override
+    protected void finished(Description description) {
+        long duration = System.currentTimeMillis() - startTime;
+        System.out.println("\nFinished " + description.getClassName() + ":" + description.getMethodName() + " in " + duration + " ms\n");
+    }
+}


[7/7] git commit: Fix some synchronisation issues that cause test to sometime fail

Posted by gn...@apache.org.
Fix some synchronisation issues that cause test to sometime fail

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

Branch: refs/heads/master
Commit: 9014ebd27729e463f5d4555577fb2f7589d0e41d
Parents: 9b6b166
Author: Guillaume Nodet <gn...@apache.org>
Authored: Thu Feb 6 12:06:08 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Thu Feb 6 12:06:08 2014 +0100

----------------------------------------------------------------------
 .../client/channel/AbstractClientChannel.java   |  9 ++++++++-
 .../sshd/client/session/ClientSessionImpl.java  | 20 ++++++++++++--------
 .../sshd/server/shell/InvertedShellWrapper.java |  4 ++--
 3 files changed, 22 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9014ebd2/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
index ef53018..1fe7c02 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
@@ -129,7 +129,14 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
 
     @Override
     protected void postClose() {
-        IoUtils.closeQuietly(invertedIn, invertedOut, invertedErr, in, out, err);
+        // Close inverted streams after
+        // If the inverted stream is closed before, there's a small time window
+        // in which we have:
+        //    ChannePipedInputStream#closed = true
+        //    ChannePipedInputStream#writerClosed = false
+        // which leads to an IOException("Pipe closed") when reading.
+        IoUtils.closeQuietly(in, out, err);
+        IoUtils.closeQuietly(invertedIn, invertedOut, invertedErr);
         super.postClose();
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9014ebd2/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 0686e29..c4a7f86 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -161,18 +161,22 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
     }
 
     private String nextServiceName() {
-        return nextServiceFactory.getName();
+        synchronized (lock) {
+            return nextServiceFactory.getName();
+        }
     }
 
     protected void switchToNextService() throws IOException {
-        if (nextService == null) {
-            throw new IllegalStateException("No service available");
+        synchronized (lock) {
+            if (nextService == null) {
+                throw new IllegalStateException("No service available");
+            }
+            currentServiceFactory = nextServiceFactory;
+            currentService = nextService;
+            nextServiceFactory = null;
+            nextService = null;
+            currentService.start();
         }
-        currentServiceFactory = nextServiceFactory;
-        currentService = nextService;
-        nextServiceFactory = null;
-        nextService = null;
-        currentService.start();
     }
 
     public ClientChannel createChannel(String type) throws IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9014ebd2/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java b/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
index d9f539a..c00ce77 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/shell/InvertedShellWrapper.java
@@ -102,7 +102,7 @@ public class InvertedShellWrapper implements Command, SessionAware {
         }
     }
 
-    public void start(Environment env) throws IOException {
+    public synchronized void start(Environment env) throws IOException {
         // TODO propagate the Environment itself and support signal sending.
         shell.start(env.getEnv());
         shellIn = shell.getInputStream();
@@ -115,7 +115,7 @@ public class InvertedShellWrapper implements Command, SessionAware {
         });
     }
 
-    public void destroy() {
+    public synchronized void destroy() {
         shell.destroy();
         if (shutdownExecutor && executor instanceof ExecutorService) {
             ((ExecutorService) executor).shutdown();


[5/7] git commit: [SSHD-228] Introduce a Closeable interface and related utilities

Posted by gn...@apache.org.
[SSHD-228] Introduce a Closeable interface and related utilities

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

Branch: refs/heads/master
Commit: 8baa36e0e50bbd683dd7f4275d194c94504da4f0
Parents: 787f31e
Author: Guillaume Nodet <gn...@apache.org>
Authored: Wed Feb 5 12:59:55 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Wed Feb 5 13:01:38 2014 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/sshd/SshServer.java    |  19 +-
 .../client/channel/AbstractClientChannel.java   |  23 +-
 .../sshd/client/channel/ChannelSession.java     |   3 +-
 .../sshd/client/session/ClientSessionImpl.java  |  15 +-
 .../client/session/ClientUserAuthService.java   |   5 +-
 .../session/ClientUserAuthServiceNew.java       |   6 +-
 .../session/ClientUserAuthServiceOld.java       |   6 +-
 .../java/org/apache/sshd/common/Channel.java    |   2 +-
 .../java/org/apache/sshd/common/Closeable.java  |  44 ++++
 .../java/org/apache/sshd/common/Service.java    |   2 +-
 .../java/org/apache/sshd/common/Session.java    |   2 +-
 .../sshd/common/channel/AbstractChannel.java    | 132 +++++-----
 .../common/forward/DefaultTcpipForwarder.java   |   2 +-
 .../sshd/common/forward/TcpipClientChannel.java |  15 +-
 .../sshd/common/forward/TcpipServerChannel.java |   2 +-
 .../apache/sshd/common/io/IoCloseFuture.java    |  42 ----
 .../org/apache/sshd/common/io/IoSession.java    |   5 +-
 .../apache/sshd/common/io/mina/MinaSession.java |  43 ++--
 .../sshd/common/io/nio2/Nio2Acceptor.java       |   9 +-
 .../apache/sshd/common/io/nio2/Nio2Service.java |  10 +-
 .../apache/sshd/common/io/nio2/Nio2Session.java | 117 ++++-----
 .../session/AbstractConnectionService.java      |  59 ++---
 .../sshd/common/session/AbstractSession.java    |  83 ++-----
 .../apache/sshd/common/util/CloseableUtils.java | 247 +++++++++++++++++++
 .../sshd/server/channel/ChannelSession.java     |  71 +++---
 .../sshd/server/session/ServerSession.java      |   2 +-
 .../server/session/ServerUserAuthService.java   |   8 +-
 .../sshd/server/x11/X11ForwardSupport.java      |  17 +-
 28 files changed, 584 insertions(+), 407 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/SshServer.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/SshServer.java b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
index de45dff..c6ccdd1 100644
--- a/sshd-core/src/main/java/org/apache/sshd/SshServer.java
+++ b/sshd-core/src/main/java/org/apache/sshd/SshServer.java
@@ -30,7 +30,6 @@ import java.util.EnumSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
@@ -62,7 +61,6 @@ import org.apache.sshd.common.file.nativefs.NativeFileSystemFactory;
 import org.apache.sshd.common.forward.DefaultTcpipForwarderFactory;
 import org.apache.sshd.common.forward.TcpipServerChannel;
 import org.apache.sshd.common.future.CloseFuture;
-import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.io.DefaultIoServiceFactory;
 import org.apache.sshd.common.io.IoAcceptor;
 import org.apache.sshd.common.io.IoServiceFactory;
@@ -83,6 +81,7 @@ import org.apache.sshd.common.session.ConnectionService;
 import org.apache.sshd.common.signature.SignatureDSA;
 import org.apache.sshd.common.signature.SignatureECDSA;
 import org.apache.sshd.common.signature.SignatureRSA;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.OsUtils;
 import org.apache.sshd.common.util.SecurityUtils;
 import org.apache.sshd.server.Command;
@@ -364,25 +363,19 @@ public class SshServer extends AbstractFactoryManager implements ServerFactoryMa
     }
 
     public void stop(boolean immediately) throws InterruptedException {
-        List<AbstractSession> sessions = new ArrayList<AbstractSession>();
+        List<AbstractSession> sessions;
         if (acceptor != null) {
             acceptor.unbind();
             sessions = getActiveSessions();
+        } else {
+            sessions = Collections.emptyList();
         }
-        final CountDownLatch latch = new CountDownLatch(sessions.size());
-        SshFutureListener<CloseFuture> listener = new SshFutureListener<CloseFuture>() {
-            public void operationComplete(CloseFuture future) {
-                latch.countDown();
-            }
-        };
-        for (AbstractSession session : sessions) {
-            session.close(immediately).addListener(listener);
-        }
+        CloseFuture future = CloseableUtils.parallel(sessions).close(immediately);
 
         stopSessionTimeoutListener();
 
         if (!immediately) {
-            latch.await();
+            future.await();
         }
         if (acceptor != null) {
             acceptor.dispose();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
index d2eb80d..ef53018 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
@@ -33,6 +33,7 @@ import org.apache.sshd.common.channel.AbstractChannel;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.IoUtils;
 
 /**
@@ -138,22 +139,22 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
             for (;;) {
                 int cond = 0;
                 if (openFuture != null && openFuture.isOpened()) {
-                    cond |= OPENED;
+                    cond |= ClientChannel.OPENED;
                 }
                 if (closeFuture.isClosed()) {
-                    cond |= CLOSED | EOF;
+                    cond |= ClientChannel.CLOSED | ClientChannel.EOF;
                 }
                 if (eof) {
-                    cond |= EOF;
+                    cond |= ClientChannel.EOF;
                 }
                 if (exitStatus != null) {
-                    cond |= EXIT_STATUS;
+                    cond |= ClientChannel.EXIT_STATUS;
                 }
                 if (exitSignal != null) {
-                    cond |= EXIT_SIGNAL;
+                    cond |= ClientChannel.EXIT_SIGNAL;
                 }
                 if ((cond & mask) != 0) {
-                    log.trace("WaitFor call returning on channel {}, mask={}, cond={}", new Object[] { id, mask, cond });
+                    log.trace("WaitFor call returning on channel {}, mask={}, cond={}", new Object[] { this, mask, cond });
                     return cond;
                 }
                 if (timeout > 0) {
@@ -162,19 +163,19 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
                     } else {
                         timeout = t - System.currentTimeMillis();
                         if (timeout <= 0) {
-                            cond |= TIMEOUT;
+                            cond |= ClientChannel.TIMEOUT;
                             return cond;
                         }
                     }
                 }
                 try {
-                    log.trace("Waiting for lock on channel {}, mask={}, cond={}", new Object[] { id, mask, cond });
+                    log.trace("Waiting for lock on channel {}, mask={}, cond={}", new Object[] { this, mask, cond });
                     if (timeout > 0) {
                         lock.wait(timeout);
                     } else {
                         lock.wait();
                     }
-                    log.trace("Lock notified on channel {}", id);
+                    log.trace("Lock notified on channel {}", this);
                 } catch (InterruptedException e) {
                     // Ignore
                 }
@@ -187,7 +188,7 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
             throw new SshException("Session has been closed");
         }
         openFuture = new DefaultOpenFuture(lock);
-        log.debug("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
+        log.debug("Send SSH_MSG_CHANNEL_OPEN on channel {}", this);
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
         buffer.putString(type);
         buffer.putInt(id);
@@ -232,7 +233,7 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
 
     protected void doWriteData(byte[] data, int off, int len) throws IOException {
         // If we're already closing, ignore incoming data
-        if (closing.get()) {
+        if (state.get() != CloseableUtils.AbstractCloseable.OPENED) {
             return;
         }
         if (out != null) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
index a93a138..87f4fbd 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSession.java
@@ -28,6 +28,7 @@ import org.apache.sshd.common.channel.ChannelPipedInputStream;
 import org.apache.sshd.common.channel.ChannelPipedOutputStream;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 
 /**
  * TODO Add javadoc
@@ -99,7 +100,7 @@ public class ChannelSession extends AbstractClientChannel {
                 }
             }
         } catch (Exception e) {
-            if (!closing.get()) {
+            if (state.get() == CloseableUtils.AbstractCloseable.OPENED) {
                 log.info("Caught exception", e);
                 close(false);
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 460a50e..0686e29 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -102,6 +102,18 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         sendKexInit();
     }
 
+    protected Service[] getServices() {
+        Service[] services;
+        if (nextService != null) {
+            services = new Service[] { currentService, nextService };
+        } else if (currentService != null) {
+            services = new Service[] { currentService };
+        } else {
+            services = new Service[0];
+        }
+        return services;
+    }
+
     public ClientFactoryManager getFactoryManager() {
         return (ClientFactoryManager) factoryManager;
     }
@@ -257,7 +269,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
             for (;;) {
                 int cond = 0;
                 if (closeFuture.isClosed()) {
-                    cond |= CLOSED;
+                    cond |= ClientSession.CLOSED;
                 }
                 if (authed) { // authFuture.isSuccess()
                     cond |= AUTHED;
@@ -364,7 +376,6 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
 
     @Override
     public void resetIdleTimeout() {
-
     }
 
     public Map<Object, Object> getMetadataMap() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
index d5c0b27..78a28b2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthService.java
@@ -29,6 +29,7 @@ import org.apache.sshd.common.Session;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -94,9 +95,7 @@ public class ClientUserAuthService implements Service {
         } else if (delegateOld != null) {
             return delegateOld.close(immediately);
         } else {
-            CloseFuture future = new DefaultCloseFuture(session.getLock());
-            future.setClosed();
-            return future;
+            return CloseableUtils.closed();
         }
     }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
index 10304d8..1acd2ff 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
@@ -39,6 +39,7 @@ import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -216,12 +217,11 @@ public class ClientUserAuthServiceNew implements Service {
     }
 
     public CloseFuture close(boolean immediately) {
+        log.debug("Closing authentication service");
         if (!authFuture.isDone()) {
             authFuture.setException(new SshException("Session is closed"));
         }
-        CloseFuture future = new DefaultCloseFuture(session.getLock());
-        future.setClosed();
-        return future;
+        return CloseableUtils.closed();
     }
 
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceOld.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceOld.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceOld.java
index 389fe39..95b63b0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceOld.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceOld.java
@@ -31,6 +31,7 @@ import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -168,12 +169,11 @@ public class ClientUserAuthServiceOld implements Service {
     }
 
     public CloseFuture close(boolean immediately) {
+        log.debug("Closing authentication service");
         if (!authFuture.isDone()) {
             authFuture.setException(new SshException("Session is closed"));
         }
-        CloseFuture future = new DefaultCloseFuture(lock);
-        future.setClosed();
-        return future;
+        return CloseableUtils.closed();
     }
 
     public AuthFuture auth(UserAuth userAuth) throws IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/Channel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Channel.java b/sshd-core/src/main/java/org/apache/sshd/common/Channel.java
index 684898f..d0ec2b3 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Channel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Channel.java
@@ -31,7 +31,7 @@ import org.apache.sshd.common.util.Buffer;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public interface Channel {
+public interface Channel extends Closeable {
 
     int getId();
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/Closeable.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Closeable.java b/sshd-core/src/main/java/org/apache/sshd/common/Closeable.java
new file mode 100644
index 0000000..9c88fa6
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Closeable.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.common;
+
+import org.apache.sshd.common.future.CloseFuture;
+
+/**
+ * A {@code Closeable} is a resource that can be closed.
+ * The close method is invoked to release resources that the object is
+ * holding.
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public interface Closeable {
+
+    /**
+     * Close this resource asynchronously and return a future.
+     * Resources support two closing modes: a graceful mode
+     * which will cleanly close the resource and an immediate mode
+     * which will close the resources abruptly.
+     *
+     * @param immediately <code>true</code> if the resource should be shut down abruptly,
+     *                    <code>false</code> for a graceful close
+     * @return a future
+     */
+    CloseFuture close(boolean immediately);
+
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/Service.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Service.java b/sshd-core/src/main/java/org/apache/sshd/common/Service.java
index 02f2052..1023475 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Service.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Service.java
@@ -29,7 +29,7 @@ import org.apache.sshd.common.util.Buffer;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public interface Service {
+public interface Service extends Closeable {
 
     Session getSession();
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Session.java b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
index c0af02f..d61d220 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
@@ -29,7 +29,7 @@ import org.apache.sshd.common.util.Buffer;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public interface Session {
+public interface Session extends Closeable {
 
     /**
      * Returns the value of the user-defined attribute of this session.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
index a4a90c9..e0585b5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
@@ -21,9 +21,10 @@ package org.apache.sshd.common.channel;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.sshd.common.Channel;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.FactoryManager;
 import org.apache.sshd.common.RequestHandler;
 import org.apache.sshd.common.Session;
@@ -35,31 +36,30 @@ import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.session.ConnectionService;
 import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.common.util.BufferUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.apache.sshd.common.util.CloseableUtils;
 
 /**
  * TODO Add javadoc
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public abstract class AbstractChannel implements Channel {
+public abstract class AbstractChannel extends CloseableUtils.AbstractInnerCloseable implements Channel {
 
     public static final int DEFAULT_WINDOW_SIZE = 0x200000;
     public static final int DEFAULT_PACKET_SIZE = 0x8000;
 
-    protected final Logger log = LoggerFactory.getLogger(getClass());
-    protected final Object lock = new Object();
+    protected static final int CLOSE_SENT = 0x01;
+    protected static final int CLOSE_RECV = 0x02;
+
     protected final Window localWindow = new Window(this, null, getClass().getName().contains(".client."), true);
     protected final Window remoteWindow = new Window(this, null, getClass().getName().contains(".client."), false);
     protected ConnectionService service;
     protected Session session;
     protected int id;
     protected int recipient;
-    protected final CloseFuture closeFuture = new DefaultCloseFuture(lock);
     protected volatile boolean eof;
-    protected final AtomicBoolean closing = new AtomicBoolean();
-    protected boolean closedByOtherSide;
+    protected AtomicInteger gracefulState = new AtomicInteger();
+    private DefaultCloseFuture gracefulFuture = new DefaultCloseFuture(lock);
     protected final List<RequestHandler<Channel>> handlers = new ArrayList<RequestHandler<Channel>>();
 
     public void addRequestHandler(RequestHandler<Channel> handler) {
@@ -89,7 +89,7 @@ public abstract class AbstractChannel implements Channel {
     public void handleRequest(Buffer buffer) throws IOException {
         String req = buffer.getString();
         boolean wantReply = buffer.getBoolean();
-        log.debug("Received SSH_MSG_CHANNEL_REQUEST {} on channel {} (wantReply {})", new Object[] { req, id, wantReply });
+        log.debug("Received SSH_MSG_CHANNEL_REQUEST {} on channel {} (wantReply {})", new Object[] { req, this, wantReply });
         for (RequestHandler<Channel> handler : handlers) {
             RequestHandler.Result result;
             try {
@@ -138,72 +138,56 @@ public abstract class AbstractChannel implements Channel {
         }
     }
 
-    public CloseFuture close(boolean immediately) {
-        if (closing.compareAndSet(false, true)) {
-            if (immediately) {
-                log.debug("Closing channel {} immediately", id);
-                preClose(immediately).addListener(new SshFutureListener<CloseFuture>() {
-                    public void operationComplete(CloseFuture future) {
-                        postClose();
-                        closeFuture.setClosed();
-                        notifyStateChanged();
-                        service.unregisterChannel(AbstractChannel.this);
-                    }
-                });
-            } else {
-                log.debug("Closing channel {} gracefully", id);
-                preClose(immediately).addListener(new SshFutureListener<CloseFuture>() {
-                    public void operationComplete(CloseFuture future) {
-                        log.debug("Send SSH_MSG_CHANNEL_CLOSE on channel {}", id);
-                        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE);
-                        buffer.putInt(recipient);
-                        try {
-                            session.writePacket(buffer).addListener(new SshFutureListener<IoWriteFuture>() {
-                                public void operationComplete(IoWriteFuture future) {
-                                    if (closedByOtherSide) {
-                                        log.debug("Message SSH_MSG_CHANNEL_CLOSE written on channel {}", id);
-                                        postClose();
-                                        closeFuture.setClosed();
-                                        notifyStateChanged();
-                                    }
-                                }
-                            });
-                        } catch (IOException e) {
-                            log.debug("Exception caught while writing SSH_MSG_CHANNEL_CLOSE packet on channel " + id, e);
-                            postClose();
-                            closeFuture.setClosed();
-                            notifyStateChanged();
-                        }
-                    }
-                });
-            }
-        }
-        return closeFuture;
-    }
-
     public void handleClose() throws IOException {
-        log.debug("Received SSH_MSG_CHANNEL_CLOSE on channel {}", id);
-        closedByOtherSide = !closing.get();
-        if (closedByOtherSide) {
+            log.debug("Received SSH_MSG_CHANNEL_CLOSE on channel {}", this);
+        if (gracefulState.compareAndSet(0, CLOSE_RECV)) {
             close(false);
-        } else {
-            postClose();
-            closeFuture.setClosed();
-            notifyStateChanged();
+        } else if (gracefulState.compareAndSet(CLOSE_SENT, CLOSE_SENT | CLOSE_RECV)) {
+            gracefulFuture.setClosed();
         }
     }
 
-    protected CloseFuture preClose(boolean immediately) {
-        CloseFuture future = new DefaultCloseFuture(lock);
-        future.setClosed();
-        return future;
+    protected Closeable getGracefulCloseable() {
+        return new Closeable() {
+            public CloseFuture close(boolean immediately) {
+                if (!immediately) {
+                    log.debug("Send SSH_MSG_CHANNEL_CLOSE on channel {}", AbstractChannel.this);
+                    Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE);
+                    buffer.putInt(recipient);
+                    try {
+                        session.writePacket(buffer).addListener(new SshFutureListener<IoWriteFuture>() {
+                            public void operationComplete(IoWriteFuture future) {
+                                log.debug("Message SSH_MSG_CHANNEL_CLOSE written on channel {}", AbstractChannel.this);
+                                if (gracefulState.compareAndSet(0, CLOSE_SENT)) {
+                                    // Waiting for CLOSE message to come back from the remote side
+                                } else if (gracefulState.compareAndSet(CLOSE_RECV, CLOSE_SENT | CLOSE_RECV)) {
+                                    gracefulFuture.setValue(true);
+                                }
+                            }
+                        });
+                    } catch (IOException e) {
+                        log.debug("Exception caught while writing SSH_MSG_CHANNEL_CLOSE packet on channel " + AbstractChannel.this, e);
+                        close(true);
+                    }
+                } else {
+                    gracefulFuture.setClosed();
+                }
+                return gracefulFuture;
+            }
+        };
+    }
+
+    protected Closeable getInnerCloseable() {
+        return getGracefulCloseable();
     }
 
     protected void postClose() {
+        super.postClose();
+        service.unregisterChannel(AbstractChannel.this);
     }
 
     protected void writePacket(Buffer buffer) throws IOException {
-        if (!closing.get()) {
+        if (state.get() == OPENED) {
             session.writePacket(buffer);
         } else {
             log.debug("Discarding output packet because channel is being closed");
@@ -215,7 +199,7 @@ public abstract class AbstractChannel implements Channel {
         if (len < 0 || len > Buffer.MAX_LEN) {
             throw new IllegalStateException("Bad item length: " + len);
         }
-        log.debug("Received SSH_MSG_CHANNEL_DATA on channel {}", id);
+        log.debug("Received SSH_MSG_CHANNEL_DATA on channel {}", this);
         if (log.isTraceEnabled()) {
             log.trace("Received channel data: {}", BufferUtils.printHex(buffer.array(), buffer.rpos(), len));
         }
@@ -226,7 +210,7 @@ public abstract class AbstractChannel implements Channel {
         int ex = buffer.getInt();
         // Only accept extended data for stderr
         if (ex != 1) {
-            log.debug("Send SSH_MSG_CHANNEL_FAILURE on channel {}", id);
+            log.debug("Send SSH_MSG_CHANNEL_FAILURE on channel {}", this);
             buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE);
             buffer.putInt(recipient);
             writePacket(buffer);
@@ -236,7 +220,7 @@ public abstract class AbstractChannel implements Channel {
         if (len < 0 || len > Buffer.MAX_LEN) {
             throw new IllegalStateException("Bad item length: " + len);
         }
-        log.debug("Received SSH_MSG_CHANNEL_EXTENDED_DATA on channel {}", id);
+        log.debug("Received SSH_MSG_CHANNEL_EXTENDED_DATA on channel {}", this);
         if (log.isTraceEnabled()) {
             log.trace("Received channel extended data: {}", BufferUtils.printHex(buffer.array(), buffer.rpos(), len));
         }
@@ -244,19 +228,19 @@ public abstract class AbstractChannel implements Channel {
     }
 
     public void handleEof() throws IOException {
-        log.debug("Received SSH_MSG_CHANNEL_EOF on channel {}", id);
+        log.debug("Received SSH_MSG_CHANNEL_EOF on channel {}", this);
         eof = true;
         notifyStateChanged();
     }
 
     public void handleWindowAdjust(Buffer buffer) throws IOException {
-        log.debug("Received SSH_MSG_CHANNEL_WINDOW_ADJUST on channel {}", id);
+        log.debug("Received SSH_MSG_CHANNEL_WINDOW_ADJUST on channel {}", this);
         int window = buffer.getInt();
         remoteWindow.expand(window);
     }
 
     public void handleFailure() throws IOException {
-        log.debug("Received SSH_MSG_CHANNEL_FAILURE on channel {}", id);
+        log.debug("Received SSH_MSG_CHANNEL_FAILURE on channel {}", this);
         // TODO: do something to report failed requests?
     }
 
@@ -265,7 +249,7 @@ public abstract class AbstractChannel implements Channel {
     protected abstract void doWriteExtendedData(byte[] data, int off, int len) throws IOException;
 
     protected void sendEof() throws IOException {
-        log.debug("Send SSH_MSG_CHANNEL_EOF on channel {}", id);
+        log.debug("Send SSH_MSG_CHANNEL_EOF on channel {}", this);
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_EOF);
         buffer.putInt(recipient);
         writePacket(buffer);
@@ -284,4 +268,8 @@ public abstract class AbstractChannel implements Channel {
         buffer.putInt(len);
         writePacket(buffer);
     }
+
+    public String toString() {
+        return getClass().getSimpleName() + "[id=" + id + ", recipient=" + recipient + "]";
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
index de9661e..0781ab0 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
@@ -192,7 +192,7 @@ public class DefaultTcpipForwarder implements TcpipForwarder, IoHandler {
     public void sessionClosed(IoSession session) throws Exception {
         TcpipClientChannel channel = (TcpipClientChannel) session.getAttribute(TcpipClientChannel.class);
         if (channel != null) {
-            LOGGER.debug("Session closed, will now close the channel");
+            LOGGER.debug("IoSession {} closed, will now close the channel", session);
             channel.close(false);
         }
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
index 50e66ce..50534b2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
@@ -24,6 +24,7 @@ import java.net.InetSocketAddress;
 import org.apache.sshd.client.channel.AbstractClientChannel;
 import org.apache.sshd.client.future.DefaultOpenFuture;
 import org.apache.sshd.client.future.OpenFuture;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.SshdSocketAddress;
@@ -31,9 +32,9 @@ import org.apache.sshd.common.channel.ChannelOutputStream;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.future.SshFutureListener;
-import org.apache.sshd.common.io.IoCloseFuture;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 
 /**
  * TODO Add javadoc
@@ -79,7 +80,7 @@ public class TcpipClientChannel extends AbstractClientChannel {
             throw new SshException("Session has been closed");
         }
         openFuture = new DefaultOpenFuture(lock);
-        log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
+        log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", this);
         Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
         buffer.putString(type);
         buffer.putInt(id);
@@ -99,14 +100,8 @@ public class TcpipClientChannel extends AbstractClientChannel {
     }
 
     @Override
-    protected synchronized CloseFuture preClose(boolean immediately) {
-        final CloseFuture future = new DefaultCloseFuture(null);
-        serverSession.close(immediately).addListener(new SshFutureListener<IoCloseFuture>() {
-            public void operationComplete(IoCloseFuture f) {
-                future.setClosed();
-            }
-        });
-        return future;
+    protected Closeable getInnerCloseable() {
+        return CloseableUtils.sequential(serverSession, super.getInnerCloseable());
     }
 
     protected synchronized void doWriteData(byte[] data, int off, int len) throws IOException {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
index b1c756c..1781f66 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipServerChannel.java
@@ -110,7 +110,7 @@ public class TcpipServerChannel extends AbstractServerChannel {
         out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.SSH_MSG_CHANNEL_DATA);
         IoHandler handler = new IoHandler() {
             public void messageReceived(IoSession session, Readable message) throws Exception {
-                if (closing.get()) {
+                if (state.get() != OPENED) {
                     log.debug("Ignoring write to channel {} in CLOSING state", id);
                 } else {
                     Buffer buffer = new Buffer();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/IoCloseFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/IoCloseFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/io/IoCloseFuture.java
deleted file mode 100644
index 507fd59..0000000
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/IoCloseFuture.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.sshd.common.io;
-
-import org.apache.sshd.common.future.SshFuture;
-
-/**
- * An {@link org.apache.sshd.common.future.SshFuture} for asynchronous close requests.
- *
- * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
- */
-public interface IoCloseFuture extends SshFuture<IoCloseFuture> {
-
-    /**
-     * Returns <tt>true</tt> if the close request is finished and the target is closed.
-     */
-    boolean isClosed();
-
-    /**
-     * Marks this future as closed and notifies all threads waiting for this
-     * future.  This method is invoked by SSHD internally.  Please do not call
-     * this method directly.
-     */
-    void setClosed();
-
-}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java b/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
index b65861d..9e78fba 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/IoSession.java
@@ -20,10 +20,11 @@ package org.apache.sshd.common.io;
 
 import java.net.SocketAddress;
 
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.util.Buffer;
 
-public interface IoSession {
+public interface IoSession extends Closeable {
 
     /**
      * Returns a unique identifier for this session.  Every session has its own
@@ -75,7 +76,7 @@ public interface IoSession {
      *                    {@code false} to close this session after all queued
      *                    write requests are flushed.
      */
-    IoCloseFuture close(boolean immediately);
+    CloseFuture close(boolean immediately);
 
     /**
      * Returns the IoService that created this session.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
index 0ebf626..0768f1a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/mina/MinaSession.java
@@ -21,19 +21,21 @@ package org.apache.sshd.common.io.mina;
 import java.net.SocketAddress;
 
 import org.apache.mina.core.buffer.IoBuffer;
-import org.apache.mina.core.future.CloseFuture;
+import org.apache.mina.core.future.IoFuture;
 import org.apache.mina.core.future.IoFutureListener;
 import org.apache.mina.core.future.WriteFuture;
+import org.apache.sshd.common.Closeable;
+import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.future.DefaultSshFuture;
-import org.apache.sshd.common.io.IoCloseFuture;
 import org.apache.sshd.common.io.IoService;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 
 /**
  */
-public class MinaSession implements IoSession {
+public class MinaSession extends CloseableUtils.AbstractInnerCloseable implements IoSession {
 
     private final MinaService service;
     private final org.apache.mina.core.session.IoSession session;
@@ -77,27 +79,19 @@ public class MinaSession implements IoSession {
         return session.write(buffer);
     }
 
-    public IoCloseFuture close(boolean immediately) {
-        class Future extends DefaultSshFuture<IoCloseFuture> implements IoCloseFuture {
-            Future(Object lock) {
-                super(lock);
+    @Override
+    protected Closeable getInnerCloseable() {
+        return new Closeable() {
+            public org.apache.sshd.common.future.CloseFuture close(boolean immediately) {
+                final DefaultCloseFuture future = new DefaultCloseFuture(lock);
+                session.close(false).addListener(new IoFutureListener<IoFuture>() {
+                    public void operationComplete(IoFuture f) {
+                        future.setValue(true);
+                    }
+                });
+                return future;
             }
-
-            public boolean isClosed() {
-                return getValue() instanceof Boolean;
-            }
-
-            public void setClosed() {
-                setValue(Boolean.TRUE);
-            }
-        }
-        final IoCloseFuture future = new Future(null);
-        session.close(immediately).addListener(new IoFutureListener<CloseFuture>() {
-            public void operationComplete(CloseFuture cf) {
-                future.setClosed();
-            }
-        });
-        return future;
+        };
     }
 
     public IoWriteFuture write(Buffer buffer) {
@@ -143,4 +137,7 @@ public class MinaSession implements IoSession {
         return service;
     }
 
+    public String toString() {
+        return getClass().getSimpleName() + "[local=" + session.getLocalAddress() + ", remote=" + session.getRemoteAddress() + "]";
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
index 7afdf6c..e8468db 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Acceptor.java
@@ -33,9 +33,11 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.sshd.common.FactoryManager;
+import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.io.IoAcceptor;
 import org.apache.sshd.common.io.IoHandler;
 import org.apache.sshd.common.io.IoSession;
+import org.apache.sshd.common.util.CloseableUtils;
 
 /**
  */
@@ -91,8 +93,11 @@ public class Nio2Acceptor extends Nio2Service implements IoAcceptor {
 
     public void doDispose() {
         unbind();
-        for (IoSession session : sessions.values()) {
-            session.close(true);
+        try {
+            CloseFuture future = CloseableUtils.parallel(sessions.values()).close(true);
+            future.await(5, TimeUnit.SECONDS);
+        } catch (Exception e) {
+            logger.debug("Exception caught while closing session", e);
         }
         for (SocketAddress address : channels.keySet()) {
             try {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
index 64cbc51..4cbf186 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Service.java
@@ -25,13 +25,16 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.sshd.common.FactoryManager;
 import org.apache.sshd.common.RuntimeSshException;
+import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.io.IoHandler;
 import org.apache.sshd.common.io.IoService;
 import org.apache.sshd.common.io.IoSession;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -79,8 +82,11 @@ public abstract class Nio2Service implements IoService {
     }
 
     protected void doDispose() {
-        for (IoSession session : sessions.values()) {
-            session.close(true);
+        try {
+            CloseFuture future = CloseableUtils.parallel(sessions.values()).close(true);
+            future.await(5, TimeUnit.SECONDS);
+        } catch (Exception e) {
+            logger.debug("Exception caught while closing session", e);
         }
         group.shutdown();
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
index f4dbf32..d28cb16 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/nio2/Nio2Session.java
@@ -28,26 +28,23 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Queue;
 import java.util.concurrent.LinkedTransferQueue;
-import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.sshd.common.future.DefaultSshFuture;
-import org.apache.sshd.common.io.IoCloseFuture;
+import org.apache.sshd.common.future.SshFuture;
 import org.apache.sshd.common.io.IoHandler;
 import org.apache.sshd.common.io.IoService;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.Readable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  */
-public class Nio2Session implements IoSession {
+public class Nio2Session extends CloseableUtils.AbstractCloseable implements IoSession {
 
-    private static final Logger LOGGER = LoggerFactory.getLogger(Nio2Session.class);
     private static final AtomicLong sessionIdGenerator = new AtomicLong(100);
 
     private final long id = sessionIdGenerator.incrementAndGet();
@@ -58,8 +55,6 @@ public class Nio2Session implements IoSession {
     private final SocketAddress localAddress;
     private final SocketAddress remoteAddress;
 
-    private final AtomicBoolean closing = new AtomicBoolean();
-    private final IoCloseFuture closeFuture = new DefaultIoCloseFuture(null);
     private final Queue<DefaultIoWriteFuture> writes = new LinkedTransferQueue<DefaultIoWriteFuture>();
     private final AtomicReference<DefaultIoWriteFuture> currentWrite = new AtomicReference<DefaultIoWriteFuture>();
 
@@ -69,7 +64,7 @@ public class Nio2Session implements IoSession {
         this.socket = socket;
         this.localAddress = socket.getLocalAddress();
         this.remoteAddress = socket.getRemoteAddress();
-        LOGGER.debug("Creating Nio2Session on {} from {}", localAddress, remoteAddress);
+        log.debug("Creating IoSession on {} from {}", localAddress, remoteAddress);
     }
 
     public long getId() {
@@ -106,10 +101,10 @@ public class Nio2Session implements IoSession {
     }
 
     public IoWriteFuture write(Buffer buffer) {
-        LOGGER.debug("Writing {} bytes", buffer.available());
+        log.debug("Writing {} bytes", buffer.available());
         ByteBuffer buf = ByteBuffer.wrap(buffer.array(), buffer.rpos(), buffer.available());
         final DefaultIoWriteFuture future = new DefaultIoWriteFuture(null, buf);
-        if (closing.get()) {
+        if (state.get() != OPENED) {
             Throwable exc = new ClosedChannelException();
             future.setException(exc);
             exceptionCaught(exc);
@@ -121,15 +116,15 @@ public class Nio2Session implements IoSession {
     }
 
     private void exceptionCaught(Throwable exc) {
-        if (!closing.get()) {
-            if (!socket.isOpen()) {
+        if (!closeFuture.isClosed()) {
+            if (state.get() != OPENED || !socket.isOpen()) {
                 close(true);
             } else {
                 try {
-                    LOGGER.debug("Caught exception, now calling handler");
+                    log.debug("Caught exception, now calling handler");
                     handler.exceptionCaught(this, exc);
                 } catch (Throwable t) {
-                    LOGGER.info("Exception handler threw exception, closing the session", t);
+                    log.info("Exception handler threw exception, closing the session", t);
                     close(true);
                 }
             }
@@ -145,6 +140,7 @@ public class Nio2Session implements IoSession {
                         if (future.buffer.hasRemaining()) {
                             socket.write(future.buffer, null, this);
                         } else {
+                            log.debug("Finished writing");
                             future.setWritten();
                             finishWrite();
                         }
@@ -167,49 +163,38 @@ public class Nio2Session implements IoSession {
         }
     }
 
-    public IoCloseFuture close(boolean immediately) {
-        if (closing.compareAndSet(false, true)) {
-            LOGGER.debug("Closing Nio2Session");
-            if (!immediately) {
-                try {
-                    boolean logged = false;
-                    synchronized (writes) {
-                        while (!writes.isEmpty()) {
-                            if (!logged) {
-                                LOGGER.debug("Waiting for writes to finish");
-                                logged = true;
-                            }
-                            writes.wait();
-                        }
-                    }
-                } catch (InterruptedException e) {
-                    // Wait has been interrupted, just close the socket
-                }
-            }
-            for (;;) {
-                DefaultIoWriteFuture future = writes.poll();
-                if (future != null) {
-                    future.setException(new ClosedChannelException());
-                } else {
-                    break;
-                }
-            }
-            try {
-                LOGGER.debug("Closing socket");
-                socket.close();
-            } catch (IOException e) {
-                LOGGER.info("Exception caught while closing session", e);
-            }
-            service.sessionClosed(this);
-            closeFuture.setClosed();
-            try {
-                handler.sessionClosed(this);
-            } catch (Exception e) {
-                // Ignore
-                LOGGER.debug("Exception caught while calling IoHandler#sessionClosed", e);
+    @Override
+    protected SshFuture doCloseGracefully() {
+        synchronized (writes) {
+            return CloseableUtils.parallel(writes.toArray(new SshFuture[writes.size()]));
+        }
+    }
+
+    @Override
+    protected void doCloseImmediately() {
+        for (;;) {
+            DefaultIoWriteFuture future = writes.poll();
+            if (future != null) {
+                future.setException(new ClosedChannelException());
+            } else {
+                break;
             }
         }
-        return closeFuture;
+        try {
+            socket.close();
+        } catch (IOException e) {
+            log.info("Exception caught while closing socket", e);
+        }
+        service.sessionClosed(this);
+        closeFuture.setClosed();
+        state.set(CLOSED);
+        try {
+            handler.sessionClosed(this);
+        } catch (Exception e) {
+            // Ignore
+            log.debug("Exception caught while calling IoHandler#sessionClosed", e);
+        }
+        log.debug("{} closed", this);
     }
 
     public IoService getService() {
@@ -222,7 +207,7 @@ public class Nio2Session implements IoSession {
             public void completed(Integer result, Object attachment) {
                 try {
                     if (result >= 0) {
-                        LOGGER.debug("Read {} bytes", result);
+                        log.debug("Read {} bytes", result);
                         buffer.flip();
                         Readable buf = new Readable() {
                             public int available() {
@@ -235,7 +220,7 @@ public class Nio2Session implements IoSession {
                         handler.messageReceived(Nio2Session.this, buf);
                         startReading();
                     } else {
-                        LOGGER.debug("Socket has been disconnected, closing IoSession now");
+                        log.debug("Socket has been disconnected, closing IoSession now");
                         Nio2Session.this.close(true);
                     }
                 } catch (Throwable exc) {
@@ -248,18 +233,6 @@ public class Nio2Session implements IoSession {
         });
     }
 
-    static class DefaultIoCloseFuture extends DefaultSshFuture<IoCloseFuture> implements IoCloseFuture {
-        DefaultIoCloseFuture(Object lock) {
-            super(lock);
-        }
-        public boolean isClosed() {
-            return getValue() instanceof Boolean;
-        }
-        public void setClosed() {
-            setValue(Boolean.TRUE);
-        }
-    }
-
     static class DefaultIoWriteFuture extends DefaultSshFuture<IoWriteFuture> implements IoWriteFuture {
         private final ByteBuffer buffer;
         DefaultIoWriteFuture(Object lock, ByteBuffer buffer) {
@@ -283,4 +256,8 @@ public class Nio2Session implements IoSession {
             setValue(exception);
         }
     }
+
+    public String toString() {
+        return getClass().getSimpleName() + "[local=" + localAddress + ", remote=" + remoteAddress + "]";
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/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 b917171..140fa30 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
@@ -29,6 +29,7 @@ import org.apache.sshd.agent.common.AgentForwardSupport;
 import org.apache.sshd.client.channel.AbstractClientChannel;
 import org.apache.sshd.client.future.OpenFuture;
 import org.apache.sshd.common.Channel;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.RequestHandler;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.Session;
@@ -39,6 +40,7 @@ import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.server.channel.OpenChannelException;
 import org.apache.sshd.server.x11.X11ForwardSupport;
 import org.slf4j.Logger;
@@ -51,9 +53,7 @@ import static org.apache.sshd.common.SshConstants.*;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public abstract class AbstractConnectionService implements ConnectionService {
-
-    protected final Logger log = LoggerFactory.getLogger(getClass());
+public abstract class AbstractConnectionService extends CloseableUtils.AbstractInnerCloseable implements ConnectionService {
 
     /** Map of channels keyed by the identifier */
     protected final Map<Integer, Channel> channels = new ConcurrentHashMap<Integer, Channel>();
@@ -66,8 +66,6 @@ public abstract class AbstractConnectionService implements ConnectionService {
     protected final TcpipForwarder tcpipForwarder;
     protected final AgentForwardSupport agentForward;
     protected final X11ForwardSupport x11Forward;
-    protected final CloseFuture closeFuture;
-    protected volatile boolean closing;
     protected boolean allowMoreSessions = true;
 
     protected AbstractConnectionService(Session session) {
@@ -75,7 +73,6 @@ public abstract class AbstractConnectionService implements ConnectionService {
         agentForward = new AgentForwardSupport(this);
         x11Forward = new X11ForwardSupport(this);
         tcpipForwarder = session.getFactoryManager().getTcpipForwarderFactory().create(this);
-        closeFuture = new DefaultCloseFuture(getSession().getLock());
     }
 
     public AbstractSession getSession() {
@@ -89,37 +86,19 @@ public abstract class AbstractConnectionService implements ConnectionService {
         return tcpipForwarder;
     }
 
-    public CloseFuture close(boolean immediately) {
-        tcpipForwarder.close();
-        agentForward.close();
-        x11Forward.close();
-        synchronized (getSession().getLock()) {
-            if (!closing) {
-                try {
-                    closing = true;
-                    log.debug("Closing session");
-                    List<Channel> channelToClose = new ArrayList<Channel>(channels.values());
-                    if (channelToClose.size() > 0) {
-                        final AtomicInteger latch = new AtomicInteger(channelToClose.size());
-                        for (Channel channel : channelToClose) {
-                            log.debug("Closing channel {}", channel.getId());
-                            channel.close(immediately).addListener(new SshFutureListener<CloseFuture>() {
-                                public void operationComplete(CloseFuture future) {
-                                    if (latch.decrementAndGet() == 0) {
-                                        closeFuture.setClosed();
-                                    }
-                                }
-                            });
-                        }
-                    } else {
-                        closeFuture.setClosed();
+    @Override
+    protected Closeable getInnerCloseable() {
+        return CloseableUtils.sequential(
+                new Closeable() {
+                    public CloseFuture close(boolean immediately) {
+                        tcpipForwarder.close();
+                        agentForward.close();
+                        x11Forward.close();
+                        return CloseableUtils.closed();
                     }
-                } catch (Throwable t) {
-                    log.warn("Error closing session", t);
-                }
-            }
-            return closeFuture;
-        }
+                },
+                CloseableUtils.parallel(channels.values())
+        );
     }
 
     protected int getNextChannelId() {
@@ -134,6 +113,9 @@ public abstract class AbstractConnectionService implements ConnectionService {
      * @throws IOException
      */
     public int registerChannel(Channel channel) throws IOException {
+        if (state.get() != OPENED) {
+            throw new IllegalStateException("Session is being closed");
+        }
         int channelId = getNextChannelId();
         channel.init(this, session, channelId);
         channels.put(channelId, channel);
@@ -323,7 +305,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
 
         log.debug("Received SSH_MSG_CHANNEL_OPEN {}", type);
 
-        if (closing) {
+        if (state.get() != OPENED) {
             Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
@@ -440,4 +422,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         ((AbstractSession) session).requestFailure(buffer);
     }
 
+    public String toString() {
+        return getClass().getSimpleName();
+    }
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index 99dcdb2..b2b999e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -30,6 +30,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.concurrent.atomic.AtomicReference;
 
 import org.apache.sshd.common.Cipher;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.Compression;
 import org.apache.sshd.common.Digest;
 import org.apache.sshd.common.FactoryManager;
@@ -42,19 +43,15 @@ import org.apache.sshd.common.Session;
 import org.apache.sshd.common.SessionListener;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
-import org.apache.sshd.common.future.CloseFuture;
-import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.future.DefaultSshFuture;
 import org.apache.sshd.common.future.SshFuture;
 import org.apache.sshd.common.future.SshFutureListener;
-import org.apache.sshd.common.io.IoCloseFuture;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.common.util.BufferUtils;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.Readable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import static org.apache.sshd.common.SshConstants.*;
 
@@ -71,7 +68,7 @@ import static org.apache.sshd.common.SshConstants.*;
  *
  * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
  */
-public abstract class AbstractSession implements Session {
+public abstract class AbstractSession extends CloseableUtils.AbstractInnerCloseable implements Session {
 
     /**
      * Name of the property where this session is stored in the attributes of the
@@ -87,22 +84,12 @@ public abstract class AbstractSession implements Session {
 
     /** Client or server side */
     protected final boolean isServer;
-    /** Our logger */
-    protected final Logger log = LoggerFactory.getLogger(getClass());
     /** The factory manager used to retrieve factories of Ciphers, Macs and other objects */
     protected final FactoryManager factoryManager;
     /** The underlying MINA session */
     protected final IoSession ioSession;
     /** The pseudo random generator */
     protected final Random random;
-    /** Lock object for this session state */
-    protected final Object lock = new Object();
-    /**
-     * A future that will be set 'closed' when the connection is closed.
-     */
-    protected final CloseFuture closeFuture = new DefaultCloseFuture(lock);
-    /** The session is being closed */
-    protected volatile boolean closing;
     /** Boolean indicating if this session has been authenticated or not */
     protected boolean authed;
     /** The name of the authenticated useer */
@@ -422,7 +409,7 @@ public abstract class AbstractSession implements Session {
     public void exceptionCaught(Throwable t) {
         // Ignore exceptions that happen while closing
         synchronized (lock) {
-            if (closing) {
+            if (state.get() == OPENED) {
                 return;
             }
         }
@@ -441,51 +428,24 @@ public abstract class AbstractSession implements Session {
         close(true);
     }
 
-    /**
-     * Close this session.
-     * This method will close all channels, then close the underlying MINA session.
-     * The call will not block until the mina session is actually closed.
-     */
-    public CloseFuture close(final boolean immediately) {
-	    final AbstractSession s = this;
-        class IoSessionCloser implements SshFutureListener<IoCloseFuture> {
-            public void operationComplete(IoCloseFuture future) {
-                synchronized (lock) {
-                    log.debug("IoSession closed");
-                    closeFuture.setClosed();
-                    lock.notifyAll();
-                }
-                log.info("Session {}@{} closed", s.getUsername(), s.getIoSession().getRemoteAddress());
-                // Fire 'close' event
-                final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
-                for (SessionListener sl : l) {
-                    sl.sessionClosed(s);
-                }
-            }
-        }
-        synchronized (lock) {
-            if (!closing) {
-                try {
-                    closing = true;
-                    if (currentService != null) {
-                        currentService.close(immediately).addListener(new SshFutureListener<CloseFuture>() {
-                            public void operationComplete(CloseFuture future) {
-                                log.debug("Closing IoSession");
-                                ioSession.close(immediately).addListener(new IoSessionCloser());
-                            }
-                        });
-                    } else {
-                        log.debug("Closing IoSession");
-                        ioSession.close(immediately).addListener(new IoSessionCloser());
-                    }
-                } catch (Throwable t) {
-                    log.warn("Error closing session", t);
-                }
-            }
-            return closeFuture;
+    protected Closeable getInnerCloseable() {
+        return CloseableUtils.sequential(lock,
+                CloseableUtils.parallel(lock, getServices()), ioSession);
+    }
+
+    protected void postClose() {
+        super.postClose();
+        // Fire 'close' event
+        final ArrayList<SessionListener> l = new ArrayList<SessionListener>(listeners);
+        for (SessionListener sl : l) {
+            sl.sessionClosed(this);
         }
     }
 
+    protected Service[] getServices() {
+        return currentService != null ? new Service[] { currentService } : new Service[0];
+    }
+
     /**
      * Encode and send the given buffer.
      * The buffer has to have 5 bytes free at the beginning to allow the encoding to take place.
@@ -1067,6 +1027,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occured sending the packet
      */
     public void disconnect(int reason, String msg) throws IOException {
+        log.info("Disconnecting: {}", msg);
         Buffer buffer = createBuffer(SshConstants.SSH_MSG_DISCONNECT);
         buffer.putInt(reason);
         buffer.putString(msg);
@@ -1315,4 +1276,8 @@ public abstract class AbstractSession implements Session {
         }
     }
 
+    public String toString() {
+        return getClass().getSimpleName() + "[" + getUsername() + "@" + getIoSession().getRemoteAddress() + "]";
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/common/util/CloseableUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/CloseableUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/CloseableUtils.java
new file mode 100644
index 0000000..2393d72
--- /dev/null
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/CloseableUtils.java
@@ -0,0 +1,247 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sshd.common.util;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.sshd.common.Closeable;
+import org.apache.sshd.common.future.CloseFuture;
+import org.apache.sshd.common.future.DefaultCloseFuture;
+import org.apache.sshd.common.future.DefaultSshFuture;
+import org.apache.sshd.common.future.SshFuture;
+import org.apache.sshd.common.future.SshFutureListener;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class to help with {@link Closeable}s.
+ *
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+public class CloseableUtils {
+
+    public static CloseFuture closed() {
+        CloseFuture future = new DefaultCloseFuture(null);
+        future.setClosed();
+        return future;
+    }
+
+    public static Closeable parallel(final Collection<? extends Closeable> closeables) {
+        return parallel(null, closeables);
+    }
+
+    public static Closeable parallel(final Object lock, final Collection<? extends Closeable> closeables) {
+        return parallel(lock, closeables.toArray(new Closeable[closeables.size()]));
+    }
+
+    public static Closeable parallel(final Closeable... closeables) {
+        return parallel(null, closeables);
+    }
+
+    public static Closeable parallel(final Object lock, final Closeable... closeables) {
+        if (closeables.length == 0) {
+            return new Closeable() {
+                public CloseFuture close(boolean immediately) {
+                    final CloseFuture future = new DefaultCloseFuture(lock);
+                    future.setClosed();
+                    return future;
+                }
+            };
+        } else if (closeables.length == 1) {
+            return closeables[0];
+        } else {
+            return new Closeable() {
+                public CloseFuture close(boolean immediately) {
+                    final CloseFuture future = new DefaultCloseFuture(lock);
+                    final AtomicInteger count = new AtomicInteger(closeables.length);
+                    SshFutureListener<CloseFuture> listener = new SshFutureListener<CloseFuture>() {
+                        public void operationComplete(CloseFuture f) {
+                            if (count.decrementAndGet() == 0) {
+                                future.setClosed();
+                            }
+                        }
+                    };
+                    for (Closeable c : closeables) {
+                        c.close(immediately).addListener(listener);
+                    }
+                    return future;
+                }
+            };
+        }
+    }
+
+    public static Closeable sequential(final Collection<? extends Closeable> closeables) {
+        return sequential(null, closeables);
+    }
+
+    public static Closeable sequential(final Object lock, final Collection<? extends Closeable> closeables) {
+        return sequential(lock, closeables.toArray(new Closeable[closeables.size()]));
+    }
+
+    public static Closeable sequential(final Closeable... closeables) {
+        return sequential(null, closeables);
+    }
+
+    public static Closeable sequential(final Object lock, final Closeable... closeables) {
+        if (closeables.length == 0) {
+            return new Closeable() {
+                public CloseFuture close(boolean immediately) {
+                    final CloseFuture future = new DefaultCloseFuture(lock);
+                    future.setClosed();
+                    return future;
+                }
+            };
+        } else if (closeables.length == 1) {
+            return closeables[0];
+        } else {
+            return new Closeable() {
+                public CloseFuture close(final boolean immediately) {
+                    final DefaultCloseFuture future = new DefaultCloseFuture(lock);
+                    final Iterator<Closeable> iterator = Arrays.asList(closeables).iterator();
+                    SshFutureListener<CloseFuture> listener = new SshFutureListener<CloseFuture>() {
+                        public void operationComplete(CloseFuture previousFuture) {
+                            if (iterator.hasNext()) {
+                                Closeable c = iterator.next();
+                                CloseFuture nextFuture = c.close(immediately);
+                                nextFuture.addListener(this);
+                            } else {
+                                future.setClosed();
+                            }
+                        }
+                    };
+                    listener.operationComplete(null);
+                    return future;
+                }
+            };
+        }
+    }
+
+    public static SshFuture parallel(final SshFuture... futures) {
+        if (futures.length == 0) {
+            final DefaultSshFuture<SshFuture> future = new DefaultSshFuture<SshFuture>(null);
+            future.setValue(true);
+            return future;
+        } else if (futures.length == 1) {
+            return futures[0];
+        } else {
+            final CloseFuture future = new DefaultCloseFuture(null);
+            final AtomicInteger count = new AtomicInteger(futures.length);
+            SshFutureListener<?> listener = new SshFutureListener<SshFuture>() {
+                public void operationComplete(SshFuture f) {
+                    if (count.decrementAndGet() == 0) {
+                        future.setClosed();
+                    }
+                }
+            };
+            for (SshFuture f : futures) {
+                f.addListener(listener);
+            }
+            return future;
+        }
+    }
+
+    public static abstract class AbstractCloseable implements Closeable {
+
+        protected static final int OPENED = 0;
+        protected static final int GRACEFUL = 1;
+        protected static final int IMMEDIATE = 2;
+        protected static final int CLOSED = 3;
+
+        /** Our logger */
+        protected final Logger log = LoggerFactory.getLogger(getClass());
+        /** Lock object for this session state */
+        protected final Object lock = new Object();
+        /** State of this object */
+        protected final AtomicInteger state = new AtomicInteger();
+        /** A future that will be set 'closed' when the object is actually closed */
+        protected final CloseFuture closeFuture = new DefaultCloseFuture(lock);
+
+        public CloseFuture close(boolean immediately) {
+            if (immediately) {
+                if (state.compareAndSet(0, IMMEDIATE) || state.compareAndSet(GRACEFUL, IMMEDIATE)) {
+                    log.debug("Closing {} immediately", this);
+                    doCloseImmediately();
+                } else {
+                    log.debug("{} is already {}", this, state.get() == CLOSED ? "closed" : "closing");
+                }
+            } else {
+                if (state.compareAndSet(0, GRACEFUL)) {
+                    log.debug("Closing {} gracefully", this);
+                    SshFuture grace = doCloseGracefully();
+                    if (grace != null) {
+                        grace.addListener(new SshFutureListener() {
+                            public void operationComplete(SshFuture future) {
+                                if (state.compareAndSet(GRACEFUL, IMMEDIATE)) {
+                                    doCloseImmediately();
+                                }
+                            }
+                        });
+                    } else {
+                        if (state.compareAndSet(GRACEFUL, IMMEDIATE)) {
+                            doCloseImmediately();
+                        }
+                    }
+                } else {
+                    log.debug("{} is already {}", this, state.get() == CLOSED ? "closed" : "closing");
+                }
+            }
+            return closeFuture;
+        }
+
+        protected SshFuture doCloseGracefully() {
+            return null;
+        }
+
+        protected void doCloseImmediately() {
+            postClose();
+        }
+
+        protected void postClose() {
+            closeFuture.setClosed();
+            state.set(CLOSED);
+            log.debug("{} closed", this);
+        }
+    }
+
+    public static abstract class AbstractInnerCloseable extends AbstractCloseable {
+
+        protected abstract Closeable getInnerCloseable();
+
+        @Override
+        protected SshFuture doCloseGracefully() {
+            return getInnerCloseable().close(false);
+        }
+
+        @Override
+        protected void doCloseImmediately() {
+            getInnerCloseable().close(true).addListener(new SshFutureListener<CloseFuture>() {
+                public void operationComplete(CloseFuture future) {
+                    postClose();
+                }
+            });
+        }
+    }
+
+    private CloseableUtils() {
+    }
+}

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
index 2f5d68b..cbe21f7 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/channel/ChannelSession.java
@@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit;
 import org.apache.sshd.agent.SshAgent;
 import org.apache.sshd.agent.SshAgentFactory;
 import org.apache.sshd.common.Channel;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.ForwardingFilter;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.PtyMode;
@@ -43,6 +44,7 @@ import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.IoUtils;
 import org.apache.sshd.common.util.LoggingFilterOutputStream;
 import org.apache.sshd.server.ChannelSessionAware;
@@ -184,37 +186,44 @@ public class ChannelSession extends AbstractServerChannel {
     }
 
     @Override
-    protected CloseFuture preClose(boolean immediately) {
-        if (immediately) {
-            commandExitFuture.setClosed();
-        } else if (!commandExitFuture.isClosed()) {
-            log.debug("Wait 5s for shell to exit cleanly");
-            IoUtils.closeQuietly(receiver);
-            final TimerTask task = new TimerTask() {
-                @Override
-                public void run() {
+    protected Closeable getInnerCloseable() {
+        return CloseableUtils.sequential(getCommandCloseable(), super.getInnerCloseable());
+    }
+
+    protected Closeable getCommandCloseable() {
+        return new Closeable() {
+            public CloseFuture close(boolean immediately) {
+                if (immediately) {
                     commandExitFuture.setClosed();
+                } else if (!commandExitFuture.isClosed()) {
+                    IoUtils.closeQuietly(receiver);
+                    final TimerTask task = new TimerTask() {
+                        @Override
+                        public void run() {
+                            commandExitFuture.setClosed();
+                        }
+                    };
+                    long timeout = DEFAULT_COMMAND_EXIT_TIMEOUT;
+                    String val = getSession().getFactoryManager().getProperties().get(ServerFactoryManager.COMMAND_EXIT_TIMEOUT);
+                    if (val != null) {
+                        try {
+                            timeout = Long.parseLong(val);
+                        } catch (NumberFormatException e) {
+                            // Ignore
+                        }
+                    }
+                    log.debug("Wait {} ms for shell to exit cleanly", timeout);
+                    getSession().getFactoryManager().getScheduledExecutorService().schedule(task, timeout, TimeUnit.MILLISECONDS);
+                    commandExitFuture.addListener(new SshFutureListener<CloseFuture>() {
+                        public void operationComplete(CloseFuture future) {
+                            task.cancel();
+                        }
+                    });
                 }
-            };
-            long timeout = DEFAULT_COMMAND_EXIT_TIMEOUT;
-            String val = getSession().getFactoryManager().getProperties().get(ServerFactoryManager.COMMAND_EXIT_TIMEOUT);
-            if (val != null) {
-                try {
-                   timeout = Long.parseLong(val);
-                } catch (NumberFormatException e) {
-                    // Ignore
-                }
+                return commandExitFuture;
             }
-            getSession().getFactoryManager().getScheduledExecutorService().schedule(task, timeout, TimeUnit.MILLISECONDS);
-            commandExitFuture.addListener(new SshFutureListener<CloseFuture>() {
-                public void operationComplete(CloseFuture future) {
-                    task.cancel();
-                }
-            });
-        }
-        return commandExitFuture;
+        };
     }
-
     @Override
     protected void postClose() {
         if (command != null) {
@@ -234,7 +243,7 @@ public class ChannelSession extends AbstractServerChannel {
 
     protected void doWriteData(byte[] data, int off, int len) throws IOException {
         // If we're already closing, ignore incoming data
-        if (closing.get()) {
+        if (state.get() != OPENED) {
             return;
         }
         if (receiver != null) {
@@ -524,12 +533,14 @@ public class ChannelSession extends AbstractServerChannel {
     }
 
     protected void closeShell(int exitValue) throws IOException {
-        if (!closing.get()) {
+        if (state.get() == OPENED) {
             sendEof();
             sendExitStatus(exitValue);
+            commandExitFuture.setClosed();
             close(false);
+        } else {
+            commandExitFuture.setClosed();
         }
-        commandExitFuture.setClosed();
     }
 
     private class ChannelSessionRequestHandler implements RequestHandler<Channel> {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
index ea2a21a..d8cda02 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerSession.java
@@ -94,7 +94,7 @@ public class ServerSession extends AbstractSession {
      * @throws IOException
      */
     protected void checkForTimeouts() throws IOException {
-        if (!closing) {
+        if (state.get() == OPENED) {
             long now = System.currentTimeMillis();
             if (!authed && now > authTimeoutTimestamp) {
                 disconnect(SshConstants.SSH2_DISCONNECT_PROTOCOL_ERROR, "Session has timed out waiting for authentication after " + authTimeoutMs + " ms.");


[2/7] git commit: Optimize test imports

Posted by gn...@apache.org.
Optimize test imports

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

Branch: refs/heads/master
Commit: 5f55ec1461e94d695421d9dc6e616b819ca284c6
Parents: 52d3da9
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Feb 3 22:14:36 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Mon Feb 3 22:14:36 2014 +0100

----------------------------------------------------------------------
 .../org/apache/sshd/AbstractSessionTest.java    |  7 +++---
 .../test/java/org/apache/sshd/AgentTest.java    | 10 +++++----
 .../org/apache/sshd/AuthenticationTest.java     |  1 -
 .../test/java/org/apache/sshd/ClientTest.java   | 14 +++++++-----
 .../java/org/apache/sshd/CompressionTest.java   |  5 +----
 .../java/org/apache/sshd/KeepAliveTest.java     |  9 ++++----
 .../src/test/java/org/apache/sshd/LoadTest.java | 12 ++--------
 .../org/apache/sshd/PortForwardingLoadTest.java |  3 ---
 .../src/test/java/org/apache/sshd/ScpTest.java  |  3 ---
 .../test/java/org/apache/sshd/ServerTest.java   |  1 -
 .../src/test/java/org/apache/sshd/SftpTest.java |  2 --
 .../java/org/apache/sshd/SpringConfigTest.java  |  2 --
 .../java/org/apache/sshd/SshServerTest.java     |  6 ++---
 .../java/org/apache/sshd/WelcomeBannerTest.java |  5 ++---
 .../file/virtualfs/VirtualFileSystemTest.java   |  2 +-
 .../org/apache/sshd/common/util/BufferTest.java |  4 ++--
 .../sshd/common/util/SttySupportTest.java       |  8 +++----
 .../jaas/JaasPasswordAuthenticatorTest.java     | 23 +++++++++++---------
 .../PEMGeneratorHostKeyProviderTest.java        |  8 +++----
 .../SimpleGeneratorHostKeyProviderTest.java     |  8 +++----
 .../server/shell/InvertedShellWrapperTest.java  |  6 ++---
 .../java/org/apache/sshd/util/BogusChannel.java |  4 ++--
 .../apache/sshd/util/BogusForwardingFilter.java |  4 ++--
 .../test/java/org/apache/sshd/util/Utils.java   |  8 +++----
 24 files changed, 69 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
index 230dcc5..5a919ea 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AbstractSessionTest.java
@@ -20,16 +20,15 @@ package org.apache.sshd;
 
 import java.io.IOException;
 
-import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.common.util.Buffer;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
 import org.apache.sshd.util.BaseTest;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
 /**
  * Test basic stuff on AbstractSession.
  *

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
index bcdb5fc..6aa0ded 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AgentTest.java
@@ -21,15 +21,13 @@ package org.apache.sshd;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
 import java.security.KeyPair;
 import java.security.PublicKey;
 import java.util.List;
 
 import org.apache.sshd.agent.SshAgent;
-import org.apache.sshd.agent.local.ProxyAgentFactory;
 import org.apache.sshd.agent.local.LocalAgentFactory;
+import org.apache.sshd.agent.local.ProxyAgentFactory;
 import org.apache.sshd.agent.unix.AgentClient;
 import org.apache.sshd.agent.unix.AgentServer;
 import org.apache.sshd.client.channel.ChannelShell;
@@ -37,7 +35,11 @@ import org.apache.sshd.common.KeyPairProvider;
 import org.apache.sshd.common.util.SecurityUtils;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.Environment;
-import org.apache.sshd.util.*;
+import org.apache.sshd.util.BaseTest;
+import org.apache.sshd.util.BogusPasswordAuthenticator;
+import org.apache.sshd.util.BogusPublickeyAuthenticator;
+import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.Test;
 
 import static org.apache.sshd.util.Utils.createTestKeyPairProvider;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
index 85ecb10..09d06b7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/AuthenticationTest.java
@@ -18,7 +18,6 @@
  */
 package org.apache.sshd;
 
-import java.net.ServerSocket;
 import java.security.KeyPair;
 
 import org.apache.sshd.common.KeyPairProvider;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
index 290115b..8c3604f 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -23,11 +23,9 @@ import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
-import java.net.ServerSocket;
 import java.security.KeyPair;
 import java.util.concurrent.CountDownLatch;
 
-import org.apache.mina.core.future.WriteFuture;
 import org.apache.sshd.client.channel.ChannelExec;
 import org.apache.sshd.client.future.AuthFuture;
 import org.apache.sshd.client.future.OpenFuture;
@@ -46,16 +44,20 @@ import org.apache.sshd.common.util.NoCloseOutputStream;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.CommandFactory;
 import org.apache.sshd.server.command.UnknownCommand;
-import org.apache.sshd.util.*;
+import org.apache.sshd.util.BaseTest;
+import org.apache.sshd.util.BogusPasswordAuthenticator;
+import org.apache.sshd.util.BogusPublickeyAuthenticator;
+import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.TeeOutputStream;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import org.junit.Before;
-import org.junit.Ignore;
-import org.junit.Test;
 
 /**
  * TODO Add javadoc

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
index 6fc8c5f..ed9270c 100644
--- a/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/CompressionTest.java
@@ -20,12 +20,9 @@ package org.apache.sshd;
 
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.ServerSocket;
 import java.util.Arrays;
 
 import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.Logger;
-import com.jcraft.jsch.UserInfo;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.common.compression.CompressionDelayedZlib;
 import org.apache.sshd.common.compression.CompressionNone;
@@ -37,9 +34,9 @@ import org.apache.sshd.util.JSchLogger;
 import org.apache.sshd.util.SimpleUserInfo;
 import org.apache.sshd.util.Utils;
 import org.junit.After;
+import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
-import org.junit.Test;
 
 /**
  * Test compression algorithms.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
index 1e11901..5b8ffcf 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
@@ -19,15 +19,16 @@
 package org.apache.sshd;
 
 import java.io.ByteArrayOutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.net.ServerSocket;
 import java.util.concurrent.CountDownLatch;
 
 import org.apache.sshd.client.ClientFactoryManager;
 import org.apache.sshd.server.Command;
 import org.apache.sshd.server.ServerFactoryManager;
-import org.apache.sshd.util.*;
+import org.apache.sshd.util.BaseTest;
+import org.apache.sshd.util.BogusPasswordAuthenticator;
+import org.apache.sshd.util.BogusPublickeyAuthenticator;
+import org.apache.sshd.util.EchoShellFactory;
+import org.apache.sshd.util.Utils;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
index 3a0886a..4b62d4c 100644
--- a/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/LoadTest.java
@@ -20,18 +20,11 @@ package org.apache.sshd;
 
 import java.io.ByteArrayOutputStream;
 import java.io.OutputStream;
-import java.io.PipedInputStream;
-import java.io.PipedOutputStream;
-import java.net.ServerSocket;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 
-import org.apache.sshd.ClientChannel;
-import org.apache.sshd.ClientSession;
-import org.apache.sshd.SshClient;
-import org.apache.sshd.SshServer;
 import org.apache.sshd.client.kex.DHG1;
 import org.apache.sshd.common.Cipher;
 import org.apache.sshd.common.KeyExchange;
@@ -40,14 +33,13 @@ import org.apache.sshd.common.cipher.BlowfishCBC;
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
-import org.apache.sshd.util.TeeOutputStream;
 import org.apache.sshd.util.Utils;
 import org.junit.After;
-
-import static org.junit.Assert.assertArrayEquals;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertArrayEquals;
+
 public class LoadTest extends BaseTest {
 
     private SshServer sshd;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
index f27b3ad..d739857 100644
--- a/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/PortForwardingLoadTest.java
@@ -34,9 +34,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.Logger;
 import com.jcraft.jsch.Session;
-import com.jcraft.jsch.UserInfo;
 import org.apache.commons.httpclient.HostConfiguration;
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpVersion;
@@ -47,7 +45,6 @@ import org.apache.mina.core.service.IoAcceptor;
 import org.apache.mina.core.service.IoHandlerAdapter;
 import org.apache.mina.core.session.IoSession;
 import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
-import org.apache.sshd.SshServer;
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusForwardingFilter;
 import org.apache.sshd.util.BogusPasswordAuthenticator;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
index 9202c9d..a280f3d 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
@@ -24,7 +24,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.net.ServerSocket;
 import java.util.Properties;
 import java.util.concurrent.TimeUnit;
 
@@ -33,8 +32,6 @@ import ch.ethz.ssh2.SCPClient;
 import com.jcraft.jsch.ChannelExec;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
-import com.jcraft.jsch.Logger;
-import com.jcraft.jsch.UserInfo;
 import org.apache.sshd.client.ScpClient;
 import org.apache.sshd.server.command.ScpCommandFactory;
 import org.apache.sshd.util.BaseTest;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
index 2489537..b878326 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
@@ -19,7 +19,6 @@
 package org.apache.sshd;
 
 import java.io.ByteArrayOutputStream;
-import java.net.ServerSocket;
 import java.util.Arrays;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index 01744f8..f4fba4c 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -24,14 +24,12 @@ import java.io.File;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
-import java.net.URL;
 import java.util.Arrays;
 import java.util.EnumSet;
 import java.util.Vector;
 
 import com.jcraft.jsch.ChannelSftp;
 import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.Logger;
 import org.apache.sshd.client.SftpClient;
 import org.apache.sshd.common.NamedFactory;
 import org.apache.sshd.server.Command;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java b/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
index 84cf547..f5b493b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SpringConfigTest.java
@@ -22,8 +22,6 @@ import java.io.OutputStream;
 
 import com.jcraft.jsch.Channel;
 import com.jcraft.jsch.JSch;
-import com.jcraft.jsch.Logger;
-import com.jcraft.jsch.UserInfo;
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.JSchLogger;
 import org.apache.sshd.util.SimpleUserInfo;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
index 3b730f8..247d9dc 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SshServerTest.java
@@ -18,15 +18,15 @@
  */
 package org.apache.sshd;
 
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
 import org.apache.sshd.util.EchoShellFactory;
 import org.apache.sshd.util.Utils;
 import org.junit.Test;
 
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java b/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
index 788bced..09a778e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/WelcomeBannerTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.sshd;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 import org.apache.sshd.client.UserInteraction;
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusPasswordAuthenticator;
@@ -27,9 +29,6 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import java.net.ServerSocket;
-import java.util.concurrent.atomic.AtomicReference;
-
 import static junit.framework.Assert.assertEquals;
 
 public class WelcomeBannerTest extends BaseTest {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
index 9b806b4..8c37ddf 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/file/virtualfs/VirtualFileSystemTest.java
@@ -21,7 +21,6 @@ package org.apache.sshd.common.file.virtualfs;
 import java.io.File;
 import java.io.IOException;
 
-import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.SshServer;
 import org.apache.sshd.common.AbstractFactoryManager;
 import org.apache.sshd.common.file.FileSystemView;
@@ -30,6 +29,7 @@ import org.apache.sshd.common.file.nativefs.NativeFileSystemFactory;
 import org.apache.sshd.common.file.nativefs.NativeSshFile;
 import org.apache.sshd.common.session.AbstractSession;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
index 355e0cc..d939ed3 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/BufferTest.java
@@ -18,14 +18,14 @@
  */
 package org.apache.sshd.common.util;
 
-import static org.junit.Assert.assertEquals;
-
 import java.io.ByteArrayOutputStream;
 import java.io.DataOutputStream;
 
 import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+
 public class BufferTest extends BaseTest {
 
     @Test

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
index 97857f2..31d4811 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/SttySupportTest.java
@@ -18,14 +18,14 @@
  */
 package org.apache.sshd.common.util;
 
-import org.apache.sshd.util.BaseTest;
-import org.apache.sshd.common.PtyMode;
-import org.junit.Test;
-
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.util.Map;
 
+import org.apache.sshd.common.PtyMode;
+import org.apache.sshd.util.BaseTest;
+import org.junit.Test;
+
 /**
  * TODO Add javadoc
  *

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java b/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
index 43e98b5..69691d4 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/jaas/JaasPasswordAuthenticatorTest.java
@@ -18,27 +18,30 @@
  */
 package org.apache.sshd.server.jaas;
 
-import java.util.Map;
-import java.util.HashMap;
 import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
-import javax.security.auth.login.Configuration;
-import javax.security.auth.login.AppConfigurationEntry;
-import javax.security.auth.login.LoginException;
-import javax.security.auth.spi.LoginModule;
 import javax.security.auth.Subject;
-import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.CallbackHandler;
 import javax.security.auth.callback.NameCallback;
 import javax.security.auth.callback.PasswordCallback;
 import javax.security.auth.callback.UnsupportedCallbackException;
+import javax.security.auth.login.AppConfigurationEntry;
+import javax.security.auth.login.Configuration;
+import javax.security.auth.login.LoginException;
+import javax.security.auth.spi.LoginModule;
 
 import org.apache.sshd.util.BaseTest;
-import org.junit.Test;
-import org.junit.Before;
 import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 /**
  * TODO Add javadoc

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
index a6355c7..2ff1752 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
@@ -18,18 +18,18 @@
  */
 package org.apache.sshd.server.keyprovider;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
 import java.io.File;
 import java.security.KeyPair;
 import java.security.spec.ECGenParameterSpec;
 
-import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.common.KeyPairProvider;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 /**
  * TODO Add javadoc
  *

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
index f3d7d94..fd3b7f7 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/SimpleGeneratorHostKeyProviderTest.java
@@ -18,17 +18,17 @@
  */
 package org.apache.sshd.server.keyprovider;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
 import java.io.File;
 import java.security.spec.ECGenParameterSpec;
 
-import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.common.KeyPairProvider;
 import org.apache.sshd.common.util.SecurityUtils;
+import org.apache.sshd.util.BaseTest;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
 /**
  * TODO Add javadoc
  *

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java b/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
index 6deb7b8..b153ff1 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/shell/InvertedShellWrapperTest.java
@@ -18,15 +18,15 @@
  */
 package org.apache.sshd.server.shell;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
 import org.apache.sshd.util.BaseTest;
 import org.apache.sshd.util.BogusEnvironment;
 import org.apache.sshd.util.BogusExitCallback;
 import org.apache.sshd.util.BogusInvertedShell;
 import org.junit.Test;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-
 import static junit.framework.Assert.assertEquals;
 
 public class InvertedShellWrapperTest extends BaseTest {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java b/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
index 17065a7..f4a9d18 100644
--- a/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
+++ b/sshd-core/src/test/java/org/apache/sshd/util/BogusChannel.java
@@ -18,13 +18,13 @@
  */
 package org.apache.sshd.util;
 
+import java.io.IOException;
+
 import org.apache.sshd.client.future.DefaultOpenFuture;
 import org.apache.sshd.client.future.OpenFuture;
 import org.apache.sshd.common.channel.AbstractChannel;
 import org.apache.sshd.common.util.Buffer;
 
-import java.io.IOException;
-
 public class BogusChannel extends AbstractChannel {
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java b/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java
index 1ebd620..6e25706 100644
--- a/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java
+++ b/sshd-core/src/test/java/org/apache/sshd/util/BogusForwardingFilter.java
@@ -18,9 +18,9 @@
  */
 package org.apache.sshd.util;
 
-import org.apache.sshd.common.SshdSocketAddress;
-import org.apache.sshd.common.Session;
 import org.apache.sshd.common.ForwardingFilter;
+import org.apache.sshd.common.Session;
+import org.apache.sshd.common.SshdSocketAddress;
 
 /**
  * TODO Add javadoc

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/5f55ec14/sshd-core/src/test/java/org/apache/sshd/util/Utils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/util/Utils.java b/sshd-core/src/test/java/org/apache/sshd/util/Utils.java
index 0291bd6..ae91d64 100644
--- a/sshd-core/src/test/java/org/apache/sshd/util/Utils.java
+++ b/sshd-core/src/test/java/org/apache/sshd/util/Utils.java
@@ -18,15 +18,15 @@
  */
 package org.apache.sshd.util;
 
-import org.apache.sshd.common.KeyPairProvider;
-import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
-import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
-
 import java.io.File;
 import java.net.ServerSocket;
 import java.net.URISyntaxException;
 import java.net.URL;
 
+import org.apache.sshd.common.KeyPairProvider;
+import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
+import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
+
 public class Utils {
 
     public static KeyPairProvider createTestHostKeyProvider() {


[3/7] git commit: Add a createBuffer method on Session that does not take a length argument, as it's really rarely used.

Posted by gn...@apache.org.
Add a createBuffer method on Session that does not take a length argument, as it's really rarely used.

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

Branch: refs/heads/master
Commit: 787f31eb77e3d90f8a354e2f5ad17ddf32bb76a5
Parents: 5f55ec1
Author: Guillaume Nodet <gn...@apache.org>
Authored: Mon Feb 3 22:37:05 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Feb 4 09:45:51 2014 +0100

----------------------------------------------------------------------
 .../client/auth/UserAuthKeyboardInteractive.java    |  4 ++--
 .../apache/sshd/client/auth/UserAuthPassword.java   |  2 +-
 .../apache/sshd/client/auth/UserAuthPublicKey.java  |  4 ++--
 .../sshd/client/auth/deprecated/UserAuthAgent.java  |  2 +-
 .../deprecated/UserAuthKeyboardInteractive.java     |  4 ++--
 .../client/auth/deprecated/UserAuthPassword.java    |  2 +-
 .../client/auth/deprecated/UserAuthPublicKey.java   |  2 +-
 .../sshd/client/channel/AbstractClientChannel.java  |  2 +-
 .../sshd/client/channel/ChannelDirectTcpip.java     |  2 +-
 .../org/apache/sshd/client/channel/ChannelExec.java |  2 +-
 .../apache/sshd/client/channel/ChannelShell.java    |  8 ++++----
 .../sshd/client/channel/ChannelSubsystem.java       |  2 +-
 .../apache/sshd/client/kex/AbstractDHGClient.java   |  2 +-
 .../main/java/org/apache/sshd/client/kex/DHGEX.java |  4 ++--
 .../client/session/ClientConnectionService.java     |  2 +-
 .../sshd/client/session/ClientSessionImpl.java      |  2 +-
 .../client/session/ClientUserAuthServiceNew.java    |  2 +-
 .../main/java/org/apache/sshd/common/Session.java   |  9 +++++++++
 .../apache/sshd/common/channel/AbstractChannel.java | 14 +++++++-------
 .../sshd/common/forward/DefaultTcpipForwarder.java  |  4 ++--
 .../sshd/common/forward/TcpipClientChannel.java     |  2 +-
 .../common/session/AbstractConnectionService.java   | 16 ++++++++--------
 .../apache/sshd/common/session/AbstractSession.java | 14 +++++++++-----
 .../server/auth/UserAuthKeyboardInteractive.java    |  2 +-
 .../apache/sshd/server/auth/UserAuthPublicKey.java  |  2 +-
 .../apache/sshd/server/auth/gss/UserAuthGSS.java    |  4 ++--
 .../sshd/server/channel/AbstractServerChannel.java  |  2 +-
 .../server/global/CancelTcpipForwardHandler.java    |  2 +-
 .../sshd/server/global/TcpipForwardHandler.java     |  2 +-
 .../main/java/org/apache/sshd/server/kex/DHGEX.java |  2 +-
 .../sshd/server/session/ServerUserAuthService.java  |  8 ++++----
 .../apache/sshd/server/x11/X11ForwardSupport.java   |  2 +-
 .../src/test/java/org/apache/sshd/ClientTest.java   |  2 +-
 33 files changed, 74 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
index db74e73..539b9ef 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthKeyboardInteractive.java
@@ -69,7 +69,7 @@ public class UserAuthKeyboardInteractive implements UserAuth {
     public boolean process(Buffer buffer) throws Exception {
         if (buffer == null) {
             log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             buffer.putString(session.getUsername());
             buffer.putString(service);
             buffer.putString("keyboard-interactive");
@@ -111,7 +111,7 @@ public class UserAuthKeyboardInteractive implements UserAuth {
                 return false;
             }
 
-            buffer = session.createBuffer(SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
+            buffer = session.createBuffer(SSH_MSG_USERAUTH_INFO_RESPONSE);
             buffer.putInt(rep.length);
             for (String r : rep) {
                 buffer.putString(r);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
index 10266ac..8ecae4b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPassword.java
@@ -71,7 +71,7 @@ public class UserAuthPassword implements UserAuth {
             if (passwords.hasNext()) {
                 current = passwords.next();
                 log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
-                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 buffer.putString(session.getUsername());
                 buffer.putString(service);
                 buffer.putString("password");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
index 5ec1eeb..7dfe20b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/UserAuthPublicKey.java
@@ -99,7 +99,7 @@ public class UserAuthPublicKey implements UserAuth {
                 PublicKey key = current.getPublicKey();
                 String algo = getKeyType(key);
                 log.debug("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 buffer.putString(session.getUsername());
                 buffer.putString(service);
                 buffer.putString("publickey");
@@ -116,7 +116,7 @@ public class UserAuthPublicKey implements UserAuth {
             PublicKey key = current.getPublicKey();
             String algo = getKeyType(key);
             log.debug("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             buffer.putString(session.getUsername());
             buffer.putString(service);
             buffer.putString("publickey");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthAgent.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthAgent.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthAgent.java
index 9879d72..546b2ad 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthAgent.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthAgent.java
@@ -49,7 +49,7 @@ public class UserAuthAgent extends AbstractUserAuth {
     protected void sendNextKey(PublicKey key) throws IOException {
         try {
             log.debug("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             int pos1 = buffer.wpos() - 1;
             buffer.putString(session.getUsername());
             buffer.putString(service);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthKeyboardInteractive.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthKeyboardInteractive.java
index 44b3cd5..d71d151 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthKeyboardInteractive.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthKeyboardInteractive.java
@@ -45,7 +45,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
     public Result next(Buffer buffer) throws IOException {
         if (buffer == null) {
             log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             buffer.putString(session.getUsername());
             buffer.putString(service);
             buffer.putString("keyboard-interactive");
@@ -88,7 +88,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
                         return Result.Failure;
                     }
 
-                    buffer = session.createBuffer(SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
+                    buffer = session.createBuffer(SSH_MSG_USERAUTH_INFO_RESPONSE);
                     buffer.putInt(rep.length);
                     for (String r : rep) {
                         buffer.putString(r);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPassword.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPassword.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPassword.java
index 182e6b3..8b43da6 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPassword.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPassword.java
@@ -45,7 +45,7 @@ public class UserAuthPassword extends AbstractUserAuth {
     public Result next(Buffer buffer) throws IOException {
         if (buffer == null) {
             log.debug("Send SSH_MSG_USERAUTH_REQUEST for password");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
             buffer.putString(session.getUsername());
             buffer.putString(service);
             buffer.putString("password");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPublicKey.java b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPublicKey.java
index bbbb337..52eea0f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPublicKey.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/auth/deprecated/UserAuthPublicKey.java
@@ -51,7 +51,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
         if (buffer == null) {
             try {
                 log.debug("Send SSH_MSG_USERAUTH_REQUEST for publickey");
-                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
                 int pos1 = buffer.wpos() - 1;
                 buffer.putString(session.getUsername());
                 buffer.putString(service);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
index a721da2..d2eb80d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/AbstractClientChannel.java
@@ -188,7 +188,7 @@ public abstract class AbstractClientChannel extends AbstractChannel implements C
         }
         openFuture = new DefaultOpenFuture(lock);
         log.debug("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
index aa90440..408963b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelDirectTcpip.java
@@ -66,7 +66,7 @@ public class ChannelDirectTcpip extends AbstractClientChannel {
         }
         openFuture = new DefaultOpenFuture(lock);
         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
index 943b631..5f46cdc 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelExec.java
@@ -41,7 +41,7 @@ public class ChannelExec extends ChannelSession {
 
     protected void doOpen() throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_REQUEST exec");
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
         buffer.putInt(recipient);
         buffer.putString("exec");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
index 1404a08..0b8f552 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelShell.java
@@ -153,7 +153,7 @@ public class ChannelShell extends ChannelSession {
 
         if (agentForwarding) {
             log.debug("Send agent forwarding request");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
             buffer.putInt(recipient);
             buffer.putString("auth-agent-req@openssh.com");
             buffer.putBoolean(false);
@@ -162,7 +162,7 @@ public class ChannelShell extends ChannelSession {
 
         if (usePty) {
             log.debug("Send SSH_MSG_CHANNEL_REQUEST pty-req");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
             buffer.putInt(recipient);
             buffer.putString("pty-req");
             buffer.putBoolean(false);
@@ -184,7 +184,7 @@ public class ChannelShell extends ChannelSession {
         if (!env.isEmpty()) {
             log.debug("Send SSH_MSG_CHANNEL_REQUEST env");
             for (Map.Entry<String, String> entry : env.entrySet()) {
-                buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
                 buffer.putInt(recipient);
                 buffer.putString("env");
                 buffer.putBoolean(false);
@@ -195,7 +195,7 @@ public class ChannelShell extends ChannelSession {
         }
 
         log.debug("Send SSH_MSG_CHANNEL_REQUEST shell");
-        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
         buffer.putInt(recipient);
         buffer.putString("shell");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
index 62ada33..fd75810 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/channel/ChannelSubsystem.java
@@ -43,7 +43,7 @@ public class ChannelSubsystem extends ChannelSession {
 
     protected void doOpen() throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_REQUEST exec");
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
         buffer.putInt(recipient);
         buffer.putString("subsystem");
         buffer.putBoolean(false);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
index 872821b..91c87ef 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/kex/AbstractDHGClient.java
@@ -78,7 +78,7 @@ public abstract class AbstractDHGClient implements KeyExchange {
         e = dh.getE();
 
         log.debug("Send SSH_MSG_KEXDH_INIT");
-        Buffer buffer = s.createBuffer(SshConstants.SSH_MSG_KEXDH_INIT, 0);
+        Buffer buffer = s.createBuffer(SshConstants.SSH_MSG_KEXDH_INIT);
         buffer.putMPInt(e);
         session.writePacket(buffer);
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java b/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
index 71e56c2..639e237 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/kex/DHGEX.java
@@ -95,7 +95,7 @@ public class DHGEX implements KeyExchange {
         this.I_C = I_C;
 
         log.debug("Send SSH_MSG_KEX_DH_GEX_REQUEST");
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_REQUEST);
         buffer.putInt(min);
         buffer.putInt(prf);
         buffer.putInt(max);
@@ -122,7 +122,7 @@ public class DHGEX implements KeyExchange {
             e = dh.getE();
 
             log.debug("Send SSH_MSG_KEX_DH_GEX_INIT");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_INIT, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_INIT);
             buffer.putMPInt(e);
             session.writePacket(buffer);
             expected = SshConstants.SSH_MSG_KEX_DH_GEX_REPLY;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
index f6e6346..44bfdeb 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientConnectionService.java
@@ -86,7 +86,7 @@ public class ClientConnectionService extends AbstractConnectionService {
 
     protected void sendHeartBeat() {
         try {
-            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST);
             String request = session.getFactoryManager().getProperties().get(ClientFactoryManager.HEARTBEAT_REQUEST);
             if (request == null) {
                 request = "keepalive@sshd.apache.org";

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
index 5e99f7f..460a50e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientSessionImpl.java
@@ -347,7 +347,7 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
         }
         initialServiceRequestSent = true;
         log.debug("Send SSH_MSG_SERVICE_REQUEST for {}", currentServiceFactory.getName());
-        Buffer request = createBuffer(SshConstants.SSH_MSG_SERVICE_REQUEST, 0);
+        Buffer request = createBuffer(SshConstants.SSH_MSG_SERVICE_REQUEST);
         request.putString(currentServiceFactory.getName());
         writePacket(request);
         // Assuming that MINA-SSHD only implements "explicit server authentication" it is permissible

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
index 96d8208..10304d8 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/session/ClientUserAuthServiceNew.java
@@ -105,7 +105,7 @@ public class ClientUserAuthServiceNew implements Service {
         this.service = service;
 
         log.debug("Send SSH_MSG_USERAUTH_REQUEST for none");
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_REQUEST);
         buffer.putString(session.getUsername());
         buffer.putString(service);
         buffer.putString("none");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/common/Session.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/Session.java b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
index 9451fd8..c0af02f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/Session.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/Session.java
@@ -91,6 +91,15 @@ public interface Session {
      * (5 bytes) for the packet header.
      *
      * @param cmd the SSH command
+     * @return a new buffer ready for write
+     */
+    Buffer createBuffer(byte cmd);
+
+    /**
+     * Create a new buffer for the specified SSH packet and reserve the needed space
+     * (5 bytes) for the packet header.
+     *
+     * @param cmd the SSH command
      * @param estimatedSize estimated number of bytes the buffer will hold, 0 if unknown.
      * @return a new buffer ready for write
      */

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
index c1fdd84..a4a90c9 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/AbstractChannel.java
@@ -103,14 +103,14 @@ public abstract class AbstractChannel implements Channel {
                     return;
                 case ReplySuccess:
                     if (wantReply) {
-                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_SUCCESS, 0);
+                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_SUCCESS);
                         buffer.putInt(recipient);
                         session.writePacket(buffer);
                     }
                     return;
                 case ReplyFailure:
                     if (wantReply) {
-                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
+                        buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE);
                         buffer.putInt(recipient);
                         session.writePacket(buffer);
                     }
@@ -119,7 +119,7 @@ public abstract class AbstractChannel implements Channel {
         }
         log.warn("Unknown channel request: {}", req);
         if (wantReply) {
-            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE);
             buffer.putInt(recipient);
             session.writePacket(buffer);
         }
@@ -155,7 +155,7 @@ public abstract class AbstractChannel implements Channel {
                 preClose(immediately).addListener(new SshFutureListener<CloseFuture>() {
                     public void operationComplete(CloseFuture future) {
                         log.debug("Send SSH_MSG_CHANNEL_CLOSE on channel {}", id);
-                        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE, 0);
+                        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_CLOSE);
                         buffer.putInt(recipient);
                         try {
                             session.writePacket(buffer).addListener(new SshFutureListener<IoWriteFuture>() {
@@ -227,7 +227,7 @@ public abstract class AbstractChannel implements Channel {
         // Only accept extended data for stderr
         if (ex != 1) {
             log.debug("Send SSH_MSG_CHANNEL_FAILURE on channel {}", id);
-            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_FAILURE);
             buffer.putInt(recipient);
             writePacket(buffer);
             return;
@@ -266,7 +266,7 @@ public abstract class AbstractChannel implements Channel {
 
     protected void sendEof() throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_EOF on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_EOF, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_EOF);
         buffer.putInt(recipient);
         writePacket(buffer);
     }
@@ -279,7 +279,7 @@ public abstract class AbstractChannel implements Channel {
 
     protected void sendWindowAdjust(int len) throws IOException {
         log.debug("Send SSH_MSG_CHANNEL_WINDOW_ADJUST on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_WINDOW_ADJUST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_WINDOW_ADJUST);
         buffer.putInt(recipient);
         buffer.putInt(len);
         writePacket(buffer);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
index 1909f21..de9661e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/DefaultTcpipForwarder.java
@@ -94,7 +94,7 @@ public class DefaultTcpipForwarder implements TcpipForwarder, IoHandler {
     }
 
     public synchronized SshdSocketAddress startRemotePortForwarding(SshdSocketAddress remote, SshdSocketAddress local) throws IOException {
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST);
         buffer.putString("tcpip-forward");
         buffer.putBoolean(true);
         buffer.putString(remote.getHostName());
@@ -111,7 +111,7 @@ public class DefaultTcpipForwarder implements TcpipForwarder, IoHandler {
 
     public synchronized void stopRemotePortForwarding(SshdSocketAddress remote) throws IOException {
         if (remoteToLocal.remove(remote.getPort()) != null) {
-            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_GLOBAL_REQUEST);
             buffer.putString("cancel-tcpip-forward");
             buffer.putBoolean(false);
             buffer.putString(remote.getHostName());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
index fa61176..50e66ce 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java
@@ -80,7 +80,7 @@ public class TcpipClientChannel extends AbstractClientChannel {
         }
         openFuture = new DefaultOpenFuture(lock);
         log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
+        Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
         buffer.putString(type);
         buffer.putInt(id);
         buffer.putInt(localWindow.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/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 a53c0a5..b917171 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
@@ -324,7 +324,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         log.debug("Received SSH_MSG_CHANNEL_OPEN {}", type);
 
         if (closing) {
-            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
             buf.putString("SSH server is shutting down: " + type);
@@ -333,7 +333,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
             return;
         }
         if (!allowMoreSessions) {
-            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_CONNECT_FAILED);
             buf.putString("additional sessions disabled");
@@ -344,7 +344,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
 
         final Channel channel = NamedFactory.Utils.create(session.getFactoryManager().getChannelFactories(), type);
         if (channel == null) {
-            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE);
             buf.putInt(id);
             buf.putInt(SshConstants.SSH_OPEN_UNKNOWN_CHANNEL_TYPE);
             buf.putString("Unsupported channel type: " + type);
@@ -358,7 +358,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
             public void operationComplete(OpenFuture future) {
                 try {
                     if (future.isOpened()) {
-                        Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_CONFIRMATION, 0);
+                        Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_CONFIRMATION);
                         buf.putInt(id);
                         buf.putInt(channelId);
                         buf.putInt(channel.getLocalWindow().getSize());
@@ -367,7 +367,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
                     } else {
                         Throwable exception = future.getException();
                         if (exception != null) {
-                            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE, 0);
+                            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN_FAILURE);
                             buf.putInt(id);
                             if (exception instanceof OpenChannelException) {
                                 buf.putInt(((OpenChannelException) exception).getReasonCode());
@@ -412,13 +412,13 @@ public abstract class AbstractConnectionService implements ConnectionService {
                         return;
                     case ReplySuccess:
                         if (wantReply) {
-                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
+                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS);
                             session.writePacket(buffer);
                         }
                         return;
                     case ReplyFailure:
                         if (wantReply) {
-                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE, 0);
+                            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE);
                             session.writePacket(buffer);
                         }
                         return;
@@ -427,7 +427,7 @@ public abstract class AbstractConnectionService implements ConnectionService {
         }
         log.warn("Unknown global request: {}", req);
         if (wantReply) {
-            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_REQUEST_FAILURE);
             session.writePacket(buffer);
         }
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
index e6d1639..99dcdb2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/session/AbstractSession.java
@@ -347,7 +347,7 @@ public abstract class AbstractSession implements Session {
                     break;
                 }
                 log.debug("Accepted service {}", service);
-                Buffer response = createBuffer(SshConstants.SSH_MSG_SERVICE_ACCEPT, 0);
+                Buffer response = createBuffer(SshConstants.SSH_MSG_SERVICE_ACCEPT);
                 response.putString(service);
                 writePacket(response);
                 break;
@@ -551,6 +551,10 @@ public abstract class AbstractSession implements Session {
         }
     }
 
+    public Buffer createBuffer(byte cmd) {
+        return createBuffer(cmd, 0);
+    }
+
     /**
      * Create a new buffer for the specified SSH packet and reserve the needed space
      * (5 bytes) for the packet header.
@@ -857,7 +861,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occurred sending the packet
      */
     protected byte[] sendKexInit(String[] proposal) throws IOException {
-        Buffer buffer = createBuffer(SshConstants.SSH_MSG_KEXINIT, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_KEXINIT);
         int p = buffer.wpos();
         buffer.wpos(p + 16);
         random.fill(buffer.array(), p, 16);
@@ -910,7 +914,7 @@ public abstract class AbstractSession implements Session {
      */
     protected void sendNewKeys() throws IOException {
         log.debug("Send SSH_MSG_NEWKEYS");
-        Buffer buffer = createBuffer(SshConstants.SSH_MSG_NEWKEYS, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_NEWKEYS);
         writePacket(buffer);
     }
 
@@ -1063,7 +1067,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occured sending the packet
      */
     public void disconnect(int reason, String msg) throws IOException {
-        Buffer buffer = createBuffer(SshConstants.SSH_MSG_DISCONNECT, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_DISCONNECT);
         buffer.putInt(reason);
         buffer.putString(msg);
         buffer.putString("");
@@ -1082,7 +1086,7 @@ public abstract class AbstractSession implements Session {
      * @throws IOException if an error occurred sending the packet
      */
     protected void notImplemented() throws IOException {
-        Buffer buffer = createBuffer(SshConstants.SSH_MSG_UNIMPLEMENTED, 0);
+        Buffer buffer = createBuffer(SshConstants.SSH_MSG_UNIMPLEMENTED);
         buffer.putInt(seqi - 1);
         writePacket(buffer);
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
index e0e7152..124fab5 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthKeyboardInteractive.java
@@ -46,7 +46,7 @@ public class UserAuthKeyboardInteractive extends AbstractUserAuth {
     protected Boolean doAuth(Buffer buffer, boolean init) throws Exception {
         if (init) {
             // Prompt for password
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST);
             buffer.putString("Password authentication");
             buffer.putString("");
             buffer.putString("en-US");

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
index 3f4971a..54a76ae 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/UserAuthPublicKey.java
@@ -75,7 +75,7 @@ public class UserAuthPublicKey extends AbstractUserAuth {
             return false;
         }
         if (!hasSig) {
-            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_PK_OK, 0);
+            Buffer buf = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_PK_OK);
             buf.putString(alg);
             buf.putRawBytes(buffer.array(), oldPos, 4 + len);
             session.writePacket(buf);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java b/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
index abb73d1..7bcaffe 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/auth/gss/UserAuthGSS.java
@@ -85,7 +85,7 @@ public class UserAuthGSS extends AbstractUserAuth {
 
                     // Send the matching mechanism back to the client
 
-                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST, 0);
+                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_REQUEST);
                     byte[] out = oid.getDER();
 
                     b.putBytes(out);
@@ -162,7 +162,7 @@ public class UserAuthGSS extends AbstractUserAuth {
                 // Send return token if necessary
 
                 if (out != null && out.length > 0) {
-                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE, 0);
+                    Buffer b = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_INFO_RESPONSE);
 
                     b.putBytes(out);
                     session.writePacket(b);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
index 9655ab3..e55b687 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/channel/AbstractServerChannel.java
@@ -60,7 +60,7 @@ public abstract class AbstractServerChannel extends AbstractChannel {
         if (!exitStatusSent) {
             exitStatusSent = true;
             log.debug("Send SSH_MSG_CHANNEL_REQUEST exit-status on channel {}", id);
-            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_REQUEST);
             buffer.putInt(recipient);
             buffer.putString("exit-status");
             buffer.putByte((byte) 0);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java b/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
index 8e49f45..1f34f24 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/global/CancelTcpipForwardHandler.java
@@ -37,7 +37,7 @@ public class CancelTcpipForwardHandler implements RequestHandler<ConnectionServi
             int port = buffer.getInt();
             connectionService.getTcpipForwarder().localPortForwardingCancelled(new SshdSocketAddress(address, port));
             if (wantReply){
-                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
+                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS);
                 buffer.putInt(port);
                 connectionService.getSession().writePacket(buffer);
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java b/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
index 2ee9478..1de692b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/global/TcpipForwardHandler.java
@@ -38,7 +38,7 @@ public class TcpipForwardHandler implements RequestHandler<ConnectionService> {
             SshdSocketAddress bound = connectionService.getTcpipForwarder().localPortForwardingRequested(new SshdSocketAddress(address, port));
             port = bound.getPort();
             if (wantReply){
-                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS, 0);
+                buffer = connectionService.getSession().createBuffer(SshConstants.SSH_MSG_REQUEST_SUCCESS);
                 buffer.putInt(port);
                 connectionService.getSession().writePacket(buffer);
             }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
index 900366b..cb6015c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/kex/DHGEX.java
@@ -115,7 +115,7 @@ public class DHGEX implements KeyExchange {
             hash.init();
 
             log.debug("Send SSH_MSG_KEX_DH_GEX_GROUP");
-            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_GROUP, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_KEX_DH_GEX_GROUP);
             buffer.putMPInt(dh.getP());
             buffer.putMPInt(dh.getG());
             session.writePacket(buffer);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
index 5f7dd19..b1d0c54 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
@@ -190,13 +190,13 @@ public class ServerUserAuthService implements Service {
 
                 String welcomeBanner = getFactoryManager().getProperties().get(ServerFactoryManager.WELCOME_BANNER);
                 if (welcomeBanner != null) {
-                    buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_BANNER, 0);
+                    buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_BANNER);
                     buffer.putString(welcomeBanner);
                     buffer.putString("en");
                     session.writePacket(buffer);
                 }
 
-                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_SUCCESS, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_SUCCESS);
                 session.writePacket(buffer);
                 session.setUsername(username);
                 session.setAuthenticated();
@@ -205,7 +205,7 @@ public class ServerUserAuthService implements Service {
                 log.info("Session {}@{} authenticated", username, session.getIoSession().getRemoteAddress());
 
             } else {
-                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE, 0);
+                buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE);
                 StringBuilder sb = new StringBuilder();
                 for (List<String> l : authMethods) {
                     if (!l.isEmpty()) {
@@ -225,7 +225,7 @@ public class ServerUserAuthService implements Service {
         } else {
             log.debug("Authentication failed");
 
-            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE, 0);
+            buffer = session.createBuffer(SshConstants.SSH_MSG_USERAUTH_FAILURE);
             StringBuilder sb = new StringBuilder();
             for (List<String> l : authMethods) {
                 if (!l.isEmpty()) {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
index 7cc9e22..668e522 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
@@ -178,7 +178,7 @@ public class X11ForwardSupport implements IoHandler {
             }
             openFuture = new DefaultOpenFuture(lock);
             log.info("Send SSH_MSG_CHANNEL_OPEN on channel {}", id);
-            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN, 0);
+            Buffer buffer = session.createBuffer(SshConstants.SSH_MSG_CHANNEL_OPEN);
             buffer.putString(type);
             buffer.putInt(id);
             buffer.putInt(localWindow.getSize());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/787f31eb/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
index 8c3604f..bac4130 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -405,7 +405,7 @@ public class ClientTest extends BaseTest {
 
 //            ((AbstractSession) session).disconnect(SshConstants.SSH2_DISCONNECT_BY_APPLICATION, "Cancel");
             AbstractSession cs = (AbstractSession) session;
-            Buffer buffer = cs.createBuffer(SshConstants.SSH_MSG_DISCONNECT, 0);
+            Buffer buffer = cs.createBuffer(SshConstants.SSH_MSG_DISCONNECT);
             buffer.putInt(SshConstants.SSH2_DISCONNECT_BY_APPLICATION);
             buffer.putString("Cancel");
             buffer.putString("");


[6/7] git commit: Improve tests a bit

Posted by gn...@apache.org.
Improve tests a bit

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

Branch: refs/heads/master
Commit: 9b6b1660b003d00888524ad9e8f38d7a218205da
Parents: 8baa36e
Author: Guillaume Nodet <gn...@apache.org>
Authored: Wed Feb 5 13:07:34 2014 +0100
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Wed Feb 5 13:50:12 2014 +0100

----------------------------------------------------------------------
 .../java/org/apache/sshd/KeepAliveTest.java     | 72 ++++++++++++++++++--
 .../src/test/java/org/apache/sshd/ScpTest.java  | 14 ++--
 .../test/java/org/apache/sshd/ServerTest.java   |  7 +-
 .../src/test/java/org/apache/sshd/SftpTest.java |  2 +-
 .../PEMGeneratorHostKeyProviderTest.java        |  8 +++
 5 files changed, 86 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9b6b1660/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
index 5b8ffcf..a652d42 100644
--- a/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/KeepAliveTest.java
@@ -33,7 +33,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
 
 /**
  * TODO Add javadoc
@@ -45,9 +45,9 @@ public class KeepAliveTest extends BaseTest {
     private SshServer sshd;
     private int port;
 
-    private int heartbeat = 500;
-    private int timeout = 1000;
-    private int wait = 2000;
+    private int heartbeat = 1000;
+    private int timeout = 2000;
+    private int wait = 4000;
 
     @Before
     public void setUp() throws Exception {
@@ -80,7 +80,23 @@ public class KeepAliveTest extends BaseTest {
         ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
 
         int state = channel.waitFor(ClientChannel.CLOSED, wait);
-        assertTrue((state & ClientChannel.CLOSED) != 0);
+        assertEquals("Wrong channel state", ClientChannel.CLOSED | ClientChannel.EOF, state);
+
+        channel.close(false);
+        client.stop();
+    }
+
+    @Test
+    public void testClientNew() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
+        session.addPasswordIdentity("smx");
+        session.auth().verify();
+        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
+
+        int state = channel.waitFor(ClientChannel.CLOSED, wait);
+        assertEquals("Wrong channel state", ClientChannel.CLOSED | ClientChannel.EOF, state);
 
         channel.close(false);
         client.stop();
@@ -96,7 +112,24 @@ public class KeepAliveTest extends BaseTest {
         ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
 
         int state = channel.waitFor(ClientChannel.CLOSED, wait);
-        assertTrue((state & ClientChannel.CLOSED) == 0);
+        assertEquals("Wrong channel state", ClientChannel.TIMEOUT, state);
+
+        channel.close(false);
+        client.stop();
+    }
+
+    @Test
+    public void testClientWithHeartBeatNew() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.getProperties().put(ClientFactoryManager.HEARTBEAT_INTERVAL, Integer.toString(heartbeat));
+        client.start();
+        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
+        session.addPasswordIdentity("smx");
+        session.auth().verify();
+        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
+
+        int state = channel.waitFor(ClientChannel.CLOSED, wait);
+        assertEquals("Wrong channel state", ClientChannel.TIMEOUT, state);
 
         channel.close(false);
         client.stop();
@@ -120,7 +153,32 @@ public class KeepAliveTest extends BaseTest {
 
         TestEchoShellFactory.TestEchoShell.latch.await();
         int state = channel.waitFor(ClientChannel.CLOSED, wait);
-        assertTrue((state & ClientChannel.CLOSED) != 0);
+        assertEquals("Wrong channel state", ClientChannel.CLOSED | ClientChannel.EOF | ClientChannel.OPENED, state);
+
+        channel.close(false);
+        client.stop();
+    }
+
+    @Test
+    public void testShellClosedOnClientTimeoutNew() throws Exception {
+        TestEchoShellFactory.TestEchoShell.latch = new CountDownLatch(1);
+
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
+        session.addPasswordIdentity("smx");
+        session.auth().verify();
+        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_SHELL);
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ByteArrayOutputStream err = new ByteArrayOutputStream();
+        channel.setOut(out);
+        channel.setErr(err);
+        channel.open().await();
+
+
+        TestEchoShellFactory.TestEchoShell.latch.await();
+        int state = channel.waitFor(ClientChannel.CLOSED, wait);
+        assertEquals("Wrong channel state", ClientChannel.CLOSED | ClientChannel.EOF | ClientChannel.OPENED, state);
 
         channel.close(false);
         client.stop();

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9b6b1660/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
index a280f3d..4b978c5 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ScpTest.java
@@ -131,7 +131,7 @@ public class ScpTest extends BaseTest {
         scp.download("target/scp/remote/out.txt", "target/scp/local/out2.txt");
         assertFileLength(new File("target/scp/local/out2.txt"), data.length(), 5000);
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -191,7 +191,7 @@ public class ScpTest extends BaseTest {
         assertFileLength(new File("target/scp/local/dir/out1.txt"), data.length(), 5000);
         assertFileLength(new File("target/scp/local/dir/out2.txt"), data.length(), 5000);
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -225,7 +225,7 @@ public class ScpTest extends BaseTest {
         assertFileLength(new File("target/scp/local/dir/out1.txt"), data.length(), 5000);
         assertFileLength(new File("target/scp/local/dir/out2.txt"), data.length(), 5000);
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -259,7 +259,7 @@ public class ScpTest extends BaseTest {
         assertFileLength(new File("target/scp/local/out1.txt"), data.length(), 5000);
         assertFileLength(new File("target/scp/local/out2.txt"), data.length(), 5000);
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -299,7 +299,7 @@ public class ScpTest extends BaseTest {
         assertFileLength(new File("target/scp/local/out1.txt"), data.length(), 5000);
         assertFileLength(new File("target/scp/local/dir/out2.txt"), data.length(), 5000);
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -349,7 +349,7 @@ public class ScpTest extends BaseTest {
         assertFileLength(new File("target/scp/local/dir/out2.txt"), data.length(), 5000);
         assertEquals(lastMod, new File("target/scp/local/dir/out2.txt").lastModified());
 
-        session.close(false);
+        session.close(false).await();
         client.stop();
     }
 
@@ -506,6 +506,8 @@ public class ScpTest extends BaseTest {
         os.flush();
         header = readLine(is);
         assertEquals("E", header);
+        os.write(0);
+        os.flush();
 
         c.disconnect();
         return new String(buffer);

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9b6b1660/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
index b878326..4f4e496 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ServerTest.java
@@ -45,6 +45,7 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
@@ -137,7 +138,7 @@ public class ServerTest extends BaseTest {
         client.start();
         ClientSession s = client.connect("localhost", port).await().getSession();
         int res = s.waitFor(ClientSession.CLOSED, 5000);
-        assertTrue((res & ClientSession.CLOSED) != 0);
+        assertEquals("Session should be closed", ClientSession.CLOSED | ClientSession.WAIT_AUTH, res);
     }
 
     @Test
@@ -145,7 +146,7 @@ public class ServerTest extends BaseTest {
         final CountDownLatch latch = new CountDownLatch(1);
         TestEchoShellFactory.TestEchoShell.latch = new CountDownLatch(1);
 
-        sshd.getProperties().put(SshServer.IDLE_TIMEOUT, "1000");
+        sshd.getProperties().put(SshServer.IDLE_TIMEOUT, "2500");
         sshd.getSessionFactory().addListener(new SessionListener() {
             public void sessionCreated(Session session) {
                 System.out.println("Session created");
@@ -170,7 +171,7 @@ public class ServerTest extends BaseTest {
         shell.setErr(err);
         shell.open().await();
         int res = s.waitFor(ClientSession.CLOSED, 5000);
-        assertTrue((res & ClientSession.CLOSED) != 0);
+        assertEquals("Session should be closed", ClientSession.CLOSED | ClientSession.AUTHED, res);
         assertTrue(latch.await(1, TimeUnit.SECONDS));
         assertTrue(TestEchoShellFactory.TestEchoShell.latch.await(1, TimeUnit.SECONDS));
     }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9b6b1660/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
index f4fba4c..cc0770b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/SftpTest.java
@@ -215,7 +215,7 @@ public class SftpTest extends BaseTest {
         URI url = getClass().getClassLoader().getResource(SshClient.class.getName().replace('.', '/') + ".class").toURI();
         URI base = new File(System.getProperty("user.dir")).getAbsoluteFile().toURI();
         String path = new File(base.relativize(url).getPath()).getParent() + "/";
-//        String path = "target/classes/org/apache/sshd/";
+        path = path.replace('\\', '/');
         Vector res = c.ls(path);
         for (Object f : res) {
             System.out.println(f.toString());

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/9b6b1660/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
index 2ff1752..2944239 100644
--- a/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/server/keyprovider/PEMGeneratorHostKeyProviderTest.java
@@ -39,6 +39,10 @@ public class PEMGeneratorHostKeyProviderTest extends BaseTest {
 
     @Test
     public void testDSA() {
+        if (!SecurityUtils.isBouncyCastleRegistered()) {
+            return;
+        }
+
         File path = new File("target/keys");
         path.mkdirs();
         path = new File(path, "simple.key");
@@ -67,6 +71,10 @@ public class PEMGeneratorHostKeyProviderTest extends BaseTest {
 
     @Test
     public void testRSA() {
+        if (!SecurityUtils.isBouncyCastleRegistered()) {
+            return;
+        }
+
         File path = new File("target/keys");
         path.mkdirs();
         path = new File(path, "simple.key");


[4/7] [SSHD-228] Introduce a Closeable interface and related utilities

Posted by gn...@apache.org.
http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
index b1d0c54..341985a 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/session/ServerUserAuthService.java
@@ -34,6 +34,7 @@ import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.future.CloseFuture;
 import org.apache.sshd.common.future.DefaultCloseFuture;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.server.ServerFactoryManager;
 import org.apache.sshd.server.UserAuth;
 import org.slf4j.Logger;
@@ -112,6 +113,7 @@ public class ServerUserAuthService implements Service {
         Boolean authed = Boolean.FALSE;
 
         if (cmd == SshConstants.SSH_MSG_USERAUTH_REQUEST) {
+            log.debug("Received SSH_MSG_USERAUTH_REQUEST");
             if (this.currentAuth != null) {
                 this.currentAuth.destroy();
                 this.currentAuth = null;
@@ -152,6 +154,7 @@ public class ServerUserAuthService implements Service {
                 // This should not happen
                 throw new IllegalStateException();
             }
+            log.debug("Received authentication message {}", cmd);
             buffer.rpos(buffer.rpos() - 1);
             try {
                 authed = currentAuth.next(buffer);
@@ -250,9 +253,8 @@ public class ServerUserAuthService implements Service {
     }
 
     public CloseFuture close(boolean immediately) {
-        CloseFuture future = new DefaultCloseFuture(this);
-        future.setClosed();
-        return future;
+        log.debug("Closing authentication service");
+        return CloseableUtils.closed();
     }
 
     private ServerFactoryManager getFactoryManager() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/8baa36e0/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
index 668e522..a5010b4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
+++ b/sshd-core/src/main/java/org/apache/sshd/server/x11/X11ForwardSupport.java
@@ -25,20 +25,17 @@ import java.net.InetSocketAddress;
 import org.apache.sshd.client.channel.AbstractClientChannel;
 import org.apache.sshd.client.future.DefaultOpenFuture;
 import org.apache.sshd.client.future.OpenFuture;
+import org.apache.sshd.common.Closeable;
 import org.apache.sshd.common.SshConstants;
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.channel.ChannelOutputStream;
-import org.apache.sshd.common.future.CloseFuture;
-import org.apache.sshd.common.future.DefaultCloseFuture;
-import org.apache.sshd.common.future.SshFutureListener;
 import org.apache.sshd.common.io.IoAcceptor;
-import org.apache.sshd.common.io.IoCloseFuture;
 import org.apache.sshd.common.io.IoHandler;
 import org.apache.sshd.common.io.IoSession;
 import org.apache.sshd.common.session.ConnectionService;
 import org.apache.sshd.common.util.Buffer;
+import org.apache.sshd.common.util.CloseableUtils;
 import org.apache.sshd.common.util.Readable;
-import org.apache.sshd.server.session.ServerSession;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -195,14 +192,8 @@ public class X11ForwardSupport implements IoHandler {
         }
 
         @Override
-        protected synchronized CloseFuture preClose(boolean immediately) {
-            final CloseFuture future = new DefaultCloseFuture(null);
-            serverSession.close(immediately).addListener(new SshFutureListener<IoCloseFuture>() {
-                public void operationComplete(IoCloseFuture f) {
-                    future.setClosed();
-                }
-            });
-            return future;
+        protected Closeable getInnerCloseable() {
+            return CloseableUtils.sequential(serverSession, super.getInnerCloseable());
         }
 
         protected synchronized void doWriteData(byte[] data, int off, int len) throws IOException {