From 18b6896b3b8e049c3ce5e09d9efccaee207aa2bb Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 11 Mar 2009 15:53:36 +0000
Subject: [PATCH] Fix for issue 3874 (MakeLDIF class should be consistent with other command-line classes)

---
 opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java |  158 ++++++++++++++++++++++++++++++++++------------------
 1 files changed, 104 insertions(+), 54 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java b/opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java
index f3f47c3..6603afa 100644
--- a/opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java
+++ b/opends/src/server/org/opends/server/tools/makeldif/MakeLDIF.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.tools.makeldif;
 import org.opends.messages.Message;
@@ -31,6 +31,8 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.util.LinkedList;
 import java.util.Random;
 
@@ -38,6 +40,7 @@
 import org.opends.server.types.Entry;
 import org.opends.server.types.ExistingFileBehavior;
 import org.opends.server.types.LDIFExportConfig;
+import org.opends.server.types.NullOutputStream;
 import org.opends.server.util.LDIFWriter;
 import org.opends.server.util.args.ArgumentException;
 import org.opends.server.util.args.ArgumentParser;
@@ -73,7 +76,8 @@
   // The total number of entries that have been written.
   private long entriesWritten;
 
-
+  private PrintStream out = System.out;
+  private PrintStream err = System.err;
 
   /**
    * Invokes the <CODE>makeLDIFMain</CODE> method with the provided set of
@@ -103,20 +107,45 @@
     entriesWritten = 0L;
   }
 
-
-
   /**
    * Processes the provided set of command-line arguments and begins generating
    * the LDIF content.
    *
    * @param  args  The command-line arguments provided for this program.
-   *
+   * @param  initializeServer  Indicates whether to initialize the server.
+   * @param  initializeSchema  Indicates whether to initialize the schema.
+   * @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  A result code of zero if all processing completed properly, or
    *          a nonzero result if a problem occurred.
+   *
    */
