[FELIX-2970] - Factory Configuration Adapter Service does not copy adapter dependencies.


git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1126961 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
index 31deeba..0f1ca5e 100644
--- a/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
+++ b/dependencymanager/core/src/main/java/org/apache/felix/dm/impl/FactoryConfigurationAdapterServiceImpl.java
@@ -27,6 +27,7 @@
 
 import org.apache.felix.dm.Component;
 import org.apache.felix.dm.ComponentStateListener;
+import org.apache.felix.dm.Dependency;
 import org.apache.felix.dm.DependencyManager;
 import org.apache.felix.dm.InvocationUtil;
 import org.apache.felix.dm.PropertyMetaData;
@@ -126,17 +127,22 @@
             }
 
             // Merge adapter service properties, with CM settings 
-            Dictionary serviceProperties = m_propagate ? mergeSettings(m_serviceProperties, settings) : m_serviceProperties;
+            Dictionary serviceProperties = getServiceProperties(settings);
             newService.setInterface(m_serviceInterfaces, serviceProperties);
             newService.setImplementation(impl);
-            List dependencies = m_component.getDependencies();
-            newService.add(dependencies); // TODO check if we should create a copy of dependencies ?
             newService.setComposition(m_compositionInstance, m_compositionMethod); // if not set, no effect
             newService.setCallbacks(m_callbackObject, m_init, m_start, m_stop, m_destroy); // if not set, no effect
+            configureAutoConfigState(newService, m_component);
+            
+            List dependencies = m_component.getDependencies();
+            for (int i = 0; i < dependencies.size(); i++) {
+                newService.add(((Dependency) dependencies.get(i)).createCopy());
+            }
+            
             for (int i = 0; i < m_stateListeners.size(); i ++) {
                 newService.addStateListener((ComponentStateListener) m_stateListeners.get(i));
             }
-            configureAutoConfigState(newService, m_component);
+            
             return newService;
         }
 
@@ -145,17 +151,17 @@
          * the configuration has changed.
          */
         public void updateService(Object[] properties) {
-            Dictionary settings = (Dictionary) properties[0];
+            Dictionary cmSettings = (Dictionary) properties[0];
             Component service = (Component) properties[1];
             Object impl = service.getService();
            
             try {
                 InvocationUtil.invokeCallbackMethod(impl, m_update, 
                     new Class[][] {{ Dictionary.class }, {}}, 
-                    new Object[][] {{ settings }, {}});
+                    new Object[][] {{ cmSettings }, {}});
                 if (m_serviceInterfaces != null && m_propagate == true) {
-                    settings = mergeSettings(m_serviceProperties, settings);
-                    service.setServiceProperties(settings);
+                    Dictionary serviceProperties = getServiceProperties(cmSettings);
+                    service.setServiceProperties(serviceProperties);
                 }
             }
             
@@ -172,27 +178,34 @@
          * @param settings
          * @return
          */
-        private Dictionary mergeSettings(Dictionary adapterProperties, Dictionary settings) {
+        private Dictionary getServiceProperties(Dictionary settings) {
             Dictionary props = new Hashtable();
             
-            if (adapterProperties != null) {
-                Enumeration keys = adapterProperties.keys();
+            // Add adapter Service Properties
+            if (m_serviceProperties != null) {
+                Enumeration keys = m_serviceProperties.keys();
                 while (keys.hasMoreElements()) {
                     Object key = keys.nextElement();
-                    Object val = adapterProperties.get(key);
+                    Object val = m_serviceProperties.get(key);
                     props.put(key, val);
                 }
             }
+
+            if (m_propagate) {
+                // Add CM setting into adapter service properties.
+                // (CM setting will override existing adapter service properties).
+                Enumeration keys = settings.keys();
+                while (keys.hasMoreElements()) {
+                    Object key = keys.nextElement();
+                    if (! key.toString().startsWith(".")) {
+                        // public properties are propagated
+                        Object val = settings.get(key);
+                        props.put(key, val);
+                    }
+                }
+            }
+
             
-            Enumeration keys = settings.keys();
-            while (keys.hasMoreElements()) {
-                Object key = keys.nextElement();
-                if (! key.toString().startsWith(".")) {
-                    // public properties are propagated
-                    Object val = settings.get(key);
-                    props.put(key, val);
-                }
-            }
             return props;
         }
     
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceClient.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceClient.java
index ed9ea91..8bafe0f 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceClient.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceClient.java
@@ -27,12 +27,30 @@
 public class ServiceClient
 {
     @ServiceDependency(changed="changeServiceProvider")
-    void addServiceProvider(ServiceInterface si) {
+    void addServiceProvider(Map props, ServiceInterface si) {
+        // props should contain foo=bar, foo2=bar2
+        if (! "bar".equals(props.get("foo")))
+        {
+            throw new IllegalArgumentException("configuration does not contain foo=bar: " + props);
+        }
+        if (! "bar2".equals(props.get("foo2")))
+        {
+            throw new IllegalArgumentException("configuration does not contain foo2=bar2: " + props);
+        }
         si.doService();
     }
     
-    void changeServiceProvider(Map serviceProperties, ServiceInterface si) 
+    void changeServiceProvider(Map props, ServiceInterface si) 
     {
+        // props should contain foo=bar, foo2=bar2_modified
+        if (! "bar".equals(props.get("foo")))
+        {
+            throw new IllegalArgumentException("configuration does not contain foo=bar: " + props);
+        }
+        if (! "bar2_modified".equals(props.get("foo2")))
+        {
+            throw new IllegalArgumentException("configuration does not contain foo2=bar2: " + props);
+        }
         si.doService();
     }
 }
diff --git a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceProvider.java b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceProvider.java
index f593e5a..73e0b58 100644
--- a/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceProvider.java
+++ b/dependencymanager/test/src/main/java/org/apache/felix/dm/test/bundle/annotation/factoryconfadapter/ServiceProvider.java
@@ -60,8 +60,21 @@
     {
         if (m_started)
         {
+            // conf should contain foo2=bar2_modified
+            if (! "bar2_modified".equals(conf.get("foo2")))
+            {
+                m_sequencer.throwable(new Exception("configuration does not contain foo=bar"));
+            }
             m_sequencer.step(4);
         }
+        else
+        {
+            // conf should contain foo2=bar2
+            if (! "bar2".equals(conf.get("foo2")))
+            {
+                throw new IllegalArgumentException("configuration does not contain foo2=bar2: " + conf);
+            }
+        }
     }
 
     @Start
diff --git a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/FactoryConfigurationAdapterAnnotationTest.java b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/FactoryConfigurationAdapterAnnotationTest.java
index 38d1dd4..0165434 100644
--- a/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/FactoryConfigurationAdapterAnnotationTest.java
+++ b/dependencymanager/test/src/test/java/org/apache/felix/dm/test/annotation/FactoryConfigurationAdapterAnnotationTest.java
@@ -79,11 +79,11 @@
         {
             // Create a factory configuration in order to instantiate the ServiceProvider
             org.osgi.service.cm.Configuration cf = cm.createFactoryConfiguration("FactoryPidTest", null);
-            cf.update(new Hashtable() {{ put("foo", "bar"); }});
+            cf.update(new Hashtable() {{ put("foo2", "bar2"); }});
             // Wait for the ServiceProvider activation.
             m_ensure.waitForStep(2, 10000);
             // Update conf
-            cf.update(new Hashtable() {{ put("foo", "bar2"); }});            
+            cf.update(new Hashtable() {{ put("foo2", "bar2_modified"); }});            
             // Wait for effective update
             m_ensure.waitForStep(4, 10000);
             // Remove configuration.