Moved bundle context invalidation after all other bundle shutdown tasks.
(FELIX-971)


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@751821 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 5b23ae0..5126c62 100644
--- a/framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -1470,10 +1470,6 @@
                 // then reset its state to RESOLVED.
                 setBundleStateAndNotify(bundle, Bundle.RESOLVED);
 
-                // Clean up the bundle context.
-                ((BundleContextImpl) bundle.getBundleContext()).invalidate();
-                bundle.setBundleContext(null);
-
                 // Clean up the bundle activator
                 bundle.setActivator(null);
 
@@ -1486,6 +1482,10 @@
                 // Remove any listeners registered by this bundle.
                 m_dispatcher.removeListeners(bundle);
 
+                // Clean up the bundle context.
+                ((BundleContextImpl) bundle.getBundleContext()).invalidate();
+                bundle.setBundleContext(null);
+
                 // The spec says to expect BundleException or
                 // SecurityException, so rethrow these exceptions.
                 if (th instanceof BundleException)
@@ -1806,10 +1806,6 @@
             // clean up after itself.
             if (bundle.getBundleId() != 0)
             {
-                // Clean up the bundle context.
-                ((BundleContextImpl) bundle.getBundleContext()).invalidate();
-                bundle.setBundleContext(null);
-
                 // Clean up the bundle activator.
                 bundle.setActivator(null);
 
@@ -1823,6 +1819,10 @@
                 // listeners for a bundle when it is stopped.
                 m_dispatcher.removeListeners(bundle);
 
+                // Clean up the bundle context.
+                ((BundleContextImpl) bundle.getBundleContext()).invalidate();
+                bundle.setBundleContext(null);
+
                 setBundleStateAndNotify(bundle, Bundle.RESOLVED);
 
                 // We still need to fire the STOPPED event, but we will do