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 2012/05/12 05:24:27 UTC

svn commit: r1337437 - in /felix/trunk/scr/src: main/java/org/apache/felix/scr/impl/helper/ 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: Sat May 12 03:24:26 2012
New Revision: 1337437

URL: http://svn.apache.org/viewvc?rev=1337437&view=rev
Log:
FELIX-3506 Apply patch by David Jencks (thanks alot)
  - allow bind, updated, and unbind methods to update service registration properties

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
    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/ComponentContextImpl.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.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/MutablePropertiesTest.java
    felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java
    felix/trunk/scr/src/test/resources/integration_test_mutable_properties.xml

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/ActivateMethod.java Sat May 12 03:24:26 2012
@@ -142,12 +142,6 @@ public class ActivateMethod extends Base
         return "activate";
     }
 
-    protected boolean returnValue()
-    {
-        // allow returning Map if declared as DS 1.2-Felix or newer
-        return isDS12Felix();
-    }
-
     public MethodResult invoke(Object componentInstance, Object rawParameter, final MethodResult methodCallFailureResult)
     {
         if (methodExists())

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/helper/BaseMethod.java Sat May 12 03:24:26 2012
@@ -259,14 +259,10 @@ abstract class BaseMethod
         return MethodResult.VOID; // TODO: or null ??
     }
 
