From 359c97e13e5eaf3ebb99f0b68f1a4083743255cc Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Tue, 10 Jul 2007 20:30:09 +0000
Subject: [PATCH] Update all of the tools provided with OpenDS to ensure that they are easier to invoke programmatically.  This includes:

---
 opendj-sdk/opends/src/server/org/opends/server/tools/VerifyIndex.java |  297 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 166 insertions(+), 131 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/tools/VerifyIndex.java b/opendj-sdk/opends/src/server/org/opends/server/tools/VerifyIndex.java
index 4c496c5..9de387f 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tools/VerifyIndex.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tools/VerifyIndex.java
@@ -45,11 +45,14 @@
 import org.opends.server.types.ErrorLogCategory;
 import org.opends.server.types.ErrorLogSeverity;
 import org.opends.server.types.InitializationException;
+import org.opends.server.types.NullOutputStream;
 import org.opends.server.util.args.ArgumentException;
 import org.opends.server.util.args.ArgumentParser;
 import org.opends.server.util.args.BooleanArgument;
 import org.opends.server.util.args.StringArgument;
 
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -78,7 +81,7 @@
    */
   public static void main(String[] args)
   {
-    int retCode = mainVerifyIndex(args);
+    int retCode = mainVerifyIndex(args, true, System.out, System.err);
 
     if(errorLogPublisher != null)
     {
@@ -94,11 +97,40 @@
   /**
    * Processes the command-line arguments and invokes the verify process.
    *
-   * @param  args  The command-line arguments provided to this program.
+   * @param  args              The command-line arguments provided to this
+   *                           program.
+   * @param  initializeServer  Indicates whether to initialize the server.
+   * @param  outStream         The output stream to use for standard output, or
+   *                           {@code null} if standard output is not needed.
+   * @param  errStream         The output stream to use for standard error, or
+   *                           {@code null} if standard error is not needed.
+   *
    * @return The error code.
    */
-  public static int mainVerifyIndex(String[] args)
+  public static int mainVerifyIndex(String[] args, boolean initializeServer,
+                                    OutputStream outStream,
+                                    OutputStream errStream)
   {
+    PrintStream out;
+    if (outStream == null)
+    {
+      out = NullOutputStream.printStream();
+    }
+    else
+    {
+      out = new PrintStream(outStream);
+    }
+
+    PrintStream err;
+    if (errStream == null)
+    {
+      err = NullOutputStream.printStream();
+    }
+    else
+    {
+      err = new PrintStream(errStream);
+    }
+
     // Define the command-line arguments that may be used with this program.
     StringArgument  configClass             = null;
     StringArgument  configFile              = null;
@@ -169,7 +201,7 @@
       int    msgID   = MSGID_CANNOT_INITIALIZE_ARGS;
       String message = getMessage(msgID, ae.getMessage());
 
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -184,8 +216,8 @@
       int    msgID   = MSGID_ERROR_PARSING_ARGS;
       String message = getMessage(msgID, ae.getMessage());
 
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      System.err.println(argParser.getUsage());
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(argParser.getUsage());
       return 1;
     }
 
@@ -204,7 +236,7 @@
     int numArgs = args.length;
     if (numArgs == 0)
     {
-      System.out.println(argParser.getUsage());
+      out.println(argParser.getUsage());
       return 1;
     }
 
@@ -214,8 +246,8 @@
       int    msgID   = MSGID_VERIFYINDEX_VERIFY_CLEAN_REQUIRES_SINGLE_INDEX;
       String message = getMessage(msgID);
 
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      System.out.println(argParser.getUsage());
+      err.println(wrapText(message, MAX_LINE_WIDTH));
+      out.println(argParser.getUsage());
       return 1;
     }
 
@@ -223,140 +255,143 @@
     // configuration.
     DirectoryServer directoryServer = DirectoryServer.getInstance();
 
-    try
+    if (initializeServer)
     {
-      DirectoryServer.bootstrapClient();
-      DirectoryServer.initializeJMX();
-    }
-    catch (Exception e)
-    {
-      int    msgID   = MSGID_SERVER_BOOTSTRAP_ERROR;
-      String message = getMessage(msgID, getExceptionMessage(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
+      try
+      {
+        DirectoryServer.bootstrapClient();
+        DirectoryServer.initializeJMX();
+      }
+      catch (Exception e)
+      {
+        int    msgID   = MSGID_SERVER_BOOTSTRAP_ERROR;
+        String message = getMessage(msgID, getExceptionMessage(e));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
 
-    try
-    {
-      directoryServer.initializeConfiguration(configClass.getValue(),
-                                              configFile.getValue());
-    }
-    catch (InitializationException ie)
-    {
-      int    msgID   = MSGID_CANNOT_LOAD_CONFIG;
-      String message = getMessage(msgID, ie.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (Exception e)
-    {
-      int    msgID   = MSGID_CANNOT_LOAD_CONFIG;
-      String message = getMessage(msgID, getExceptionMessage(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
+      try
+      {
+        directoryServer.initializeConfiguration(configClass.getValue(),
+                                                configFile.getValue());
+      }
+      catch (InitializationException ie)
+      {
+        int    msgID   = MSGID_CANNOT_LOAD_CONFIG;
+        String message = getMessage(msgID, ie.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (Exception e)
+      {
+        int    msgID   = MSGID_CANNOT_LOAD_CONFIG;
+        String message = getMessage(msgID, getExceptionMessage(e));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
 
 
 
-    // Initialize the Directory Server schema elements.
-    try
-    {
-      directoryServer.initializeSchema();
-    }
-    catch (ConfigException ce)
-    {
-      int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
-      String message = getMessage(msgID, ce.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (InitializationException ie)
-    {
-      int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
-      String message = getMessage(msgID, ie.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (Exception e)
-    {
-      int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
-      String message = getMessage(msgID, getExceptionMessage(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
+      // Initialize the Directory Server schema elements.
+      try
+      {
+        directoryServer.initializeSchema();
+      }
+      catch (ConfigException ce)
+      {
+        int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
+        String message = getMessage(msgID, ce.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (InitializationException ie)
+      {
+        int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
+        String message = getMessage(msgID, ie.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (Exception e)
+      {
+        int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
+        String message = getMessage(msgID, getExceptionMessage(e));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
 
 
-    // Initialize the Directory Server core configuration.
-    try
-    {
-      CoreConfigManager coreConfigManager = new CoreConfigManager();
-      coreConfigManager.initializeCoreConfig();
-    }
-    catch (ConfigException ce)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
-      String message = getMessage(msgID, ce.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (InitializationException ie)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
-      String message = getMessage(msgID, ie.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (Exception e)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
-      String message = getMessage(msgID, getExceptionMessage(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
+      // Initialize the Directory Server core configuration.
+      try
+      {
+        CoreConfigManager coreConfigManager = new CoreConfigManager();
+        coreConfigManager.initializeCoreConfig();
+      }
+      catch (ConfigException ce)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
+        String message = getMessage(msgID, ce.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (InitializationException ie)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
+        String message = getMessage(msgID, ie.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (Exception e)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
+        String message = getMessage(msgID, getExceptionMessage(e));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
 
 
-    // Initialize the Directory Server crypto manager.
-    try
-    {
-      directoryServer.initializeCryptoManager();
-    }
-    catch (ConfigException ce)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
-      String message = getMessage(msgID, ce.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (InitializationException ie)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
-      String message = getMessage(msgID, ie.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-    catch (Exception e)
-    {
-      int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
-      String message = getMessage(msgID, getExceptionMessage(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
+      // Initialize the Directory Server crypto manager.
+      try
+      {
+        directoryServer.initializeCryptoManager();
+      }
+      catch (ConfigException ce)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
+        String message = getMessage(msgID, ce.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (InitializationException ie)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
+        String message = getMessage(msgID, ie.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+      catch (Exception e)
+      {
+        int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
+        String message = getMessage(msgID, getExceptionMessage(e));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
 
 
-    // FIXME -- Install a custom logger to capture information about the state
-    // of the verify process.
-    try
-    {
-      errorLogPublisher =
-          new ThreadFilterTextErrorLogPublisher(Thread.currentThread(),
-                                                new TextWriter.STDOUT());
-      ErrorLogger.addErrorLogPublisher(errorLogPublisher);
+      // FIXME -- Install a custom logger to capture information about the state
+      // of the verify process.
+      try
+      {
+        errorLogPublisher =
+            new ThreadFilterTextErrorLogPublisher(Thread.currentThread(),
+                                                  new TextWriter.STREAM(out));
+        ErrorLogger.addErrorLogPublisher(errorLogPublisher);
 
-    }
-    catch(Exception e)
-    {
-      System.err.println("Error installing the custom error logger: " +
-                         stackTraceToSingleLineString(e));
+      }
+      catch(Exception e)
+      {
+        err.println("Error installing the custom error logger: " +
+                    stackTraceToSingleLineString(e));
+      }
     }
 
 

--
Gitblit v1.10.0