FELIX-3790 shorten window when service events are dropped

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1415462 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
index ed5c445..d552f68 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
@@ -1215,38 +1215,31 @@
     boolean invokeBindMethod( S componentInstance, ServiceReference<T> ref )
     {
         //event driven, and we already checked this ref is not yet handled.
-        if ( componentInstance != null )
+        Map<DependencyManager<S, ?>, Map<ServiceReference<?>, RefPair<?>>> dependencyMap = m_componentManager.getDependencyMap();
+        if ( dependencyMap != null )
         {
-            Map<DependencyManager<S, ?>, Map<ServiceReference<?>, RefPair<?>>> dependencyMap = m_componentManager.getDependencyMap();
-            if ( dependencyMap != null )
+            if ( m_bindMethods == null )
             {
-                if (m_bindMethods == null)
-                {
-                    m_componentManager.log( LogService.LOG_ERROR,
+                m_componentManager.log( LogService.LOG_ERROR,
                         "For dependency {0}, bind method not set: component state {1}",
                         new Object[]
-                            { m_dependencyMetadata.getName(), new Integer(m_componentManager.getState())  }, null );
+                                {m_dependencyMetadata.getName(), new Integer( m_componentManager.getState() )}, null );
 
-                }
-                Map<ServiceReference<T>, RefPair<T>> deps = (Map)dependencyMap.get( this );
-                RefPair<T> refPair = new RefPair<T>( ref );
-                if ( !m_bindMethods.getBind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ) )
-                {
-                    //reference deactivated while we are processing.
-                    return false;
-                }
-                deps.put( ref, refPair );
-                return invokeBindMethod( componentInstance, refPair );
             }
-            return false;
+            Map<ServiceReference<T>, RefPair<T>> deps = (Map)dependencyMap.get( this );
+            RefPair<T> refPair = new RefPair<T>( ref );
+            if ( !m_bindMethods.getBind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ) )
+            {
+                //reference deactivated while we are processing.
+                return false;
+            }
+            synchronized ( deps )
+            {
+                deps.put( ref, refPair );
+            }
+            return invokeBindMethod( componentInstance, refPair );
         }
-        else
-        {
-            m_componentManager.log( LogService.LOG_DEBUG,
-                "DependencyManager : component not yet created, assuming bind method call succeeded",
-                null );
-            return true;
-        }
+        return false;
     }
 
     public void invokeBindMethodLate( final ServiceReference<T> ref )
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
index 222762e..4e6de52 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
@@ -259,7 +259,10 @@
             return null;
         }
 
-        // 3. Bind the target services
+        // 3. set the implementation object prematurely
+        setter.presetImplementationObject( implementationObject );
+
+        // 4. Bind the target services
         Map<DependencyManager<S, ?>, Map<ServiceReference<?>, RefPair<?>>> parameters = getDependencyMap();
 
         for ( DependencyManager<S, ?> dm: getDependencyManagers())
@@ -268,7 +271,12 @@
             // creating the instance fails here, so we deactivate and return
             // null.
             Map<ServiceReference<?>, RefPair<?>> params = parameters.get( dm );
-            if ( !dm.open( implementationObject, params ) )
+            boolean open;
+            synchronized ( params )
+            {
+                open = dm.open( implementationObject, params );
+            }
+            if ( !open )
             {
                 log( LogService.LOG_ERROR, "Cannot create component instance due to failure to bind reference {0}",
                         new Object[]
@@ -284,9 +292,6 @@
             }
         }
 
-        // 4. set the implementation object prematurely
-        setter.presetImplementationObject( implementationObject );
-
         // 5. Call the activate method, if present
         final MethodResult result = getComponentMethods().getActivateMethod().invoke( implementationObject, new ActivatorParameter(
                 componentContext, 1 ), null );