ATLAS-1685: fix for issues flagged by coverity scan

Signed-off-by: Madhan Neethiraj <madhan@apache.org>
diff --git a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
index 1c75636..a3a27bf 100644
--- a/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
+++ b/graphdb/api/src/main/java/org/apache/atlas/repository/graphdb/AtlasGraph.java
@@ -22,11 +22,10 @@
 import java.util.Map;
 import java.util.Set;
 
-import javax.script.Bindings;
 import javax.script.ScriptEngine;
-import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.groovy.GroovyExpression;
 import org.apache.atlas.typesystem.types.IDataType;
 
@@ -261,7 +260,7 @@
      *
      * @return script engine to execute Gremlin queries
      */
-    ScriptEngine getGremlinScriptEngine();
+    ScriptEngine getGremlinScriptEngine() throws AtlasBaseException;
 
     /**
      * Release an instance of the script engine obtained with getGremlinScriptEngine()
@@ -280,7 +279,7 @@
      *
      * @throws ScriptException
      */
-    Object executeGremlinScript(String query, boolean isPath) throws ScriptException;
+    Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException;
 
     /**
      * Executes a Gremlin script using a ScriptEngineManager provided by consumer, returns an object with the result.
diff --git a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java
index e114daa..9624c99 100644
--- a/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java
+++ b/graphdb/titan0/src/main/java/org/apache/atlas/repository/graphdb/titan0/Titan0Graph.java
@@ -35,6 +35,8 @@
 import javax.script.ScriptEngineManager;
 import javax.script.ScriptException;
 
+import org.apache.atlas.AtlasErrorCode;
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.groovy.GroovyExpression;
 import org.apache.atlas.repository.graphdb.AtlasEdge;
 import org.apache.atlas.repository.graphdb.AtlasGraph;
@@ -264,7 +266,7 @@
     }
 
     @Override
-    public Object executeGremlinScript(String query, boolean isPath) throws ScriptException {
+    public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
 
         Object result = executeGremlinScript(query);
         return convertGremlinScriptResult(isPath, result);
@@ -285,15 +287,17 @@
     }
 
     @Override
-    public ScriptEngine getGremlinScriptEngine() {
+    public ScriptEngine getGremlinScriptEngine() throws AtlasBaseException {
         ScriptEngineManager manager = new ScriptEngineManager();
         ScriptEngine        engine  = manager.getEngineByName("gremlin-groovy");
 
-        //Do not cache script compilations due to memory implications
-        if (engine != null) {
-            engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE);
+        if (engine == null) {
+            throw new AtlasBaseException(AtlasErrorCode.FAILED_TO_OBTAIN_GREMLIN_SCRIPT_ENGINE, "gremlin-groovy");
         }
 
+        //Do not cache script compilations due to memory implications
+        engine.getContext().setAttribute("#jsr223.groovy.engine.keep.globals", "phantom", ScriptContext.ENGINE_SCOPE);
+
         return engine;
     }
 
@@ -321,7 +325,7 @@
         return convertGremlinScriptResult(isPath, result);
     }
 
-    private Object executeGremlinScript(String gremlinQuery) throws ScriptException {
+    private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
         Object       result = null;
         ScriptEngine engine = getGremlinScriptEngine();
 
@@ -331,6 +335,8 @@
             bindings.put("g", getGraph());
 
             result = engine.eval(gremlinQuery, bindings);
+        } catch (ScriptException e) {
+            throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
         } finally {
             releaseGremlinScriptEngine(engine);
         }
diff --git a/graphdb/titan1/src/main/java/org/apache/atlas/repository/graphdb/titan1/Titan1Graph.java b/graphdb/titan1/src/main/java/org/apache/atlas/repository/graphdb/titan1/Titan1Graph.java
index e5a1d2c..6a61075 100644
--- a/graphdb/titan1/src/main/java/org/apache/atlas/repository/graphdb/titan1/Titan1Graph.java
+++ b/graphdb/titan1/src/main/java/org/apache/atlas/repository/graphdb/titan1/Titan1Graph.java
@@ -30,6 +30,8 @@
 import javax.script.ScriptEngine;
 import javax.script.ScriptException;
 
+import org.apache.atlas.AtlasErrorCode;
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.groovy.GroovyExpression;
 import org.apache.atlas.repository.graphdb.AtlasEdge;
 import org.apache.atlas.repository.graphdb.AtlasGraph;
@@ -316,13 +318,12 @@
     }
 
     @Override
-    public Object executeGremlinScript(String query, boolean isPath) throws ScriptException {
-
+    public Object executeGremlinScript(String query, boolean isPath) throws AtlasBaseException {
         Object result = executeGremlinScript(query);
         return convertGremlinValue(result);
     }
 
-    private Object executeGremlinScript(String gremlinQuery) throws ScriptException {
+    private Object executeGremlinScript(String gremlinQuery) throws AtlasBaseException {
         GremlinGroovyScriptEngine scriptEngine = getGremlinScriptEngine();
 
         try {
@@ -334,6 +335,8 @@
             Object result = scriptEngine.eval(gremlinQuery, bindings);
 
             return result;
+        } catch (ScriptException e) {
+            throw new AtlasBaseException(AtlasErrorCode.GREMLIN_SCRIPT_EXECUTION_FAILED, gremlinQuery);
         } finally {
             releaseGremlinScriptEngine(scriptEngine);
         }
diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
index 4506187..62fe9ac 100644
--- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
+++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java
@@ -96,9 +96,9 @@
     FAILED_TO_OBTAIN_TYPE_UPDATE_LOCK(500, "ATLAS-500-00-005", "Failed to get the lock; another type update might be in progress. Please try again"),
     FAILED_TO_OBTAIN_IMPORT_EXPORT_LOCK(500, "ATLAS-500-00-006", "Another import or export is in progress. Please try again"),
     NOTIFICATION_FAILED(500, "ATLAS-500-00-007", "Failed to notify for change {0}"),
-    GREMLIN_GROOVY_SCRIPT_ENGINE_FAILED(500, "ATLAS-500-00-008", "scriptEngine cannot be initialized for: {0}"),
+    FAILED_TO_OBTAIN_GREMLIN_SCRIPT_ENGINE(500, "ATLAS-500-00-008", "Failed to obtain gremlin script engine: {0}"),
     JSON_ERROR_OBJECT_MAPPER_NULL_RETURNED(500, "ATLAS-500-00-009", "ObjectMapper.readValue returned NULL for class: {0}"),
-    GREMLIN_SCRIPT_EXECUTION_FAILED(500, "ATLAS-500-00-00A", "Script execution failed for: {0}"),
+    GREMLIN_SCRIPT_EXECUTION_FAILED(500, "ATLAS-500-00-00A", "Gremlin script execution failed: {0}"),
 
     CURATOR_FRAMEWORK_UPDATE(500, "ATLAS-500-00-00B", "ActiveInstanceState.update resulted in exception."),
     QUICK_START(500, "ATLAS-500-00-00C", "Failed to run QuickStart: {0}"),
diff --git a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
index 1cf4434..571ce6e 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/EntityDiscoveryService.java
@@ -94,53 +94,48 @@
         AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
         GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
 
-        try {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Executing DSL query: {}", dslQuery);
-            }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Executing DSL query: {}", dslQuery);
+        }
 
-            Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
+        Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
 
-            if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
-                List   queryResult  = (List) result;
-                Object firstElement = queryResult.get(0);
+        if (result instanceof List && CollectionUtils.isNotEmpty((List)result)) {
+            List   queryResult  = (List) result;
+            Object firstElement = queryResult.get(0);
 
-                if (firstElement instanceof AtlasVertex) {
-                    for (Object element : queryResult) {
-                        if (element instanceof AtlasVertex) {
-                            ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
-                        } else {
-                            LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
-                        }
+            if (firstElement instanceof AtlasVertex) {
+                for (Object element : queryResult) {
+                    if (element instanceof AtlasVertex) {
+                        ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)element));
+                    } else {
+                        LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
                     }
-                } else if (firstElement instanceof Map &&
-                           (((Map)firstElement).containsKey("theInstance") || ((Map)firstElement).containsKey("theTrait"))) {
-                    for (Object element : queryResult) {
-                        if (element instanceof Map) {
-                            Map map = (Map)element;
+                }
+            } else if (firstElement instanceof Map &&
+                       (((Map)firstElement).containsKey("theInstance") || ((Map)firstElement).containsKey("theTrait"))) {
+                for (Object element : queryResult) {
+                    if (element instanceof Map) {
+                        Map map = (Map)element;
 
-                            if (map.containsKey("theInstance")) {
-                                Object value = map.get("theInstance");
+                        if (map.containsKey("theInstance")) {
+                            Object value = map.get("theInstance");
 
-                                if (value instanceof List && CollectionUtils.isNotEmpty((List)value)) {
-                                    Object entry = ((List)value).get(0);
+                            if (value instanceof List && CollectionUtils.isNotEmpty((List)value)) {
+                                Object entry = ((List)value).get(0);
 
-                                    if (entry instanceof AtlasVertex) {
-                                        ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)entry));
-                                    }
+                                if (entry instanceof AtlasVertex) {
+                                    ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex)entry));
                                 }
                             }
-                        } else {
-                            LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
                         }
+                    } else {
+                        LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
                     }
-                } else if (gremlinQuery.hasSelectList()) {
-                    ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
                 }
+            } else if (gremlinQuery.hasSelectList()) {
+                ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
             }
-
-        } catch (ScriptException e) {
-            throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, gremlinQuery.queryStr());
         }
 
         return ret;
diff --git a/repository/src/main/java/org/apache/atlas/discovery/EntityLineageService.java b/repository/src/main/java/org/apache/atlas/discovery/EntityLineageService.java
index b81754d..d6aca89 100644
--- a/repository/src/main/java/org/apache/atlas/discovery/EntityLineageService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/EntityLineageService.java
@@ -87,48 +87,43 @@
     }
 
     private AtlasLineageInfo getLineageInfo(String guid, LineageDirection direction, int depth) throws AtlasBaseException {
-        Map<String, AtlasEntityHeader> entities     = new HashMap<String, AtlasEntityHeader>();
-        Set<LineageRelation>           relations    = new HashSet<LineageRelation>();
+        Map<String, AtlasEntityHeader> entities     = new HashMap<>();
+        Set<LineageRelation>           relations    = new HashSet<>();
         String                         lineageQuery = getLineageQuery(guid, direction, depth);
 
-        try {
-            List paths = (List) graph.executeGremlinScript(lineageQuery, true);
+        List paths = (List) graph.executeGremlinScript(lineageQuery, true);
 
-            if (CollectionUtils.isNotEmpty(paths)) {
-                for (Object path : paths) {
-                    if (path instanceof List) {
-                        List vertices = (List) path;
+        if (CollectionUtils.isNotEmpty(paths)) {
+            for (Object path : paths) {
+                if (path instanceof List) {
+                    List vertices = (List) path;
 
-                        if (CollectionUtils.isNotEmpty(vertices)) {
-                            AtlasEntityHeader prev = null;
+                    if (CollectionUtils.isNotEmpty(vertices)) {
+                        AtlasEntityHeader prev = null;
 
-                            for (Object vertex : vertices) {
-                                if (!(vertex instanceof AtlasVertex)) {
-                                    continue;
-                                }
-
-                                AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader((AtlasVertex)vertex);
-
-                                if (!entities.containsKey(entity.getGuid())) {
-                                    entities.put(entity.getGuid(), entity);
-                                }
-
-                                if (prev != null) {
-                                    if (direction.equals(LineageDirection.INPUT)) {
-                                        relations.add(new LineageRelation(entity.getGuid(), prev.getGuid()));
-                                    } else if (direction.equals(LineageDirection.OUTPUT)) {
-                                        relations.add(new LineageRelation(prev.getGuid(), entity.getGuid()));
-                                    }
-                                }
-                                prev = entity;
+                        for (Object vertex : vertices) {
+                            if (!(vertex instanceof AtlasVertex)) {
+                                continue;
                             }
+
+                            AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader((AtlasVertex)vertex);
+
+                            if (!entities.containsKey(entity.getGuid())) {
+                                entities.put(entity.getGuid(), entity);
+                            }
+
+                            if (prev != null) {
+                                if (direction.equals(LineageDirection.INPUT)) {
+                                    relations.add(new LineageRelation(entity.getGuid(), prev.getGuid()));
+                                } else if (direction.equals(LineageDirection.OUTPUT)) {
+                                    relations.add(new LineageRelation(prev.getGuid(), entity.getGuid()));
+                                }
+                            }
+                            prev = entity;
                         }
                     }
                 }
             }
-
-        } catch (ScriptException e) {
-            throw new AtlasBaseException(AtlasErrorCode.INSTANCE_LINEAGE_QUERY_FAILED, lineageQuery);
         }
 
         return new AtlasLineageInfo(guid, entities, relations, direction, depth);
diff --git a/repository/src/main/java/org/apache/atlas/discovery/graph/GraphBackedDiscoveryService.java b/repository/src/main/java/org/apache/atlas/discovery/graph/GraphBackedDiscoveryService.java
index f84f405..23e4531 100755
--- a/repository/src/main/java/org/apache/atlas/discovery/graph/GraphBackedDiscoveryService.java
+++ b/repository/src/main/java/org/apache/atlas/discovery/graph/GraphBackedDiscoveryService.java
@@ -27,12 +27,12 @@
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
-import javax.script.ScriptException;
 
 import org.apache.atlas.AtlasClient;
 import org.apache.atlas.GraphTransaction;
 import org.apache.atlas.discovery.DiscoveryException;
 import org.apache.atlas.discovery.DiscoveryService;
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.query.Expressions;
 import org.apache.atlas.query.GremlinEvaluator;
 import org.apache.atlas.query.GremlinQuery;
@@ -197,8 +197,8 @@
         try {
             Object o = graph.executeGremlinScript(gremlinQuery, false);
             return extractResult(o);
-        } catch (ScriptException se) {
-            throw new DiscoveryException(se);
+        } catch (AtlasBaseException e) {
+            throw new DiscoveryException(e);
         }
     }
 
diff --git a/repository/src/main/java/org/apache/atlas/services/MetricsService.java b/repository/src/main/java/org/apache/atlas/services/MetricsService.java
index 3ef0138..e3bedb8 100644
--- a/repository/src/main/java/org/apache/atlas/services/MetricsService.java
+++ b/repository/src/main/java/org/apache/atlas/services/MetricsService.java
@@ -21,6 +21,7 @@
 import com.google.inject.Singleton;
 import org.apache.atlas.ApplicationProperties;
 import org.apache.atlas.AtlasException;
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.model.metrics.AtlasMetrics;
 import org.apache.atlas.repository.graph.AtlasGraphProvider;
 import org.apache.atlas.repository.graphdb.AtlasGraph;
@@ -31,7 +32,6 @@
 import org.slf4j.LoggerFactory;
 
 import javax.inject.Inject;
-import javax.script.ScriptException;
 import java.util.List;
 import java.util.Map;
 
@@ -100,7 +100,7 @@
                         LOG.debug("Executing query: {}", metricQuery);
                     }
                     executeGremlinQuery(metrics, metricQuery.group, metricQuery.name, metricQuery.query);
-                } catch (ScriptException e) {
+                } catch (AtlasBaseException e) {
                     if (LOG.isDebugEnabled()) {
                         LOG.debug("Gremlin execution failed for metric {}", metricQuery, e);
                     } else {
@@ -120,7 +120,7 @@
         return cachedMetrics;
     }
 
-    private void executeGremlinQuery(AtlasMetrics metrics, String type, String name, String query) throws ScriptException {
+    private void executeGremlinQuery(AtlasMetrics metrics, String type, String name, String query) throws AtlasBaseException {
         Object result = atlasGraph.executeGremlinScript(query, false);
 
         if (result instanceof Number) {
diff --git a/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java b/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
index 1531c9c..5165bcb 100644
--- a/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
+++ b/repository/src/test/java/org/apache/atlas/services/MetricsServiceTest.java
@@ -17,6 +17,7 @@
  */
 package org.apache.atlas.services;
 
