diff options
author | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-02-16 14:07:06 +0530 |
---|---|---|
committer | Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com> | 2018-02-16 14:25:55 +0530 |
commit | 63c6439895ce75c376fe6f0f71f223538f10d379 (patch) | |
tree | 8d914b829a2d1f3d83d3ddff3808cf892a6df6cb /huawei/vnfmadapter/VnfmadapterService | |
parent | 8ba7c5e18eb8859aa1e2148ce5cdb7ae75789de5 (diff) |
Make sure the streams are closed properly-adapter
Issue-ID: VFC-764
Change-Id: Ief029829a0a1037f6f8e0d8f127104ed739e63bd
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Diffstat (limited to 'huawei/vnfmadapter/VnfmadapterService')
2 files changed, 57 insertions, 80 deletions
diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java index 68bc7f9e..53e51de7 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/adapter/impl/AdapterResourceManager.java @@ -308,12 +308,15 @@ public class AdapterResourceManager implements IResourceManager { LOG.info("changeWorkingDirectory: " + ftpClient.changeWorkingDirectory(vnfpkg.getString(VNFD_FILE_PATH))); String vnfdPath = csarfilepath + "Artifacts/Deployment/OTHER/"; LOG.info("vnfd_file_name: " + vnfdPath + vnfpkg.getString("vnfd_file_name")); - InputStream inputStream = new FileInputStream(new File(vnfdPath + vnfpkg.getString("vnfd_file_name"))); - flag = ftpClient.storeFile(vnfpkg.getString("vnfd_file_name"), inputStream); - if(flag) { - resJson.put("message", "upload Csar success!"); + try(InputStream inputStream = new FileInputStream(new File(vnfdPath + vnfpkg.getString("vnfd_file_name")))){ + flag = ftpClient.storeFile(vnfpkg.getString("vnfd_file_name"), inputStream); + if(flag) { + resJson.put("message", "upload Csar success!"); + } + } catch(Exception e) { + LOG.error("Exception: " + e); } - inputStream.close(); + ftpClient.logout(); } catch(Exception e) { LOG.error("Exception: " + e); @@ -607,33 +610,23 @@ public class AdapterResourceManager implements IResourceManager { * @since VFC 1.0 */ public static String readVfnPkgInfoFromJson() throws IOException { - InputStream ins = null; - BufferedInputStream bins = null; String fileContent = ""; String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + Constant.VNFPKGINFO; - try { - ins = new FileInputStream(fileName); - bins = new BufferedInputStream(ins); + try (InputStream ins = new FileInputStream(fileName)) { + try(BufferedInputStream bins = new BufferedInputStream(ins)){ + byte[] contentByte = new byte[ins.available()]; + int num = bins.read(contentByte); - byte[] contentByte = new byte[ins.available()]; - int num = bins.read(contentByte); - - if(num > 0) { - fileContent = new String(contentByte); + if(num > 0) { + fileContent = new String(contentByte); + } } } catch(FileNotFoundException e) { LOG.error(fileName + "is not found!", e); - } finally { - if(ins != null) { - ins.close(); - } - if(bins != null) { - bins.close(); - } } return fileContent; @@ -641,35 +634,28 @@ public class AdapterResourceManager implements IResourceManager { private static JSONObject readVnfdIdInfoFromJson() { JSONObject jsonObject = new JSONObject(); - InputStream ins = null; - BufferedInputStream bins = null; + String fileContent = ""; String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfpkginfo" + System.getProperty(Constant.FILE_SEPARATOR) + "vnfd_ids.json"; - try { - ins = new FileInputStream(fileName); - bins = new BufferedInputStream(ins); - - byte[] contentByte = new byte[ins.available()]; - int num = bins.read(contentByte); + try (InputStream ins = new FileInputStream(fileName)) { + try (BufferedInputStream bins = new BufferedInputStream(ins)){ + byte[] contentByte = new byte[ins.available()]; + int num = bins.read(contentByte); - if(num > 0) { - fileContent = new String(contentByte); - } - if(fileContent != null) { - jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds"); + if(num > 0) { + fileContent = new String(contentByte); + } + if(fileContent != null) { + jsonObject = JSONObject.fromObject(fileContent).getJSONObject("vnfdIds"); + } } - ins.close(); - bins.close(); } catch(Exception e) { LOG.error(fileName + " read error!", e); - } finally { - } - return jsonObject; } diff --git a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/connect/AbstractSslContext.java b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/connect/AbstractSslContext.java index 61c1f8fa..bccf4815 100644 --- a/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/connect/AbstractSslContext.java +++ b/huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/csm/connect/AbstractSslContext.java @@ -82,7 +82,7 @@ public class AbstractSslContext { KeyManager[] kms = null; try { String CERT_STORE = "etc/conf/server.p12"; - String CERT_STORE_PASSWORD = "Changeme_123"; + String CERT_STORE_PASSWORD = "Changeme_123"; // NOSONAR String KEY_STORE_TYPE = "PKCS12"; if(sslConf != null) { CERT_STORE = sslConf.getString("keyStore"); @@ -90,17 +90,17 @@ public class AbstractSslContext { KEY_STORE_TYPE = sslConf.getString("keyStoreType"); } // load jks file - FileInputStream f_certStore = new FileInputStream(CERT_STORE); - KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE); - ks.load(f_certStore, CERT_STORE_PASSWORD.toCharArray()); - f_certStore.close(); + try(FileInputStream f_certStore = new FileInputStream(CERT_STORE)) { + KeyStore ks = KeyStore.getInstance(KEY_STORE_TYPE); + ks.load(f_certStore, CERT_STORE_PASSWORD.toCharArray()); - // init and create - String alg = KeyManagerFactory.getDefaultAlgorithm(); - KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg); - kmFact.init(ks, CERT_STORE_PASSWORD.toCharArray()); + // init and create + String alg = KeyManagerFactory.getDefaultAlgorithm(); + KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg); + kmFact.init(ks, CERT_STORE_PASSWORD.toCharArray()); - kms = kmFact.getKeyManagers(); + kms = kmFact.getKeyManagers(); + } } catch(Exception e) { LOG.error("create KeyManager fail!", e); } @@ -112,23 +112,22 @@ public class AbstractSslContext { try { String TRUST_STORE = "etc/conf/trust.jks"; - String TRUST_STORE_PASSWORD = "Changeme_123"; + String TRUST_STORE_PASSWORD = "Changeme_123"; // NOSONAR String TRUST_STORE_TYPE = "jks"; if(sslConf != null) { TRUST_STORE = sslConf.getString("trustStore"); TRUST_STORE_PASSWORD = sslConf.getString("trustStorePass"); TRUST_STORE_TYPE = sslConf.getString("trustStoreType"); } - FileInputStream f_trustStore = new FileInputStream(TRUST_STORE); - KeyStore ks = KeyStore.getInstance(TRUST_STORE_TYPE); - ks.load(f_trustStore, TRUST_STORE_PASSWORD.toCharArray()); - f_trustStore.close(); - - String alg = TrustManagerFactory.getDefaultAlgorithm(); - TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); - tmFact.init(ks); - tms = tmFact.getTrustManagers(); - + try(FileInputStream f_trustStore = new FileInputStream(TRUST_STORE)) { + KeyStore ks = KeyStore.getInstance(TRUST_STORE_TYPE); + ks.load(f_trustStore, TRUST_STORE_PASSWORD.toCharArray()); + + String alg = TrustManagerFactory.getDefaultAlgorithm(); + TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg); + tmFact.init(ks); + tms = tmFact.getTrustManagers(); + } } catch(Exception e) { LOG.error("create TrustManager fail!", e); } @@ -137,43 +136,35 @@ public class AbstractSslContext { /** * readSSLConfToJson - * + * * @return * @throws IOException * @since VFC 1.0 */ public static JSONObject readSSLConfToJson() throws IOException { JSONObject sslJson = null; - InputStream ins = null; - BufferedInputStream bins = null; + String fileContent = ""; String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot() + System.getProperty(Constant.FILE_SEPARATOR) + "etc" + System.getProperty(Constant.FILE_SEPARATOR) + "conf" + System.getProperty(Constant.FILE_SEPARATOR) + "sslconf.json"; - try { - ins = new FileInputStream(fileName); - bins = new BufferedInputStream(ins); + try (InputStream ins = new FileInputStream(fileName)) { + try(BufferedInputStream bins = new BufferedInputStream(ins)) { - byte[] contentByte = new byte[ins.available()]; - int num = bins.read(contentByte); + byte[] contentByte = new byte[ins.available()]; + int num = bins.read(contentByte); - if(num > 0) { - fileContent = new String(contentByte); + if(num > 0) { + fileContent = new String(contentByte); + } + sslJson = JSONObject.fromObject(fileContent); } - sslJson = JSONObject.fromObject(fileContent); } catch(FileNotFoundException e) { LOG.error(fileName + "is not found!", e); } catch(Exception e) { - LOG.error("read sslconf file fail.please check if the 'sslconf.json' is exist."); - } finally { - if(ins != null) { - ins.close(); - } - if(bins != null) { - bins.close(); - } + LOG.error("read sslconf file fail.please check if the 'sslconf.json' is exist.", e); } return sslJson; |