FELIX-2821 Apply patch by Stephen Flynn (thanks alot). Basically when closing the DependencyManagers, the manager list is copied, reversed and this reversed copy is iterated. And added an integration test to prove correctness.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1066946 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
index 4c6e2d8..cac636b 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
@@ -21,12 +21,12 @@
 import java.security.Permission;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
-
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.Reference;
 import org.apache.felix.scr.impl.BundleComponentActivator;
@@ -634,11 +634,27 @@
         return satisfied;
     }
 
+    /**
+     * Returns an iterator over the {@link DependencyManager} objects
+     * representing the declared references in declaration order
+     */
     Iterator getDependencyManagers()
     {
         return m_dependencyManagers.iterator();
     }
 
+    /**
+     * Returns an iterator over the {@link DependencyManager} objects
+     * representing the declared references in reversed declaration order
+     */
+    Iterator getReversedDependencyManagers()
+    {
+        List list = new ArrayList( m_dependencyManagers );
+        Collections.reverse( list );
+        return list.iterator();
+    }
+
+
     DependencyManager getDependencyManager(String name)
     {
         Iterator it = getDependencyManagers();
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 6c4ad20..f15114d 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
@@ -204,7 +204,7 @@
                         { dm.getName() }, null );
 
                 // make sure, we keep no bindings
-                it = getDependencyManagers();
+                it = getReversedDependencyManagers();
                 while ( it.hasNext() )
                 {
                     dm = ( DependencyManager ) it.next();
@@ -261,8 +261,7 @@
             reason ), true );
 
         // 2. Unbind any bound services
-        Iterator it = getDependencyManagers();
-
+        Iterator it = getReversedDependencyManagers();
         while ( it.hasNext() )
         {
             DependencyManager dm = ( DependencyManager ) it.next();
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java b/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java
index 5c8ae65..8c1d3b6 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java
@@ -20,11 +20,12 @@
 
 
 import java.util.Hashtable;
-
 import junit.framework.TestCase;
 
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.integration.components.SimpleComponent;
+import org.apache.felix.scr.integration.components.SimpleComponent2;
+import org.apache.felix.scr.integration.components.SimpleService2Impl;
 import org.apache.felix.scr.integration.components.SimpleServiceImpl;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -1227,4 +1228,38 @@
         TestCase.assertFalse( comp32.m_multiRef.contains( srv6 ) );
         TestCase.assertTrue( comp32.m_multiRef.contains( srv7 ) );
     }
+
+    @Test
+    public void test_multi_service_bind_unbind_order()
+    {
+        final Component component = findComponentByName( "test_multi_service_bind_unbind_order" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_DISABLED, component.getState() );
+
+        final SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+        final SimpleService2Impl srv2 = SimpleService2Impl.create( bundleContext, "srv2" );
+
+        // async enabling
+        component.enable();
+        delay();
+
+        TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+        final SimpleComponent2 comp10 = SimpleComponent2.INSTANCE;
+        TestCase.assertNotNull( comp10 );
+        TestCase.assertEquals( 2, comp10.getBindings().size() );
+        TestCase.assertEquals( "bindSimpleService", comp10.getBindings().get( 0 ) );
+        TestCase.assertEquals( "bindSimpleService2", comp10.getBindings().get( 1 ) );
+
+        component.disable();
+        delay();
+
+        TestCase.assertEquals( 4, comp10.getBindings().size() );
+        TestCase.assertEquals( "bindSimpleService", comp10.getBindings().get( 0 ) );
+        TestCase.assertEquals( "bindSimpleService2", comp10.getBindings().get( 1 ) );
+        TestCase.assertEquals( "unbindSimpleService2", comp10.getBindings().get( 2 ) );
+        TestCase.assertEquals( "unbindSimpleService", comp10.getBindings().get( 3 ) );
+
+        srv1.drop();
+        srv2.drop();
+    }
 }
