Fix Sonar blocker issue for resource leak
Issue-ID: APPC-186
Change-Id: I6177d3976e4e91a6f29bee21558dc603aed44f1f
Signed-off-by: Summers, Gregory (gs2953) <gs2953@att.com>
diff --git a/appc-client/code-generator/src/main/java/org/openecomp/appc/tools/generator/extensions/YangContextBuilderImpl.java b/appc-client/code-generator/src/main/java/org/openecomp/appc/tools/generator/extensions/YangContextBuilderImpl.java
index 3284616..8325ecb 100644
--- a/appc-client/code-generator/src/main/java/org/openecomp/appc/tools/generator/extensions/YangContextBuilderImpl.java
+++ b/appc-client/code-generator/src/main/java/org/openecomp/appc/tools/generator/extensions/YangContextBuilderImpl.java
@@ -39,36 +39,35 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
-import java.util.*;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
public class YangContextBuilderImpl implements ContextBuilder {
@Override
public Map<String, Object> buildContext(String sourceFile, String contextConf) throws FileNotFoundException {
- InputStream source = new FileInputStream(sourceFile);
- if (source == null) {
+ try ( InputStream source = new FileInputStream(sourceFile) ) {}
+ catch ( IOException ex) {
throw new FileNotFoundException("YANG file <" + sourceFile + ">not found");
}
- YangTextSchemaContextResolver yangContextResolver = YangTextSchemaContextResolver
- .create("yang-context-resolver");
- try {
+ Optional<SchemaContext> sc = null;
+ try ( YangTextSchemaContextResolver yangContextResolver =
+ YangTextSchemaContextResolver.create("yang-context-resolver")) {
yangContextResolver.registerSource(new URL("file:///" + sourceFile));
+ sc = yangContextResolver.getSchemaContext();
} catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
- // TODO Auto-generated catch block
e.printStackTrace();
}
- Optional<SchemaContext> sc = yangContextResolver.getSchemaContext();
Map<String, Object> map = new HashMap<>();
- if (sc.isPresent()) {
-
+ if ( null != sc && sc.isPresent()) {
Set<Module> modules = sc.get().getModules();
for (Module module : modules) {
ModuleEffectiveStatementImpl impl = (ModuleEffectiveStatementImpl) module;
map.put("module", module);
}
-
}
return map;