FELIX-3680 don't drop service events when enabling

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1397396 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 964c78b..4418807 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
@@ -21,12 +21,14 @@
 
 import java.security.Permission;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
+import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.Reference;
 import org.apache.felix.scr.impl.BundleComponentActivator;
@@ -73,6 +75,8 @@
     // the target service filter
     private volatile Filter m_targetFilter;
 
+    private final Collection<ServiceReference> added = new ArrayList<ServiceReference>();
+
 
     /**
      * Constructor that receives several parameters.
@@ -130,8 +134,17 @@
                     // consider the service if the filter matches
                     if ( targetFilterMatch( ref ) )
                     {
-                        m_size++;
-                        serviceAdded( ref );
+                        added.add( ref );
+                        synchronized (added)
+                        {
+                            //wait for enable to complete
+                        }
+                        if (added.contains( ref ))
+                        {
+                            added.remove( ref );
+                            m_size++;
+                            serviceAdded( ref );
+                        }
                     }
                     else
                     {
@@ -547,13 +560,24 @@
 
         if ( hasGetPermission() )
         {
-            // get the current number of registered services available
-            ServiceReference refs[] = getFrameworkServiceReferences();
-            m_size = ( refs == null ) ? 0 : refs.length;
+            synchronized ( added )
+            {
+                // register the service listener
+                String filterString = "(" + Constants.OBJECTCLASS + "=" + m_dependencyMetadata.getInterface() + ")";
+                m_componentManager.getActivator().getBundleContext().addServiceListener( this, filterString );
 
-            // register the service listener
-            String filterString = "(" + Constants.OBJECTCLASS + "=" + m_dependencyMetadata.getInterface() + ")";
-            m_componentManager.getActivator().getBundleContext().addServiceListener( this, filterString );
+                // get the current number of registered services available
+                ServiceReference refs[] = getFrameworkServiceReferences();
+                if (refs != null)
+                {
+                    for (ServiceReference ref: refs)
+                    {
+                        added.remove( ref );
+                    }
+                }
+                m_size = ( refs == null ) ? 0 : refs.length;
+            }
+
 
             m_componentManager.log( LogService.LOG_DEBUG,
                 "Registered for service events, currently {0} service(s) match the filter", new Object[]
@@ -565,8 +589,8 @@
             m_size = 0;
 
             m_componentManager.log( LogService.LOG_DEBUG,
-                "Not registered for service events since the bundle has no permission to get service {0}", new Object[]
-                    { m_dependencyMetadata.getInterface() }, null );
+                    "Not registered for service events since the bundle has no permission to get service {0}", new Object[]
+                    {m_dependencyMetadata.getInterface()}, null );
         }
     }
 
@@ -1256,7 +1280,7 @@
             // if the component instance has already been cleared by the
             // close() method
             m_componentManager.log( LogService.LOG_DEBUG,
-                "DependencyManager : Component not set, no need to call updated method", null );
+                    "DependencyManager : Component not set, no need to call updated method", null );
         }
     }