From a13a4e5acebf19669a99eb7fbc88842faf404a3d Mon Sep 17 00:00:00 2001
From: Violette Roche-Montane <violette.roche-montane@forgerock.com>
Date: Fri, 04 Apr 2014 09:46:13 +0000
Subject: [PATCH] Checkpoint for OPENDJ-1303 "opendj-cli" Preparing the ground to code cleanup the LDAPConnectionConsoleInteraction. - Added messages from utility.properties / quicksetup.properties. - Added functions to Utils. - Minor mods on the ConnectionFactoryProvider : accessors, added getters and extracted checkForConflictingArguments().

---
 opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
index d93be76..47f5413 100644
--- a/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
+++ b/opendj-sdk/opendj-cli/src/main/java/com/forgerock/opendj/cli/Utils.java
@@ -51,6 +51,8 @@
 import javax.net.ssl.SSLHandshakeException;
 
 import org.forgerock.i18n.LocalizableMessage;
+import org.forgerock.i18n.LocalizableMessageBuilder;
+import org.forgerock.i18n.LocalizableMessageDescriptor;
 import org.forgerock.opendj.ldap.DN;
 import org.forgerock.opendj.ldap.RDN;
 
@@ -492,6 +494,30 @@
     }
 
     /**
+     * Returns a localized message for a given properties key an throwable.
+     *
+     * @param message
+     *            prefix
+     * @param t
+     *            the throwable for which we want to get a message.
+     * @return a localized message for a given properties key and throwable.
+     */
+    public static LocalizableMessage getThrowableMsg(final LocalizableMessage message, final Throwable t) {
+        final LocalizableMessageBuilder mb = new LocalizableMessageBuilder(message);
+        LocalizableMessageDescriptor.Arg1<Object> tag;
+        if (isOutOfMemory(t)) {
+            tag = INFO_EXCEPTION_OUT_OF_MEMORY_DETAILS;
+        } else {
+            tag = INFO_EXCEPTION_DETAILS;
+        }
+        String detail = t.toString();
+        if (detail != null) {
+            mb.append("  ").append(tag.get(detail));
+        }
+        return mb.toMessage();
+    }
+
+    /**
      * Returns <CODE>true</CODE> if we can write on the provided path and <CODE>false</CODE> otherwise.
      *
      * @param path
@@ -517,7 +543,7 @@
      * Returns {@code true} if the the provided string is a DN and {@code false} otherwise.
      *
      * @param dn
-     *            the String we are analyzing.
+     *            The String we are analyzing.
      * @return {@code true} if the the provided string is a DN and {@code false} otherwise.
      */
     public static boolean isDN(String dn) {
@@ -536,7 +562,30 @@
      *            The UID to be used to generate the DN.
      * @return The DN of the administrator for the given UID.
      */
-    private static String getAdministratorDN(String uid) {
+    public static String getAdministratorDN(String uid) {
         return "cn=" + RDN.valueOf(uid) + ",cn=Administrators, cn=admin data";
     }
+
+    /**
+     * Tells whether this throwable has been generated for an out of memory error or not.
+     *
+     * @param t
+     *            The throwable to analyze.
+     * @return {@code true} if the throwable was generated by an out of memory error and false otherwise.
+     */
+    private static boolean isOutOfMemory(Throwable t) {
+        boolean isOutOfMemory = false;
+        while (!isOutOfMemory && (t != null)) {
+            if (t instanceof OutOfMemoryError) {
+                isOutOfMemory = true;
+            } else if (t instanceof IOException) {
+                final String msg = t.toString();
+                if (msg != null) {
+                    isOutOfMemory = msg.contains("Not enough space");
+                }
+            }
+            t = t.getCause();
+        }
+        return isOutOfMemory;
+    }
 }

--
Gitblit v1.10.0