From 2fbe9cdd87842d0dbe6f284b87ba883dc76c9620 Mon Sep 17 00:00:00 2001 From: agazzarini Date: Wed, 13 Nov 2019 18:13:58 +0100 Subject: [PATCH 1/3] [ SEARCH-875 ] Minor refactoring and formatting --- .../main/java/org/alfresco/solr/utils/Utils.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java index 2254b9cf0..b3e90e502 100644 --- a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java +++ b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java @@ -18,6 +18,8 @@ */ package org.alfresco.solr.utils; +import java.io.Closeable; +import java.io.IOException; import java.util.Collection; import java.util.Collections; @@ -53,4 +55,16 @@ public abstract class Utils return null; } } + + public static void silentyClose(Closeable resource) + { + try + { + resource.close(); + } + catch(IOException ignore) + { + // Nothing to be done here + } + } } From 3de46bf59d86598197f71001003c60deb071f8dc Mon Sep 17 00:00:00 2001 From: agazzarini Date: Thu, 14 Nov 2019 10:29:41 +0100 Subject: [PATCH 2/3] [ SEARCH-875 ] Javadoc on silentlyClose utility method. --- .../java/org/alfresco/solr/utils/Utils.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java index b3e90e502..04e553119 100644 --- a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java +++ b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java @@ -56,11 +56,36 @@ public abstract class Utils } } + /** + * Silently closes the given {@link Closeable} resource without raising any exception. + * This utility method is specifically useful when we have to close a resource in a lamba statement: since the + * close() method could throw an {@link IOException} the compiler requires an enclosing try / catch block which + * makes the code less readable. + * + *

+ *

+ * + * try { if (resource != null) resource.close } catch (IOException exception) { ... } + * + *

+ *
+ * + * In these contexts a call to this method reduces the amount of code needed: + * + *

+ *

+ * + * silentlyClose(resource); + * + *

+ * + * @param resource the {@link Closeable} resource we want to silently close. + */ public static void silentyClose(Closeable resource) { try { - resource.close(); + if (resource != null) resource.close(); } catch(IOException ignore) { From 773bdf41b1e1569fe5b41aaf4d865b6a1b86c4d2 Mon Sep 17 00:00:00 2001 From: agazzarini Date: Thu, 14 Nov 2019 15:14:38 +0100 Subject: [PATCH 3/3] [ SEARCH-875 ] Review comments addressed --- .../src/main/java/org/alfresco/solr/utils/Utils.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java index 04e553119..3b586ca11 100644 --- a/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java +++ b/search-services/alfresco-search/src/main/java/org/alfresco/solr/utils/Utils.java @@ -18,6 +18,9 @@ */ package org.alfresco.solr.utils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.Closeable; import java.io.IOException; import java.util.Collection; @@ -25,6 +28,8 @@ import java.util.Collections; public abstract class Utils { + private static final Logger LOGGER = LoggerFactory.getLogger(Utils.class); + /** * Returns the same input collection if that is not null, otherwise a new empty collection. * Provides a safe way for iterating over a returned collection (which could be null). @@ -89,7 +94,7 @@ public abstract class Utils } catch(IOException ignore) { - // Nothing to be done here + LOGGER.warn("Unable to properly close the resource instance {}. See the stacktrace below for further details.", resource, ignore); } } }