From da5c8adf5e12114e6267a0b71cad4a50d9bbecf0 Mon Sep 17 00:00:00 2001 From: Tomek Kaminski Date: Wed, 10 Jan 2018 18:03:41 +0100 Subject: Fix sonar blocker Fix https://sonar.onap.org/issues/search#issues=AWBUkk54-08if2a6vTdT Change-Id: Ic50564d4d47d76ac9bfa83b8ecd6f3d23f11904c Issue-ID: APPC-383 Signed-off-by: Tomek Kaminski --- .../org/onap/appc/ccadaptor/SshJcraftWrapper.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java index 1f161c3b0..4ef81ef1b 100644 --- a/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java +++ b/appc-config/appc-config-adaptor/provider/src/main/java/org/onap/appc/ccadaptor/SshJcraftWrapper.java @@ -202,6 +202,7 @@ public class SshJcraftWrapper { String CmdThatWasSent = removeWhiteSpaceAndNewLineCharactersAroundString(cmdThatWasSent); int readCounts = 0; aggregatedReceivedString = ""; + FileWriter fileWriter = null; long deadline = new Date().getTime() + timeout; try { @@ -235,7 +236,8 @@ public class SshJcraftWrapper { StringTokenizer st = new StringTokenizer(cmdThatWasSent); st.nextToken(); routerFileName = st.nextToken(); - out = new BufferedWriter(new FileWriter(routerFileName)); + fileWriter = new FileWriter(routerFileName); + out = new BufferedWriter(fileWriter); routerLogFileName = "/tmp/" + RouterName; _tmpFile = new File(routerLogFileName); debugLog.printRTAriDebug(fn, @@ -308,10 +310,18 @@ public class SshJcraftWrapper { debugLog.printRTAriDebug(fn, "Caught an IOException: ee=" + ee.toString()); dbLog.outputStackTrace(ee); throw new TimedOutException(ee.toString()); + } finally { + try { + if (fileWriter != null) { + fileWriter.close(); + } + } catch(IOException ex) { + debugLog.printRTAriDebug(fn, "Failed to close fileWriter output stream: ex=" + ex); + } } String result = stripOffCmdFromRouterResponse(sbReceive.toString()); debugLog.printRTAriDebug(fn, "Leaving method successfully"); - return (result); + return result; } public boolean checkIfReceivedStringMatchesDelimeter(String delimeters, String receivedString, @@ -332,11 +342,7 @@ public class SshJcraftWrapper { appendToFile(debugLogFileName, fn + " :::cmdThatWasSent='" + cmdThatWasSent + "' x=" + x + " y=" + y + "\n"); } - if ((x != -1) && (y == x)) { - return (true); - } else { - return (false); - } + return (x != -1) && (y == x); } if (cmdThatWasSent.indexOf("show config") != -1) { appendToFile(debugLogFileName, fn + "In the block for 'show config'\n"); @@ -794,7 +800,7 @@ public class SshJcraftWrapper { } } } catch (JSchException e) { - debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e.toString()); + debugLog.printRTAriDebug(fn, "Caught an JSchException e=" + e); debugLog.printRTAriDebug(fn, "ncharsSent=" + ncharsSent + " ncharsTotalReceived=" + ncharsTotalReceived + " ncharsRead=" + ncharsRead); -- cgit 1.2.3-korg