You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2018/05/14 10:26:44 UTC

mina-sshd git commit: Added failed operation hints in exceptions generated by AbstractSshFuture(s)

Repository: mina-sshd
Updated Branches:
  refs/heads/master 0cfb7be85 -> 3182ad821


Added failed operation hints in exceptions generated by AbstractSshFuture(s)


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

Branch: refs/heads/master
Commit: 3182ad8217af52c6e8540ff0a295fc085efaf8b2
Parents: 0cfb7be
Author: Goldstein Lyor <ly...@c-b4.com>
Authored: Mon May 14 13:26:15 2018 +0300
Committer: Goldstein Lyor <ly...@c-b4.com>
Committed: Mon May 14 13:26:38 2018 +0300

----------------------------------------------------------------------
 .../sshd/client/future/DefaultAuthFuture.java   |  2 +-
 .../client/future/DefaultConnectFuture.java     |  2 +-
 .../sshd/client/future/DefaultOpenFuture.java   |  2 +-
 .../common/channel/ChannelAsyncInputStream.java |  4 +-
 .../sshd/common/future/AbstractSshFuture.java   | 78 +++++++++++++-------
 .../common/future/DefaultKeyExchangeFuture.java |  2 +-
 .../sshd/common/future/DefaultSshFuture.java    |  6 +-
 .../sshd/common/io/AbstractIoWriteFuture.java   |  2 +-
 8 files changed, 62 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultAuthFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultAuthFuture.java b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultAuthFuture.java
