Reference Guide

Table Of Contents
Consuming Services with OSGi Declarative Services
OSGi Declarative Services may also be used to consume other services: injecting references of
other components (Dependency components) into our components (via OSGi’s dependency-
injection framework).
Assume the business service implementation (SwitchManager) depends on the
SystemInformationService - a service provided by the HP VAN SDN Controller to request system
information such as the system IP Address, and so on. Also assume the relation is mandatory
meaning the service cannot operate without such dependency and thus it should not be published
until the dependency is satisfied (SystemInformationService is available and has been injected into
SwitchManager).
Assume SwitchManager depends on the AlertService - a service provided by the HP VAN SDN
Controller to post alerts. However, assume this dependency is optional, which means
SwitchManager is activated and published even though the AlertService is not.
Since SwitchManager is not tied to OSGi, adding mandatory dependencies is as simple as
defining constraints at construction time. Mutators are used to set optional dependencies (a better
way to handle optional-dependencies is to use the decorator pattern [34] to decorate business
logic with optional services). The following listing shows the modified SwitchManager which now
depends on SystemInformationService and AlertService. Dependency services are defined in a
different module thus the business logic module needs to declare such dependencies in its POM
file. Open the hm-bl/pom.xml file and add the XML extract from SystemInformationService listing
to the <dependencies> node; after updating the POM file update the Eclipse project dependencies
(see Updating Project Dependencies on page 146).
Dependent SwitchManager.java:
package com.hp.hm.impl;
import com.hp.sdn.adm.alert.AlertService;
import com.hp.sdn.adm.system.SystemInformationService;
...
public class SwitchManager implements SwitchService {
// Mandatory dependency.
private final SystemInformationService systemInformationService;
// Optional dependency. NOTE: A better design would use the decorator
// pattern to decorate business logic with optional services.
private AlertService alertService;
public SwitchManager(SystemInformationService systemInformationService) {
// Mandatory dependencies are set at construction time.
if (systemInformationService == null) {
throw new NullPointerException(...);
}
this.systemInformationService = systemInformationService;
}
166