+import org.apache.atlas.exception.AtlasBaseException;
 import org.apache.atlas.model.metrics.AtlasMetrics;
 import org.apache.atlas.repository.graphdb.AtlasGraph;
 import org.apache.atlas.type.AtlasTypeRegistry;
@@ -26,7 +27,6 @@
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
-import javax.script.ScriptException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
@@ -49,7 +49,7 @@
     private Number mockCount = 10;
 
     @BeforeClass
-    public void init() throws ScriptException {
+    public void init() throws AtlasBaseException {
         Map<String, Object> mockMap = new HashMap<>();
         mockMap.put("a", 1);
         mockMap.put("b", 2);
@@ -66,7 +66,7 @@
         metricsService = new MetricsService(mockConfig, mockGraph);
     }
 
-    private void setupMockGraph() throws ScriptException {
+    private void setupMockGraph() throws AtlasBaseException {
         if (mockGraph == null) mockGraph = mock(AtlasGraph.class);
         when(mockGraph.executeGremlinScript(anyString(), eq(false))).thenAnswer(new Answer<Object>() {
             @Override
@@ -81,7 +81,7 @@
     }
 
     @Test
-    public void testGetMetrics() throws InterruptedException, ScriptException {
+    public void testGetMetrics() throws InterruptedException, AtlasBaseException {
         assertNotNull(metricsService);
         AtlasMetrics metrics = metricsService.getMetrics(false);
         assertNotNull(metrics);
diff --git a/webapp/src/main/java/org/apache/atlas/web/resources/ExportService.java b/webapp/src/main/java/org/apache/atlas/web/resources/ExportService.java
index ffdbfac..159369c 100644
--- a/webapp/src/main/java/org/apache/atlas/web/resources/ExportService.java
+++ b/webapp/src/main/java/org/apache/atlas/web/resources/ExportService.java
@@ -628,7 +628,7 @@
 
         private       int                 progressReportCount = 0;
 
-        ExportContext(AtlasExportResult result, ZipSink sink) {
+        ExportContext(AtlasExportResult result, ZipSink sink) throws AtlasBaseException {
             this.result = result;
             this.sink   = sink;