index 4e5c406..324c97f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultAuthFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultAuthFuture.java
@@ -39,7 +39,7 @@ public class DefaultAuthFuture extends DefaultVerifiableSshFuture<AuthFuture> im
     public AuthFuture verify(long timeoutMillis) throws IOException {
         Boolean result = verifyResult(Boolean.class, timeoutMillis);
         if (!result) {
-            throw new SshException("Authentication failed");
+            throw formatExceptionMessage(SshException::new, "Authentication failed while waiting %d msec.", timeoutMillis);
         }
 
         return this;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultConnectFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultConnectFuture.java b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultConnectFuture.java
index e746954..0a1ef17 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultConnectFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultConnectFuture.java
@@ -41,7 +41,7 @@ public class DefaultConnectFuture extends DefaultVerifiableSshFuture<ConnectFutu
         long startTime = System.nanoTime();
         ClientSession session = verifyResult(ClientSession.class, timeout);
         long endTime = System.nanoTime();
-        if (log.isDebugEnabled()) {
+        if (debugEnabled) {
             IoSession ioSession = session.getIoSession();
             log.debug("Connected to " + ioSession.getRemoteAddress() + " after " + (endTime - startTime) + " nanos");
         }

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultOpenFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultOpenFuture.java b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultOpenFuture.java
index aa91c92..550a50b 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultOpenFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/future/DefaultOpenFuture.java
@@ -38,7 +38,7 @@ public class DefaultOpenFuture extends DefaultVerifiableSshFuture<OpenFuture> im
     public OpenFuture verify(long timeoutMillis) throws IOException {
         Boolean result = verifyResult(Boolean.class, timeoutMillis);
         if (!result) {
-            throw new SshException("Channel opening failed");
+            throw formatExceptionMessage(SshException::new, "Channel opening failed while waiting %d msec.", timeoutMillis);
         }
 
         return this;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncInputStream.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncInputStream.java b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncInputStream.java
index e4a1304..e699540 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncInputStream.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/channel/ChannelAsyncInputStream.java
@@ -158,7 +158,7 @@ public class ChannelAsyncInputStream extends AbstractCloseable implements IoInpu
             long startTime = System.nanoTime();
             Number result = verifyResult(Number.class, timeoutMillis);
             long endTime = System.nanoTime();
-            if (log.isDebugEnabled()) {
+            if (debugEnabled) {
                 log.debug("Read " + result + " bytes after " + (endTime - startTime) + " nanos");
             }
 
@@ -177,7 +177,7 @@ public class ChannelAsyncInputStream extends AbstractCloseable implements IoInpu
             } else if (v instanceof Number) {
                 return ((Number) v).intValue();
             } else {
-                throw new IllegalStateException("Unknown read value type: " + ((v == null) ? "null" : v.getClass().getName()));
+                throw formatExceptionMessage(IllegalStateException::new, "Unknown read value type: %s", (v == null) ? "null" : v.getClass().getName());
             }
         }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/common/future/AbstractSshFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/future/AbstractSshFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/future/AbstractSshFuture.java
index b1217c4..1dca54c 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/future/AbstractSshFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/future/AbstractSshFuture.java
@@ -22,6 +22,7 @@ package org.apache.sshd.common.future;
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.io.StreamCorruptedException;
+import java.util.function.Function;
 
 import org.apache.sshd.common.SshException;
 import org.apache.sshd.common.util.GenericUtils;
@@ -37,6 +38,8 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
      */
     protected static final Object CANCELED = new Object();
 
+    protected final boolean debugEnabled;
+    protected final boolean traceEnabled;
     private final Object id;
 
     /**
@@ -44,6 +47,8 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
      */
     protected AbstractSshFuture(Object id) {
         this.id = id;
+        this.debugEnabled = log.isDebugEnabled();
+        this.traceEnabled = log.isTraceEnabled();
     }
 
     @Override
@@ -61,8 +66,9 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
         try {
             return await0(timeoutMillis, false) != null;
         } catch (InterruptedIOException e) {
-            throw new InternalError("Unexpected interrupted exception wile awaitUninterruptibly "
-                    + timeoutMillis + " msec.: " + e.getMessage(), e);
+            throw formatExceptionMessage(msg -> new InternalError(msg, e),
+                    "Unexpected interrupted exception wile awaitUninterruptibly %d msec: %s",
+                    timeoutMillis, e.getMessage());
         }
     }
 
@@ -70,28 +76,28 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
      * <P>Waits (interruptible) for the specified timeout (msec.) and then checks
      * the result:</P>
      * <UL>
-     * <LI><P>
-     * If result is {@code null} then timeout is assumed to have expired - throw
-     * an appropriate {@link IOException}
-     * </P></LI>
+     *      <LI><P>
+     *      If result is {@code null} then timeout is assumed to have expired - throw
+     *      an appropriate {@link IOException}
+     *      </P></LI>
      *
-     * <LI><P>
-     * If the result is of the expected type, then cast and return it
-     * </P></LI>
+     *      <LI><P>
+     *      If the result is of the expected type, then cast and return it
+     *      </P></LI>
      *
-     * <LI><P>
-     * If the result is an {@link IOException} then re-throw it
-     * </P></LI>
+     *      <LI><P>
+     *      If the result is an {@link IOException} then re-throw it
+     *      </P></LI>
      *
-     * <LI><P>
-     * If the result is a {@link Throwable} then throw an {@link IOException}
-     * whose cause is the original exception
-     * </P></LI>
+     *      <LI><P>
+     *      If the result is a {@link Throwable} then throw an {@link IOException}
+     *      whose cause is the original exception
+     *      </P></LI>
      *
-     * <LI><P>
-     * Otherwise (should never happen), throw a {@link StreamCorruptedException}
-     * with the name of the result type
-     * </P></LI>
+     *      <LI><P>
+     *      Otherwise (should never happen), throw a {@link StreamCorruptedException}
+     *      with the name of the result type
+     *      </P></LI>
      * </UL>
      *
      * @param <R>          The generic result type
@@ -103,7 +109,7 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
     protected <R> R verifyResult(Class<? extends R> expectedType, long timeout) throws IOException {
         Object value = await0(timeout, true);
         if (value == null) {
-            throw new SshException("Failed to get operation result within specified timeout: " + timeout);
+            throw formatExceptionMessage(SshException::new, "Failed to get operation result within specified timeout: %s", timeout);
         }
 
         Class<?> actualType = value.getClass();
@@ -122,9 +128,10 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
                 throw (IOException) value;
             }
 
-            throw new SshException("Failed (" + t.getClass().getSimpleName() + ") to execute: " + t.getMessage(), GenericUtils.resolveExceptionCause(t));
+            Throwable cause = GenericUtils.resolveExceptionCause(t);
+            throw formatExceptionMessage(msg -> new SshException(msg, cause), "Failed (%s) to execute: %s", t.getClass().getSimpleName(), t.getMessage());
         } else {    // what else can it be ????
-            throw new StreamCorruptedException("Unknown result type: " + actualType.getName());
+            throw formatExceptionMessage(StreamCorruptedException::new, "Unknown result type: %s", actualType.getName());
         }
     }
 
