FELIX-3717 make sure unbind and updated methods get the service parameter if used even if the bind method didn't need it

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1399527 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
index 24420c2..86a8a1f 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/BindMethod.java
@@ -554,25 +554,25 @@
         return null;
     }
 
-    public RefPair getServiceObject( ServiceReference ref, BundleContext context )
+    public boolean getServiceObject( RefPair refPair, BundleContext context )
     {
         //??? this resolves which we need.... better way?
-        if ( methodExists() )
+        if ( refPair.getServiceObject() == null && methodExists() )
         {
             if (m_paramStyle == SERVICE_OBJECT || m_paramStyle == SERVICE_OBJECT_AND_MAP) {
-                Object service = context.getService( ref );
+                Object service = context.getService( refPair.getRef() );
                 if ( service == null )
                 {
                     getLogger().log(
                          LogService.LOG_WARNING,
-                         "Could not get service from ref " + ref, null );
-                    return null;
+                         "Could not get service from ref " + refPair.getRef(), null );
+                    return false;
                 }
-
-                return new RefPair(ref, service);
+                refPair.setServiceObject( service );
+                return true;
             }
         }
-        return new RefPair(ref, null);
+        return true;
     }
 
     protected Object[] getParameters( Method method, Object rawParameter )
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
index f61326a..b177638 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/DependencyManager.java
@@ -935,7 +935,8 @@
             }
             else
             {
-                refPair = new RefPair( serviceReference,  serviceObject );
+                refPair = new RefPair( serviceReference );
+                refPair.setServiceObject( serviceObject );
                 ((Map)m_componentManager.getDependencyMap().get( this )).put( serviceReference, refPair );
             }
         }
@@ -1039,7 +1040,7 @@
         unbind( componentInstance, getBoundServiceReferences() );
     }
 
-    //returns Map<ServiceReference, Object[]>
+    //returns Map<ServiceReference, RefPair>
     boolean prebind( Map dependencyMap)
     {
         // If no references were received, we have to check if the dependency
@@ -1073,9 +1074,9 @@
             {
                 for ( int index = 0; index < refs.length; index++ )
                 {
-                    RefPair refPair = m_bindMethods.getBind().getServiceObject( refs[index], m_componentManager.getActivator().getBundleContext() );
+                    RefPair refPair = new RefPair( refs[index] );
                     // success is if we have the minimal required number of services bound
-                    if ( refPair != null )
+                    if ( m_bindMethods.getBind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ) )
                     {
                         result.put( refs[index], refPair );
                         // of course, we have success if the service is bound
@@ -1094,9 +1095,9 @@
             ServiceReference ref = getFrameworkServiceReference();
             if ( ref != null )
             {
-                RefPair refPair = m_bindMethods.getBind().getServiceObject( ref, m_componentManager.getActivator().getBundleContext() );
+                RefPair refPair = new RefPair( ref );
                 // success is if we have the minimal required number of services bound
-                if ( refPair != null )
+                if ( m_bindMethods.getBind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ) )
                 {
                     result.put( ref, refPair );
                     // of course, we have success if the service is bound
@@ -1104,7 +1105,7 @@
                 }
                 else if ( isOptional() )
                 {
-                    m_componentManager.getActivator().registerMissingDependency(this, ref);
+                    m_componentManager.getActivator().registerMissingDependency( this, ref );
                 }
             }
         }
@@ -1234,9 +1235,8 @@
 
                 }
                 Map deps = ( Map ) dependencyMap.get( this );
-                BundleContext bundleContext = m_componentManager.getActivator().getBundleContext();
-                RefPair refPair = m_bindMethods.getBind().getServiceObject( ref, bundleContext );
-                if ( refPair == null )
+                RefPair refPair = new RefPair( ref );
+                if ( !m_bindMethods.getBind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ) )
                 {
                     //reference deactivated while we are processing.
                     return false;
@@ -1354,6 +1354,15 @@
                 //TODO should this be possible? If so, reduce or eliminate logging
                 m_componentManager.log( LogService.LOG_WARNING,
                         "DependencyManager : invokeUpdatedMethod : Component set, but reference not present", null );
+                return;
+            }
+            if ( !m_bindMethods.getUpdated().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ))
+            {
+                m_componentManager.log( LogService.LOG_WARNING,
+                        "DependencyManager : invokeUpdatedMethod : Service not available from service registry for ServiceReference {0} for reference {1}",
+                        new Object[] {ref, getName()}, null );
+                return;
+
             }
             MethodResult methodResult = m_bindMethods.getUpdated().invoke( componentInstance, refPair, MethodResult.VOID );
             if ( methodResult != null)
@@ -1394,6 +1403,15 @@
                 //TODO should this be possible? If so, reduce or eliminate logging
                 m_componentManager.log( LogService.LOG_WARNING,
                         "DependencyManager : invokeUnbindMethod : Component set, but reference not present", null );
+                return;
+            }
+            if ( !m_bindMethods.getUnbind().getServiceObject( refPair, m_componentManager.getActivator().getBundleContext() ))
+            {
+                m_componentManager.log( LogService.LOG_WARNING,
+                        "DependencyManager : invokeUnbindMethod : Service not available from service registry for ServiceReference {0} for reference {1}",
+                        new Object[] {ref, getName()}, null );
+                return;
+
             }
             MethodResult methodResult = m_bindMethods.getUnbind().invoke( componentInstance, refPair, MethodResult.VOID );
             if ( methodResult != null )
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java
index eb041db..34cf14e 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/RefPair.java
@@ -30,10 +30,9 @@
     private final ServiceReference ref;
     private Object serviceObject;
 
-    public RefPair( ServiceReference ref, Object serviceObject )
+    public RefPair( ServiceReference ref )
     {
         this.ref = ref;
-        this.serviceObject = serviceObject;
     }
 
     public ServiceReference getRef()
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java b/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java
index b06bc3a..e184790 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/helper/BindMethodTest.java
@@ -436,7 +436,8 @@
         ImmediateComponentManager icm = new ImmediateComponentManager( null, null, metadata, new ComponentMethods() );
         BindMethod bm = new BindMethod( icm, methodName, component.getClass(),
                 FakeService.class.getName(), isDS11, false );
-        RefPair refPair = bm.getServiceObject( m_serviceReference, m_context );
+        RefPair refPair = new RefPair( m_serviceReference );
+        assertTrue( bm.getServiceObject( refPair, m_context ) );
         bm.invoke( component, refPair, null );
         assertEquals( expectCallPerformed, component.callPerformed );
     }