Work in progress, internal refactorings and bugfixes.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1035252 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/Component.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/Component.java
index 24cf9ce..81b0f0f 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/Component.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/Component.java
@@ -255,6 +255,8 @@
 	 */
 	public Object[] getCompositionInstances();
 	
+    public void invokeCallbackMethod(Object[] instances, String methodName, Class[][] signatures, Object[][] parameters);
+	
 	/**
 	 * Returns the dependency manager associated with this component.
 	 */
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/InvocationUtil.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/InvocationUtil.java
similarity index 98%
rename from dependencymanager/core/src/main/java/org/apache/felix/dm/impl/InvocationUtil.java
rename to dependencymanager/core/src/main/java/org/apache/felix/dm/InvocationUtil.java
index 73a5bfb..64b54c1 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/InvocationUtil.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/InvocationUtil.java
@@ -1,4 +1,4 @@
-package org.apache.felix.dm.impl;
+package org.apache.felix.dm;
 
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ComponentImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ComponentImpl.java
index 8ebecbb..b1bb057 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ComponentImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/ComponentImpl.java
@@ -40,6 +40,7 @@
 import org.apache.felix.dm.DependencyActivation;
 import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.DependencyService;
+import org.apache.felix.dm.InvocationUtil;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
 
@@ -484,7 +485,7 @@
 	}
 
 	public String toString() {
-	    return "ServiceImpl[" + m_serviceName + " " + m_implementation + "]";
+	    return this.getClass().getSimpleName() + "[" + m_serviceName + " " + m_implementation + "]";
 	}
 
 	public synchronized Dictionary getServiceProperties() {
@@ -1007,10 +1008,12 @@
     }
 
     private void unconfigureServices(State state) {
+        System.err.println("unconfigureServices " + state);
         Iterator i = state.getDependencies().iterator();
         while (i.hasNext()) {
             Dependency dependency = (Dependency) i.next();
             if (dependency.isRequired()) {
+                System.err.println("unconfigureServices invokeremoved " + dependency);
                 dependency.invokeRemoved(this);
             }
 //            if (dependency instanceof ServiceDependencyImpl) {
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
index f1f5b3e..972aa25 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
@@ -26,6 +26,7 @@
 import java.util.List;
 
 import org.apache.felix.dm.DependencyManager;
+import org.apache.felix.dm.InvocationUtil;
 import org.apache.felix.dm.PropertyMetaData;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentStateListener;
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FilterService.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FilterService.java
index 5b4f900..b3a14ac 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FilterService.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FilterService.java
@@ -23,10 +23,10 @@
 import java.util.Iterator;
 import java.util.List;
 
-import org.apache.felix.dm.Dependency;
-import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentStateListener;
+import org.apache.felix.dm.Dependency;
+import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.ServiceRegistration;
 
 /**
@@ -253,6 +253,10 @@
         m_service.stop();
     }
     
+    public void invokeCallbackMethod(Object[] instances, String methodName, Class[][] signatures, Object[][] parameters) {
+        m_service.invokeCallbackMethod(instances, methodName, signatures, parameters);
+    }
+    
     public Object[] getCompositionInstances() {
         return m_service.getCompositionInstances();
     }
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/BundleDependencyImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/BundleDependencyImpl.java
index ff09fc8..07d3d74 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/BundleDependencyImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/BundleDependencyImpl.java
@@ -28,8 +28,8 @@
 import org.apache.felix.dm.Dependency;
 import org.apache.felix.dm.DependencyService;
 import org.apache.felix.dm.ComponentDependencyDeclaration;
+import org.apache.felix.dm.InvocationUtil;
 import org.apache.felix.dm.impl.DefaultNullObject;
-import org.apache.felix.dm.impl.InvocationUtil;
 import org.apache.felix.dm.impl.Logger;
 import org.apache.felix.dm.tracker.BundleTracker;
 import org.apache.felix.dm.tracker.BundleTrackerCustomizer;
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
index 487bcc9..4a652cb 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ResourceDependencyImpl.java
@@ -28,11 +28,11 @@
 import org.apache.felix.dm.Dependency;
 import org.apache.felix.dm.DependencyActivation;
 import org.apache.felix.dm.DependencyService;
+import org.apache.felix.dm.InvocationUtil;
 import org.apache.felix.dm.ResourceDependency;
 import org.apache.felix.dm.ResourceHandler;
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentDependencyDeclaration;
-import org.apache.felix.dm.impl.InvocationUtil;
 import org.apache.felix.dm.impl.Logger;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
index dc7474b..e1e471b 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/dependencies/ServiceDependencyImpl.java
@@ -31,12 +31,14 @@
 import java.util.Properties;
 import java.util.Set;
 
+import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentDependencyDeclaration;
 import org.apache.felix.dm.Dependency;
 import org.apache.felix.dm.DependencyService;
+import org.apache.felix.dm.InvocationUtil;
 import org.apache.felix.dm.ServiceDependency;
+import org.apache.felix.dm.ServiceUtil;
 import org.apache.felix.dm.impl.DefaultNullObject;
-import org.apache.felix.dm.impl.InvocationUtil;
 import org.apache.felix.dm.impl.Logger;
 import org.apache.felix.dm.tracker.ServiceTracker;
 import org.apache.felix.dm.tracker.ServiceTrackerCustomizer;
@@ -463,6 +465,8 @@
     public void removedService(ServiceReference ref, Object service) {
         boolean makeUnavailable = makeUnavailable();
         
+        System.out.println("removedService: " + makeUnavailable + " for " + ServiceUtil.toString(ref));
+        
         Object[] services;
         synchronized (this) {
             services = m_services.toArray();
@@ -488,6 +492,8 @@
 
     public void invokeAdded(DependencyService dependencyService, ServiceReference reference, Object service) {
         invoke(dependencyService, reference, service, m_callbackAdded);
+        //marrs
+        m_refs.add(reference);
     }
     
     public void invokeChanged(DependencyService dependencyService, ServiceReference reference, Object service) {
@@ -496,13 +502,21 @@
 
     public void invokeRemoved(DependencyService dependencyService, ServiceReference reference, Object service) {
         invoke(dependencyService, reference, service, m_callbackRemoved);
+        //marrs
+        m_refs.remove(reference);
     }
 
     public void invoke(DependencyService dependencyService, ServiceReference reference, Object service, String name) {
         if (name != null) {
             dependencyService.invokeCallbackMethod(getCallbackInstances(dependencyService), name, 
-                new Class[][] {{ServiceReference.class, m_trackedServiceName}, {ServiceReference.class, Object.class}, {ServiceReference.class}, {m_trackedServiceName}, {Object.class}, {}, {Map.class, m_trackedServiceName}},
-                new Object[][] {{reference, service}, {reference, service}, {reference}, {service}, {service}, {}, {new ServicePropertiesMap(reference), service}}    
+                new Class[][] {
+                    {Component.class, ServiceReference.class, m_trackedServiceName}, {Component.class, ServiceReference.class, Object.class}, {Component.class, ServiceReference.class}, {Component.class, m_trackedServiceName}, {Component.class, Object.class}, {Component.class}, {Component.class, Map.class, m_trackedServiceName},
+                    {ServiceReference.class, m_trackedServiceName}, {ServiceReference.class, Object.class}, {ServiceReference.class}, {m_trackedServiceName}, {Object.class}, {}, {Map.class, m_trackedServiceName}
+                },
+                new Object[][] {
+                    {dependencyService, reference, service}, {dependencyService, reference, service}, {dependencyService, reference}, {dependencyService, service}, {dependencyService, service}, {dependencyService}, {dependencyService, new ServicePropertiesMap(reference), service},
+                    {reference, service}, {reference, service}, {reference}, {service}, {service}, {}, {new ServicePropertiesMap(reference), service}
+                }    
             );
         }
     }
@@ -795,28 +809,48 @@
         return "service";
     }
 
+    private List m_refs = new ArrayList();
+    
     public void invokeAdded(DependencyService service) {
         ServiceReference[] refs = m_tracker.getServiceReferences();
         if (refs != null) {
             for (int i = 0; i < refs.length; i++) {
                 ServiceReference sr = refs[i];
                 Object svc = m_context.getService(sr);
+                System.out.println("invokeAdded " + i + " " + ServiceUtil.toString(sr));
                 invokeAdded(service, sr, svc);
+                
+                //marrs
+                m_refs.add(sr);
             }
         }
         m_references = refs;
+        
     }
     
     public void invokeRemoved(DependencyService service) {
         ServiceReference[] refs = m_references;
+        
+        
         if (refs != null) {
+            //marrs
+            refs = (ServiceReference[]) m_refs.toArray(refs);
+            
             for (int i = 0; i < refs.length; i++) {
                 ServiceReference sr = refs[i];
                 Object svc = m_context.getService(sr);
+                System.out.println("invokeRemoved " + i + " " + ServiceUtil.toString(sr));
+                if (sr.getBundle() == null) {
+                    System.out.println("invokeRemoved OLD SHIT .. SKIPPING .. not");
+//                    break;
+                }
                 invokeRemoved(service, sr, svc);
             }
         }
         m_references = null;
+        
+        //marrs
+        m_refs.clear();
     }
 
     public Dictionary getProperties() {
@@ -867,4 +901,6 @@
         m_propagateCallbackMethod = method;
         return this;
     }
+    
+    // TODO add equals and hashCode methods, so you can compare dependencies
 }