Reference Guide

Table Of Contents
private SwitchManager delegate;
@Activate
public void activate() {
// activate() is called after all mandatory dependencies
// are satisfied
delegate = new SwitchManager(systemInformationService);
delegate.setAlertService(alertService);
}
@Deactivate
public void deactivate() {
delegate = null;
}
protected void bindAlertService(AlertService service) {
alertService = service;
// TODO: Decorate the business logic with the optional service.
if (delegate != null) {
delegate.setAlertService(service);
}
}
protected void unbindAlertService(AlertService service) {
if (alertService == service) {
alertService = null;
if (delegate != null) {
delegate.setAlertService(null);
}
}
}
@Override
public Switch add(Switch device) {
return delegate.add(device);
}
...
// Follow the same pattern than “add(Switch)” for the
// remaining overridden methods.
}
Dependency services are annotated with @Reference to denote to OSGi to inject a reference into
the component. The OSGi’s dependency-injection framework calls bindAlertService(AlertService)
method when the service is available (activated) and unbindAlertService(AlertService) when the
component providing the implementation of AlertService is deactivated. If no bind/unbind
methods are provided (Like in the case of SystemInformationService) OSGi still injects a reference
directly into the variable annotated with @Reference. Defining methods to bind/unbind services
168