From 6b7027bb28c10ddf23e00def117d352b736ffe4d Mon Sep 17 00:00:00 2001
From: neil_a_wilson <neil_a_wilson@localhost>
Date: Fri, 15 Sep 2006 18:40:10 +0000
Subject: [PATCH] Update many of the command-line tools to make them more suitable for using programatically.  This includes adding an option to bypass the Directory Server initialization (which is necessary if the server is already running in the same JVM as the tool), as well as making it possible to redefine standard output and standard error (e.g., so that the output can be parsed or discarded).

---
 opends/src/server/org/opends/server/tools/LDAPConnection.java |   40 +++++++++++++++++++++++++++++++---------
 1 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/opends/src/server/org/opends/server/tools/LDAPConnection.java b/opends/src/server/org/opends/server/tools/LDAPConnection.java
index c156934..5aef5c6 100644
--- a/opends/src/server/org/opends/server/tools/LDAPConnection.java
+++ b/opends/src/server/org/opends/server/tools/LDAPConnection.java
@@ -26,6 +26,7 @@
  */
 package org.opends.server.tools;
 
+import java.io.PrintStream;
 import java.net.ConnectException;
 import java.net.Socket;
 import java.net.UnknownHostException;
@@ -79,6 +80,9 @@
   private ASN1Reader asn1Reader;
   private int versionNumber = 3;
 
+  private PrintStream out;
+  private PrintStream err;
+
   /**
    * Constructor for the LDAPConnection object.
    *
@@ -89,10 +93,28 @@
    */
   public LDAPConnection(String host, int port, LDAPConnectionOptions options)
   {
+    this(host, port, options, System.out, System.err);
+  }
+
+  /**
+   * Constructor for the LDAPConnection object.
+   *
+   * @param   host    The hostname to send the request to.
+   * @param   port    The port number on which the directory server is accepting
+   *                  requests.
+   * @param  options  The set of options for this connection.
+   * @param  out      The print stream to use for standard output.
+   * @param  err      The print stream to use for standard error.
+   */
+  public LDAPConnection(String host, int port, LDAPConnectionOptions options,
+                        PrintStream out, PrintStream err)
+  {
     this.hostName = host;
     this.portNumber = port;
     this.connectionOptions = options;
     this.versionNumber = options.getVersionNumber();
+    this.out = out;
+    this.err = err;
   }
 
   /**
@@ -263,7 +285,7 @@
       }
       if(result != null)
       {
-        System.out.println(result);
+        out.println(result);
       }
 
       for (LDAPControl c : responseControls)
@@ -275,14 +297,14 @@
           {
             int    msgID   = MSGID_BIND_AUTHZID_RETURNED;
             String message = getMessage(msgID, controlValue.stringValue());
-            System.out.println(message);
+            out.println(message);
           }
         }
         else if (c.getOID().equals(OID_NS_PASSWORD_EXPIRED))
         {
           int    msgID   = MSGID_BIND_PASSWORD_EXPIRED;
           String message = getMessage(msgID);
-          System.out.println(message);
+          out.println(message);
         }
         else if (c.getOID().equals(OID_NS_PASSWORD_EXPIRING))
         {
@@ -295,7 +317,7 @@
 
           int    msgID   = MSGID_BIND_PASSWORD_EXPIRING;
           String message = getMessage(msgID, timeString);
-          System.out.println(message);
+          out.println(message);
         }
         else if (c.getOID().equals(OID_PASSWORD_POLICY_CONTROL))
         {
@@ -311,17 +333,17 @@
               case PASSWORD_EXPIRED:
                 int    msgID   = MSGID_BIND_PASSWORD_EXPIRED;
                 String message = getMessage(msgID);
-                System.out.println(message);
+                out.println(message);
                 break;
               case ACCOUNT_LOCKED:
                 msgID   = MSGID_BIND_ACCOUNT_LOCKED;
                 message = getMessage(msgID);
-                System.out.println(message);
+                out.println(message);
                 break;
               case CHANGE_AFTER_RESET:
                 msgID   = MSGID_BIND_MUST_CHANGE_PASSWORD;
                 message = getMessage(msgID);
-                System.out.println(message);
+                out.println(message);
                 break;
             }
           }
@@ -338,12 +360,12 @@
 
                 int    msgID   = MSGID_BIND_PASSWORD_EXPIRING;
                 String message = getMessage(msgID, timeString);
-                System.out.println(message);
+                out.println(message);
                 break;
               case GRACE_LOGINS_REMAINING:
                 msgID   = MSGID_BIND_GRACE_LOGINS_REMAINING;
                 message = getMessage(msgID, pwPolicyControl.getWarningValue());
-                System.out.println(message);
+                out.println(message);
                 break;
             }
           }

--
Gitblit v1.10.0