Previous Topic

Next Topic

Book Contents

Book Index

Registering the Provider as an OSGi Service

Register the condition provider as an OSGi service in order to make it available to the Home Automation Manager.

Alternatively, use the createRegistrationProperties method from BaseCommandProvider to automatically build the dictionary.

The following example shows a service provider Activator class that registers the condition provider from here. The condition provider is also registered as a bundle listener in order for it to receive an event every time a new bundle is started:

  package demo.condition.impl;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.event.EventAdmin;

import com.prosyst.mbs.services.configtree.Configurable;
import com.prosyst.mbs.services.ham.spi.ConditionProvider;
import com.prosyst.util.ref.Log;

public class Activator implements BundleActivator {

  private static BundleContext context;

  static BundleContext getContext() {
  return context;
  }
    
  DemoConditionProvider provider;
  ServiceRegistration servRerg;
  
  public void start(BundleContext bundleContext) throws Exception {
    Activator.context = bundleContext;
        
    //We retrieve the EventAdmin service.
    //It is used by condition providers to send events to the HAM.
    ServiceReference adminRef = context.getServiceReference(EventAdmin.class.getName());
    EventAdmin eventAdmin = (EventAdmin) context.getService(adminRef);
      

          
    //And register our Condition Provider:
    Log log = new Log(context);
    provider = new DemoConditionProvider(log, eventAdmin);
    servRerg = context.registerService(
    
    //The provider class is registered both as a
    //ConditionProvider and as a Configurable
    new String[] {ConditionProvider.class.getName(),
    Configurable.class.getName() }, provider, provider.createRegistrationProperties());
    
    //Since this condition provider is also a bundle
    //listener, we have to register it as such:
     context.addBundleListener(provider);
}

  public void stop(BundleContext bundleContext) throws Exception {
    Activator.context = null;
  }

}