Search code examples
javaosgiipojo

iPOJO Component injection callbacks are not invoked unless validate back is added


My iPOJO component's injection callbacks are not getting invoked unless I add the validate callback method.

import javax.swing.DefaultBoundedRangeModel;
import org.apache.felix.ipojo.annotations.*;
import org.osgi.service.log.*;

@Component
@Instantiate
@Provides
public class MyBoundedRangeModel extends DefaultBoundedRangeModel {

  @Requires
  org.osgi.service.log.LogService logService;

  @Bind
  public void bindLogService(LogService logService) {
    System.out.println("Binded the log service");
  }

  @Unbind
  public void unbindLogService(LogService logService) {
    System.out.println("Unbinded the log service");
  }
//  @Validate
//  public void validate(){
//      System.out.println("Validated the pdf bounded range model");
//  }
}

But the OSGi console lists the component is valid when I type command 'instances'. What's the problem ?

g! instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -> valid 
Instance temp.MyBoundedRangeModel-0 -> valid 


g! Instance temp.MyBoundedRangeModel-0
instance name="temp.MyBoundedRangeModel-0" state="valid" bundle="7" component.type="temp.MyBoundedRangeModel"
  handler name="org.apache.felix.ipojo:requires" state="valid"
    requires specification="org.osgi.service.log.LogService" id="org.osgi.service.log.LogService" optional="false" aggregate="false" proxy="true" binding-policy="dynamic" state="resolved"
      selected service.id="22"
      matches service.id="22"
    requires specification="org.osgi.service.log.LogService" id="LogService" optional="false" aggregate="false" proxy="true" binding-policy="dynamic" state="resolved"
      selected service.id="22"
      matches service.id="22"
  handler name="org.apache.felix.ipojo:provides" state="valid"
    provides specifications="[javax.swing.BoundedRangeModel,java.io.Serializable]" state="registered" service.id="51"
      property name="instance.name" value="temp.MyBoundedRangeModel-0"
      property name="factory.name" value="temp.MyBoundedRangeModel"
  handler name="org.apache.felix.ipojo:architecture" state="valid"

Solution

  • The @Bind callback will be called when the object is created (when it's required, so in your case when the published service is used for the first time).

    Instead of adding a @Validate callback, and only if you need to create the object early, just use:

    @Component(immediate=true)