From f6dbdd26a7e9f8ff4aa02c48d1348c163b8cb9fb Mon Sep 17 00:00:00 2001
From: boli <boli@localhost>
Date: Tue, 21 Apr 2009 19:16:11 +0000
Subject: [PATCH] Fix for issue 3011: 

---
 opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java               |   57 --------------
 opendj-sdk/opends/src/server/org/opends/server/tasks/RestartTaskThread.java            |    5 
 opendj-sdk/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java |    6 +
 opendj-sdk/opends/src/server/org/opends/server/loggers/debug/DebugAspect.java          |   14 ---
 opendj-sdk/opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java |    6 
 opendj-sdk/opends/src/server/org/opends/server/core/ServerShutdownMonitor.java         |    9 +-
 opendj-sdk/opends/src/server/org/opends/server/api/DirectoryThread.java                |  119 ++++++++++++++++++++++++-----
 7 files changed, 114 insertions(+), 102 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/api/DirectoryThread.java b/opendj-sdk/opends/src/server/org/opends/server/api/DirectoryThread.java
index 0cf23af..c2d66aa 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/api/DirectoryThread.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/api/DirectoryThread.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.api;
 
@@ -33,7 +33,21 @@
 
 import org.opends.server.backends.task.Task;
 import org.opends.server.core.DirectoryServer;
-
+import static org.opends.server.loggers.debug.DebugLogger.
+    debugEnabled;
+import static org.opends.server.loggers.debug.DebugLogger.getTracer;
+import org.opends.server.loggers.debug.DebugTracer;
+import static org.opends.server.loggers.ErrorLogger.logError;
+import org.opends.server.types.DebugLogLevel;
+import org.opends.server.types.DN;
+import static org.opends.server.util.StaticUtils.stackTraceToString;
+import static org.opends.server.util.ServerConstants.
+    ALERT_TYPE_UNCAUGHT_EXCEPTION;
+import static org.opends.server.util.ServerConstants.
+    ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION;
+import org.opends.messages.Message;
+import static org.opends.messages.CoreMessages.
+    ERR_UNCAUGHT_THREAD_EXCEPTION;
 
 
 /**
@@ -62,6 +76,18 @@
 public class DirectoryThread
        extends Thread
 {
+  /**
+   * The tracer object for the debug logger.
+   */
+  private static final DebugTracer TRACER = getTracer();
+
+  /**
+   * The directory thread group that all directory threads will be a
+   * member of.
+   */
+  public static final DirectoryThreadGroup DIRECTORY_THREAD_GROUP =
+      new DirectoryThreadGroup();
+
   // The stack trace taken at the time that this thread was created.
   private StackTraceElement[] creationStackTrace;
 
@@ -71,7 +97,73 @@
   // A reference to the thread that was used to create this thread.
   private Thread parentThread;
 
