Fix Sonar blocker issue for resource leak 37/15737/2
authorSummers, Gregory (gs2953) <gs2953@att.com>
Tue, 26 Sep 2017 21:12:27 +0000 (16:12 -0500)
committerSkip Wonnell <skip@att.com>
Wed, 27 Sep 2017 12:56:11 +0000 (12:56 +0000)
Issue-ID: APPC-186
Change-Id: I6177d3976e4e91a6f29bee21558dc603aed44f1f
Signed-off-by: Summers, Gregory (gs2953) <gs2953@att.com>
appc-client/code-generator/src/main/java/org/openecomp/appc/tools/generator/extensions/YangContextBuilderImpl.java

index 3284616..8325ecb 100644 (file)
@@ -39,36 +39,35 @@ import java.io.FileNotFoundException;
 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;