Handle BundleActivator.stop in event handling thread gracefully.

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@406351 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/Activator.java b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/Activator.java
index 865b897..1528245 100644
--- a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/Activator.java
+++ b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/Activator.java
@@ -37,6 +37,7 @@
 import org.apache.felix.eventadmin.impl.security.TopicPermissions;
 import org.apache.felix.eventadmin.impl.tasks.AsyncDeliverTasks;
 import org.apache.felix.eventadmin.impl.tasks.BlockTask;
+import org.apache.felix.eventadmin.impl.tasks.DeliverTask;
 import org.apache.felix.eventadmin.impl.tasks.DeliverTasks;
 import org.apache.felix.eventadmin.impl.tasks.DispatchTask;
 import org.apache.felix.eventadmin.impl.tasks.SyncDeliverTasks;
@@ -242,10 +243,7 @@
      * delivered. An IllegalStateException will be thrown on new events starting with
      * the begin of this method. However, it might take some time until we settle
      * down which is somewhat cumbersome given that the spec asks for return in 
-     * a timely manner. Note that calling the stop method in one of the event 
-     * delivery threads will cause the handler to be timed-out. Furthermore, calling
-     * stop in one of the event delivery threads with time-out disabled will lead to
-     * a deadlock.
+     * a timely manner. 
      *
      * @param context The bundle context passed by the framework
      * 
@@ -258,8 +256,6 @@
         
         m_admin.stop();
         
-        m_pool.close();
-        
         // This tasks will be unblocked once the queues are empty
         final BlockTask asyncShutdownBlock = new BlockTask();
         
@@ -273,20 +269,26 @@
   
         m_admin = null;
         
-        m_pool = null;
-        
         m_asyncQueue = null;
         
         m_syncQueue = null;
         
         m_registration = null;
         
-        // Wait till the queues are empty (i.e., all pending events are delivered)
-        // Warning: if this is one of the event delivery threads this will lead to 
-        // a deadlock in case that time-outs are disabled. 
+        final DispatchTask task = m_pool.getTask(Thread.currentThread(), null);
+        
+        if(null != task)
+        {
+            task.handover();
+        }
+        
         asyncShutdownBlock.block();
         
         syncShutdownBlock.block();
+        
+        m_pool.close();
+        
+        m_pool = null;
     }
     
     /*