+  /**
+   * A thread group for all directory threads. This implements a
+   * custom unhandledException handler that logs the error.
+   */
+  private static class DirectoryThreadGroup extends ThreadGroup
+      implements AlertGenerator
+  {
+    private final LinkedHashMap<String,String> alerts;
 
+    /**
+     * Private constructor for DirectoryThreadGroup.
+     */
+    private DirectoryThreadGroup()
+    {
+      super("Directory Server Thread Group");
+      alerts = new LinkedHashMap<String,String>();
+      alerts.put(ALERT_TYPE_UNCAUGHT_EXCEPTION,
+          ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public DN getComponentEntryDN() {
+      return DN.NULL_DN;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String getClassName() {
+      return "org.oepnds.server.api.DirectoryThread";
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public LinkedHashMap<String, String> getAlerts() {
+      return alerts;
+    }
+
+    /**
+     * Provides a means of handling a case in which a thread is about
+     * to die because of an unhandled exception.  This method does
+     * nothing to try to prevent the death of that thread, but will
+     * at least log it so that it can be available for debugging
+     * purposes.
+     *
+     * @param  t  The thread that threw the exception.
+     * @param  e  The exception that was thrown but not properly
+     *            handled.
+     */
+    @Override
+    public void uncaughtException(Thread t, Throwable e)
+    {
+      if (debugEnabled())
+      {
+        TRACER.debugCaught(DebugLogLevel.ERROR, e);
+      }
+
+      Message message = ERR_UNCAUGHT_THREAD_EXCEPTION.get(
+          t.getName(), stackTraceToString(e));
+      logError(message);
+      DirectoryServer.sendAlertNotification(this,
+          ALERT_TYPE_UNCAUGHT_EXCEPTION, message);
+    }
+  }
 
   /**
    * Creates a new instance of this directory thread with the
@@ -83,7 +175,7 @@
    */
   public DirectoryThread(Runnable target, String threadName)
   {
-    super (DirectoryServer.getDirectoryThreadGroup(), target,
+    super (DIRECTORY_THREAD_GROUP, target,
            threadName);
 
 
@@ -99,26 +191,7 @@
    */
   protected DirectoryThread(String threadName)
   {
-    super(DirectoryServer.getDirectoryThreadGroup(), threadName);
-
-
-    init();
-  }
-
-
-  /**
-   * Creates a new instance of this directory thread with the
-   * specified name as a part of the given thread group.
-   *
-   * @param  threadGroup  The thread group in which this thread is to
-   *                      be placed.
-   * @param  threadName   The human-readable name to use for this
-   *                      thread for debugging purposes.
-   */
-  protected DirectoryThread(ThreadGroup threadGroup,
-                            String threadName)
-  {
-    super(threadGroup, threadName);
+    super(DIRECTORY_THREAD_GROUP, threadName);
 
 
     init();
diff --git a/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java b/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
index dae0c0f..6dbb1f0 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/core/DirectoryServer.java
@@ -242,7 +242,7 @@
  * components.
  */
 public class DirectoryServer
-       implements Thread.UncaughtExceptionHandler, AlertGenerator
+       implements AlertGenerator
 {
   /**
    * The tracer object for the debug logger.
@@ -717,10 +717,6 @@
   private SynchronizationProviderConfigManager
                synchronizationProviderConfigManager;
 
-  // The thread group for all threads associated with the Directory Server.
-  private ThreadGroup directoryThreadGroup;
-
-
   // Registry for base DN and naming context information.
   private BaseDnRegistry baseDnRegistry;
 
@@ -1038,23 +1034,12 @@
     }
 
 
-    // Create the thread group that should be used for all Directory Server
-    // threads.
-    directoryThreadGroup = new ThreadGroup("Directory Server Thread Group");
-
-
     // Add a shutdown hook so that the server can be notified when the JVM
     // starts shutting down.
     shutdownHook = new DirectoryServerShutdownHook();
     Runtime.getRuntime().addShutdownHook(shutdownHook);
 
 
-    // Register this class as the default uncaught exception handler for the
-    // JVM.  The uncaughtException method will be called if a thread dies
-    // because it did not properly handle an exception.
-    Thread.setDefaultUncaughtExceptionHandler(this);
-
-
     // Create the MBean server that we will use for JMX interaction.
     initializeJMX();
 
@@ -2912,20 +2897,6 @@
 
 
   /**
-   * Retrieves the thread group that should be used by all threads associated
-   * with the Directory Server.
-   *
-   * @return  The thread group that should be used by all threads associated
-   *          with the Directory Server.
-   */
-  public static ThreadGroup getDirectoryThreadGroup()
-  {
-    return directoryServer.directoryThreadGroup;
-  }
-
-
-
-  /**
    * Retrieves a reference to the Directory Server configuration handler.
    *
    * @return  A reference to the Directory Server configuration handler.
@@ -9090,8 +9061,6 @@
 
     alerts.put(ALERT_TYPE_SERVER_STARTED, ALERT_DESCRIPTION_SERVER_STARTED);
     alerts.put(ALERT_TYPE_SERVER_SHUTDOWN, ALERT_DESCRIPTION_SERVER_SHUTDOWN);
-    alerts.put(ALERT_TYPE_UNCAUGHT_EXCEPTION,
-               ALERT_DESCRIPTION_UNCAUGHT_EXCEPTION);
     alerts.put(ALERT_TYPE_ENTERING_LOCKDOWN_MODE,
                ALERT_DESCRIPTION_ENTERING_LOCKDOWN_MODE);
     alerts.put(ALERT_TYPE_LEAVING_LOCKDOWN_MODE,
@@ -9103,30 +9072,6 @@
 
 
   /**
-   * Provides a means of handling a case in which a thread is about to die
-   * because of an unhandled exception.  This method does nothing to try to
-   * prevent the death of that thread, but will at least log it so that it can
-   * be available for debugging purposes.
-   *
-   * @param  thread     The thread that threw the exception.
-   * @param  exception  The exception that was thrown but not properly handled.
-   */
-  public void uncaughtException(Thread thread, Throwable exception)
-  {
-    if (debugEnabled())
-    {
-      TRACER.debugCaught(DebugLogLevel.ERROR, exception);
-    }
-
-    Message message = ERR_UNCAUGHT_THREAD_EXCEPTION.get(
-        thread.getName(), stackTraceToString(exception));
-    logError(message);
-    sendAlertNotification(this, ALERT_TYPE_UNCAUGHT_EXCEPTION, message);
-  }
-
-
-
-  /**
    * Indicates whether the server is currently in the process of shutting down.
    * @return <CODE>true</CODE> if this server is currently in the process of
    * shutting down and <CODE>false</CODE> otherwise.
diff --git a/opendj-sdk/opends/src/server/org/opends/server/core/ServerShutdownMonitor.java b/opendj-sdk/opends/src/server/org/opends/server/core/ServerShutdownMonitor.java
index 15ece15..c16952c 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/core/ServerShutdownMonitor.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/core/ServerShutdownMonitor.java
@@ -22,12 +22,13 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.core;
 
 
 
+import org.opends.server.api.DirectoryThread;
 import java.util.Iterator;
 import java.util.LinkedList;
 
@@ -38,7 +39,7 @@
  * shutdown process and may help nudge it along if it appears to get hung.
  */
 public class ServerShutdownMonitor
-       extends Thread
+       extends DirectoryThread
 {
   // Indicates whether the monitor has completed and the shutdown may be
   // finalized with a call to System.exit;
@@ -56,7 +57,7 @@
    */
   public ServerShutdownMonitor()
   {
-    setName("Directory Server Shutdown Monitor");
+    super("Directory Server Shutdown Monitor");
     setDaemon(true);
 
 
@@ -71,7 +72,7 @@
     // we'll make sure to allocate enough room for double the threads that we
     // think are currently running.
     threadList = new LinkedList<Thread>();
-    ThreadGroup threadGroup = DirectoryServer.getDirectoryThreadGroup();
+    ThreadGroup threadGroup = DirectoryThread.DIRECTORY_THREAD_GROUP;
     Thread[] threadArray = new Thread[threadGroup.activeCount() * 2];
     int numThreads = threadGroup.enumerate(threadArray, true);
     for (int i=0; i < numThreads; i++)
diff --git a/opendj-sdk/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java b/opendj-sdk/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
index 3649159..f6b019e 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/extensions/SoftReferenceEntryCache.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.extensions;
 import org.opends.messages.Message;
@@ -43,6 +43,7 @@
 import org.opends.server.admin.std.server.SoftReferenceEntryCacheCfg;
 import org.opends.server.api.Backend;
 import org.opends.server.api.EntryCache;
+import org.opends.server.api.DirectoryThread;
 import org.opends.server.config.ConfigException;
 import org.opends.server.core.DirectoryServer;
 import org.opends.server.loggers.debug.DebugTracer;
@@ -128,7 +129,8 @@
       )
       throws ConfigException, InitializationException
   {
-    cleanerThread = new Thread(this, "Soft Reference Entry Cache Cleaner");
+    cleanerThread = new DirectoryThread(this,
+        "Soft Reference Entry Cache Cleaner");
     cleanerThread.setDaemon(true);
     cleanerThread.start();
 
diff --git a/opendj-sdk/opends/src/server/org/opends/server/loggers/debug/DebugAspect.java b/opendj-sdk/opends/src/server/org/opends/server/loggers/debug/DebugAspect.java
index d5ca856..1d6f4cd 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/loggers/debug/DebugAspect.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/loggers/debug/DebugAspect.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.loggers.debug;
 
@@ -83,15 +83,6 @@
   }
 
   /**
-   * Pointcut for matching the getDirectoryThreadGroup method.
-   */
-  @Pointcut("execution(public static ThreadGroup org.opends.server." +
-      "core.DirectoryServer.getDirectoryThreadGroup(..))")
-  private void getThreadGroupMethod()
-  {
-  }
-
-  /**
    * Pointcut for matching all getDebugProperties() methods.
    * TODO: Make this less general. Find out if pointcut matches
    * subclass methods.
@@ -120,8 +111,7 @@
    * debug logger.
    */
   @Pointcut("toStringMethod() || getMessageMethod() || " +
-      "getDebugPropertiesMethod() || debugRelatedClasses() || " +
-      "getThreadGroupMethod()")
+      "getDebugPropertiesMethod() || debugRelatedClasses()")
   private void excluded()
   {
   }
diff --git a/opendj-sdk/opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java b/opendj-sdk/opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java
index 1089c55..613cf78 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/monitors/EntryCacheMonitorProvider.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008 Sun Microsystems, Inc.
+ *      Copyright 2008-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.monitors;
 
@@ -58,8 +58,8 @@
   // The entry cache with which this monitor is associated.
   private EntryCache<? extends EntryCacheCfg> entryCache;
 
-  // Global entry cache monitor configuration.
-  private static EntryCacheMonitorProviderCfg monitorConfiguration;
+  // Entry cache monitor configuration.
+  private EntryCacheMonitorProviderCfg monitorConfiguration;
 
   /**
    * Creates default instance of this monitor provider.
diff --git a/opendj-sdk/opends/src/server/org/opends/server/tasks/RestartTaskThread.java b/opendj-sdk/opends/src/server/org/opends/server/tasks/RestartTaskThread.java
index c738ede..c3ce706 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/tasks/RestartTaskThread.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/tasks/RestartTaskThread.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2006-2008 Sun Microsystems, Inc.
+ *      Copyright 2006-2009 Sun Microsystems, Inc.
  */
 package org.opends.server.tasks;
 import org.opends.messages.Message;
@@ -30,6 +30,7 @@
 
 
 import org.opends.server.core.DirectoryServer;
+import org.opends.server.api.DirectoryThread;
 
 
 
@@ -49,7 +50,7 @@
  * exit before we get a chance to restart it if all non-daemon threads go away.
  */
 public class RestartTaskThread
-       extends Thread
+       extends DirectoryThread
 {
   /**
    * The fully-qualified name of this class.

--
Gitblit v1.10.0