diff options
author | Krishnajinka <kris.jinka@samsung.com> | 2018-07-20 16:54:19 +0900 |
---|---|---|
committer | Krishnajinka <kris.jinka@samsung.com> | 2018-07-20 16:55:35 +0900 |
commit | b30fcd4ea8301f5cf0b176601eac9724b3347e25 (patch) | |
tree | 2587031f2ec8336c6e4abbad8ab41939e6c0ed83 /huawei/vnfmadapter/VnfmadapterService | |
parent | 7d894a773aedd079e7b65414299334982a38532d (diff) |
FIX MAJOR SONAR ISSUES IN SVNFM
optimise the use of try blocks and also use
the try with resources blocks for efficiency
Issue-ID: VFC-967
Change-Id: I1b2a22f1341a76879b1573c2d4caf71e7a75c34a
Signed-off-by: Krishnajinka <kris.jinka@samsung.com>
Diffstat (limited to 'huawei/vnfmadapter/VnfmadapterService')
4 files changed, 16 insertions, 75 deletions
diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/DownloadCsarManager.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/DownloadCsarManager.java index b09c50d7..4474c39b 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/DownloadCsarManager.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/DownloadCsarManager.java @@ -172,10 +172,9 @@ public class DownloadCsarManager { */ public static int unzipCSAR(String fileName, String filePath) { final int BUFFER = 2048; - int status = 0; - ZipFile zipFile = null; - try { - zipFile = new ZipFile(fileName); + int status = Constant.UNZIP_SUCCESS; + + try(ZipFile zipFile = new ZipFile(fileName);) { Enumeration emu = zipFile.entries(); while(emu.hasMoreElements()) { ZipEntry entry = (ZipEntry)emu.nextElement(); @@ -192,33 +191,19 @@ public class DownloadCsarManager { if(parent != null && (!parent.exists())) { parent.mkdirs(); } - try(FileOutputStream fos = new FileOutputStream(file)){ - try(BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER)){ - + try(FileOutputStream fos = new FileOutputStream(file); + BufferedOutputStream bos = new BufferedOutputStream(fos, BUFFER);){ int count; byte data[] = new byte[BUFFER]; while((count = bis.read(data, 0, BUFFER)) != -1) { bos.write(data, 0, count); } bos.flush(); - } } } - - status = Constant.UNZIP_SUCCESS; - } catch(Exception e) { status = Constant.UNZIP_FAIL; LOG.error("Exception: " + e); - } finally { - if(zipFile != null) { - try { - zipFile.close(); - } catch(IOException e) { - LOG.error("IOException: " + e); - ; - } - } } return status; } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java index 26d2ad57..64017c79 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestHttpContentExchange.java @@ -81,40 +81,14 @@ public class RestHttpContentExchange extends ContentExchange { if(data == null) { return ""; } - ByteArrayInputStream input = null; - GZIPInputStream gzis = null; - InputStreamReader reader = null; final StringBuilder out = new StringBuilder(); - try { - input = new ByteArrayInputStream(data); - gzis = new GZIPInputStream(input); - reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING)); + try (ByteArrayInputStream input = new ByteArrayInputStream(data); + GZIPInputStream gzis = new GZIPInputStream(input); + InputStreamReader reader = new InputStreamReader(gzis, Charset.forName(RestfulClientConst.ENCODING));) { final char[] buff = new char[1024]; for(int n; (n = reader.read(buff)) != -1;) { out.append(new String(buff, 0, n)); } - } finally { - if(reader != null) { - try { - reader.close(); - } catch(final IOException e) { - LOGGER.error("decompress Gzip reader exception:", e); - } - } - if(gzis != null) { - try { - gzis.close(); - } catch(final IOException e) { - LOGGER.error("decompress Gzip exception:", e); - } - } - if(input != null) { - try { - input.close(); - } catch(final IOException e) { - LOGGER.error("decompress Gzip input exception:", e); - } - } } return out.toString(); diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestfulConfigure.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestfulConfigure.java index 8b71941e..83bf9b53 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestfulConfigure.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/common/restclient/RestfulConfigure.java @@ -134,11 +134,10 @@ public class RestfulConfigure { LOG.error(filePath + "isn't exist."); return null; } - BufferedReader reader = null; + final StringBuilder jsonstr = new StringBuilder(); JSONObject jo = null; - try { - reader = new BufferedReader(new FileReader(file)); + try (BufferedReader reader = new BufferedReader(new FileReader(file));) { final ReaderHelper rHelpper = new ReaderHelper(reader); String tempString = null; while((tempString = rHelpper.getLine()) != null) { @@ -147,14 +146,6 @@ public class RestfulConfigure { jo = JSONObject.fromObject(jsonstr.toString()); } catch(final IOException e) { LOG.error("load file exception:" + e); - } finally { - if(reader != null) { - try { - reader.close(); - } catch(final IOException e) { - LOG.error("close error.", e); - } - } } return jo; } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java index 7b0a4db6..e92da07a 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/vnf/ScaleManager.java @@ -99,7 +99,6 @@ public abstract class ScaleManager { private static void writeVmIdsToFile(String vnfId, JSONArray vms) { String filePath = getVmIdsFilePath(vnfId); - FileOutputStream outStream = null; try { File destFile = FileUtils.getFile(filePath); @@ -112,28 +111,23 @@ public abstract class ScaleManager { if(!destFile.exists()) { destFile.createNewFile(); } - outStream = new FileOutputStream(destFile); - outStream.write(vms.toString().getBytes()); + try(FileOutputStream outStream = new FileOutputStream(destFile)) { + outStream.write(vms.toString().getBytes()); + } } catch(IOException e) { LOG.error("function=writeVmIdsToFile, msg=write vms to file ioexception, e : {}", e); - } finally { - IOUtils.closeQuietly(outStream); } } private static JSONArray readVmIdsFile(String vnfId) { String filePath = getVmIdsFilePath(vnfId); - InputStream ins = null; - BufferedInputStream bins = null; - String fileContent = ""; - try { - ins = FileUtils.openInputStream(FileUtils.getFile(filePath)); - bins = new BufferedInputStream(ins); + String fileContent = ""; + try(InputStream ins = FileUtils.openInputStream(FileUtils.getFile(filePath)); + BufferedInputStream bins = new BufferedInputStream(ins);) { byte[] contentByte = new byte[ins.available()]; int num = bins.read(contentByte); - if(num > 0) { fileContent = new String(contentByte); } @@ -145,9 +139,6 @@ public abstract class ScaleManager { LOG.error("function=readVmIdsFile, msg=read vms from file IOException, filePath : {}", filePath); } catch(JSONException e) { LOG.error("function=readVmIdsFile, msg=read vms from file JSONException, fileContent : {}", fileContent); - } finally { - IOUtils.closeQuietly(bins); - IOUtils.closeQuietly(ins); } return new JSONArray(); } |