@@ -134,12 +141,11 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
      *
      * @param timeoutMillis The delay we will wait for the Future to be ready
      * @param interruptable Tells if the wait can be interrupted or not.
-     *                      If {@code true} and the thread is interrupted then an {@link InterruptedIOException}
-     *                      is thrown.
+     * If {@code true} and the thread is interrupted then an {@link InterruptedIOException}
+     * is thrown.
      * @return The non-{@code null} result object if the Future is ready,
      * {@code null} if the timeout expired and no result was received
-     * @throws InterruptedIOException If the thread has been interrupted
-     *                                when it's not allowed.
+     * @throws InterruptedIOException If the thread has been interrupted when it's not allowed.
      */
     protected abstract Object await0(long timeoutMillis, boolean interruptable) throws InterruptedIOException;
 
@@ -154,7 +160,7 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
         } catch (Throwable t) {
             log.warn("notifyListener({}) failed ({}) to invoke {}: {}",
                      this, t.getClass().getSimpleName(), l, t.getMessage());
-            if (log.isDebugEnabled()) {
+            if (debugEnabled) {
                 log.debug("notifyListener(" + this + ")[" + l + "] invocation failure details", t);
             }
         }
@@ -165,6 +171,22 @@ public abstract class AbstractSshFuture<T extends SshFuture> extends AbstractLog
         return (T) this;
     }
 
+    /**
+     * Generates an exception whose message is prefixed by the future simple class name + {@link #getId() identifier}
+     * as a hint to the context of the failure.
+     *
+     * @param <E> Type of {@link Throwable} being generated
+     * @param exceptionCreator The exception creator from the formatted message
+     * @param format The extra payload format as per {@link String#format(String, Object...)}
+     * @param args The formatting arguments
+     * @return The generated exception
+     */
+    protected <E extends Throwable> E formatExceptionMessage(Function<? super String, ? extends E> exceptionCreator, String format, Object... args) {
+        String messagePayload = String.format(format, args);
+        String excMessage = getClass().getSimpleName() + "[" + getId() + "]: " + messagePayload;
+        return exceptionCreator.apply(excMessage);
+    }
+
     @Override
     public String toString() {
         return getClass().getSimpleName() + "[id=" + getId() + "]";

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultKeyExchangeFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultKeyExchangeFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultKeyExchangeFuture.java
index 880bb2a..a4ae41e 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultKeyExchangeFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultKeyExchangeFuture.java
@@ -35,7 +35,7 @@ public class DefaultKeyExchangeFuture extends DefaultVerifiableSshFuture<KeyExch
     public KeyExchangeFuture verify(long timeoutMillis) throws IOException {
         Boolean result = verifyResult(Boolean.class, timeoutMillis);
         if (!result) {
-            throw new SshException("Key exchange failed");
+            throw formatExceptionMessage(SshException::new, "Key exchange failed while waiting %d msec.", timeoutMillis);
         }
 
         return this;

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
index d05ce3b..cd475ff 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/future/DefaultSshFuture.java
@@ -70,7 +70,11 @@ public class DefaultSshFuture<T extends SshFuture> extends AbstractSshFuture<T>
                 } catch (InterruptedException e) {
                     if (interruptable) {
                         curTime = System.currentTimeMillis();
-                        throw (InterruptedIOException) new InterruptedIOException("Interrupted after " + (curTime - startTime) + " msec.").initCause(e);
+                        throw formatExceptionMessage(msg -> {
+                            InterruptedIOException exc = new InterruptedIOException(msg);
+                            exc.initCause(e);
+                            return exc;
+                        }, "Interrupted after %d msec.", curTime - startTime);
                     }
                 }
 

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/3182ad82/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoWriteFuture.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoWriteFuture.java b/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoWriteFuture.java
index 947b678..87ca963 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoWriteFuture.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/AbstractIoWriteFuture.java
@@ -36,7 +36,7 @@ public abstract class AbstractIoWriteFuture extends DefaultVerifiableSshFuture<I
     public IoWriteFuture verify(long timeout) throws IOException {
         Boolean result = verifyResult(Boolean.class, timeout);
         if (!result) {
-            throw new SshException("Write failed signalled");
+            throw formatExceptionMessage(SshException::new, "Write failed signalled while wait %d msec.", timeout);
         }
 
         return this;