FELIX-5194 Actually pay attention to whether change count changes, and insist on R5 ca

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1731776 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/bnd.bnd b/scr/bnd.bnd
index 3795574..132079f 100644
--- a/scr/bnd.bnd
+++ b/scr/bnd.bnd
@@ -30,7 +30,7 @@
 # optional import for Gogo annotations 
 # The Felix Shell support is optional 
 Import-Package: \
- org.osgi.service.cm;version="[1.2,2)";resolution:=optional, \
+ org.osgi.service.cm;version="[1.5,2)";resolution:=optional, \
  org.osgi.service.log;version="[1.3,2)";resolution:=optional, \
  org.osgi.service.metatype;version="[1.1,2)";resolution:=optional, \
  org.osgi.service.packageadmin;version="[1.2,2)";resolution:=optional, \
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java
index 59e0ca5..6f0b03f 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ComponentHolder.java
@@ -76,14 +76,6 @@
     boolean configurationUpdated( TargetedPID targetedPid, TargetedPID factoryTargetedPid, Dictionary<String, Object> props, long changeCount );
     
     /**
-     * Change count (or fake R4 imitation)
-     * @param pid a PID for the component we are interested in.
-     * @param targetedPid For a singleton pid, same as "pid"; for a factory pid, the factory pid.
-     * @return the last change count from a configurationUpdated call for the given pid.
-     */
-    long getChangeCount( TargetedPID pid, TargetedPID targetedPid );
-    
-    /**
      * Returns the targeted PID used to configure this component
      * @param pid a targetedPID containing the service pid for the component desired (the rest of the targeted pid is ignored)
      * @param factoryPid a targetedPID containing the factory pid for the component desired.
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurableComponentHolder.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurableComponentHolder.java
index 9b32df6..c5feb22 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurableComponentHolder.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ConfigurableComponentHolder.java
@@ -351,14 +351,18 @@
         final Map<AbstractComponentManager<S>, Map<String, Object>> scms = new HashMap< AbstractComponentManager<S>, Map<String, Object>>();
         boolean created = false;
 
-        //TODO better change count tracking
         synchronized (m_components) {
             //Find or create the component manager, or return if not satisfied.
             if (factoryPid != null) {
                 checkFactoryPidIndex(factoryPid);
+                Long oldChangeCount = m_factoryChangeCount.get(pid.getServicePid());
+                TargetedPID oldTargetedPID = m_factoryTargetedPids.get(pid.getServicePid());
+                if (oldChangeCount != null && changeCount <= oldChangeCount && factoryPid.equals(oldTargetedPID)) {
+                	return false;
+                }
+                m_factoryChangeCount.put(pid.getServicePid(), changeCount);
                 m_factoryConfigurations.put(pid.getServicePid(), props);
                 m_factoryTargetedPids.put(pid.getServicePid(), factoryPid);
-                m_factoryChangeCount.put(pid.getServicePid(), changeCount);
                 if (m_enabled && isSatisfied()) {
                     if (m_singleComponent != null && !m_componentMetadata.isObsoleteFactoryComponentFactory()) {
                         AbstractComponentManager<S> scm = m_singleComponent;
@@ -380,8 +384,11 @@
             } else {
                 //singleton pid
                 int index = getSingletonPidIndex(pid);
-                m_targetedPids[index] = pid;
+                if (m_changeCount[index] != null && changeCount <= m_changeCount[index] && pid.equals(m_targetedPids[index])) {
+                	return false;
+                }
                 m_changeCount[index] = changeCount;
+                m_targetedPids[index] = pid;
                 m_configurations[index] = props;
                 if (m_enabled && isSatisfied()) {
                     if (m_singleComponent != null) {
@@ -570,31 +577,6 @@
         return true;
     }
 
-    /**
-     * @param pid the Targeted PID we need the change count for
-     * @param targetedPid the targeted factory pid for a factory configuration or the pid for a singleton configuration
-     * @return pid for this service pid.
-     */
-    public long getChangeCount( TargetedPID pid, TargetedPID targetedPid)
-    {
-        int index = m_componentMetadata.getPidIndex(targetedPid);
-        Long result;
-        if ( index == -1 )
-        {
-            throw new IllegalArgumentException("pid not recognized as for this component: " + pid);
-        }
-        if ( m_factoryPidIndex != null && index == m_factoryPidIndex )
-        {
-            result = m_factoryChangeCount.get(pid.getServicePid());
-        }
-        else
-        {
-            result = m_changeCount[index];
-        }
-        return result == null? -1: result;
-
-    }
-
     public List<? extends ComponentManager<?>> getComponents()
         {
         synchronized ( m_components )
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/RegionConfigurationSupport.java b/scr/src/main/java/org/apache/felix/scr/impl/config/RegionConfigurationSupport.java
index 81e101f..60cb475 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/RegionConfigurationSupport.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/RegionConfigurationSupport.java
@@ -49,28 +49,6 @@
 public class RegionConfigurationSupport implements ConfigurationListener
 {
 
-    private static final ChangeCount changeCounter;
-    static
-    {
-        ChangeCount cc = null;
-        try
-        {
-            Configuration.class.getMethod( "getChangeCount", (Class<?>[]) null );
-            cc = new R5ChangeCount();
-        }
-        catch ( SecurityException e )
-        {
-        }
-        catch ( NoSuchMethodException e )
-        {
-        }
-        if ( cc == null )
-        {
-            cc = new R4ChangeCount();
-        }
-        changeCounter = cc;
-    }
-
     private final BundleContext caBundleContext;
     private final Long bundleId;
     
@@ -175,7 +153,7 @@
                                         config = getConfiguration( ca, config.getPid() );
                                         if ( checkBundleLocation( config, bundleContext.getBundle() ) )
                                         {
-                                            long changeCount = changeCounter.getChangeCount( config, false, -1 );
+                                            long changeCount = config.getChangeCount();
                                             created |= holder.configurationUpdated( new TargetedPID( config.getPid() ),
                                             		new TargetedPID( config.getFactoryPid() ),
                                                     config.getProperties(),
@@ -201,7 +179,7 @@
                                         if ( singleton != null
                                                 && checkBundleLocation( singleton, bundleContext.getBundle() ) )
                                         {
-                                            long changeCount = changeCounter.getChangeCount( singleton, false, -1 );
+                                            long changeCount = singleton.getChangeCount();
                                             holder.configurationUpdated( new TargetedPID( singleton.getPid() ), null,
                                                     singleton.getProperties(), changeCount );
                                         }
@@ -504,7 +482,7 @@
                             final ConfigurationAdmin ca = ( ConfigurationAdmin ) cao;
                             final Configuration config = getConfiguration( ca, pid.getRawPid() );
                             return new ConfigurationInfo(config.getProperties(), config.getBundleLocation(),
-                                    changeCounter.getChangeCount( config, true, componentHolder.getChangeCount( pid, targetedPid ) ) );
+                            		config.getChangeCount());
                         }
                         else
                         {
@@ -687,26 +665,4 @@
         return value.replaceAll( "([\\\\\\*\\(\\)])", "\\\\$1" );
     }
 
-
-    private interface ChangeCount {
-        long getChangeCount( Configuration configuration, boolean fromEvent, long previous );
-    }
-
-    private static class R5ChangeCount implements ChangeCount {
-
-        public long getChangeCount(Configuration configuration, boolean fromEvent, long previous)
-        {
-            return configuration.getChangeCount();
-        }
-    }
-
-    private static class R4ChangeCount implements ChangeCount {
-
-        public long getChangeCount(Configuration configuration, boolean fromEvent, long previous)
-        {
-            return fromEvent? previous + 1:0;
-        }
-
-    }
-
 }