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/RestoreDB.java |   89 +++++++++++++++++++++++++++++---------------
 1 files changed, 59 insertions(+), 30 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/RestoreDB.java b/opends/src/server/org/opends/server/tools/RestoreDB.java
index 539065f..50465ba 100644
--- a/opends/src/server/org/opends/server/tools/RestoreDB.java
+++ b/opends/src/server/org/opends/server/tools/RestoreDB.java
@@ -28,6 +28,8 @@
 
 
 
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -52,6 +54,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.types.RestoreConfig;
 import org.opends.server.util.args.ArgumentException;
 import org.opends.server.util.args.ArgumentParser;
@@ -84,7 +87,7 @@
 
   public static void main(String[] args)
   {
-    int retCode = mainRestoreDB(args);
+    int retCode = mainRestoreDB(args, true, System.out, System.err);
 
     if(errorLogPublisher != null)
     {
@@ -106,7 +109,7 @@
    */
   public static int mainRestoreDB(String[] args)
   {
-    return mainRestoreDB(args, true);
+    return mainRestoreDB(args, true, System.out, System.err);
   }
 
   /**
@@ -115,11 +118,37 @@
    * @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 mainRestoreDB(String[] args, boolean initializeServer)
+  public static int mainRestoreDB(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 displayUsage      = null;
     BooleanArgument listBackups          = null;
@@ -195,7 +224,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;
     }
 
@@ -210,8 +239,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;
     }
 
@@ -238,7 +267,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;
       }
 
@@ -251,14 +280,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;
       }
 
@@ -273,21 +302,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;
       }
 
@@ -302,21 +331,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;
       }
 
@@ -330,21 +359,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;
       }
 
@@ -355,14 +384,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));
       }
     }
 
@@ -393,34 +422,34 @@
       {
         int    msgID   = MSGID_RESTOREDB_LIST_BACKUP_ID;
         String message = getMessage(msgID, backupInfo.getBackupID());
-        System.out.println(message);
+        out.println(message);
 
         msgID   = MSGID_RESTOREDB_LIST_BACKUP_DATE;
         message = getMessage(msgID,
                              dateFormat.format(backupInfo.getBackupDate()));
-        System.out.println(message);
+        out.println(message);
 
         msgID   = MSGID_RESTOREDB_LIST_INCREMENTAL;
         message = getMessage(msgID, String.valueOf(backupInfo.isIncremental()));
-        System.out.println(message);
+        out.println(message);
 
         msgID   = MSGID_RESTOREDB_LIST_COMPRESSED;
         message = getMessage(msgID, String.valueOf(backupInfo.isCompressed()));
-        System.out.println(message);
+        out.println(message);
 
         msgID   = MSGID_RESTOREDB_LIST_ENCRYPTED;
         message = getMessage(msgID, String.valueOf(backupInfo.isEncrypted()));
-        System.out.println(message);
+        out.println(message);
 
         byte[] hash = backupInfo.getUnsignedHash();
         msgID   = MSGID_RESTOREDB_LIST_HASHED;
         message = getMessage(msgID, String.valueOf(hash != null));
-        System.out.println(message);
+        out.println(message);
 
         byte[] signature = backupInfo.getSignedHash();
         msgID   = MSGID_RESTOREDB_LIST_SIGNED;
         message = getMessage(msgID, String.valueOf(signature != null));
-        System.out.println(message);
+        out.println(message);
 
         StringBuilder dependencyList = new StringBuilder();
         HashSet<String> dependencyIDs = backupInfo.getDependencies();
@@ -442,9 +471,9 @@
 
         msgID   = MSGID_RESTOREDB_LIST_DEPENDENCIES;
         message = getMessage(msgID, dependencyList.toString());
-        System.out.println(message);
+        out.println(message);
 
-        System.out.println();
+        out.println();
       }
 
       return 1;

--
Gitblit v1.10.0