You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by fm...@apache.org on 2011/02/03 21:31:10 UTC

svn commit: r1066946 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/manager/ test/java/org/apache/felix/scr/integration/ test/java/org/apache/felix/scr/integration/components/ test/resources/

Author: fmeschbe
Date: Thu Feb  3 20:31:09 2011
New Revision: 1066946

URL: http://svn.apache.org/viewvc?rev=1066946&view=rev
Log:
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.

Added:
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java   (with props)
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java   (with props)
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java   (with props)
Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java
    felix/trunk/scr/src/test/resources/integration_test_simple_components_service_binding.xml

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java?rev=1066946&r1=1066945&r2=1066946&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java Thu Feb  3 20:31:09 2011
@@ -21,12 +21,12 @@ package org.apache.felix.scr.impl.manage
 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 @@ public abstract class AbstractComponentM
         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();

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java?rev=1066946&r1=1066945&r2=1066946&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java Thu Feb  3 20:31:09 2011
@@ -204,7 +204,7 @@ public class ImmediateComponentManager e
                         { dm.getName() }, null );
 
                 // make sure, we keep no bindings
-                it = getDependencyManagers();
+                it = getReversedDependencyManagers();
                 while ( it.hasNext() )
                 {
                     dm = ( DependencyManager ) it.next();
@@ -261,8 +261,7 @@ public class ImmediateComponentManager e
             reason ), true );
 
         // 2. Unbind any bound services
-        Iterator it = getDependencyManagers();
-
+        Iterator it = getReversedDependencyManagers();
         while ( it.hasNext() )
         {
             DependencyManager dm = ( DependencyManager ) it.next();

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java?rev=1066946&r1=1066945&r2=1066946&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/ServiceBindTest.java Thu Feb  3 20:31:09 2011
@@ -20,11 +20,12 @@ package org.apache.felix.scr.integration
 
 
 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 @@ public class ServiceBindTest extends Com
         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

Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java?rev=1066946&view=auto
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java (added)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java Thu Feb  3 20:31:09 2011
@@ -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" );
+    }
+}

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleComponent2.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java?rev=1066946&view=auto
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java (added)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java Thu Feb  3 20:31:09 2011
@@ -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();
+}

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Added: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java?rev=1066946&view=auto
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java (added)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java Thu Feb  3 20:31:09 2011
@@ -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;
+    }
+}

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/SimpleService2Impl.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url

Modified: felix/trunk/scr/src/test/resources/integration_test_simple_components_service_binding.xml
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/resources/integration_test_simple_components_service_binding.xml?rev=1066946&r1=1066945&r2=1066946&view=diff
==============================================================================
--- felix/trunk/scr/src/test/resources/integration_test_simple_components_service_binding.xml (original)
+++ felix/trunk/scr/src/test/resources/integration_test_simple_components_service_binding.xml Thu Feb  3 20:31:09 2011
@@ -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>