Repository: incubator-unomi
Updated Branches:
  refs/heads/master db066aa8e -> 46900c554


UNOMI-109 NPE when an exception is caught in the ElasticSearch persistence 
implementation
This is fixed by catching the result into a Boolean class and then checking if 
it is null before returning the result.

Signed-off-by: Serge Huber <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/46900c55
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/46900c55
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/46900c55

Branch: refs/heads/master
Commit: 46900c554b4f51aa64261dcf2bf2dbb58928eede
Parents: db066aa
Author: Serge Huber <[email protected]>
Authored: Fri Jun 30 22:03:40 2017 +0200
Committer: Serge Huber <[email protected]>
Committed: Fri Jun 30 22:03:40 2017 +0200

----------------------------------------------------------------------
 .../ElasticSearchPersistenceServiceImpl.java    | 72 ++++++++++++++++----
 1 file changed, 60 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/46900c55/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
 
b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
index bdd676d..8bf6024 100644
--- 
a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
+++ 
b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/ElasticSearchPersistenceServiceImpl.java
@@ -674,8 +674,7 @@ public class ElasticSearchPersistenceServiceImpl implements 
PersistenceService,
 
     @Override
     public boolean save(final Item item, final boolean useBatching) {
-
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result =  new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 try {
                     String source = 
CustomObjectMapper.getObjectMapper().writeValueAsString(item);
@@ -720,7 +719,11 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
-
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override
@@ -730,7 +733,7 @@ public class ElasticSearchPersistenceServiceImpl implements 
PersistenceService,
 
     @Override
     public boolean update(final String itemId, final Date dateHint, final 
Class clazz, final Map source) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 try {
                     String itemType = (String) 
clazz.getField("ITEM_TYPE").get(null);
@@ -756,11 +759,16 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override
     public boolean updateWithQueryAndScript(final Date dateHint, final 
Class<?> clazz, final String[] scripts, final Map<String, Object>[] 
scriptParams, final Condition[] conditions) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 try {
                     String itemType = (String) 
clazz.getField("ITEM_TYPE").get(null);
@@ -810,11 +818,16 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override
     public boolean updateWithScript(final String itemId, final Date dateHint, 
final Class<?> clazz, final String script, final Map<String, Object> 
scriptParams) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 try {
                     String itemType = (String) 
clazz.getField("ITEM_TYPE").get(null);
@@ -843,11 +856,16 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override
     public <T extends Item> boolean remove(final String itemId, final Class<T> 
clazz) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 //Index the query = register it in the percolator
                 try {
@@ -861,10 +879,15 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     public <T extends Item> boolean removeByQuery(final Condition query, final 
Class<T> clazz) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 try {
                     String itemType = (String) 
clazz.getField("ITEM_TYPE").get(null);
@@ -910,10 +933,15 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     public boolean createIndex(final String indexName) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) {
                 IndicesExistsResponse indicesExistsResponse = 
client.admin().indices().prepareExists(indexName).execute().actionGet();
                 boolean indexExists = indicesExistsResponse.isExists();
@@ -930,10 +958,15 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 return !indexExists;
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     public boolean removeIndex(final String indexName) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) {
                 IndicesExistsResponse indicesExistsResponse = 
client.admin().indices().prepareExists(indexName).execute().actionGet();
                 boolean indexExists = indicesExistsResponse.isExists();
@@ -944,6 +977,11 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 return indexExists;
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     private void internalCreateIndex(String indexName, Map<String, String> 
mappings) {
@@ -1075,7 +1113,7 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
     }
 
     public boolean saveQuery(final String queryName, final String query) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 //Index the query = register it in the percolator
                 try {
@@ -1090,6 +1128,11 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override
@@ -1103,7 +1146,7 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
 
     @Override
     public boolean removeQuery(final String queryName) {
-        return new InClassLoaderExecute<Boolean>() {
+        Boolean result = new InClassLoaderExecute<Boolean>() {
             protected Boolean execute(Object... args) throws Exception {
                 //Index the query = register it in the percolator
                 try {
@@ -1116,6 +1159,11 @@ public class ElasticSearchPersistenceServiceImpl 
implements PersistenceService,
                 }
             }
         }.catchingExecuteInClassLoader(true);
+        if (result == null) {
+            return false;
+        } else {
+            return result;
+        }
     }
 
     @Override

Reply via email to