-  public int makeLDIFMain(String[] args)
+  public int makeLDIFMain(String[] args, boolean initializeServer,
+      boolean initializeSchema,
+      OutputStream outStream,
+      OutputStream errStream)
   {
-    // Create and initialize the argument parser for this program.
+    if (outStream == null)
+    {
+      out = NullOutputStream.printStream();
+    }
+    else
+    {
+      out = new PrintStream(outStream);
+    }
+
+    if (errStream == null)
+    {
+      err = NullOutputStream.printStream();
+    }
+    else
+    {
+      err = new PrintStream(errStream);
+    }
+
+//  Create and initialize the argument parser for this program.
     Message toolDescription = INFO_MAKELDIF_TOOL_DESCRIPTION.get();
     ArgumentParser  argParser = new ArgumentParser(CLASS_NAME, toolDescription,
                                                    false);
@@ -188,7 +217,7 @@
     catch (ArgumentException ae)
     {
       Message message = ERR_CANNOT_INITIALIZE_ARGS.get(ae.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -201,8 +230,8 @@
     catch (ArgumentException ae)
     {
       Message message = ERR_ERROR_PARSING_ARGS.get(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;
     }
 
@@ -215,46 +244,52 @@
     }
 
 
-    // Initialize the Directory Server configuration handler using the
-    // information that was provided.
-    DirectoryServer directoryServer = DirectoryServer.getInstance();
-    directoryServer.bootstrapClient();
+    if (initializeServer)
+    {
+      // Initialize the Directory Server configuration handler using the
+      // information that was provided.
+      DirectoryServer directoryServer = DirectoryServer.getInstance();
+      directoryServer.bootstrapClient();
 
-    try
-    {
-      directoryServer.initializeJMX();
-    }
-    catch (Exception e)
-    {
-      Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_JMX.get(
-              String.valueOf(configFile.getValue()), e.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
+      try
+      {
+        directoryServer.initializeJMX();
+      }
+      catch (Exception e)
+      {
+        Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_JMX.get(
+            String.valueOf(configFile.getValue()), e.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
+
+      try
+      {
+        directoryServer.initializeConfiguration(configClass.getValue(),
+            configFile.getValue());
+      }
+      catch (Exception e)
+      {
+        Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_CONFIG.get(
+            String.valueOf(configFile.getValue()), e.getMessage());
+        err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
     }
 
-    try
+    if (initializeSchema)
     {
-      directoryServer.initializeConfiguration(configClass.getValue(),
-                                              configFile.getValue());
-    }
-    catch (Exception e)
-    {
-      Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_CONFIG.get(
-              String.valueOf(configFile.getValue()), e.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
-    }
-
-    try
-    {
-      directoryServer.initializeSchema();
-    }
-    catch (Exception e)
-    {
-      Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_SCHEMA.get(
-              String.valueOf(configFile.getValue()), e.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
-      return 1;
+      try
+      {
+        DirectoryServer.getInstance().initializeSchema();
+      }
+      catch (Exception e)
+      {
+        Message message = ERR_MAKELDIF_CANNOT_INITIALIZE_SCHEMA.get(
+            String.valueOf(configFile.getValue()), e.getMessage());
+        System.err.println(wrapText(message, MAX_LINE_WIDTH));
+        return 1;
+      }
     }
 
 
@@ -284,7 +319,7 @@
     {
       Message message = ERR_MAKELDIF_NO_SUCH_RESOURCE_DIRECTORY.get(
               resourcePath.getValue());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -301,14 +336,14 @@
     {
       Message message = ERR_MAKELDIF_IOEXCEPTION_DURING_PARSE.get(
               ioe.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
     catch (Exception e)
     {
       Message message = ERR_MAKELDIF_EXCEPTION_DURING_PARSE.get(
               e.getMessage());
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -318,7 +353,7 @@
     {
       for (Message s : warnings)
       {
-        System.err.println(wrapText(s, MAX_LINE_WIDTH));
+        err.println(wrapText(s, MAX_LINE_WIDTH));
       }
     }
 
@@ -335,7 +370,7 @@
     {
       Message message = ERR_MAKELDIF_UNABLE_TO_CREATE_LDIF.get(
               ldifFile.getValue(), String.valueOf(ioe));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
 
@@ -349,7 +384,7 @@
     {
       Message message = ERR_MAKELDIF_ERROR_WRITING_LDIF.get(
               ldifFile.getValue(), stackTraceToSingleLineString(e));
-      System.err.println(wrapText(message, MAX_LINE_WIDTH));
+      err.println(wrapText(message, MAX_LINE_WIDTH));
       return 1;
     }
     finally
@@ -366,6 +401,21 @@
   }
 
 
+  /**
+   * Processes the provided set of command-line arguments and begins generating
+   * the LDIF content.
+   *
+   * @param  args  The command-line arguments provided for this program.
+   *
+   * @return  A result code of zero if all processing completed properly, or
+   *          a nonzero result if a problem occurred.
+   */
+  public int makeLDIFMain(String[] args)
+  {
+     return makeLDIFMain(args, true, true, System.out, System.err);
+  }
+
+
 
   /**
    * Writes the provided entry to the appropriate target.
@@ -390,7 +440,7 @@
       if ((++entriesWritten % 1000) == 0)
       {
         Message message = INFO_MAKELDIF_PROCESSED_N_ENTRIES.get(entriesWritten);
-        System.out.println(wrapText(message, MAX_LINE_WIDTH));
+        out.println(wrapText(message, MAX_LINE_WIDTH));
       }
 
       return true;
@@ -416,7 +466,7 @@
   public void closeEntryWriter()
   {
     Message message = INFO_MAKELDIF_PROCESSING_COMPLETE.get(entriesWritten);
-    System.out.println(wrapText(message, MAX_LINE_WIDTH));
+    out.println(wrapText(message, MAX_LINE_WIDTH));
   }
 }
 

--
Gitblit v1.10.0