Allow bundles to add listeners/services while in STOPPING state. (FELIX-3082)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1161715 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/framework/src/main/java/org/apache/felix/framework/Felix.java b/framework/src/main/java/org/apache/felix/framework/Felix.java
index 65511cd..a0d9ed4 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -2394,6 +2394,13 @@
                 // Clean up the bundle activator.
                 bundle.setActivator(null);
 
+                // Clean up the bundle context.
+                // We invalidate this first to make sure it cannot be used
+                // after stopping the activator.
+                BundleContextImpl bci = (BundleContextImpl) bundle._getBundleContext();
+                bci.invalidate();
+                bundle.setBundleContext(null);
+
                 // Unregister any services offered by this bundle.
                 m_registry.unregisterServices(bundle);
 
@@ -2402,11 +2409,7 @@
 
                 // The spec says that we must remove all event
                 // listeners for a bundle when it is stopped.
-                m_dispatcher.removeListeners(bundle._getBundleContext());
-
-                // Clean up the bundle context.
-                ((BundleContextImpl) bundle._getBundleContext()).invalidate();
-                bundle.setBundleContext(null);
+                m_dispatcher.removeListeners(bci);
 
                 setBundleStateAndNotify(bundle, Bundle.RESOLVED);
 
@@ -3073,7 +3076,7 @@
         // Acquire bundle lock.
         try
         {
-            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE);
+            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
         }
         catch (IllegalStateException ex)
         {
@@ -3113,7 +3116,7 @@
         // Acquire bundle lock.
         try
         {
-            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE);
+            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
         }
         catch (IllegalStateException ex)
         {
@@ -3237,7 +3240,7 @@
         // Acquire bundle lock.
         try
         {
-            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE);
+            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
         }
         catch (IllegalStateException ex)
         {
@@ -3288,7 +3291,7 @@
         // Acquire bundle lock.
         try
         {
-            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE);
+            acquireBundleLock(bundle, Bundle.STARTING | Bundle.ACTIVE | Bundle.STOPPING);
         }
         catch (IllegalStateException ex)
         {