The component implementation class


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.

Lifecycle interface

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:

ServiceBinderContext

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:

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
    }

}

Figure 1. An example of the ServiceBinderContext and Lifecycle interface


Last revision: 05 February 2004
(c) H. Cervantes and R.S. Hall

Next : Adding interceptors

Previous : A simple example

Index