Fixed Sonar "Blocker Bugs" 58/88158/5
authorVasyl Razinkov <vasylrazinkov@sidero.ie>
Tue, 21 May 2019 13:21:48 +0000 (14:21 +0100)
committerVasyl Razinkov <vasylrazinkov@sidero.ie>
Mon, 27 May 2019 20:23:18 +0000 (20:23 +0000)
Fixed potential "thread-Leak"

Signed-off-by: Vasyl Razinkov <vasylrazinkov@sidero.ie>
Issue-ID: AAI-2459
Change-Id: Ic36ff778486a7a860070b05873dc88d7d31f736b

aai-core/src/main/java/org/onap/aai/dbmap/AAIGraph.java
aai-core/src/main/java/org/onap/aai/dbmap/InMemoryGraph.java

index 0e7c057..ca4e02f 100644 (file)
@@ -24,15 +24,11 @@ package org.onap.aai.dbmap;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
-
+import org.apache.commons.configuration.ConfigurationException;
 import org.apache.commons.configuration.PropertiesConfiguration;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.structure.Graph;
@@ -107,17 +103,18 @@ public class AAIGraph {
         return isInit;
     }
 
-    private void loadGraph(String name, String configPath, String serviceName) throws Exception {
+    private void loadGraph(final String name, final String configPath, final String serviceName)
+        throws AAIException, ConfigurationException {
         // Graph being opened by JanusGraphFactory is being placed in hashmap to be used later
         // These graphs shouldn't be closed until the application shutdown
         try {
-            PropertiesConfiguration propertiesConfiguration = new AAIGraphConfig.Builder(configPath)
-                    .forService(serviceName).withGraphType(name).buildConfiguration();
-            JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
+            final PropertiesConfiguration propertiesConfiguration =
+                new AAIGraphConfig.Builder(configPath).forService(serviceName).withGraphType(name).buildConfiguration();
+            final JanusGraph graph = JanusGraphFactory.open(propertiesConfiguration);
 
-            Properties graphProps = new Properties();
+            final Properties graphProps = new Properties();
             propertiesConfiguration.getKeys()
-                    .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
+                .forEachRemaining(k -> graphProps.setProperty(k, propertiesConfiguration.getString(k)));
 
             if ("inmemory".equals(graphProps.get("storage.backend"))) {
                 // Load the propertyKeys, indexes and edge-Labels into the DB
@@ -130,14 +127,12 @@ public class AAIGraph {
             }
 
             graphs.put(name, graph);
-        } catch (FileNotFoundException fnfe) {
+        } catch (final FileNotFoundException fnfe) {
             throw new AAIException("AAI_4001");
-        } catch (IOException e) {
-            throw new AAIException("AAI_4002");
         }
     }
 
-    private void loadSnapShotToInMemoryGraph(JanusGraph graph, Properties graphProps) {
+    private void loadSnapShotToInMemoryGraph(final JanusGraph graph, final Properties graphProps) {
         if (logger.isDebugEnabled()) {
             logger.debug("Load Snapshot to InMemory Graph");
         }
@@ -159,7 +154,7 @@ public class AAIGraph {
         }
     }
 
-    private void loadSchema(JanusGraph graph) {
+    private void loadSchema(final JanusGraph graph) {
         // Load the propertyKeys, indexes and edge-Labels into the DB
         JanusGraphManagement graphMgt = graph.openManagement();
 
@@ -183,16 +178,16 @@ public class AAIGraph {
         return graphs.get(REALTIME_DB);
     }
 
-    public void graphShutdown(DBConnectionType connectionType) {
+    public void graphShutdown(final DBConnectionType connectionType) {
 
         graphs.get(this.getGraphName(connectionType)).close();
     }
 
-    public JanusGraph getGraph(DBConnectionType connectionType) {
+    public JanusGraph getGraph(final DBConnectionType connectionType) {
         return graphs.get(this.getGraphName(connectionType));
     }
 
-    private String getGraphName(DBConnectionType connectionType) {
+    private String getGraphName(final DBConnectionType connectionType) {
         String graphName = "";
         if (DBConnectionType.CACHED.equals(connectionType)) {
             graphName = this.CACHED_DB;
@@ -203,7 +198,7 @@ public class AAIGraph {
         return graphName;
     }
 
-    private void logAndPrint(EELFLogger logger, String msg) {
+    private void logAndPrint(final EELFLogger logger, final String msg) {
         System.out.println(msg);
         logger.info(msg);
     }
index a5d3cda..54986f3 100644 (file)
@@ -22,13 +22,10 @@ package org.onap.aai.dbmap;
 
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
-
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Properties;
-
-import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.structure.io.IoCore;
 import org.janusgraph.core.JanusGraph;
 import org.janusgraph.core.JanusGraphFactory;
@@ -36,56 +33,52 @@ import org.janusgraph.core.JanusGraphTransaction;
 import org.janusgraph.core.schema.JanusGraphManagement;
 import org.onap.aai.dbgen.GraphSONPartialIO;
 import org.onap.aai.dbgen.SchemaGenerator;
-import org.onap.aai.logging.LogFormatTools;
 
 public class InMemoryGraph {
 
     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(InMemoryGraph.class);
     private JanusGraph graph = null;
 
-    public InMemoryGraph(Builder builder) throws IOException {
+    public InMemoryGraph(final Builder builder) throws IOException {
         /*
          * Create a In-memory graph
          */
-        InputStream is = new FileInputStream(builder.propertyFile);
-        try {
+        try (final InputStream is = new FileInputStream(builder.propertyFile);) {
             graph = JanusGraphFactory.open(builder.propertyFile);
 
-            Properties graphProps = new Properties();
+            final Properties graphProps = new Properties();
             graphProps.load(is);
-            JanusGraphManagement graphMgt = graph.openManagement();
+            final JanusGraphManagement graphMgt = graph.openManagement();
             if (builder.isSchemaEnabled) {
                 LOGGER.info("Schema Enabled");
                 SchemaGenerator.loadSchemaIntoJanusGraph(graph, graphMgt, graphProps.getProperty("storage.backend"));
             }
-            JanusGraphTransaction transaction = graph.newTransaction();
-            LOGGER.info("Loading snapshot");
-            if (builder.isPartialGraph) {
-                if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
-                    transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
-                } else {
-                    transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
+            try (final JanusGraphTransaction transaction = graph.newTransaction();) {
+                LOGGER.info("Loading snapshot");
+                if (builder.isPartialGraph) {
+                    if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
+                        transaction.io(GraphSONPartialIO.build()).readGraph(builder.graphsonLocation);
+                    } else {
+                        transaction.io(GraphSONPartialIO.build()).reader().create().readGraph(builder.seqInputStream,
                             graph);
-                }
-            } else {
-                if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
-                    transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
+                    }
                 } else {
-                    transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
+                    if ((builder.graphsonLocation != null) && (builder.graphsonLocation.length() > 0)) {
+                        transaction.io(IoCore.graphson()).readGraph(builder.graphsonLocation);
+                    } else {
+                        transaction.io(IoCore.graphson()).reader().create().readGraph(builder.seqInputStream, graph);
+                    }
                 }
+                transaction.commit();
+            } catch (final IOException e) {
+                LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n", e);
+                throw new IllegalStateException("Could not load datasnapshot to in memory graph");
             }
-            transaction.commit();
 
-        } catch (Exception e) {
-            // TODO : Changesysout to logger
-            e.printStackTrace();
-            LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n" + LogFormatTools.getStackTop(e));
+        } catch (final IOException e) {
+            LOGGER.error("ERROR: Could not load datasnapshot to in memory graph. \n", e);
             throw new IllegalStateException("Could not load datasnapshot to in memory graph");
-
-        } finally {
-            is.close();
         }
-
     }
 
     public static class Builder {