Unregister the service manually on stop plus prevent timed-out dispatch threads to call the bundle context after we are stopped

git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@399066 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 5e74432..865b897 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
@@ -44,6 +44,7 @@
 import org.apache.felix.eventadmin.impl.util.LogWrapper;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.event.EventAdmin;
 import org.osgi.service.event.TopicPermission;
 
@@ -119,6 +120,9 @@
     // the wrapper).
     private volatile EventAdminImpl m_admin;
     
+    // The registration of the security decorator factory (i.e., the service)
+    private volatile ServiceRegistration m_registration;
+    
     /**
      * Called upon starting of the bundle. Constructs and registers the EventAdmin
      * service with the framework. Note that the properties of the service are 
@@ -226,7 +230,7 @@
         // register the admin wrapped in a service factory (SecureEventAdminFactory)
         // that hands-out the m_admin object wrapped in a decorator that checks 
         // appropriated permissions of each calling bundle
-        context.registerService(EventAdmin.class.getName(), 
+        m_registration = context.registerService(EventAdmin.class.getName(),
             new SecureEventAdminFactory(m_admin, publishPermissions), null);
         
         // Finally, adapt the outside events to our kind of events as per spec
@@ -249,6 +253,9 @@
      */
     public void stop(final BundleContext context)
     {
+        // We need to unregister manually
+        m_registration.unregister();
+        
         m_admin.stop();
         
         m_pool.close();
@@ -272,6 +279,8 @@
         
         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. 
diff --git a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/handler/BlacklistingHandlerTasks.java b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/handler/BlacklistingHandlerTasks.java
index 38c6828..ff0b863 100644
--- a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/handler/BlacklistingHandlerTasks.java
+++ b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/handler/BlacklistingHandlerTasks.java
@@ -200,12 +200,17 @@
      * @param handlerRef The service reference to unget
      */
     public void ungetEventHandler(final EventHandler handler, 
-    		final ServiceReference handlerRef)
+            final ServiceReference handlerRef)
     {
-    		if(m_nullEventHandler != handler)
-    		{
-    			m_context.ungetService(handlerRef);
-    		}
+            if(m_nullEventHandler != handler)
+            {
+                // Is the handler not unregistered or blacklisted?
+                if(!m_blackList.contains(handlerRef) && (null != 
+                    handlerRef.getBundle()))
+                {
+                    m_context.ungetService(handlerRef);
+                }
+            }
     }
 
     /*
diff --git a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/tasks/HandlerTaskImpl.java b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/tasks/HandlerTaskImpl.java
index 90cf25c..b9a243a 100644
--- a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/tasks/HandlerTaskImpl.java
+++ b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/tasks/HandlerTaskImpl.java
@@ -89,4 +89,4 @@
     {
         m_handlerTasks.blackList(m_eventHandlerRef);
     }
-}
\ No newline at end of file
+}
diff --git a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java
index 6985d8e..e4a5804 100644
--- a/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java
+++ b/org.apache.felix.eventadmin/src/main/java/org/apache/felix/eventadmin/impl/util/LogWrapper.java
@@ -295,8 +295,8 @@
                 // class as well.
                 for (Iterator iter = m_loggerRefs.iterator(); iter.hasNext();)
                 {
-                	   final ServiceReference next = (ServiceReference) iter.next();
-                	   
+                    final ServiceReference next = (ServiceReference) iter.next();
+                       
                     org.osgi.service.log.LogService logger = 
                         (org.osgi.service.log.LogService) m_context.getService(next);
     
@@ -344,8 +344,8 @@
                 // class as well.
                 for (Iterator iter = m_loggerRefs.iterator(); iter.hasNext();)
                 {
-                	   final ServiceReference next = (ServiceReference) iter.next();
-                	   
+                       final ServiceReference next = (ServiceReference) iter.next();
+                       
                     org.osgi.service.log.LogService logger = 
                         (org.osgi.service.log.LogService) m_context.getService(next);