Reference Guide

Table Of Contents
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.References;
import com.hp.hm.api.SwitchService;
import com.hp.sdn.rs.misc.ServiceLocator;
...
@Component(immediate=true, specVersion="1.1")
@References(
value={
// Add a @Reference (Separated by comma) for each
// domain service exposed to the REST layer.
@Reference(name="SwitchService",
referenceInterface = SwitchService.class,
policy=ReferencePolicy.DYNAMIC,
cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE
)
}
)
public class ServiceAssistant {
// Add a bind/unbind methods for each Domain Service
// exposed to the REST layer.
protected void bindSwitchService(SwitchService service,
Map<String, Object> properties) {
ServiceLocator.INSTANCE.register(SwitchService.class,
service, properties);
}
protected void unbindSwitchService(SwitchService service) {
ServiceLocator.INSTANCE.unregister(SwitchService.class, service);
}
}
ServiceAssistant shows an alternative way of declaring dependencies. ServiceAssistant is
annotated with @References instead of declaring a variable of type SwitchService and then
annotate it with @Reference as in Consuming Services with OSGi Declarative Services on page
166 under the Dependent SwitchComponent.java listing. In this case we wouldn’t use the variable
since we pass the bound service to the ServiceLocator.
The sample application’s domain service (SwitchService) is ready to be used by the REST layer.
The following listing shows an extract of a modified SwitchResource (from Creating Domain
Service Resource (REST Interface of Business Logic Service) on page 169) that makes use of the
inherited get(Class<?>) method to get a reference to the SwitchService.
Consuming Domain Services:
package com.hp.hm.rs;
181