FELIX-303 applied the suggested fix for getCallbackInstances(), FELIX-1546 added invocation of callbacks for required dependencies (work on this issue is ongoing).

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@880638 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java b/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
index 414659c..f4250cb 100644
--- a/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
+++ b/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceDependency.java
@@ -337,13 +337,14 @@
     public void addedService(ServiceReference ref, Object service) {
         if (makeAvailable()) {
             m_service.dependencyAvailable(this);
+            // try to invoke callback, if specified, but only for optional dependencies
+            // because callbacks for required dependencies are handled differently
+            if (!isRequired()) {
+                invokeAdded(ref, service);
+            }
         }
         else {
             m_service.dependencyChanged(this);
-        }
-        // try to invoke callback, if specified, but only for optional dependencies
-        // because callbacks for required dependencies are handled differently
-        if (!isRequired()) {
             invokeAdded(ref, service);
         }
     }
@@ -355,7 +356,7 @@
     public void invokeAdded(ServiceReference reference, Object serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackAdded != null)) {
-                invokeCallbackMethod(callbackInstances, m_callbackAdded, reference, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackAdded, reference, serviceInstance);
         }
     }
 
@@ -372,20 +373,21 @@
     public void invokeChanged(ServiceReference reference, Object serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackChanged != null)) {
-                if (m_reference == null) {
-                    Thread.dumpStack();
-                }
-                invokeCallbackMethod(callbackInstances, m_callbackChanged, reference, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackChanged, reference, serviceInstance);
         }
     }
 
     public void removedService(ServiceReference ref, Object service) {
         if (makeUnavailable()) {
             m_service.dependencyUnavailable(this);
+            // try to invoke callback, if specified, but only for optional dependencies
+            // because callbacks for required dependencies are handled differently
+            if (!isRequired()) {
+                invokeRemoved(ref, service);
+            }
         }
-        // try to invoke callback, if specified, but only for optional dependencies
-        // because callbacks for required dependencies are handled differently
-        if (!isRequired()) {
+        else {
+            m_service.dependencyChanged(this);
             invokeRemoved(ref, service);
         }
         
@@ -400,10 +402,7 @@
     public void invokeRemoved(ServiceReference reference, Object serviceInstance) {
         Object[] callbackInstances = getCallbackInstances();
         if ((callbackInstances != null) && (m_callbackRemoved != null)) {
-                if (m_reference == null) {
-                    Thread.dumpStack();
-                }
-                invokeCallbackMethod(callbackInstances, m_callbackRemoved, reference, serviceInstance);
+            invokeCallbackMethod(callbackInstances, m_callbackRemoved, reference, serviceInstance);
         }
     }
     
@@ -424,14 +423,7 @@
     }
     
     private synchronized Object[] getCallbackInstances() {
-        Object[] callbackInstances = ((ServiceImpl) m_service).getCompositionInstances();
-        if (m_callbackInstance == null) {
-            return callbackInstances;
-        }
-        Object[] res = new Object[callbackInstances.length + 1];
-        res[0] = m_callbackInstance; //this could also be extended to an array...?
-        System.arraycopy(callbackInstances, 0, res, 1, callbackInstances.length);
-        return res;
+        return m_callbackInstance != null ? new Object[] { m_callbackInstance } : ((ServiceImpl) m_service).getCompositionInstances();
     }
 
     private void invokeCallbackMethod(Object[] instances, String methodName, ServiceReference reference, Object service) {
diff --git a/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java b/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
index 0e00202..0ff8473 100644
--- a/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
+++ b/dependencymanager/src/main/java/org/apache/felix/dependencymanager/ServiceRegistrationImpl.java
@@ -31,7 +31,7 @@
  */
 public final class ServiceRegistrationImpl implements ServiceRegistration {
     public static final ServiceRegistrationImpl ILLEGAL_STATE = new ServiceRegistrationImpl();
-    private ServiceRegistration m_registration;
+    private volatile ServiceRegistration m_registration;
 
     public ServiceRegistrationImpl() {
         m_registration = null;