-    protected void processResult( Object configResults, Object result, Method method )
-    {
-        //no op
-    }
-
     protected boolean returnValue()
     {
-        return false;
+        // allow returning Map if declared as DS 1.2-Felix or newer
+        return isDS12Felix();
     }
 
     /**

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=1337437&r1=1337436&r2=1337437&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 Sat May 12 03:24:26 2012
@@ -30,6 +30,7 @@ import java.util.List;
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.Reference;
 import org.apache.felix.scr.impl.BundleComponentActivator;
+import org.apache.felix.scr.impl.helper.MethodResult;
 import org.apache.felix.scr.impl.metadata.ComponentMetadata;
 import org.apache.felix.scr.impl.metadata.ReferenceMetadata;
 import org.apache.felix.scr.impl.metadata.ServiceMetadata;
@@ -797,7 +798,7 @@ public abstract class AbstractComponentM
 
     public abstract Dictionary getProperties();
 
-    public abstract void setServiceProperties(Dictionary serviceProperties, boolean updateServiceRegistration);
+    public abstract void setServiceProperties( Dictionary serviceProperties );
 
     /**
      * Returns the subset of component properties to be used as service
@@ -900,6 +901,15 @@ public abstract class AbstractComponentM
         m_state = newState;
     }
 
+    public void setServiceProperties( MethodResult methodResult )
+    {
+        if ( methodResult.hasResult() )
+        {
+            Dictionary serviceProps = ( methodResult.getResult() == null) ? null : new Hashtable( methodResult.getResult() );
+            setServiceProperties(serviceProps );
+        }
+    }
+
     //--------- State classes
 
     /**

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentContextImpl.java Sat May 12 03:24:26 2012
@@ -136,7 +136,7 @@ public class ComponentContextImpl implem
 
     public void setServiceProperties(Dictionary properties)
     {
-        getComponentManager().setServiceProperties(properties, true);
+        getComponentManager().setServiceProperties(properties );
     }
 
 }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/ComponentFactoryImpl.java Sat May 12 03:24:26 2012
@@ -209,7 +209,7 @@ public class ComponentFactoryImpl extend
         return props;
     }
 
-    public void setServiceProperties(Dictionary serviceProperties, boolean updateServiceRegistration)
+    public void setServiceProperties( Dictionary serviceProperties )
     {
         throw new IllegalStateException( "ComponentFactory service properties are immutable" );
     }

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java (original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java Sat May 12 03:24:26 2012
@@ -1047,7 +1047,7 @@ public class DependencyManager implement
         {
             if ( m_bind != null )
             {
-                return m_bind.invoke( m_componentInstance, new BindMethod.Service()
+                MethodResult result = m_bind.invoke( m_componentInstance, new BindMethod.Service()
                 {
                     public ServiceReference getReference()
                     {
@@ -1060,7 +1060,13 @@ public class DependencyManager implement
                     {
                         return getService( ref );
                     }
-                }, MethodResult.VOID ) != null;
+                }, MethodResult.VOID );
+                if ( result == null )
+                {
+                    return false;
+                }
+                m_componentManager.setServiceProperties( result );
+                return true;
             }
 
             // Concurrency Issue: The component instance still exists but
@@ -1113,10 +1119,9 @@ public class DependencyManager implement
     {
         // The updated method is only invoked if the implementation object is not
         // null. This is valid for both immediate and delayed components
-        //TODO should updated methods be able to change config properties?
         if ( m_componentInstance != null )
         {
-            m_updated.invoke( m_componentInstance, new BindMethod.Service()
+            MethodResult methodResult = m_updated.invoke( m_componentInstance, new BindMethod.Service()
             {
                 public ServiceReference getReference()
                 {
@@ -1129,6 +1134,10 @@ public class DependencyManager implement
                     return getService( ref );
                 }
             }, MethodResult.VOID );
+            if ( methodResult != null)
+            {
+                m_componentManager.setServiceProperties( methodResult );
+            }
         }
         else
         {
@@ -1157,7 +1166,7 @@ public class DependencyManager implement
         // null. This is valid for both immediate and delayed components
         if ( m_componentInstance != null )
         {
-            m_unbind.invoke( m_componentInstance, new BindMethod.Service()
+            MethodResult methodResult = m_unbind.invoke( m_componentInstance, new BindMethod.Service()
             {
                 public ServiceReference getReference()
                 {
@@ -1170,6 +1179,10 @@ public class DependencyManager implement
                     return getService( ref );
                 }
             }, MethodResult.VOID );
+            if ( methodResult != null )
+            {
+                m_componentManager.setServiceProperties( methodResult );
+            }
         }
         else
         {

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=1337437&r1=1337436&r2=1337437&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 Sat May 12 03:24:26 2012
@@ -23,7 +23,6 @@ import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Map;
 
 import org.apache.felix.scr.impl.BundleComponentActivator;
 import org.apache.felix.scr.impl.config.ComponentHolder;
@@ -233,7 +232,7 @@ public class ImmediateComponentManager e
         // 4. Call the activate method, if present
         final MethodResult result = m_activateMethod.invoke(implementationObject, new ActivatorParameter(
             componentContext, 1), null);
-        if (result == null)
+        if ( result == null )
         {
             // 112.5.8 If the activate method throws an exception, SCR must log an error message
             // containing the exception with the Log Service and activation fails
@@ -246,9 +245,9 @@ public class ImmediateComponentManager e
 
             return null;
         }
-        else if (result.hasResult())
+        else
         {
-            setServiceProperties(result.getResult(), true);
+            setServiceProperties( result );
         }
 
         return implementationObject;
@@ -272,9 +271,9 @@ public class ImmediateComponentManager e
         // exception with the Log Service and continue) has already been logged
         final MethodResult result = m_deactivateMethod.invoke( implementationObject, new ActivatorParameter( componentContext,
             reason ), null );
-        if (result != null && result.hasResult())
+        if ( result != null )
         {
-            setServiceProperties(result.getResult(), true);
+            setServiceProperties( result );
         }
 
         // 2. Unbind any bound services
@@ -376,14 +375,7 @@ public class ImmediateComponentManager e
         return m_properties;
     }
 
-
-    public void setServiceProperties(Map serviceProperties, boolean updateServiceRegistration)
-    {
-        Dictionary serviceProps = (serviceProperties == null) ? null : new Hashtable(serviceProperties);
-        setServiceProperties(serviceProps, updateServiceRegistration);
-    }
-
-    public void setServiceProperties(Dictionary serviceProperties, boolean updateServiceRegistration)
+    public void setServiceProperties( Dictionary serviceProperties )
     {
         if ( serviceProperties == null || serviceProperties.isEmpty() )
         {
@@ -397,10 +389,7 @@ public class ImmediateComponentManager e
             m_serviceProperties.put( ComponentConstants.COMPONENT_ID, new Long( getId() ) );
         }
 
-        if (updateServiceRegistration)
-        {
-            updateServiceRegistration();
-        }
+        updateServiceRegistration();
     }
 
     public Dictionary getServiceProperties() {
@@ -550,7 +539,7 @@ public class ImmediateComponentManager e
         // 4. call method (nothing to do when failed, since it has already been logged)
         final MethodResult result = m_modifyMethod.invoke(getInstance(),
             new ActivatorParameter(m_componentContext, -1), null);
-        if (result == null)
+        if ( result == null )
         {
             // log an error if the declared method cannot be found
             log( LogService.LOG_ERROR, "Declared modify method ''{0}'' cannot be found, configuring by reactivation",
@@ -558,10 +547,6 @@ public class ImmediateComponentManager e
                     { getComponentMetadata().getModified() }, null );
             return false;
         }
-        else if (result.hasResult())
-        {
-            setServiceProperties(result.getResult(), false);
-        }
 
         // 5. update the target filter on the services now, this may still
         // result in unsatisfied dependencies, in which case we abort
@@ -575,10 +560,17 @@ public class ImmediateComponentManager e
             return false;
         }
 
-        // 6. update service registration properties
-        updateServiceRegistration();
+        // 6. update service registration properties if we didn't just do it
+        if ( result.hasResult() )
+        {
+            setServiceProperties( result );
+        }
+        else
+        {
+            updateServiceRegistration();
+        }
 
-        // 7. everything set and done, the component has been udpated
+        // 7. everything set and done, the component has been updated
         return true;
     }
 

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/MutablePropertiesTest.java Sat May 12 03:24:26 2012
@@ -27,6 +27,7 @@ import junit.framework.Assert;
 import junit.framework.TestCase;
 import org.apache.felix.scr.Component;
 import org.apache.felix.scr.integration.components.MutatingService;
+import org.apache.felix.scr.integration.components.SimpleServiceImpl;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
@@ -41,7 +42,7 @@ public class MutablePropertiesTest exten
     static
     {
         // uncomment to enable debugging of this test class
-        // paxRunnerVmOption = DEBUG_VM_OPTION;
+        //paxRunnerVmOption = DEBUG_VM_OPTION;
 
         descriptorFile = "/integration_test_mutable_properties.xml";
     }
@@ -83,11 +84,11 @@ public class MutablePropertiesTest exten
     @Test
     public void test_mutable_properties_returned() throws InvalidSyntaxException
     {
-        final Component component = findComponentByName( "components.mutable.properties2" );
+        final Component component = findComponentByName( "components.mutable.properties.return" );
         TestCase.assertNotNull( component );
         TestCase.assertEquals( Component.STATE_REGISTERED, component.getState() );
 
-        ServiceReference[] serviceReferences = bundleContext.getServiceReferences( MutatingService.class.getName(), "(service.pid=components.mutable.properties2)" );
+        ServiceReference[] serviceReferences = bundleContext.getServiceReferences( MutatingService.class.getName(), "(service.pid=components.mutable.properties.return)" );
         TestCase.assertEquals( 1, serviceReferences.length );
         ServiceReference serviceReference = serviceReferences[0];
         checkProperties( serviceReference, 8, "otherValue", "p1", "p2" );
@@ -102,7 +103,7 @@ public class MutablePropertiesTest exten
         checkProperties(serviceReference, 5, "anotherValue", "p1", "p2");
 
         //configure with configAdmin
-        configure( "components.mutable.properties2" );
+        configure( "components.mutable.properties.return" );
         delay();
         delay();
         //no change
@@ -114,10 +115,40 @@ public class MutablePropertiesTest exten
 
         bundleContext.ungetService(serviceReference);
     }
+    @Test
+    public void test_mutable_properties_bind_returned() throws InvalidSyntaxException
+    {
+        final Component component = findComponentByName( "components.mutable.properties.bind" );
+        TestCase.assertNotNull( component );
+        TestCase.assertEquals( Component.STATE_REGISTERED, component.getState() );
+
+        ServiceReference[] serviceReferences = bundleContext.getServiceReferences( MutatingService.class.getName(), "(service.pid=components.mutable.properties.bind)" );
+        TestCase.assertEquals( 1, serviceReferences.length );
+        ServiceReference serviceReference = serviceReferences[0];
+        checkProperties( serviceReference, 8, "otherValue", "p1", "p2" );
+        MutatingService s = ( MutatingService ) bundleContext.getService( serviceReference );
+
+        SimpleServiceImpl srv1 = SimpleServiceImpl.create( bundleContext, "srv1" );
+        checkProperties( serviceReference, 5, null, "p1", "p2" );
+        Assert.assertEquals("bound", serviceReference.getProperty("SimpleService"));
+
+        srv1.update( "foo" );
+        checkProperties( serviceReference, 5, null, "p1", "p2" );
+        Assert.assertEquals("updated", serviceReference.getProperty("SimpleService"));
+
+        srv1.drop();
+        checkProperties( serviceReference, 5, null, "p1", "p2" );
+        Assert.assertEquals("unbound", serviceReference.getProperty("SimpleService"));
+
+        bundleContext.ungetService(serviceReference);
+    }
 
     private void checkProperties(ServiceReference serviceReference, int count, String otherValue, String p1, String p2) {
         Assert.assertEquals("wrong property count", count, serviceReference.getPropertyKeys().length);
-        Assert.assertEquals(otherValue, serviceReference.getProperty(PROP_NAME));
+        if ( otherValue != null )
+        {
+            Assert.assertEquals(otherValue, serviceReference.getProperty(PROP_NAME));
+        }
         if ( count > 5 ) {
             Assert.assertEquals(p1, serviceReference.getProperty("p1"));
             Assert.assertEquals(p2, serviceReference.getProperty("p2"));

Modified: felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java (original)
+++ felix/trunk/scr/src/test/java/org/apache/felix/scr/integration/components/MutatingServiceImpl.java Sat May 12 03:24:26 2012
@@ -19,6 +19,7 @@
 package org.apache.felix.scr.integration.components;
 
 
+import java.util.Collections;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Map;
@@ -45,26 +46,43 @@ public class MutatingServiceImpl impleme
     {
         this.activateContext = activateContext;
         Map result = new Hashtable( (Map )activateContext.getProperties() );
-        result.put( "theValue", "anotherValue1");
+        result.put( "theValue", "anotherValue1" );
         return result;
     }
 
     private Map modifiedMutate( ComponentContext activateContext )
     {
         Map result = new Hashtable( (Map )activateContext.getProperties() );
-        result.put( "theValue", "anotherValue2");
+        result.put( "theValue", "anotherValue2" );
         return result;
     }
 
     private Map deactivateMutate( ComponentContext activateContext )
     {
         Map result = new Hashtable( (Map )activateContext.getProperties() );
-        result.put( "theValue", "anotherValue3");
+        result.put( "theValue", "anotherValue3" );
         return result;
     }
 
-    public void updateProperties(Dictionary changes) {
-        ((ExtComponentContext)activateContext).setServiceProperties(changes);
+    public void updateProperties( Dictionary changes )
+    {
+        ( ( ExtComponentContext ) activateContext ).setServiceProperties( changes );
+    }
+
+    private Map bindSimpleService( SimpleService ss )
+    {
+        return Collections.singletonMap( "SimpleService", "bound" );
     }
 
+    private Map unbindSimpleService( SimpleService ss )
+    {
+        return Collections.singletonMap( "SimpleService", "unbound" );
+    }
+
+    private Map updateSimpleService( SimpleService ss )
+    {
+        return Collections.singletonMap( "SimpleService", "updated" );
+    }
+
+
 }

Modified: felix/trunk/scr/src/test/resources/integration_test_mutable_properties.xml
URL: http://svn.apache.org/viewvc/felix/trunk/scr/src/test/resources/integration_test_mutable_properties.xml?rev=1337437&r1=1337436&r2=1337437&view=diff
==============================================================================
--- felix/trunk/scr/src/test/resources/integration_test_mutable_properties.xml (original)
+++ felix/trunk/scr/src/test/resources/integration_test_mutable_properties.xml Sat May 12 03:24:26 2012
@@ -33,7 +33,8 @@
         <property name="p1" value="p1" />
         <property name="p2" value="p2" />
     </scr:component>
-    <scr:component name="components.mutable.properties2"
+
+    <scr:component name="components.mutable.properties.return"
             xmlns:scr="http://felix.apache.org/xmlns/scr/v1.2.0-felix"
             enabled="true"
             configuration-policy="optional"
@@ -44,9 +45,35 @@
         <service>
             <provide interface="org.apache.felix.scr.integration.components.MutatingService" />
         </service>
-        <property name="service.pid" value="components.mutable.properties2" />
+        <property name="service.pid" value="components.mutable.properties.return" />
         <property name="theValue" value="otherValue" />
         <property name="p1" value="p1" />
         <property name="p2" value="p2" />
     </scr:component>
+
+    <scr:component name="components.mutable.properties.bind"
+                   xmlns:scr="http://felix.apache.org/xmlns/scr/v1.2.0-felix"
+                   enabled="true"
+                   configuration-policy="optional"
+                   activate="activateMutate"
+                   modified="modifiedMutate"
+                   deactivate="deactivateMutate">
+        <implementation class="org.apache.felix.scr.integration.components.MutatingServiceImpl"/>
+        <service>
+            <provide interface="org.apache.felix.scr.integration.components.MutatingService"/>
+        </service>
+        <reference
+                name="simpleService"
+                interface="org.apache.felix.scr.integration.components.SimpleService"
+                cardinality="0..1"
+                policy="dynamic"
+                bind="bindSimpleService"
+                unbind="unbindSimpleService"
+                updated="updateSimpleService"
+                />
+        <property name="service.pid" value="components.mutable.properties.bind"/>
+        <property name="theValue" value="otherValue"/>
+        <property name="p1" value="p1"/>
+        <property name="p2" value="p2"/>
+    </scr:component>
 </components>