FELIX-4089 Extender do not deactivate managed components when stopped

* Used BundleTracker to track ACTIVE bundles
* Cleanly deactivate managed bundles when iPOJO bundle is stopping
* Notice that we have now a dependency on compendium R4.2

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1487779 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core/pom.xml b/ipojo/runtime/core/pom.xml
index 82058de..4409ef3 100644
--- a/ipojo/runtime/core/pom.xml
+++ b/ipojo/runtime/core/pom.xml
@@ -61,7 +61,7 @@
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
-            <version>4.0.0</version>
+            <version>4.2.0</version>
         </dependency>
         <dependency>
             <groupId>org.apache.felix</groupId>
@@ -129,7 +129,7 @@
                             org.osgi.framework.wiring;resolution:=optional,
                             org.osgi.service.cm,
                             org.osgi.service.log,
-                            org.osgi.util.tracker;version=1.3,
+                            org.osgi.util.tracker;version=1.4, <!-- BundleTracker is in R4.2 -->
                             !sun.io,
                             !net.sourceforge.cobertura.*, <!--  To support code coverage -->
                             !org.objectweb.asm.signature,
diff --git a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
index 561623c..4caa387 100644
--- a/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
+++ b/ipojo/runtime/core/src/main/java/org/apache/felix/ipojo/extender/internal/Extender.java
@@ -32,11 +32,13 @@
 import org.apache.felix.ipojo.extender.internal.queue.pref.enforce.EnforcedQueueService;
 import org.apache.felix.ipojo.util.Logger;
 import org.osgi.framework.*;
+import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
 
 /**
  * iPOJO main activator.
  */
-public class Extender implements BundleActivator, SynchronousBundleListener {
+public class Extender implements BundleActivator {
     /**
      * Enables the iPOJO internal dispatcher.
      * This internal dispatcher helps the OSGi framework to support large
@@ -93,6 +95,11 @@
     private LifecycleQueueService m_queueService;
 
     /**
+     * Track ACTIVE bundles.
+     */
+    private BundleTracker m_tracker;
+
+    /**
      * The iPOJO bundle is starting.
      * This method configures the iPOJO system (internal dispatcher and bundle processing). Then it initiates the
      * bundle processing.
@@ -150,16 +157,24 @@
         // Begin by initializing core handlers
         m_processor.activate(m_bundle);
 
-        synchronized (this) {
-            // listen to any changes in bundles.
-            m_context.addBundleListener(this);
-            // compute already started bundles.
-            for (int i = 0; i < context.getBundles().length; i++) {
-                if (context.getBundles()[i].getState() == Bundle.ACTIVE) {
-                    m_processor.activate(context.getBundles()[i]);
+        m_tracker = new BundleTracker(context, Bundle.ACTIVE, new BundleTrackerCustomizer() {
+            public Object addingBundle(final Bundle bundle, final BundleEvent event) {
+                if (bundle.getBundleId() == m_bundle.getBundleId()) {
+                    // Not interested in our own bundle
+                    return null;
                 }
+                m_processor.activate(bundle);
+                return bundle;
             }
-        }
+
+            public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Object object) {}
+
+            public void removedBundle(final Bundle bundle, final BundleEvent event, final Object object) {
+                m_processor.deactivate(bundle);
+            }
+        });
+
+        m_tracker.open();
 
         m_logger.log(Logger.INFO, "iPOJO Main Extender started");
     }
@@ -171,7 +186,9 @@
      * @throws Exception something terrible happen
      */
     public void stop(BundleContext context) throws Exception {
-        context.removeBundleListener(this);
+        m_tracker.close();
+
+        m_processor.deactivate(m_bundle);
 
         //Shutdown ConfigurationTracker
         ConfigurationTracker.shutdown();
@@ -190,28 +207,6 @@
     }
 
     /**
-     * A bundle event was caught.
-     *
-     * @param event the event
-     */
-    public void bundleChanged(BundleEvent event) {
-        if (m_bundle.getBundleId() != (event.getBundle().getBundleId())) {
-            // Do not process our-self (already done)
-            switch (event.getType()) {
-                case BundleEvent.STARTED:
-                    // Put the bundle in the queue
-                    m_processor.activate(event.getBundle());
-                    break;
-                case BundleEvent.STOPPING:
-                    m_processor.deactivate(event.getBundle());
-                    break;
-                default:
-                    break;
-            }
-        }
-    }
-
-    /**
      * Gets iPOJO bundle context.
      *
      * @return the iPOJO Bundle Context