FELIX-2778 (DM compat layer does not cleanup left-over Service instances when bundle is stopped)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1058320 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java b/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
index ce64c15..27f942b 100644
--- a/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
+++ b/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/DependencyActivatorBase.java
@@ -20,6 +20,7 @@
 
 import java.util.List;
 
+import org.apache.felix.dependencymanager.impl.ServiceImpl;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 
@@ -82,6 +83,7 @@
      */
     public void stop(BundleContext context) throws Exception {
         destroy(m_context, m_manager);
+        cleanup(m_manager);
         m_manager = null;
         m_context = null;
     }
@@ -112,4 +114,22 @@
     public ConfigurationDependency createConfigurationDependency() {
     	return new org.apache.felix.dependencymanager.impl.ConfigurationDependencyImpl(m_manager);
     }
+    
+    /**
+     * Cleans up all components and their dependencies.
+     * 
+     * @param manager the dependency manager
+     */
+    private void cleanup(DependencyManager manager) {
+        List services = manager.getServices();
+        for (int i = services.size() - 1; i >= 0; i--) {
+            Service service = (Service) services.get(i);
+            manager.remove(service);
+            // remove any state listeners that are still registered
+            if (service instanceof ServiceImpl) {
+                ServiceImpl si = (ServiceImpl) service;
+                si.removeStateListeners();
+            }
+        }
+    }
 }
diff --git a/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/impl/ServiceImpl.java b/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/impl/ServiceImpl.java
index 940f4bb..e2c3fc7 100644
--- a/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/impl/ServiceImpl.java
+++ b/dependencymanager/compat/src/main/java/org/apache/felix/dependencymanager/impl/ServiceImpl.java
@@ -21,6 +21,7 @@
 import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -134,6 +135,21 @@
             m_delegate.removeStateListener(wrappedListener);
         }
     }
+    
+    public void removeStateListeners()
+    {
+        synchronized (m_stateListeners)
+        {
+        	Iterator it = m_stateListeners.values().iterator();
+        	while (it.hasNext())
+        	{
+        		org.apache.felix.dm.ComponentStateListener wrappedListener =
+        			(org.apache.felix.dm.ComponentStateListener) it.next();
+        		m_delegate.removeStateListener(wrappedListener);
+        	}
+        	m_stateListeners.clear();
+        }
+    }
 
     public List getDependencies()
     {