camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [2/2] git commit: CAMEL-7277 camel-ssh should close the session when execution is finished.
Date Fri, 07 Mar 2014 03:23:15 GMT
CAMEL-7277 camel-ssh should close the session when execution is finished.


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

Branch: refs/heads/camel-2.11.x
Commit: 5d0193555169bf8c940b64683be5645cd80e3fde
Parents: d26d7c5
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Fri Mar 7 11:17:34 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Fri Mar 7 11:22:47 2014 +0800

----------------------------------------------------------------------
 .../apache/camel/component/ssh/SshEndpoint.java | 109 +++++++++++--------
 1 file changed, 61 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5d019355/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java
b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java
index e107009..f80f7d5 100644
--- a/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java
+++ b/components/camel-ssh/src/main/java/org/apache/camel/component/ssh/SshEndpoint.java
@@ -94,55 +94,68 @@ public class SshEndpoint extends ScheduledPollEndpoint {
 
         log.debug("Connected to {}:{}", getHost(), getPort());
 
-        AuthFuture authResult;
-        ClientSession session = connectFuture.getSession();
-
-        KeyPairProvider keyPairProvider;
-        final String certResource = getCertResource();
-        if (certResource != null) {
-            log.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
-            keyPairProvider = new ResourceHelperKeyPairProvider(new String[]{certResource},
getCamelContext().getClassResolver());
-        } else {
-            keyPairProvider = getKeyPairProvider();
+        ClientChannel channel = null;
+        ClientSession session = null;
+        
+        try {
+            AuthFuture authResult;
+            session = connectFuture.getSession();
+    
+            KeyPairProvider keyPairProvider;
+            final String certResource = getCertResource();
+            if (certResource != null) {
+                log.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
+                keyPairProvider = new ResourceHelperKeyPairProvider(new String[]{certResource},
getCamelContext().getClassResolver());
+            } else {
+                keyPairProvider = getKeyPairProvider();
+            }
+    
+            if (keyPairProvider != null) {
+                log.debug("Attempting to authenticate username '{}' using Key...", getUsername());
+                KeyPair pair = keyPairProvider.loadKey(getKeyType());
+                authResult = session.authPublicKey(getUsername(), pair);
+            } else {
+                log.debug("Attempting to authenticate username '{}' using Password...", getUsername());
+                authResult = session.authPassword(getUsername(), getPassword());
+            }
+    
+            authResult.await(getTimeout());
+    
+            if (!authResult.isDone() || authResult.isFailure()) {
+                log.debug("Failed to authenticate");
+                throw new RuntimeCamelException("Failed to authenticate username " + getUsername());
+            }
+        
+            channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);
+
+            ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0});
+            channel.setIn(in);
+    
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            channel.setOut(out);
+    
+            ByteArrayOutputStream err = new ByteArrayOutputStream();
+            channel.setErr(err);
+            OpenFuture openFuture = channel.open();
+            openFuture.await(getTimeout());
+            if (openFuture.isOpened()) {
+                channel.waitFor(ClientChannel.CLOSED, 0);
+                result = new SshResult(command, channel.getExitStatus(),
+                        new ByteArrayInputStream(out.toByteArray()),
+                        new ByteArrayInputStream(err.toByteArray()));
+    
+            }
+            return result;
+        } finally {
+            if (channel != null) {
+                channel.close(true);
+            }
+            // need to make sure the session is closed 
+            if (session != null) {
+                session.close(false);
+            }
         }
-
-        if (keyPairProvider != null) {
-            log.debug("Attempting to authenticate username '{}' using Key...", getUsername());
-            KeyPair pair = keyPairProvider.loadKey(getKeyType());
-            authResult = session.authPublicKey(getUsername(), pair);
-        } else {
-            log.debug("Attempting to authenticate username '{}' using Password...", getUsername());
-            authResult = session.authPassword(getUsername(), getPassword());
-        }
-
-        authResult.await(getTimeout());
-
-        if (!authResult.isDone() || authResult.isFailure()) {
-            log.debug("Failed to authenticate");
-            throw new RuntimeCamelException("Failed to authenticate username " + getUsername());
-        }
-
-        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);
-
-        ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0});
-        channel.setIn(in);
-
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        channel.setOut(out);
-
-        ByteArrayOutputStream err = new ByteArrayOutputStream();
-        channel.setErr(err);
-        OpenFuture openFuture = channel.open();
-        openFuture.await(getTimeout());
-        if (openFuture.isOpened()) {
-            channel.waitFor(ClientChannel.CLOSED, 0);
-            result = new SshResult(command, channel.getExitStatus(),
-                    new ByteArrayInputStream(out.toByteArray()),
-                    new ByteArrayInputStream(err.toByteArray()));
-
-        }
-
-        return result;
+        
     }
 
     @Override


Mime
View raw message