SshSupport#runSshCommand: don't throw exception in finally block
The CommandFailedException which was thrown in finally block is silently discarded [1]. Refactor this method to throw the exception after the finally block. This fixes the warning "Null comparison always yields false: The variable failure can only be null at this location". [1] https://wiki.sei.cmu.edu/confluence/display/java/ERR04-J.+Do+not+complete+abruptly+from+a+finally+block https://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.20.2 Change-Id: Idbfc303d9c9046ab9a43e0d4c6d65d325bdaf0ed Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
This commit is contained in:
parent
ebea36aa07
commit
d9e957dc24
|
@ -94,6 +94,7 @@ public static String runSshCommand(URIish sshUri,
|
||||||
CommandFailedException failure = null;
|
CommandFailedException failure = null;
|
||||||
@SuppressWarnings("resource")
|
@SuppressWarnings("resource")
|
||||||
MessageWriter stderr = new MessageWriter();
|
MessageWriter stderr = new MessageWriter();
|
||||||
|
String out;
|
||||||
try (MessageWriter stdout = new MessageWriter()) {
|
try (MessageWriter stdout = new MessageWriter()) {
|
||||||
session = SshSessionFactory.getInstance().getSession(sshUri,
|
session = SshSessionFactory.getInstance().getSession(sshUri,
|
||||||
provider, fs, 1000 * timeout);
|
provider, fs, 1000 * timeout);
|
||||||
|
@ -108,12 +109,12 @@ public static String runSshCommand(URIish sshUri,
|
||||||
// waitFor with timeout has a bug - JSch' exitValue() throws the
|
// waitFor with timeout has a bug - JSch' exitValue() throws the
|
||||||
// wrong exception type :(
|
// wrong exception type :(
|
||||||
if (process.waitFor() == 0) {
|
if (process.waitFor() == 0) {
|
||||||
return stdout.toString();
|
out = stdout.toString();
|
||||||
} else {
|
} else {
|
||||||
return null; // still running after timeout
|
out = null; // still running after timeout
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
return null; // error
|
out = null; // error
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (errorThread != null) {
|
if (errorThread != null) {
|
||||||
|
@ -147,10 +148,11 @@ public static String runSshCommand(URIish sshUri,
|
||||||
if (session != null) {
|
if (session != null) {
|
||||||
SshSessionFactory.getInstance().releaseSession(session);
|
SshSessionFactory.getInstance().releaseSession(session);
|
||||||
}
|
}
|
||||||
if (failure != null) {
|
|
||||||
throw failure;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (failure != null) {
|
||||||
|
throw failure;
|
||||||
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue