From 13a63611d24de8c5727e62a215b26354c1a22278 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 19 Mar 2008 18:09:56 +0000
Subject: [PATCH] Fix for issue 2197 (dsconfig: interactive mode should display the effective dsconfig command)

---
 opends/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java |  148 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 2 deletions(-)

diff --git a/opends/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java b/opends/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
index c99f564..6599232 100644
--- a/opends/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
+++ b/opends/src/server/org/opends/server/util/cli/LDAPConnectionConsoleInteraction.java
@@ -115,6 +115,12 @@
 
   private Message heading = INFO_LDAP_CONN_HEADING_CONNECTION_PARAMETERS.get();
 
+  // A copy of the secureArgList for convenience.
+  private SecureConnectionCliArgs copySecureArgsList = null;
+
+  // The command builder that we can return with the connection information.
+  private CommandBuilder commandBuilder;
+
   /**
    * Enumeration description protocols for interactive CLI choices.
    */
@@ -274,6 +280,18 @@
                                           SecureConnectionCliArgs secureArgs) {
     this.app = app;
     this.secureArgsList = secureArgs;
+    this.commandBuilder = new CommandBuilder(null);
+    copySecureArgsList = new SecureConnectionCliArgs();
+    try
+    {
+      copySecureArgsList.createGlobalArguments();
+    }
+    catch (Throwable t)
+    {
+      // This is  a bug: we should always be able to create the global arguments
+      // no need to localize this one.
+      throw new RuntimeException("Unexpected error: "+t, t);
+    }
   }
 
   /**
@@ -301,6 +319,9 @@
   public void run(boolean canUseSSL, boolean canUseStartTLS)
           throws ArgumentException
   {
+    // Reset everything
+    commandBuilder.clearArguments();
+    copySecureArgsList.createGlobalArguments();
     boolean secureConnection = (canUseSSL || canUseStartTLS) &&
       (
           secureArgsList.useSSLArg.isPresent()
@@ -372,6 +393,10 @@
       }
     }
 
+    copySecureArgsList.hostNameArg.clearValues();
+    copySecureArgsList.hostNameArg.addValue(hostName);
+    commandBuilder.addArgument(copySecureArgsList.hostNameArg);
+
     useSSL = secureArgsList.useSSL();
     useStartTLS = secureArgsList.useStartTLS();
     boolean connectionTypeIsSet =
@@ -455,6 +480,15 @@
       }
     }
 
+    if (useSSL)
+    {
+      commandBuilder.addArgument(copySecureArgsList.useSSLArg);
+    }
+    else if (useStartTLS)
+    {
+      commandBuilder.addArgument(copySecureArgsList.useStartTLSArg);
+    }
+
     if ((useSSL || useStartTLS) && (trustManager == null))
     {
       initializeTrustManager();
@@ -528,6 +562,10 @@
       }
     }
 
+    copySecureArgsList.portArg.clearValues();
+    copySecureArgsList.portArg.addValue(String.valueOf(portNumber));
+    commandBuilder.addArgument(copySecureArgsList.portArg);
+
     // Get the LDAP bind credentials.
     bindDN = secureArgsList.bindDnArg.getValue();
     adminUID = secureArgsList.adminUidArg.getValue();
@@ -629,6 +667,18 @@
               .unableToReadConnectionParameters(e);
         }
       }
+      if (useAdmin)
+      {
+        copySecureArgsList.adminUidArg.clearValues();
+        copySecureArgsList.adminUidArg.addValue(getAdministratorUID());
+        commandBuilder.addArgument(copySecureArgsList.adminUidArg);
+      }
+      else
+      {
+        copySecureArgsList.bindDnArg.clearValues();
+        copySecureArgsList.bindDnArg.addValue(getBindDN());
+        commandBuilder.addArgument(copySecureArgsList.bindDnArg);
+      }
     }
     else
     {
@@ -655,6 +705,10 @@
             throw ArgumentExceptionFactory.missingBindPassword(bindDN);
           }
         }
+        copySecureArgsList.bindPasswordFileArg.clearValues();
+        copySecureArgsList.bindPasswordFileArg.getNameToValueMap().putAll(
+            secureArgsList.bindPasswordFileArg.getNameToValueMap());
+        commandBuilder.addArgument(secureArgsList.bindPasswordFileArg);
       }
       else if (bindPassword == null || bindPassword.equals("-"))
       {
@@ -695,6 +749,10 @@
               .unableToReadConnectionParameters(e);
         }
       }
+      copySecureArgsList.bindPasswordArg.clearValues();
+      copySecureArgsList.bindPasswordArg.addValue(bindPassword);
+      commandBuilder.addObfuscatedArgument(
+          copySecureArgsList.bindPasswordArg);
     }
   }
 
@@ -707,10 +765,17 @@
   private ApplicationTrustManager getTrustManagerInternal()
   throws ArgumentException
   {
+    // Remove these arguments since this method might be called several times.
+    commandBuilder.removeArgument(copySecureArgsList.trustAllArg);
+    commandBuilder.removeArgument(copySecureArgsList.trustStorePathArg);
+    commandBuilder.removeArgument(copySecureArgsList.trustStorePasswordArg);
+    commandBuilder.removeArgument(copySecureArgsList.trustStorePasswordFileArg);
+
     // If we have the trustALL flag, don't do anything
     // just return null
     if (secureArgsList.trustAllArg.isPresent())
     {
+      commandBuilder.addArgument(copySecureArgsList.trustAllArg);
       return null;
     }
 
@@ -755,6 +820,7 @@
         {
           if (result.getValue().equals(TrustMethod.TRUSTALL.getChoice()))
           {
+            commandBuilder.addArgument(copySecureArgsList.trustAllArg);
             // If we have the trustALL flag, don't do anything
             // just return null
             return null;
@@ -771,6 +837,10 @@
             // The certificate will be displayed to the user
             askForTrustStore = false;
             trustStoreInMemory = true;
+
+            // There is no direct equivalent for this option, so propose the
+            // trust all option as command-line argument.
+            commandBuilder.addArgument(copySecureArgsList.trustAllArg);
           }
           else
           {
@@ -791,9 +861,10 @@
       }
     }
 
-    // If we not trust all server certificates, we have to get info
+    // If we do not trust all server certificates, we have to get info
     // about truststore. First get the truststore path.
     truststorePath = secureArgsList.trustStorePathArg.getValue();
+
     if (app.isInteractive() && !secureArgsList.trustStorePathArg.isPresent()
         && askForTrustStore)
     {
@@ -841,6 +912,13 @@
       }
     }
 
+    if (truststorePath != null)
+    {
+      copySecureArgsList.trustStorePathArg.clearValues();
+      copySecureArgsList.trustStorePathArg.addValue(truststorePath);
+      commandBuilder.addArgument(copySecureArgsList.trustStorePathArg);
+    }
+
     // Then the truststore password.
     //  As the most common case is to have no password for truststore,
     // we don't ask it in the interactive mode.
@@ -877,6 +955,7 @@
         }
       }
     }
+
     // We've got all the information to get the truststore manager
     try
     {
@@ -898,6 +977,23 @@
       {
         truststore.load(null, null);
       }
+
+      if (secureArgsList.trustStorePasswordFileArg.isPresent())
+      {
+        copySecureArgsList.trustStorePasswordFileArg.clearValues();
+        copySecureArgsList.trustStorePasswordFileArg.getNameToValueMap().putAll(
+            secureArgsList.trustStorePasswordFileArg.getNameToValueMap());
+        commandBuilder.addArgument(
+            copySecureArgsList.trustStorePasswordFileArg);
+      }
+      else
+      {
+        copySecureArgsList.trustStorePasswordArg.clearValues();
+        copySecureArgsList.trustStorePasswordArg.addValue(truststorePassword);
+        commandBuilder.addObfuscatedArgument(
+            copySecureArgsList.trustStorePasswordArg);
+      }
+
       return new ApplicationTrustManager(truststore);
     }
     catch (Exception e)
@@ -915,6 +1011,12 @@
   private KeyManager getKeyManagerInternal()
   throws ArgumentException
   {
+//  Remove these arguments since this method might be called several times.
+    commandBuilder.removeArgument(copySecureArgsList.certNicknameArg);
+    commandBuilder.removeArgument(copySecureArgsList.keyStorePathArg);
+    commandBuilder.removeArgument(copySecureArgsList.keyStorePasswordArg);
+    commandBuilder.removeArgument(copySecureArgsList.keyStorePasswordFileArg);
+
     // Do we need client side authentication ?
     // If one of the client side authentication args is set, we assume
     // that we
@@ -980,6 +1082,14 @@
       }
     }
 
+    if (keystorePath != null)
+    {
+      copySecureArgsList.keyStorePathArg.clearValues();
+      copySecureArgsList.keyStorePathArg.addValue(keystorePath);
+      commandBuilder.addArgument(copySecureArgsList.keyStorePathArg);
+    }
+
+
     // Then the keystore password.
     keystorePassword = secureArgsList.keyStorePasswordArg.getValue();
 
@@ -1094,6 +1204,29 @@
     ApplicationKeyManager akm = new ApplicationKeyManager(keystore,
         keystorePassword.toCharArray());
 
+
+    if (secureArgsList.keyStorePasswordFileArg.isPresent())
+    {
+      copySecureArgsList.keyStorePasswordFileArg.clearValues();
+      copySecureArgsList.keyStorePasswordFileArg.getNameToValueMap().putAll(
+          secureArgsList.keyStorePasswordFileArg.getNameToValueMap());
+      commandBuilder.addArgument(
+          copySecureArgsList.keyStorePasswordFileArg);
+    }
+    else
+    {
+      copySecureArgsList.keyStorePasswordArg.clearValues();
+      copySecureArgsList.keyStorePasswordArg.addValue(keystorePassword);
+      commandBuilder.addObfuscatedArgument(
+          copySecureArgsList.keyStorePasswordArg);
+    }
+
+    if (certifNickname != null)
+    {
+      copySecureArgsList.certNicknameArg.clearValues();
+      copySecureArgsList.certNicknameArg.addValue(certifNickname);
+    }
+
     if (certifNickname != null)
     {
       return new SelectableCertificateKeyManager(akm, certifNickname);
@@ -1499,7 +1632,7 @@
   }
 
  /**
-  * Populates an a set of LDAP options with state from this interaction.
+  * Populates a set of LDAP options with state from this interaction.
   *
   * @param  options existing set of options; may be null in which case this
   *         method will create a new set of <code>LDAPConnectionOptions</code>
@@ -1662,6 +1795,17 @@
  }
 
  /**
+  * Returns the command builder with the equivalent arguments on the
+  * non-interactive mode.
+  * @return the command builder with the equivalent arguments on the
+  * non-interactive mode.
+  */
+ public CommandBuilder getCommandBuilder()
+ {
+   return commandBuilder;
+ }
+
+ /**
   * Displays the heading if it was not displayed before.
   *
   */

--
Gitblit v1.10.0