From fcb76a5d9f223831c2ec2012b7dd26aac53152a3 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Mon, 5 Nov 2018 13:23:21 +0900 Subject: Fix sonar blocker issue in sli/plugins Fix use try with resources issue in PropertiesNode.java Issue-ID: CCSDK-647 Change-Id: I62f2c0ecae65efb9d53022518fe36931ba5991e3 Signed-off-by: Parshad Patel --- .../ccsdk/sli/plugins/prop/PropertiesNode.java | 60 +++++++++++----------- 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'properties-node/provider/src/main') diff --git a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java index cefc9c23e..612592b58 100644 --- a/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java +++ b/properties-node/provider/src/main/java/org/onap/ccsdk/sli/plugins/prop/PropertiesNode.java @@ -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 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 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 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 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); } -- cgit 1.2.3-korg