From 2ca2955041a76c5d3f3bf5bd74c3bbcc4010fda1 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Mon, 31 Oct 2016 14:00:38 +0000
Subject: [PATCH] OPENDJ-2772 Code cleanup

---
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFSearch.java             |   38 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFModify.java             |   37 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Base64.java                 |   38 +---
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPCompare.java            |   37 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java             |   47 +++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFDiff.java               |   37 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java     |   37 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ToolConsoleApplication.java |   43 +++++
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java             |   37 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MakeLDIF.java               |   36 ++--
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Utils.java                  |   16 ++
 opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPDelete.java             |   37 ++--
 12 files changed, 219 insertions(+), 221 deletions(-)

diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Base64.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Base64.java
index edb80fa..2dd6434 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Base64.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Base64.java
@@ -20,7 +20,6 @@
 import static com.forgerock.opendj.cli.CliMessages.ERR_FILEARG_NO_SUCH_FILE;
 import static com.forgerock.opendj.cli.CommonArguments.showUsageArgument;
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict;
 import static com.forgerock.opendj.ldap.CoreMessages.ERR_BASE64_DECODE_INVALID_LENGTH;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException;
@@ -28,6 +27,7 @@
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
 import static com.forgerock.opendj.ldap.tools.Utils.parseArguments;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static com.forgerock.opendj.util.StaticUtils.getExceptionMessage;
 import static org.forgerock.util.Utils.closeSilently;
 
@@ -49,11 +49,11 @@
 
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
 import com.forgerock.opendj.cli.SubCommand;
 import com.forgerock.opendj.cli.SubCommandArgumentParser;
 import org.forgerock.opendj.ldap.ResultCode;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * Tool that can be used for performing base64 encoding and decoding.
@@ -62,7 +62,7 @@
  * sets of three bytes with eight significant bits each to sets of four bytes
  * with six significant bits each.
  */