\ No newline at end of file
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java
new file mode 100644
index 0000000..7c29ad5
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration.components;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class SimpleComponent2
+{
+
+    public static SimpleComponent2 INSTANCE;
+
+    private List<String> bindings = new ArrayList<String>();
+
+
+    public List<String> getBindings()
+    {
+        return bindings;
+    }
+
+
+    @SuppressWarnings("unused")
+    private void activate()
+    {
+        INSTANCE = this;
+    }
+
+
+    @SuppressWarnings("unused")
+    private void deactivate()
+    {
+        INSTANCE = null;
+    }
+
+
+    public void bindSimpleService( @SuppressWarnings("unused") SimpleService simpleService )
+    {
+        bindings.add( "bindSimpleService" );
+    }
+
+
+    public void unbindSimpleService( @SuppressWarnings("unused") SimpleService simpleService )
+    {
+        bindings.add( "unbindSimpleService" );
+    }
+
+
+    public void bindSimpleService2( @SuppressWarnings("unused") SimpleService2 simpleService2 )
+    {
+        bindings.add( "bindSimpleService2" );
+    }
+
+
+    public void unbindSimpleService2( @SuppressWarnings("unused") SimpleService2 simpleService2 )
+    {
+        bindings.add( "unbindSimpleService2" );
+    }
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java
new file mode 100644
index 0000000..b79c7db
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration.components;
+
+
+public interface SimpleService2
+{
+
+    String getValue2();
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java
new file mode 100644
index 0000000..3f64104
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration.components;
+
+
+import java.util.Properties;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
+
+
+public class SimpleService2Impl implements SimpleService2
+{
+
+    private String m_value;
+
+    private int m_ranking;
+
+    private String m_filterProp;
+
+    private ServiceRegistration m_registration;
+
+
+    public static SimpleService2Impl create( BundleContext bundleContext, String value )
+    {
+        return create( bundleContext, value, 0 );
+    }
+
+
+    public static SimpleService2Impl create( BundleContext bundleContext, String value, int ranking )
+    {
+        SimpleService2Impl instance = new SimpleService2Impl( value, ranking );
+        Properties props = instance.getProperties();
+        instance.setRegistration( bundleContext.registerService( SimpleService2.class.getName(), instance, props ) );
+        return instance;
+    }
+
+
+    SimpleService2Impl( final String value, final int ranking )
+    {
+        this.m_value = value;
+        this.m_ranking = ranking;
+        this.m_filterProp = "match";
+    }
+
+
+    private Properties getProperties()
+    {
+        final Properties props = new Properties();
+        props.put( "value", m_value );
+        props.put( "filterprop", m_filterProp );
+        if ( m_ranking != 0 )
+        {
+            props.put( Constants.SERVICE_RANKING, Integer.valueOf( m_ranking ) );
+        }
+        return props;
+    }
+
+
+    public void update( String value )
+    {
+        if ( this.m_registration != null )
+        {
+            this.m_value = value;
+            this.m_registration.setProperties( getProperties() );
+        }
+    }
+
+
+    public void setFilterProperty( String filterProp )
+    {
+        if ( this.m_registration != null )
+        {
+            this.m_filterProp = filterProp;
+            this.m_registration.setProperties( getProperties() );
+        }
+    }
+
+
+    public void drop()
+    {
+        ServiceRegistration sr = getRegistration();
+        if ( sr != null )
+        {
+            setRegistration( null );
+            sr.unregister();
+        }
+    }
+
+
+    public String getValue2()
+    {
+        return m_value;
+    }
+
+
+    public void setRegistration( ServiceRegistration registration )
+    {
+        m_registration = registration;
+    }
+
+
+    public ServiceRegistration getRegistration()
+    {
+        return m_registration;
+    }
+
+
+    @Override
+    public String toString()
+    {
+        return getClass().getSimpleName() + ": value=" + getValue2() + ", filterprop=" + m_filterProp;
+    }
+}
diff --git a/scr/src/test/resources/integration_test_simple_components_service_binding.xml b/scr/src/test/resources/integration_test_simple_components_service_binding.xml
index afba9e1..c8804cc 100644
--- a/scr/src/test/resources/integration_test_simple_components_service_binding.xml
+++ b/scr/src/test/resources/integration_test_simple_components_service_binding.xml
@@ -269,4 +269,26 @@
         />
     </scr:component>
 
+    <scr:component name="test_multi_service_bind_unbind_order"
+        enabled="false"
+        configuration-policy="ignore">
+        <implementation class="org.apache.felix.scr.integration.components.SimpleComponent2" />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService"
+            cardinality="0..n"
+            policy="static"
+            bind="bindSimpleService"
+            unbind="unbindSimpleService"
+        />
+        <reference
+            name="ref"
+            interface="org.apache.felix.scr.integration.components.SimpleService2"
+            cardinality="0..n"
+            policy="static"
+            bind="bindSimpleService2"
+            unbind="unbindSimpleService2"
+        />
+    </scr:component>
+
 </components>