Fix sonar blocker issue in sli/plugins 09/71809/1
authorParshad Patel <pars.patel@samsung.com>
Mon, 5 Nov 2018 04:23:21 +0000 (13:23 +0900)
committerParshad Patel <pars.patel@samsung.com>
Mon, 5 Nov 2018 04:23:34 +0000 (13:23 +0900)
Fix use try with resources issue in PropertiesNode.java

Issue-ID: CCSDK-647
Change-Id: I62f2c0ecae65efb9d53022518fe36931ba5991e3
Signed-off-by: Parshad Patel <pars.patel@samsung.com>
properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java

index cefc9c2..612592b 100644 (file)
@@ -45,19 +45,36 @@ public class PropertiesNode implements SvcLogicJavaPlugin {
         Properties prop = new Properties();
         try {
             File file = new File(param.fileName);
-            InputStream in = new FileInputStream(file);
-            Map<String, String> mm = null;
-            String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : "";
-            if(param.fileBasedParsing){
-                byte[] data = new byte[(int) file.length()];
-                if ("json".equalsIgnoreCase(getFileExtension(param.fileName))) {
-                    in.read(data);
-                    String str = new String(data, "UTF-8");
-                    mm = JsonParser.convertToProperties(str);
-                } else if ("xml".equalsIgnoreCase(getFileExtension(param.fileName))) {
-                    in.read(data);
-                    String str = new String(data, "UTF-8");
-                    mm = XmlParser.convertToProperties(str, param.listNameList);
+            try(InputStream in = new FileInputStream(file)){
+                Map<String, String> mm = null;
+                String pfx = param.contextPrefix != null ? param.contextPrefix + '.' : "";
+                if(param.fileBasedParsing){
+                    byte[] data = new byte[(int) file.length()];
+                    if ("json".equalsIgnoreCase(getFileExtension(param.fileName))) {
+                        in.read(data);
+                        String str = new String(data, "UTF-8");
+                        mm = JsonParser.convertToProperties(str);
+                    } else if ("xml".equalsIgnoreCase(getFileExtension(param.fileName))) {
+                        in.read(data);
+                        String str = new String(data, "UTF-8");
+                        mm = XmlParser.convertToProperties(str, param.listNameList);
+                    } else {
+                        prop.load(in);
+                        for (Object key : prop.keySet()) {
+                            String name = (String) key;
+                            String value = prop.getProperty(name);
+                            if (value != null && value.trim().length() > 0) {
+                                ctx.setAttribute(pfx + name, value.trim());
+                                log.info("+++ " + pfx + name + ": [" + value + "]");
+                            }
+                        }
+                    }
+                    if (mm != null){
+                        for (Map.Entry<String,String> entry : mm.entrySet()){
+                            ctx.setAttribute(pfx + entry.getKey(), entry.getValue());
+                            log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]");
+                        }
+                    }
                 } else {
                     prop.load(in);
                     for (Object key : prop.keySet()) {
@@ -69,24 +86,7 @@ public class PropertiesNode implements SvcLogicJavaPlugin {
                         }
                     }
                 }
-                if (mm != null){
-                    for (Map.Entry<String,String> entry : mm.entrySet()){
-                        ctx.setAttribute(pfx + entry.getKey(), entry.getValue());
-                        log.info("+++ " + pfx + entry.getKey() + ": [" + entry.getValue() + "]");
-                    }
-                }
-            } else {
-                prop.load(in);
-                for (Object key : prop.keySet()) {
-                    String name = (String) key;
-                    String value = prop.getProperty(name);
-                    if (value != null && value.trim().length() > 0) {
-                        ctx.setAttribute(pfx + name, value.trim());
-                        log.info("+++ " + pfx + name + ": [" + value + "]");
-                    }
-                }
             }
-            in.close();
         } catch (IOException e) {
             throw new SvcLogicException("Cannot read property file: " + param.fileName + ": " + e.getMessage(), e);
         }