From 7b09f76c7501f692657385025ccb10be5d974c7a 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:

---
 opends/src/server/org/opends/server/tools/BackUpDB.java |   80 +++++++++++++++++++++++++++-------------
 1 files changed, 54 insertions(+), 26 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/BackUpDB.java b/opends/src/server/org/opends/server/tools/BackUpDB.java
index 0a409f4..c3e2a9d 100644
--- a/opends/src/server/org/opends/server/tools/BackUpDB.java
+++ b/opends/src/server/org/opends/server/tools/BackUpDB.java
@@ -29,6 +29,8 @@
 
 
 import java.io.File;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Date;
@@ -54,6 +56,7 @@
 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;
@@ -86,7 +89,7 @@
    */
   public static void main(String[] args)
   {
-    int retCode = mainBackUpDB(args);
+    int retCode = mainBackUpDB(args, true, System.out, System.err);
 
     if(errorLogPublisher != null)
     {
@@ -108,7 +111,7 @@
    */
   public static int mainBackUpDB(String[] args)
   {
-    return mainBackUpDB(args, true);
+    return mainBackUpDB(args, true, System.out, System.err);
   }
 
   /**
@@ -117,11 +120,36 @@
    * @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 mainBackUpDB(String[] args, boolean initializeServer)
+  public static int mainBackUpDB(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.
     BooleanArgument backUpAll         = null;
     BooleanArgument compress          = null;
@@ -239,7 +267,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;
     }
 
@@ -254,8 +282,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;
     }
 
@@ -277,7 +305,7 @@
         int    msgID   = MSGID_BACKUPDB_CANNOT_MIX_BACKUP_ALL_AND_BACKEND_ID;
         String message = getMessage(msgID, backUpAll.getLongIdentifier(),
                                     backendID.getLongIdentifier());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
     }
@@ -286,7 +314,7 @@
       int    msgID   = MSGID_BACKUPDB_NEED_BACKUP_ALL_OR_BACKEND_ID;
       String message = getMessage(msgID, backUpAll.getLongIdentifier(),
                                   backendID.getLongIdentifier());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -316,7 +344,7 @@
         String message = getMessage(msgID,
                                     incrementalBaseID.getLongIdentifier(),
                                     incremental.getLongIdentifier());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -335,7 +363,7 @@
       int    msgID   = MSGID_BACKUPDB_SIGN_REQUIRES_HASH;
       String message = getMessage(msgID, signHash.getLongIdentifier(),
                                   hash.getLongIdentifier());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -353,7 +381,7 @@
         int    msgID   = MSGID_BACKUPDB_CANNOT_CREATE_BACKUP_DIR;
         String message = getMessage(msgID, backupDirectory.getValue(),
                                     getExceptionMessage(e));
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
     }
@@ -373,7 +401,7 @@
       {
         int    msgID   = MSGID_SERVER_BOOTSTRAP_ERROR;
         String message = getMessage(msgID, getExceptionMessage(e));
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -386,14 +414,14 @@
       {
         int    msgID   = MSGID_CANNOT_LOAD_CONFIG;
         String message = getMessage(msgID, ie.getMessage());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        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));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -408,21 +436,21 @@
       {
         int    msgID   = MSGID_CANNOT_LOAD_SCHEMA;
         String message = getMessage(msgID, ce.getMessage());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        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));
+        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));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -437,21 +465,21 @@
       {
         int    msgID   = MSGID_CANNOT_INITIALIZE_CORE_CONFIG;
         String message = getMessage(msgID, ce.getMessage());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        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));
+        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));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -465,21 +493,21 @@
       {
         int    msgID   = MSGID_CANNOT_INITIALIZE_CRYPTO_MANAGER;
         String message = getMessage(msgID, ce.getMessage());
-        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        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));
+        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));
+        err.println(wrapText(message, MAX_LINE_WIDTH));
         return 1;
       }
 
@@ -490,14 +518,14 @@
       {
         errorLogPublisher =
             new ThreadFilterTextErrorLogPublisher(Thread.currentThread(),
-                                                  new TextWriter.STDOUT());
+                                                  new TextWriter.STREAM(out));
         ErrorLogger.addErrorLogPublisher(errorLogPublisher);
 
       }
       catch(Exception e)
       {
-        System.err.println("Error installing the custom error logger: " +
-                           stackTraceToSingleLineString(e));
+        err.println("Error installing the custom error logger: " +
+                    stackTraceToSingleLineString(e));
       }
     }
 

--
Gitblit v1.10.0