The bundle context was implemented such that it was invalid when a bundle
was in a stopped state, but that it could become valid again if the bundle
was re-started. It appears that the spec should be interpreted such that
a bundle context can never become valid again, like the bundle activator.
This patch implements this behavior.


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@418718 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
index d151b5f..92aa71c 100644
--- a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
+++ b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/BundleContextImpl.java
@@ -27,6 +27,7 @@
 {
     private Felix m_felix = null;
     private BundleImpl m_bundle = null;
+    private boolean m_valid = true;
 
     protected BundleContextImpl(Felix felix, BundleImpl bundle)
     {
@@ -34,6 +35,11 @@
         m_bundle = bundle;
     }
 
+    protected void invalidate()
+    {
+        m_valid = false;
+    }
+
     public void addImportPackage() throws BundleException
     {
         throw new BundleException("Not implemented yet.");
@@ -56,14 +62,14 @@
 
     public String getProperty(String name)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.getProperty(name);
     }
 
     public Bundle getBundle()
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_bundle;
     }
@@ -71,7 +77,7 @@
     public Filter createFilter(String expr)
         throws InvalidSyntaxException
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return new FilterImpl(m_felix.getLogger(), expr);
     }
@@ -85,35 +91,35 @@
     public Bundle installBundle(String location, InputStream is)
         throws BundleException
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.installBundle(location, is);
     }
 
     public Bundle getBundle(long id)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.getBundle(id);
     }
 
     public Bundle[] getBundles()
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.getBundles();
     }
 
     public void addBundleListener(BundleListener l)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.addBundleListener(m_bundle, l);
     }
 
     public void removeBundleListener(BundleListener l)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.removeBundleListener(l);
     }
@@ -133,28 +139,28 @@
     public void addServiceListener(ServiceListener l, String s)
         throws InvalidSyntaxException
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.addServiceListener(m_bundle, l, s);
     }
 
     public void removeServiceListener(ServiceListener l)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.removeServiceListener(l);
     }
 
     public void addFrameworkListener(FrameworkListener l)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.addFrameworkListener(m_bundle, l);
     }
 
     public void removeFrameworkListener(FrameworkListener l)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         m_felix.removeFrameworkListener(l);
     }
@@ -168,14 +174,14 @@
     public ServiceRegistration registerService(
         String[] clazzes, Object svcObj, Dictionary dict)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.registerService(m_bundle, clazzes, svcObj, dict);
     }
 
     public ServiceReference getServiceReference(String clazz)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         try
         {
@@ -275,7 +281,7 @@
 
     public ServiceReference[] getAllServiceReferences(String clazz, String filter) throws InvalidSyntaxException
     {
-        checkBundleContextValid();
+        checkValidity();
         
         // TODO: Implement BundleContext.getAllServiceReferences()
         return null;
@@ -284,14 +290,14 @@
     public ServiceReference[] getServiceReferences(String clazz, String filter)
         throws InvalidSyntaxException
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.getServiceReferences(m_bundle, clazz, filter);
     }
 
     public Object getService(ServiceReference ref)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         if (ref == null)
         {
@@ -302,7 +308,7 @@
 
     public boolean ungetService(ServiceReference ref)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         if (ref == null)
         {
@@ -315,19 +321,22 @@
 
     public File getDataFile(String s)
     {
-        checkBundleContextValid();
+        checkValidity();
         
         return m_felix.getDataFile(m_bundle, s);
     }
 
-    private void checkBundleContextValid()
+    private void checkValidity()
     {
-        switch(m_bundle.getState())
+        if (m_valid)
         {
-            case Bundle.ACTIVE:
-            case Bundle.STARTING:
-            case Bundle.STOPPING:
-                return;
+            switch (m_bundle.getState())
+            {
+                case Bundle.ACTIVE:
+                case Bundle.STARTING:
+                case Bundle.STOPPING:
+                    return;
+            }
         }
         
         throw new IllegalStateException("Invalid BundleContext.");
diff --git a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
index 437ce38..e00e845 100644
--- a/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
+++ b/org.apache.felix.framework/src/main/java/org/apache/felix/framework/Felix.java
@@ -1362,6 +1362,13 @@
             // then reset its state to RESOLVED.
             info.setState(Bundle.RESOLVED);
 
+            // Clean up the bundle context, if necessary.
+            if (info.getContext() != null)
+            {
+                ((BundleContextImpl) info.getContext()).invalidate();
+                info.setContext(null);
+            }
+
             // Unregister any services offered by this bundle.
             m_registry.unregisterServices(bundle);
 
@@ -1655,6 +1662,13 @@
             rethrow = th;
         }
                   
+        // Clean up the bundle context, if necessary.
+        if (info.getContext() != null)
+        {
+            ((BundleContextImpl) info.getContext()).invalidate();
+            info.setContext(null);
+        }
+
         // Unregister any services offered by this bundle.
         m_registry.unregisterServices(bundle);