Reference Guide

Table Of Contents
public void setAlertService(AlertService alertService) {
// Mutators are used for optional dependencies.
this.alertService = alertService;
}
...
}
SystemInformationService Module Dependency:
<dependency>
<groupId>com.hp.sdn</groupId>
<artifactId>sdn-adm-api</artifactId>
<version>${sdn.version}</version>
</dependency>
As previously mentioned, SwitchManager is not tied to OSGi so it expects a non-null instance of
SystemInformationService (It doesn’t care how the instance is obtained) and code does not need to
be included to handle the case when the implementation of SystemInformationService is no longer
available. SwitchManager focuses on implementing the business logic. Note how
SystemInformationService is an interface, so its implementation may be changed without affecting
the business logic.
SwitchComponent will be updated to obtain a reference of SystemInformationService and
AlertService via OSGi declarative services. SwitchComponent must deal with the fact that
components may come and go, thus the injected references need to be bound and unbound. The
following listing shows SwitchComponent consuming the services.
Dependent SwitchComponent.java:
package com.hp.hm.impl;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
...
@Component
@Service
public class SwitchComponent implements SwitchService {
@Reference(policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.MANDATORY_UNARY)
private volatile SystemInformationService systemInformationService;
@Reference(policy = ReferencePolicy.DYNAMIC,
cardinality = ReferenceCardinality.OPTIONAL_UNARY)
private volatile AlertService alertService;
// Note: A better design would use the decorator pattern to decorate
// business logic with optional services. That would allow us to use
// SwitchService instead of SwitchManager as the delegate type.
167