incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r757355 - in /incubator/sling/trunk/bundles/extensions/threaddump: pom.xml src/main/java/org/apache/sling/extensions/threaddump/Activator.java
Date Mon, 23 Mar 2009 09:05:49 GMT
Author: fmeschbe
Date: Mon Mar 23 09:05:47 2009
New Revision: 757355

URL: http://svn.apache.org/viewvc?rev=757355&view=rev
Log:
SLING-894 Register BundleActivaotr as an UncaughtExceptionHandler
reverting to the previous handler when the bundle is stopped and
chaining to the previous handler after logging the exception.

Modified:
    incubator/sling/trunk/bundles/extensions/threaddump/pom.xml
    incubator/sling/trunk/bundles/extensions/threaddump/src/main/java/org/apache/sling/extensions/threaddump/Activator.java

Modified: incubator/sling/trunk/bundles/extensions/threaddump/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/extensions/threaddump/pom.xml?rev=757355&r1=757354&r2=757355&view=diff
==============================================================================
--- incubator/sling/trunk/bundles/extensions/threaddump/pom.xml (original)
+++ incubator/sling/trunk/bundles/extensions/threaddump/pom.xml Mon Mar 23 09:05:47 2009
@@ -61,6 +61,7 @@
                     <instructions>
                         <Import-Package>
                             org.osgi.framework,
+                            org.slf4j.*,
                             javax.servlet.*;
                             org.apache.felix.shell;
                             org.apache.felix.webconsole;resolution:=optional
@@ -113,6 +114,10 @@
             <artifactId>servlet-api</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
         </dependency>

Modified: incubator/sling/trunk/bundles/extensions/threaddump/src/main/java/org/apache/sling/extensions/threaddump/Activator.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/bundles/extensions/threaddump/src/main/java/org/apache/sling/extensions/threaddump/Activator.java?rev=757355&r1=757354&r2=757355&view=diff
==============================================================================
--- incubator/sling/trunk/bundles/extensions/threaddump/src/main/java/org/apache/sling/extensions/threaddump/Activator.java
(original)
+++ incubator/sling/trunk/bundles/extensions/threaddump/src/main/java/org/apache/sling/extensions/threaddump/Activator.java
Mon Mar 23 09:05:47 2009
@@ -18,16 +18,30 @@
  */
 package org.apache.sling.extensions.threaddump;
 
+import java.lang.Thread.UncaughtExceptionHandler;
 import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-public class Activator implements BundleActivator {
+public class Activator implements BundleActivator, UncaughtExceptionHandler {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private UncaughtExceptionHandler oldHandler;
 
     public void start(BundleContext bundleContext) {
+
+        // install handler for uncaught exceptions
+        oldHandler = Thread.getDefaultUncaughtExceptionHandler();
+        Thread.setDefaultUncaughtExceptionHandler(this);
+
+        // install thread handler shell command
         try {
             register(bundleContext,
                 new String[] { "org.apache.felix.shell.Command" },
@@ -35,6 +49,8 @@
         } catch (Throwable t) {
             // shell service might not be available, don't care
         }
+
+        // install Web Console plugin
         try {
             ThreadDumperPanel tdp = new ThreadDumperPanel();
             tdp.activate(bundleContext);
@@ -46,11 +62,12 @@
                 "org.apache.felix.webconsole.ConfigurationPrinter" }, tdp,
                 properties);
         } catch (Throwable t) {
-            // shell service might not be available, don't care
+            // web console might not be available, don't care
         }
     }
 
     public void stop(BundleContext bundleContext) {
+        Thread.setDefaultUncaughtExceptionHandler(oldHandler);
     }
 
     private void register(BundleContext context, String[] serviceNames,
@@ -66,6 +83,31 @@
             + serviceNames[0] + ")");
         properties.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
 
-            context.registerService(serviceNames, service, properties);
+        context.registerService(serviceNames, service, properties);
     }
+
+    // ---------- UncaughtExceptionHandler
+
+    /**
+     * Logs the uncaught exception for the thread at level ERROR and chains to
+     * the old handler, which was installed before this handler has been
+     * installed.
+     * 
+     * @param t The <code>Thread</code> which got the exception but did not
+     *            handle it.
+     * @param e The uncaught <code>Throwable</code> causing the thread to die.
+     */
+    public void uncaughtException(Thread t, Throwable e) {
+        if (e instanceof ThreadDeath) {
+            log.error("Thread " + t + " has just been killed", e);
+        } else {
+            log.error("Uncaught exception in Thread " + t, e);
+        }
+
+        // chain to original handler
+        if (oldHandler != null) {
+            oldHandler.uncaughtException(t, e);
+        }
+    }
+
 }



Mime
View raw message