[FELIX-4326] Possible Invalid BundleContext exception when shutting down the extender

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1544179 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/utils/src/main/java/org/apache/felix/utils/extender/AbstractExtender.java b/utils/src/main/java/org/apache/felix/utils/extender/AbstractExtender.java
index 2dbca76..978ecc0 100644
--- a/utils/src/main/java/org/apache/felix/utils/extender/AbstractExtender.java
+++ b/utils/src/main/java/org/apache/felix/utils/extender/AbstractExtender.java
@@ -52,6 +52,7 @@
     private final ConcurrentMap<Bundle, Extension> extensions = new ConcurrentHashMap<Bundle, Extension>();
     private final ConcurrentMap<Bundle, FutureTask> destroying = new ConcurrentHashMap<Bundle, FutureTask>();
     private volatile boolean stopping;
+    private volatile boolean stopped;
 
     private boolean synchronous;
     private boolean preemptiveShutdown;
@@ -115,6 +116,7 @@
 
     public void stop(BundleContext context) throws Exception {
         stopping = true;
+        this.context.removeBundleListener(this);
         while (!extensions.isEmpty()) {
             Collection<Bundle> toDestroy = chooseBundlesToDestroy(extensions.keySet());
             if (toDestroy == null || toDestroy.isEmpty()) {
@@ -134,6 +136,7 @@
             }
             executors = null;
         }
+        stopped = true;
     }
 
     protected void doStart() throws Exception {
@@ -172,6 +175,9 @@
 
 
     public void bundleChanged(BundleEvent event) {
+        if (stopped) {
+            return;
+        }
         Bundle bundle = event.getBundle();
         if (bundle.getState() != Bundle.ACTIVE && bundle.getState() != Bundle.STARTING) {
             // The bundle is not in STARTING or ACTIVE state anymore