From 0875f1c16c07dbe57a1b6110ec6a42d51cfa85d7 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Fri, 20 Sep 2013 15:50:35 +0000
Subject: [PATCH] Fix OPENDJ-1152: Provide the ability to debug leaked pooled connections

---
 opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
index bbfd09e..3a4326f 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/com/forgerock/opendj/util/StaticUtils.java
@@ -72,6 +72,21 @@
     public static final Logger DEBUG_LOG = Logger.getLogger("org.forgerock.opendj.ldap");
 
     /**
+     * Indicates whether the SDK is being used in debug mode. In debug mode
+     * components may enable certain instrumentation in order to help debug
+     * applications.
+     */
+    public static final boolean DEBUG_ENABLED =
+            System.getProperty("org.forgerock.opendj.debug") != null;
+
+    private static final boolean DEBUG_TO_STDERR = System
+            .getProperty("org.forgerock.opendj.debug.stderr") != null;
+
+    static {
+        logIfDebugEnabled("debugging enabled", null);
+    }
+
+    /**
      * The end-of-line character for this platform.
      */
     public static final String EOL = System.getProperty("line.separator");
@@ -2162,6 +2177,51 @@
     }
 
     /**
+     * Returns the stack trace for the calling method, but only if SDK debugging
+     * is enabled.
+     *
+     * @return The stack trace for the calling method, but only if SDK debugging
+     *         is enabled, otherwise {@code null}..
+     */
+    public static StackTraceElement[] getStackTraceIfDebugEnabled() {
+        if (!DEBUG_ENABLED) {
+            return null;
+        } else {
+            final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
+            return Arrays.copyOfRange(stack, 2, stack.length);
+        }
+    }
+
+    /**
+     * Logs the provided message and stack trace if SDK debugging is enabled to
+     * either stderr or the debug logger.
+     *
+     * @param msg
+     *            The message to be logged.
+     * @param stackTrace
+     *            The stack trace, which may be {@code null}.
+     */
+    public static void logIfDebugEnabled(final String msg, final StackTraceElement[] stackTrace) {
+        if (DEBUG_ENABLED) {
+            final StringBuilder builder = new StringBuilder("OPENDJ SDK: ");
+            builder.append(msg);
+            if (stackTrace != null) {
+                builder.append(System.lineSeparator());
+                for (StackTraceElement e : stackTrace) {
+                    builder.append("\tat ");
+                    builder.append(String.valueOf(e));
+                    builder.append(System.lineSeparator());
+                }
+            }
+            if (DEBUG_TO_STDERR) {
+                System.err.println(builder.toString());
+            } else if (DEBUG_LOG.isLoggable(Level.SEVERE)) {
+                DEBUG_LOG.severe(builder.toString());
+            }
+        }
+    }
+
+    /**
      * Retrieves the printable ASCII representation of the provided byte.
      *
      * @param b

--
Gitblit v1.10.0