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/06/03 22:24:14 UTC

git commit: [SSHD-323] Provide a way to register custom channels

Repository: mina-sshd
Updated Branches:
  refs/heads/master 6499aa9a6 -> c9974d18a


[SSHD-323] Provide a way to register custom channels

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

Branch: refs/heads/master
Commit: c9974d18a5907012d5c5cd22c5a7365cad1a2824
Parents: 6499aa9
Author: Guillaume Nodet <gn...@apache.org>
Authored: Tue Jun 3 22:24:04 2014 +0200
Committer: Guillaume Nodet <gn...@apache.org>
Committed: Tue Jun 3 22:24:04 2014 +0200

----------------------------------------------------------------------
 .../sshd/client/session/ClientSessionImpl.java  | 14 ++----------
 .../java/org/apache/sshd/common/Session.java    |  9 ++++++++
 .../sshd/common/session/AbstractSession.java    |  9 ++++++++
 .../test/java/org/apache/sshd/ClientTest.java   | 24 ++++++++++++++++++++
 4 files changed, 44 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c9974d18/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 741372c..7ed3cd3 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
@@ -220,21 +220,11 @@ public class ClientSessionImpl extends AbstractSession implements ClientSession
     }
 
     private ClientUserAuthService getUserAuthService() {
-        return findService(ClientUserAuthService.class);
+        return getService(ClientUserAuthService.class);
     }
 
     private ConnectionService getConnectionService() {
-        return findService(ConnectionService.class);
-    }
-
-    private <T> T findService(Class<T> clazz) {
-        if (clazz.isInstance(currentService)) {
-            return clazz.cast(currentService);
-        }
-        if (clazz.isInstance(nextService)) {
-            return clazz.cast(nextService);
-        }
-        throw new IllegalStateException("Attempted to access unknown service " + clazz.getSimpleName());
+        return getService(ConnectionService.class);
     }
 
     public ScpClient createScpClient() {

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c9974d18/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 d61d220..042bccd 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
@@ -157,6 +157,15 @@ public interface Session extends Closeable {
     SshFuture reExchangeKeys() throws IOException;
 
     /**
+     * Get the service of the specified type.
+     * If the service is not of the specified class,
+     * an IllegalStateException will be thrown.
+     *
+     * @throws java.lang.IllegalStateException
+     */
+    <T extends Service> T getService(Class<T> clazz);
+
+    /**
      * Type safe key for storage within the user attributes of {@link org.apache.sshd.common.session.AbstractSession}.
      * Typically it is used as a static variable that is shared between the producer
      * and the consumer. To further restrict access the setting or getting it from

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c9974d18/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 ee25cef..f2cddf9 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
@@ -456,6 +456,15 @@ public abstract class AbstractSession extends CloseableUtils.AbstractInnerClosea
         return currentService != null ? new Service[] { currentService } : new Service[0];
     }
 
+    public <T extends Service> T getService(Class<T> clazz) {
+        for (Service s : getServices()) {
+            if (clazz.isInstance(s)) {
+                return clazz.cast(s);
+            }
+        }
+        throw new IllegalStateException("Attempted to access unknown service " + clazz.getSimpleName());
+    }
+
     /**
      * Encode and send the given buffer.
      * The buffer has to have 5 bytes free at the beginning to allow the encoding to take place.

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/c9974d18/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 fd2bea6..a391e9e 100644
--- a/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/ClientTest.java
@@ -40,6 +40,7 @@ import org.apache.sshd.client.auth.UserAuthKeyboardInteractive;
 import org.apache.sshd.client.auth.UserAuthPassword;
 import org.apache.sshd.client.auth.UserAuthPublicKey;
 import org.apache.sshd.client.channel.ChannelExec;
+import org.apache.sshd.client.channel.ChannelShell;
 import org.apache.sshd.client.future.AuthFuture;
 import org.apache.sshd.client.future.OpenFuture;
 import org.apache.sshd.common.Channel;
@@ -57,6 +58,7 @@ import org.apache.sshd.common.io.IoWriteFuture;
 import org.apache.sshd.common.io.mina.MinaSession;
 import org.apache.sshd.common.io.nio2.Nio2Session;
 import org.apache.sshd.common.session.AbstractSession;
+import org.apache.sshd.common.session.ConnectionService;
 import org.apache.sshd.common.util.Buffer;
 import org.apache.sshd.common.util.BufferUtils;
 import org.apache.sshd.common.util.NoCloseOutputStream;
@@ -260,6 +262,28 @@ public class ClientTest extends BaseTest {
     }
 
     @Test
+    public void testClientWithCustomChannel() throws Exception {
+        SshClient client = SshClient.setUpDefaultClient();
+        client.start();
+        ClientSession session = client.connect("smx", "localhost", port).await().getSession();
+        session.addPasswordIdentity("smx");
+        session.auth().verify();
+
+        ChannelShell channel = new ChannelShell();
+        session.getService(ConnectionService.class).registerChannel(channel);
+
+        ByteArrayOutputStream sent = new ByteArrayOutputStream();
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        ByteArrayOutputStream err = new ByteArrayOutputStream();
+        channel.setOut(out);
+        channel.setErr(err);
+        channel.open().verify();
+
+        channel.close(false).await();
+        client.stop();
+    }
+
+    @Test
     public void testClientClosingStream() throws Exception {
         SshClient client = SshClient.setUpDefaultClient();
         client.start();