From 7e8d3fa14e47157dc8af64fd95936b0a686bd7a4 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 29 Jul 2009 10:32:29 +0000
Subject: [PATCH] Fix for issue 4152 (Graphical utilities should be consistent with command-line (admin connector certificate) Load the admin-truststore when creating the keystore to be used by the graphical utilities.

---
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/InstallerHelper.java |   50 ++++++++++------
 opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/UIKeyStore.java           |   89 +++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 22 deletions(-)

diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/InstallerHelper.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/InstallerHelper.java
index a0b90c8..f2f9199 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/InstallerHelper.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/installer/InstallerHelper.java
@@ -102,6 +102,10 @@
 
   private static final String INITIAL_CLIENT_HEAP_ARG = "-Xms8m";
 
+  private static final String SERVER_HEAP_ARGS = "-Xms256m -Xmx512m";
+
+  private static final long SERVER_MAX_HEAP_BYTES = 512 * 1024 * 1024;
+
   /**
    * Invokes the method ConfigureDS.configMain with the provided parameters.
    * @param args the arguments to be passed to ConfigureDS.configMain.
@@ -926,10 +930,17 @@
       boolean supportsClient = supportsClient(javaHome, installPath);
       boolean supportsServer = supportsServer(javaHome, installPath);
 
-
-      boolean supportsClientInitialHeap = supportsInitialHeap(javaHome,
-          installPath);
-
+      boolean supportsClientInitialHeap =
+        supportsOption(INITIAL_CLIENT_HEAP_ARG, javaHome, installPath);
+      boolean supportsServerInitialHeap = false;
+      // If the current max memory is bigger than the max heap we want to set,
+      // assume that the JVM ergonomics are going to be able to allocate enough
+      // memory.
+      if (Runtime.getRuntime().maxMemory() < SERVER_MAX_HEAP_BYTES)
+      {
+        supportsServerInitialHeap =
+          supportsOption(SERVER_HEAP_ARGS, javaHome, installPath);
+      }
       // Scripts to which we will pass -client argument
       String[] clientScripts =
       {
@@ -950,12 +961,25 @@
           "upgrade", "verify-index", "dbtest"
       };
 
-      if (supportsServer)
+      if (supportsServer || supportsServerInitialHeap)
       {
         for (int i=0; i<serverScripts.length; i++)
         {
           writer.newLine();
-          writer.write(serverScripts[i]+".java-args=-server");
+          String arg = "";
+          if (supportsServer)
+          {
+            arg = "-server";
+          }
+          if (supportsServerInitialHeap)
+          {
+            if (arg.length() > 0)
+            {
+              arg += " ";
+            }
+            arg += SERVER_HEAP_ARGS;
+          }
+          writer.write(serverScripts[i]+".java-args="+arg);
         }
       }
       else
@@ -967,7 +991,6 @@
         }
       }
 
-
       if (supportsClient || supportsClientInitialHeap)
       {
         for (int i=0; i<clientScripts.length; i++)
@@ -1085,19 +1108,6 @@
   }
 
   /**
-   * Tells whether the provided java installation supports the server option
-   * or not.
-   * @param javaHome the java installation path.
-   * @param installPath the install path of the server.
-   * @return <CODE>true</CODE> if the provided java installation supports the
-   * server option and <CODE>false</CODE> otherwise.
-   */
-  private boolean supportsInitialHeap(String javaHome, String installPath)
-  {
-    return supportsOption(INITIAL_CLIENT_HEAP_ARG, javaHome, installPath);
-  }
-
-  /**
    * Tells whether the provided java installation supports the client option
    * or not.
    * @param javaHome the java installation path.
diff --git a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/UIKeyStore.java b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/UIKeyStore.java
index f540e70..d1cf8c5 100644
--- a/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/UIKeyStore.java
+++ b/opendj-sdk/opends/src/quicksetup/org/opends/quicksetup/util/UIKeyStore.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008 Sun Microsystems, Inc.
+ *      Copyright 2008-2009 Sun Microsystems, Inc.
  */
 
 package org.opends.quicksetup.util;
@@ -37,6 +37,7 @@
 import java.security.NoSuchAlgorithmException;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
+import java.util.Enumeration;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -118,6 +119,7 @@
       {
         keyStore.load(null, null);
       }
+      loadLocalAdminTrustStore(keyStore);
     }
     return keyStore;
   }
@@ -150,7 +152,7 @@
     {
       Utils.createFile(f);
     }
-    FileOutputStream fos = new FileOutputStream(getKeyStorePath());
+    FileOutputStream fos = new FileOutputStream(getKeyStorePath(), true);
     k.store(fos, new char[]{});
     if (fos != null)
     {
@@ -169,4 +171,87 @@
     return System.getProperty("user.home") + File.separator +
     ".opends" + File.separator + "gui-keystore";
   }
+
+  /**
+   * Loads the local admin truststore.
+   * @param keyStore the keystore where the admin truststore will be loaded.
+   */
+  private static void loadLocalAdminTrustStore(KeyStore keyStore)
+  {
+    String adminTrustStorePath = getLocalAdminTrustStorePath();
+    File f = new File(adminTrustStorePath);
+    if (!f.exists())
+    {
+      LOG.log(Level.INFO, "Path "+adminTrustStorePath+ " does not exist");
+      adminTrustStorePath = null;
+    }
+    else if (f.isDirectory())
+    {
+      LOG.log(Level.SEVERE, "Path "+adminTrustStorePath+ " is a directory");
+      adminTrustStorePath = null;
+    }
+    else if (!f.canRead())
+    {
+      LOG.log(Level.SEVERE, "Path "+adminTrustStorePath+ " is not readable");
+      adminTrustStorePath = null;
+    }
+
+    if (adminTrustStorePath != null)
+    {
+      FileInputStream fos = null;
+      try
+      {
+        fos = new FileInputStream(adminTrustStorePath);
+        KeyStore adminKeyStore =
+          KeyStore.getInstance(KeyStore.getDefaultType());
+        adminKeyStore.load(fos, null);
+        Enumeration<String> aliases = adminKeyStore.aliases();
+        while (aliases.hasMoreElements())
+        {
+          String alias = aliases.nextElement();
+          if (adminKeyStore.isCertificateEntry(alias))
+          {
+            keyStore.setCertificateEntry(alias,
+                adminKeyStore.getCertificate(alias));
+          }
+          else
+          {
+            keyStore.setEntry(alias, adminKeyStore.getEntry(alias, null), null);
+          }
+        }
+      }
+      catch (Throwable t)
+      {
+        LOG.log(Level.SEVERE, "Error reading admin key store on "+
+            adminTrustStorePath, t);
+      }
+      finally
+      {
+        try
+        {
+          if (fos != null)
+          {
+            fos.close();
+          }
+        }
+        catch (Throwable t)
+        {
+          LOG.log(Level.SEVERE, "Error closing admin key store on "+
+              adminTrustStorePath, t);
+        }
+      }
+    }
+  }
+
+  /**
+   * Returns the path where the local admin trust store is.
+   * @return the path where the local admin trust store is.
+   */
+  private static String getLocalAdminTrustStorePath()
+  {
+    String instancePath =
+      Utils.getInstancePathFromClasspath(Utils.getInstallPathFromClasspath());
+    return  instancePath + File.separator + "config" +
+    File.separator + "admin-truststore";
+  }
 }

--
Gitblit v1.10.0