I am using the SCR framework in an OSGI felix platform to inject service references in my Components. This works great, except for optional dependencies. So if I have two components Foo and Bar, where Foo is given below:
@Component
public class FooImpl implements Foo {
Log log = LogFactory.getLog(this.getClass());
@Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY)
Bar bar;
public void bindBar(Bar bar) {
log.info("bar bound: "+bar);
}
public void unbindBar(Bar map) {
log.info("bar unbound: "+bar);
}
@override
public void fooHello() {
log.info("Hello, this is an implementation of Foo");
}
}
this works, as long as the bundle defining the interface of Bar is deployed in my OSGi platform. If no Bar implementation component is activated in the platform, SCR is still happy and will activate my FooImpl component, ofcourse without reference to any Bar implementation. However, if the Bar interface is not deployed in the platform, SCR crashes during activation of my component, likely due to an exception from the inspection of my component via reflection, but I could not determine that definitively.
So, is there a way to deploy an OSGI bundle with optional dependencies that are not present in the platform that include SCR Components that have optional references to interfaces coming from these optional OSGi dependencies?