From f94af448f0113d0b2701dd647379961234e91e16 Mon Sep 17 00:00:00 2001
From: Yannick Lecaillez <yannick.lecaillez@forgerock.com>
Date: Mon, 05 Sep 2016 09:30:52 +0000
Subject: [PATCH] Refactoring: importer/rebuild-index exception handling

---
 opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java |   49 +++++++++++++++----------------------------------
 1 files changed, 15 insertions(+), 34 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
index 517e3b0..8c0f919 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/backends/pluggable/BackendImpl.java
@@ -28,8 +28,10 @@
 import java.util.List;
 import java.util.Set;
 import java.util.SortedSet;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicInteger;
 
+import org.forgerock.i18n.LocalizableException;
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.slf4j.LocalizedLogger;
 import org.forgerock.opendj.config.server.ConfigChangeResult;
@@ -68,15 +70,12 @@
 import org.opends.server.types.LDIFExportConfig;
 import org.opends.server.types.LDIFImportConfig;
 import org.opends.server.types.LDIFImportResult;
-import org.opends.server.types.OpenDsException;
 import org.opends.server.types.Operation;
 import org.opends.server.types.RestoreConfig;
 import org.opends.server.util.CollectionUtils;
 import org.opends.server.util.LDIFException;
 import org.opends.server.util.RuntimeInformation;
 
-import com.forgerock.opendj.util.StaticUtils;
-
 /**
  * This is an implementation of a Directory Server Backend which stores entries locally
  * in a pluggable storage.
@@ -656,22 +655,9 @@
       rootContainer.getStorage().close();
       return getImportStrategy(rootContainer).importLDIF(importConfig);
     }
-    catch (StorageRuntimeException e)
-    {
-      throw createDirectoryException(e);
-    }
-    catch (DirectoryException e)
-    {
-      throw e;
-    }
-    catch (OpenDsException | ConfigException e)
-    {
-      throw new DirectoryException(getServerErrorResultCode(), e.getMessageObject(), e);
-    }
     catch (Exception e)
     {
-      throw new DirectoryException(getServerErrorResultCode(),
-                                   LocalizableMessage.raw(StaticUtils.stackTraceToSingleLineString(e, true)), e);
+      throw createDirectoryException(e);
     }
     finally
     {
@@ -771,22 +757,13 @@
       }
       getImportStrategy(rootContainer).rebuildIndex(rebuildConfig);
     }
-    catch (ConfigException ce)
-    {
-      throw new DirectoryException(getServerErrorResultCode(), ce.getMessageObject(), ce);
-    }
-    catch (StorageRuntimeException e)
-    {
-      throw createDirectoryException(e);
-    }
-    catch (InitializationException e)
+    catch (InitializationException | ConfigException e)
     {
       throw e;
     }
     catch (Exception e)
     {
-      throw new DirectoryException(getServerErrorResultCode(),
-                                   LocalizableMessage.raw(StaticUtils.stackTraceToSingleLineString(e, true)), e);
+      throw createDirectoryException(e);
     }
     finally
     {
@@ -955,17 +932,21 @@
    *          The StorageRuntimeException to be converted.
    * @return DirectoryException created from exception.
    */
-  private DirectoryException createDirectoryException(StorageRuntimeException e)
+  private DirectoryException createDirectoryException(Throwable e)
   {
-    Throwable cause = e.getCause();
-    if (cause instanceof OpenDsException)
+    if (e instanceof DirectoryException)
     {
-      return new DirectoryException(getServerErrorResultCode(), (OpenDsException) cause);
+      return (DirectoryException) e;
     }
-    else
+    if (e instanceof ExecutionException)
     {
-      return new DirectoryException(getServerErrorResultCode(), LocalizableMessage.raw(e.getMessage()), e);
+      return createDirectoryException(e.getCause());
     }
+    if (e instanceof LocalizableException)
+    {
+      return new DirectoryException(getServerErrorResultCode(), ((LocalizableException) e).getMessageObject());
+    }
+    return new DirectoryException(getServerErrorResultCode(), LocalizableMessage.raw(e.getMessage()), e);
   }
 
   private RootContainer newRootContainer(AccessMode accessMode)

--
Gitblit v1.10.0