-public final class Base64 extends ConsoleApplication {
+public final class Base64 extends ToolConsoleApplication {
 
     /**
      * The main method for base64 tool.
@@ -71,34 +71,11 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new Base64(System.out, System.err), args);
     }
 
-    /**
-     * Run {@link Base64} tool with the provided arguments.
-     * Output and errors will be write on the provided streams.
-     * This method can be use to run the tool programmatically.
-     *
-     * @param out
-     *      {@link PrintStream} which will be use by the tool to write results and information messages.
-     * @param err
-     *      {@link PrintStream} which will be use by the tool to write errors.
-     * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
-     */
-    public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final Base64 base64 = new Base64(out, err);
-        try {
-            return base64.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(base64);
-            return e.getResultCode();
-        }
-    }
-
-    private Base64(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    Base64(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -107,7 +84,8 @@
         return false;
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         final SubCommandArgumentParser argParser =
                 new SubCommandArgumentParser(Base64.class.getName(), INFO_BASE64_TOOL_DESCRIPTION.get(), false);
         argParser.setShortToolDescription(REF_SHORT_DESC_BASE64.get());
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPCompare.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPCompare.java
index 9193ca8..f0e0b94 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPCompare.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPCompare.java
@@ -20,7 +20,6 @@
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.Utils.readBytesFromFile;
 import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest;
 import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage;
@@ -29,6 +28,8 @@
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
 import static com.forgerock.opendj.ldap.tools.Utils.readControls;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 
 import java.io.PrintStream;
 import java.util.List;
@@ -46,11 +47,11 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /** A tool that can be used to issue Compare requests to the Directory Server. */
-public final class LDAPCompare extends ConsoleApplication {
+public final class LDAPCompare extends ToolConsoleApplication {
 
     /**
      * The main method for ldapcompare tool.
@@ -59,36 +60,29 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDAPCompare(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDAPCompare} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this ldap tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDAPCompare ldapCompare = new LDAPCompare(out, err);
-        try {
-            return ldapCompare.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldapCompare);
-            return e.getResultCode();
-        }
+        return runTool(new LDAPCompare(out, err), args);
     }
 
     private BooleanArgument verbose;
 
-    private LDAPCompare(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDAPCompare(final PrintStream out, final PrintStream err) {
       super(out, err);
     }
 
@@ -102,7 +96,8 @@
         return verbose.isPresent();
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final LocalizableMessage toolDescription = INFO_LDAPCOMPARE_TOOL_DESCRIPTION.get();
         final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPCompare.class.getName())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPDelete.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPDelete.java
index 34ae3e2..005cb2f 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPDelete.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPDelete.java
@@ -28,18 +28,18 @@
 import static com.forgerock.opendj.cli.CommonArguments.showUsageArgument;
 import static com.forgerock.opendj.cli.CommonArguments.verboseArgument;
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest;
 import static com.forgerock.opendj.ldap.tools.Utils.getConnection;
 import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage;
 import static com.forgerock.opendj.ldap.tools.Utils.printSuccessMessage;
 import static com.forgerock.opendj.ldap.tools.Utils.readControls;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.i18n.LocalizableMessage.raw;
 
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
 import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.opendj.ldap.Connection;
@@ -50,6 +50,7 @@
 import org.forgerock.opendj.ldap.requests.DeleteRequest;
 import org.forgerock.opendj.ldap.requests.Requests;
 import org.forgerock.opendj.ldap.responses.Result;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 import java.io.BufferedReader;
 import java.io.IOException;
@@ -60,7 +61,7 @@
 import java.util.List;
 
 /** A tool that can be used to issue delete requests to the Directory Server. */
-public final class LDAPDelete extends ConsoleApplication {
+public final class LDAPDelete extends ToolConsoleApplication {
 
     /**
      * The main method for ldapdelete tool.
@@ -69,34 +70,27 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDAPDelete(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDAPDelete} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this ldap tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDAPDelete ldapDelete = new LDAPDelete(out, err);
-        try {
-            return ldapDelete.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldapDelete);
-            return e.getResultCode();
-        }
+        return runTool(new LDAPDelete(out, err), args);
     }
 
-    private LDAPDelete(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDAPDelete(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -112,7 +106,8 @@
         return verbose.isPresent();
     }
 
-    private int run(String[] args) throws LDAPToolException {
+    @Override
+    int run(String... args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final LocalizableMessage toolDescription = INFO_LDAPDELETE_TOOL_DESCRIPTION.get();
         final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPDelete.class.getName())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
index cf9bfce..bd08ee8 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPModify.java
@@ -21,7 +21,6 @@
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.ldap.tools.Utils.getConnection;
 import static com.forgerock.opendj.ldap.tools.Utils.printSuccessMessage;
 import static com.forgerock.opendj.ldap.tools.Utils.readAssertionControl;
@@ -29,6 +28,8 @@
 import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage;
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.util.Utils.closeSilently;
 
 import java.io.IOException;
@@ -65,14 +66,14 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * A tool that can be used to issue update (Add/Delete/Modify/ModifyDN) requests
  * to the Directory Server.
  */
-public final class LDAPModify extends ConsoleApplication {
+public final class LDAPModify extends ToolConsoleApplication {
 
     /**
      * The main method for ldapmodify tool.
@@ -81,31 +82,23 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDAPModify(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDAPModify} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this ldap tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDAPModify ldapModify = new LDAPModify(out, err);
-        try {
-            return ldapModify.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldapModify);
-            return e.getResultCode();
-        }
+        return runTool(new LDAPModify(out, err), args);
     }
 
     private final class VisitorImpl implements ChangeRecordVisitor<Integer, Void> {
@@ -240,7 +233,8 @@
     private Collection<Control> controls;
     private BooleanArgument verbose;
 
-    private LDAPModify(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDAPModify(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -254,7 +248,8 @@
         return verbose.isPresent();
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPModify.class.getName())
                 .toolDescription(INFO_LDAPMODIFY_TOOL_DESCRIPTION.get())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
index 9b0549c..3abb282 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPPasswordModify.java
@@ -21,11 +21,12 @@
 import static com.forgerock.opendj.cli.Utils.throwIfArgumentsConflict;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.CommonArguments.*;
 import static com.forgerock.opendj.ldap.tools.Utils.addControlsToRequest;
 import static com.forgerock.opendj.ldap.tools.Utils.printErrorMessage;
 import static com.forgerock.opendj.ldap.tools.Utils.readControls;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 
 import java.io.PrintStream;
 
@@ -41,9 +42,9 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.FileBasedArgument;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * A tool that can be used to issue LDAP password modify extended requests to
@@ -57,7 +58,7 @@
  * All of these are optional components that may be included or omitted from the
  * request.
  */
-public final class LDAPPasswordModify extends ConsoleApplication {
+public final class LDAPPasswordModify extends ToolConsoleApplication {
 
     /**
      * The main method for ldappasswordmodify tool.
@@ -66,36 +67,29 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDAPPasswordModify(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDAPPasswordModify} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this ldap tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDAPPasswordModify ldapPasswordModify = new LDAPPasswordModify(out, err);
-        try {
-            return ldapPasswordModify.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldapPasswordModify);
-            return e.getResultCode();
-        }
+        return runTool(new LDAPPasswordModify(out, err), args);
     }
 
     private BooleanArgument verbose;
 
-    private LDAPPasswordModify(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDAPPasswordModify(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -109,7 +103,8 @@
         return verbose.isPresent();
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final LocalizableMessage toolDescription = INFO_LDAPPWMOD_TOOL_DESCRIPTION.get();
         final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPPasswordModify.class.getName())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
index 114e1d6..f08fe28 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDAPSearch.java
@@ -63,12 +63,12 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.BooleanArgument;
 import com.forgerock.opendj.cli.ConnectionFactoryProvider;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.IntegerArgument;
 import com.forgerock.opendj.cli.MultiChoiceArgument;
 import com.forgerock.opendj.cli.StringArgument;
 import com.forgerock.opendj.ldap.controls.AccountUsabilityResponseControl;
 import com.forgerock.opendj.util.StaticUtils;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 import static com.forgerock.opendj.cli.CliMessages.INFO_NUM_ENTRIES_PLACEHOLDER;
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
@@ -81,6 +81,8 @@
 import static com.forgerock.opendj.ldap.tools.Utils.readAssertionControl;
 import static com.forgerock.opendj.ldap.tools.Utils.readControls;
 import static com.forgerock.opendj.ldap.tools.Utils.readFilterFromString;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.util.Utils.*;
 
 import static com.forgerock.opendj.cli.ArgumentConstants.*;
@@ -89,7 +91,7 @@
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
 /** A tool that can be used to issue Search requests to the Directory Server. */
-public final class LDAPSearch extends ConsoleApplication {
+public final class LDAPSearch extends ToolConsoleApplication {
 
     /**
      * The main method for ldapsearch tool.
@@ -98,34 +100,23 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDAPSearch(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDAPSearch} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this ldap tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDAPSearch ldapSearch = new LDAPSearch(out, err);
-        try {
-            return ldapSearch.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldapSearch);
-            return e.getResultCode();
-        } catch (final ArgumentException e) {
-            ldapSearch.errPrintln(ERR_ERROR_PARSING_ARGS.get(e.getMessageObject()));
-            return ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue();
-        }
+        return runTool(new LDAPSearch(out, err), args);
     }
 
     private class LDAPSearchResultHandler implements SearchResultHandler {
@@ -209,7 +200,8 @@
 
     private EntryWriter ldifWriter;
 
-    private LDAPSearch(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDAPSearch(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -223,7 +215,16 @@
         return verbose.isPresent();
     }
 
-    private int run(final String[] args) throws LDAPToolException, ArgumentException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
+        try {
+            return runLdapSearch(args);
+        } catch (final ArgumentException e) {
+            throw newToolParamException(e, ERR_ERROR_PARSING_ARGS.get(e.getMessageObject()));
+        }
+    }
+
+    int runLdapSearch(final String[] args) throws LDAPToolException, ArgumentException {
         // Create the command-line argument parser for use with this program.
         final LocalizableMessage toolDescription = INFO_LDAPSEARCH_TOOL_DESCRIPTION.get();
         final LDAPToolArgumentParser argParser = LDAPToolArgumentParser.builder(LDAPSearch.class.getName())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFDiff.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFDiff.java
index 1677c8b..89478d4 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFDiff.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFDiff.java
@@ -20,13 +20,14 @@
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
 import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn;
 import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolInputStream;
 import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream;
 import static com.forgerock.opendj.ldap.tools.Utils.parseArguments;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.util.Utils.closeSilently;
 
 import java.io.IOException;
@@ -46,14 +47,14 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.ArgumentParser;
 import com.forgerock.opendj.cli.BooleanArgument;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * This utility can be used to compare two LDIF files and report the differences
  * in LDIF format.
  */
-public final class LDIFDiff extends ConsoleApplication {
+public final class LDIFDiff extends ToolConsoleApplication {
 
     static final int NO_DIFFERENCES_FOUND = 0;
     static final int DIFFERENCES_FOUND = 1;
@@ -65,34 +66,27 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDIFDiff(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDIFDiff} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDIFDiff ldifDiff = new LDIFDiff(out, err);
-        try {
-            return ldifDiff.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldifDiff);
-            return e.getResultCode();
-        }
+        return runTool(new LDIFDiff(out, err), args);
     }
 
-    private LDIFDiff(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDIFDiff(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -101,7 +95,8 @@
         return false;
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFDiff.class.getName())
                 .toolDescription(INFO_LDIFDIFF_TOOL_DESCRIPTION.get())
                 .trailingArguments(2, "source target")
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFModify.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFModify.java
index 44956865..40e273d 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFModify.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFModify.java
@@ -21,11 +21,12 @@
 import static com.forgerock.opendj.cli.ToolVersionHandler.newSdkVersionHandler;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn;
 import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolInputStream;
 import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream;
 import static com.forgerock.opendj.ldap.tools.Utils.parseArguments;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.util.Utils.closeSilently;
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
@@ -54,14 +55,14 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.ArgumentParser;
 import com.forgerock.opendj.cli.BooleanArgument;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * A tool that can be used to issue update (Add/Delete/Modify/ModifyDN) requests
  * to a set of entries contained in an LDIF file.
  */
-public final class LDIFModify extends ConsoleApplication {
+public final class LDIFModify extends ToolConsoleApplication {
 
     /**
      * The main method for ldifmodify tool.
@@ -70,34 +71,27 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDIFModify(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDIFModify} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDIFModify ldifModify = new LDIFModify(out, err);
-        try {
-            return ldifModify.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldifModify);
-            return e.getResultCode();
-        }
+        return runTool(new LDIFModify(out, err), args);
     }
 
-    private LDIFModify(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDIFModify(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -106,7 +100,8 @@
         return false;
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         // Create the command-line argument parser for use with this program.
         final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFModify.class.getName())
                 .toolDescription(INFO_LDIFMODIFY_TOOL_DESCRIPTION.get())
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFSearch.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFSearch.java
index a68f079..e8a28ed 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFSearch.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/LDIFSearch.java
@@ -20,7 +20,6 @@
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.CommonArguments.*;
 
 import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn;
@@ -28,6 +27,8 @@
 import static com.forgerock.opendj.ldap.tools.Utils.getLDIFToolOutputStream;
 import static com.forgerock.opendj.ldap.tools.Utils.parseArguments;
 import static com.forgerock.opendj.ldap.tools.Utils.readFilterFromString;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 
 import java.io.IOException;
 import java.io.PrintStream;
@@ -35,7 +36,6 @@
 import java.util.List;
 
 import org.forgerock.i18n.LocalizableException;
-import org.forgerock.i18n.LocalizableMessage;
 import org.forgerock.i18n.LocalizedIllegalArgumentException;
 import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.Filter;
@@ -51,13 +51,13 @@
 import com.forgerock.opendj.cli.ArgumentException;
 import com.forgerock.opendj.cli.ArgumentParser;
 import com.forgerock.opendj.cli.BooleanArgument;
-import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.cli.IntegerArgument;
 import com.forgerock.opendj.cli.MultiChoiceArgument;
 import com.forgerock.opendj.cli.StringArgument;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /** This utility can be used to perform search operations against data in an LDIF file. */
-public final class LDIFSearch extends ConsoleApplication {
+public final class LDIFSearch extends ToolConsoleApplication {
 
     /**
      * The main method for ldifsearch tool.
@@ -66,34 +66,27 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new LDIFSearch(System.out, System.err), args);
     }
 
     /**
-     * Run {@link LDIFSearch} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final LDIFSearch ldifSearch = new LDIFSearch(out, err);
-        try {
-            return ldifSearch.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(ldifSearch);
-            return e.getResultCode();
-        }
+        return runTool(new LDIFSearch(out, err), args);
     }
 
-    private LDIFSearch(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    LDIFSearch(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -102,7 +95,8 @@
         return false;
     }
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         final ArgumentParser argParser = LDAPToolArgumentParser.builder(LDIFSearch.class.getName())
                 .toolDescription(INFO_LDIFSEARCH_TOOL_DESCRIPTION.get())
                 .trailingArgumentsUnbounded(2, "source filter [attributes ...]")
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MakeLDIF.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MakeLDIF.java
index 789ad87..29e0b39 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MakeLDIF.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/MakeLDIF.java
@@ -24,9 +24,10 @@
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolExceptionAlreadyPrinted;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolParamException;
 import static com.forgerock.opendj.ldap.tools.ToolsMessages.*;
-import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.ldap.tools.Utils.computeWrapColumn;
 import static com.forgerock.opendj.ldap.tools.Utils.parseArguments;
+import static com.forgerock.opendj.ldap.tools.Utils.runTool;
+import static com.forgerock.opendj.ldap.tools.Utils.runToolAndExit;
 import static org.forgerock.util.Utils.closeSilently;
 
 import java.io.BufferedWriter;
@@ -50,9 +51,10 @@
 import org.forgerock.opendj.ldap.ResultCode;
 import org.forgerock.opendj.ldif.EntryGenerator;
 import org.forgerock.opendj.ldif.LDIFEntryWriter;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /** Program that generate LDIF content based on a template. */
-public final class MakeLDIF extends ConsoleApplication {
+public final class MakeLDIF extends ToolConsoleApplication {
     /** The value for the constant option in LDIF generator tools. */
     public static final String OPTION_LONG_CONSTANT = "constant";
 
@@ -66,34 +68,27 @@
      *            The command-line arguments provided to this program.
      */
     public static void main(final String[] args) {
-        System.exit(filterExitCode(run(System.out, System.err, args)));
+        runToolAndExit(new MakeLDIF(System.out, System.err), args);
     }
 
     /**
-     * Run {@link MakeLDIF} tool with the provided arguments.
-     * Output and errors will be written on the provided streams.
-     * This method can be used to run the tool programmatically.
+     * This method should be used to run this tool programmatically.
+     * Output and errors will be printed on provided {@link PrintStream}.
      *
      * @param out
-     *      {@link PrintStream} which will be used by the tool to write results and information messages.
+     *            The {@link PrintStream} to use to write tool output.
      * @param err
-     *      {@link PrintStream} which will be used by the tool to write errors.
+     *            The {@link PrintStream} to use to write tool errors.
      * @param args
-     *      Arguments set to pass to the tool.
-     * @return
-     *      An integer which represents the result code of the tool.
+     *            The arguments to use with this tool.
+     * @return The code returned by the tool
      */
     public static int run(final PrintStream out, final PrintStream err, final String... args) {
-        final MakeLDIF makeLDIF = new MakeLDIF(out, err);
-        try {
-            return makeLDIF.run(args);
-        } catch (final LDAPToolException e) {
-            e.printErrorMessage(makeLDIF);
-            return e.getResultCode();
-        }
+        return runTool(new MakeLDIF(out, err), args);
     }
 
-    private MakeLDIF(final PrintStream out, final PrintStream err) {
+    @VisibleForTesting
+    MakeLDIF(final PrintStream out, final PrintStream err) {
         super(out, err);
     }
 
@@ -105,7 +100,8 @@
     /** The total number of entries that have been written. */
     private long numberOfEntriesWritten;
 
-    private int run(final String[] args) throws LDAPToolException {
+    @Override
+    int run(final String... args) throws LDAPToolException {
         final LocalizableMessage toolDescription = INFO_MAKELDIF_TOOL_DESCRIPTION.get();
         final ArgumentParser argParser = LDAPToolArgumentParser.builder(MakeLDIF.class.getName())
                 .toolDescription(toolDescription)
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ToolConsoleApplication.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ToolConsoleApplication.java
new file mode 100644
index 0000000..22b7841
--- /dev/null
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/ToolConsoleApplication.java
@@ -0,0 +1,43 @@
+/*
+ * The contents of this file are subject to the terms of the Common Development and
+ * Distribution License (the License). You may not use this file except in compliance with the
+ * License.
+ *
+ * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
+ * specific language governing permission and limitations under the License.
+ *
+ * When distributing Covered Software, include this CDDL Header Notice in each file and include
+ * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
+ * Header, with the fields enclosed by brackets [] replaced by your own identifying
+ * information: "Portions Copyright [year] [name of copyright owner]".
+ *
+ * Copyright 2016 ForgeRock AS.
+ */
+package com.forgerock.opendj.ldap.tools;
+
+
+import java.io.PrintStream;
+
+import com.forgerock.opendj.cli.ConsoleApplication;
+
+/** Represents an OpenDJ client console-based. */
+abstract class ToolConsoleApplication extends ConsoleApplication {
+
+    ToolConsoleApplication(final PrintStream out, final PrintStream err) {
+        super(out, err);
+    }
+
+    /**
+     * Run this {@link ToolConsoleApplication} tool with the provided arguments.
+     * Output and errors will be written on the provided streams.
+     * This method can be used to run the tool programmatically.
+     *
+     * @param args
+     *      Arguments set to pass to the tool.
+     * @return
+     *      An integer which represents the result code of the tool.
+     * @throws LDAPToolException
+     *      If an error occurs with either client inputs or tool execution.
+     */
+    abstract int run(final String... args) throws LDAPToolException;
+}
diff --git a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Utils.java b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Utils.java
index 207a99e..aa3d126 100644
--- a/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Utils.java
+++ b/opendj-ldap-toolkit/src/main/java/com/forgerock/opendj/ldap/tools/Utils.java
@@ -18,6 +18,7 @@
 
 import static com.forgerock.opendj.cli.ArgumentConstants.USE_SYSTEM_STREAM_TOKEN;
 import static com.forgerock.opendj.cli.CliConstants.NO_WRAPPING_BY_DEFAULT;
+import static com.forgerock.opendj.cli.Utils.filterExitCode;
 import static com.forgerock.opendj.cli.Utils.readBytesFromFile;
 import static com.forgerock.opendj.cli.Utils.secondsToTimeString;
 import static com.forgerock.opendj.ldap.tools.LDAPToolException.newToolException;
@@ -75,6 +76,7 @@
 import com.forgerock.opendj.cli.ConsoleApplication;
 import com.forgerock.opendj.ldap.controls.AccountUsabilityRequestControl;
 import org.forgerock.opendj.ldap.responses.Result;
+import org.forgerock.util.annotations.VisibleForTesting;
 
 /**
  * This class provides utility functions for all the client side tools.
@@ -458,6 +460,20 @@
         return NO_WRAPPING_BY_DEFAULT;
     }
 
+    static void runToolAndExit(final ToolConsoleApplication tool, final String[] args) {
+        System.exit(filterExitCode(runTool(tool, args)));
+    }
+
+    @VisibleForTesting
+    static int runTool(final ToolConsoleApplication tool, final String... args) {
+        try {
+            return tool.run(args);
+        } catch (final LDAPToolException e) {
+            e.printErrorMessage(tool);
+            return e.getResultCode();
+        }
+    }
+
     /** Prevent instantiation. */
     private Utils() {
         // Do nothing.

--
Gitblit v1.10.0