The component implementation class is a public class that implements
the service interfaces declared inside the component descriptor.
Additionnally, it must provide bind and unbind control methods that are
necessary to support the creation of bindings by the instance manager.
When multiple required services are declared, it may be necessary for the component instance to be notified when the configuration phase is finished. Similarly, it may be necessary to be notified when the component instance is about to be invalidated (versus a re-configuration). This is the purpose of the methods defined in the Lifecycle interface. If the component implementation class implements this interface, the following methods are called at two different moments:
activate():
During configuration, after all the bindings have been created, but
before the provided
services are registered.
deactivate():
During execution, when the instance is about to be invalidated, after
its services are unregistered
and before its bindings are destroyed.
When writing an the component implementation class, it may be necessary to access the OSGi's bundle context that is provided to the activator of the bundle (altough this is usually not necessary). ServiceBinderContext is a class provided to solve this issue. If the instance class constructor receives a single ServiceBinderContext parameter, a context will be passed into it during instantiation. The methods that are provided in this context allow to:
Access the bundle's BundleContext.
Obtain the InstanceReference object corresponding to this instance.
Obtain a list of all of the instance references that belong to the bundle.
The InstanceReference represents a component instance. This
reference allows the object created from the implementation class to
access the properties defined in the component descriptor.
The following example shows the previously introduced client, but
this
time it receives a ServiceBinderContext
and implements the Lifecycle interface.
When receiving its context, it accesses one of the
properties
associated to the instance. Changes with respect to the previous
version
are in bold.
| Implementation class package org.simpleclient.impl; /* Services that are implemented by this class */ import org.simpleclient.interfaces.SimpleClientServiceA; import org.simpleclient.interfaces.SimpleClientServiceB; /* External services */ import org.simpleservice.interfaces.SimpleService; /* A List is used since the cardinality of the dependency is 1..n */ import java.util.ArrayList; import org.ungoverned.gravity.servicebinder.ServiceBinderContext; import org.ungoverned.gravity.servicebinder.Lifecycle; public class ClientImpl implements Lifecycle, SimpleClientServiceA, SimpleClientServiceB { ArrayList m_simpleServiceRefs = new ArrayList(); ServiceBinderContext m_context; public ServiceImpl(ServiceBinderContext context) { m_context = context; Object prop = m_context.getInstanceReference().get("author"); System.out.println(" Client instantiated ! My author is :"+prop); } // Implementation of SimpleClientServiceA and SimpleClientServiceB not shown ... // Bind method public void setSimpleServiceReference(SimpleService ref) { System.out.println("-> SimpleClient: Hello SimpleService !"); m_simpleServiceRefs.add(ref); ref.Test(); } // Unbind method public void unsetSimpleServiceReference(SimpleService ref) { System.out.println("-> SimpleClient: Goodbye SimpleService ..."); m_simpleServiceRefs.remove(ref); } public void activate() { // Configuration is finished, the services are not yet registered } public void deactivate() { // Invalidation has started, the services have been unregistered, the // unbind method has not yet been called } } |
Next : Adding interceptors
Previous : A simple example