Make sure the streams are closed properly - api 23/31923/2
authorKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Fri, 16 Feb 2018 08:35:39 +0000 (14:05 +0530)
committerKanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
Fri, 16 Feb 2018 08:55:53 +0000 (14:25 +0530)
Issue-ID: VFC-764

Change-Id: I718adfb397e611c9f106053950f82f7d25656190
Signed-off-by: Kanagaraj Manickam k00365106 <kanagaraj.manickam@huawei.com>
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/api/internalsvc/impl/VnfmAdapter2DriverMgrService.java
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/api/internalsvc/impl/VnfmAdapterMgrService.java
huawei/vnfmadapter/VnfmadapterService/service/src/main/java/org/onap/vfc/nfvo/vnfm/svnfm/vnfmadapter/service/constant/Constant.java

index fb5e5b8..535053b 100644 (file)
@@ -40,7 +40,7 @@ import net.sf.json.JSONObject;
  * <br>
  * <p>
  * </p>
- * 
+ *
  * @author
  * @version VFC 1.0 Jan 23, 2017
  */
@@ -77,38 +77,29 @@ public class VnfmAdapter2DriverMgrService implements IVnfmAdapter2DriverMgrServi
 
     /**
      * Retrieve VIM driver information.
-     * 
+     *
      * @return
      * @throws IOException
      */
     public static String readVnfmAdapterInfoFromJson() 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)
                 + "adapterInfo" + System.getProperty(Constant.FILE_SEPARATOR) + VNFMADAPTER2DRIVERMGR;
 
-        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;
@@ -167,6 +158,8 @@ public class VnfmAdapter2DriverMgrService implements IVnfmAdapter2DriverMgrServi
                     }
                 } catch(InterruptedException e) {
                     LOG.error(e.getMessage(), e);
+                    // Restore interrupted state...
+                    Thread.currentThread().interrupt();
                 }
 
                 sendRequest(this.paramsMap, this.adapterInfo);
index 3afdafe..28c018c 100644 (file)
@@ -76,38 +76,29 @@ public class VnfmAdapterMgrService implements IVnfmAdapterMgrService {
 
     /**
      * Retrieve VIM driver information.
-     * 
+     *
      * @return
      * @throws IOException
      */
     public String readVnfmAdapterInfoFromJson() 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)
                 + "adapterInfo" + System.getProperty(Constant.FILE_SEPARATOR) + VNFMADAPTERINFO;
 
-        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;
@@ -166,6 +157,8 @@ public class VnfmAdapterMgrService implements IVnfmAdapterMgrService {
                     }
                 } catch(InterruptedException e) {
                     LOG.error(e.getMessage(), e);
+                    // Restore interrupted state...
+                    Thread.currentThread().interrupt();
                 }
 
                 sendRequest(this.paramsMap, this.adapterInfo);
index 3b1036e..d26f849 100644 (file)
@@ -152,11 +152,11 @@ public class Constant {
 
     public static final String FILE_SEPARATOR = "file.separator";
 
-    public static final String PASSWORD = "password";
+    public static final String PASSWORD = "password";  // NOSONAR
 
     public static final String USERNAME = "userName";
 
-    public static final String LOCAL_HOST = "127.0.0.1";
+    public static final String LOCAL_HOST = "127.0.0.1";  // NOSONAR
 
     private Constant() {
         // private constructor