FELIX-3651 deal with weaker/stronger pid bindings

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1480106 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurationSupport.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurationSupport.java
index 7f61098..bfa8d4f 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurationSupport.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurationSupport.java
@@ -243,14 +243,13 @@
             {
                 switch (event.getType()) {
                 case ConfigurationEvent.CM_DELETED:
-                    //TODO fall back to less-strong pid match
                     componentHolder.configurationDeleted(pid.getServicePid());
+                    //fall back to less-strong pid match
+                    configureComponentHolder( componentHolder );
                     break;
 
                 case ConfigurationEvent.CM_UPDATED:
                 {
-                    //TODO what if this is a new config with a stronger (or weaker) binding than 
-                    // an existing matching config?
                     final BundleComponentActivator activator = componentHolder.getActivator();
                     if (activator == null)
                     {
@@ -263,13 +262,23 @@
                         break;
                     }
 
-                    final Configuration config = getConfiguration( pid, componentHolder, bundleContext );
-                    if ( checkBundleLocation( config, bundleContext.getBundle() ) )
+                    TargetedPID targetedPid = factoryPid == null? pid: factoryPid;
+                    TargetedPID oldTargetedPID = componentHolder.getConfigurationTargetedPID();
+                    if ( targetedPid.equals(oldTargetedPID) || targetedPid.bindsStronger( oldTargetedPID ))
                     {
-                        long changeCount = changeCounter.getChangeCount( config, true, componentHolder.getChangeCount( pid.getServicePid() ) );
-                        componentHolder.configurationUpdated( pid.getServicePid(), config.getProperties(), changeCount, factoryPid == null? pid: factoryPid );
+                        final Configuration config = getConfiguration( pid, componentHolder, bundleContext );
+                        if ( checkBundleLocation( config, bundleContext.getBundle() ) )
+                        {
+                            //If this is replacing a weaker targetedPID delete the old one.
+                            if ( !targetedPid.equals(oldTargetedPID) && oldTargetedPID != null)
+                            {
+                                componentHolder.configurationDeleted( pid.getServicePid() );
+                            }
+                            long changeCount = changeCounter.getChangeCount( config, true, componentHolder.getChangeCount( pid.getServicePid() ) );
+                            componentHolder.configurationUpdated( pid.getServicePid(), config.getProperties(), changeCount, targetedPid );
+                        }
                     }
-                    
+
                     break;
                 }
                 case ConfigurationEvent.CM_LOCATION_CHANGED:
@@ -287,8 +296,9 @@
                         break;
                     }
 
+                    TargetedPID targetedPid = factoryPid == null? pid: factoryPid;
                     TargetedPID oldTargetedPID = componentHolder.getConfigurationTargetedPID();
-                    if ( pid.equals(oldTargetedPID))
+                    if ( targetedPid.equals(oldTargetedPID))
                     {
                         //this sets the location to this component's bundle if not already set.  OK here
                         //since it used to be set to this bundle, ok to reset it
@@ -305,7 +315,7 @@
                         //else still matches
                         break;
                     }
-                    boolean better = pid.bindsStronger( oldTargetedPID );
+                    boolean better = targetedPid.bindsStronger( oldTargetedPID );
                     if ( better )
                     {
                         //this sets the location to this component's bundle if not already set.  OK here
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 4d4ec8c..f5834c5 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
@@ -546,7 +546,7 @@
 
         // unsatisfied component and non-ignored configuration may change targets
         // to satisfy references
-        if ( getState() == STATE_UNSATISFIED && configuration != null
+        if ( getState() == STATE_UNSATISFIED
                 && !getComponentMetadata().isConfigurationIgnored() )
         {
             log( LogService.LOG_DEBUG, "Attempting to activate unsatisfied component", null );