Class BeanConfigurationServiceRegistrationListener

java.lang.Object
net.solarnetwork.common.osgi.service.BaseServiceListener<BeanConfiguration,BeanConfigurationServiceRegistrationListener.BeanConfigurationRegisteredService>
net.solarnetwork.node.service.support.BeanConfigurationServiceRegistrationListener

public class BeanConfigurationServiceRegistrationListener extends net.solarnetwork.common.osgi.service.BaseServiceListener<BeanConfiguration,BeanConfigurationServiceRegistrationListener.BeanConfigurationRegisteredService>
An OSGi service registration listener for BeanConfiguration objects, so they can be used to dynamically configure and publish other OSGi services.

This object acts like a dynamic OSGi service factory. You configure the type of service this factory should create, and when BeanConfiguration are found new instances of the service will be created and configured from the discovered BeanConfiguration. This allows the bundle the configuration came from to be completely isolated and unaware of the implementation bundle using that configuration.

The BeanConfiguration.getConfiguration() Map will be used to configure the properties on instantiated service objects. The keys of this Map should be standard Spring JavaBean property names.

The BeanConfiguration.getAttributes() Map will be used to create OSGi service properties when the service is registered.

The BeanConfiguration.getOrdering() Integer will be used to assign an OSGi ranking to the service when it is registered.

When the BeanConfiguration is unregistered and the onUnbind(BeanConfiguration, Map) method is called, the associated service created by this factory will be unregistered as well.

For example, this might be configured via Spring DM like this:

 <osgi:list id="myConfigurationList"
                interface="net.solarnetwork.node.util.BeanConfiguration" cardinality="0..N">
                <osgi:listener bind-method="onBind" unbind-method="onUnbind" ref="myServiceBean"/>
 </osgi:list>
 
 <bean id="myServiceBean" 
                class="net.solarnetwork.node.util.BeanConfigurationServiceRegistrationListener">
                <property name="serviceClass"
                        value="net.solarnetwork.node.impl.MyServiceImplementation"/>
                <property name="serviceInterfaces"
                        value="net.solarnetwork.node.MyService"/>
                <property name="bundleContext" ref="bundleContext"/>
 </bean>
 
Version:
2.0
Author:
matt
  • Constructor Details

    • BeanConfigurationServiceRegistrationListener

      public BeanConfigurationServiceRegistrationListener(org.osgi.framework.BundleContext bundleContext)
      Constructor.
      Parameters:
      bundleContext - the bundle context
      Throws:
      IllegalArgumentException - if any argument is null
  • Method Details

    • onBind

      public void onBind(BeanConfiguration config, Map<String,?> properties)
      Callback when an object has been registered.

      This method will instantiate a new instance of getServiceClass() and configure its properties via the Map returned by BeanConfiguration.getConfiguration(). Afterwards it will register the instance as a service, using the getServiceInterfaces() as the service interfaces and getServiceProperties() as the service properties (if available) combined with the BeanConfiguration.getAttributes() (if available).

      Parameters:
      config - the configuration object
      properties - the service properties
    • onUnbind

      public void onUnbind(BeanConfiguration config, Map<String,?> properties)
      Callback when a trigger has been un-registered.

      This method will attempt to un-register a previously registered service.

      Parameters:
      config - the configuration object
      properties - the service properties
    • getServiceClass

      public Class<?> getServiceClass()
      Get the type of service to create when onBind(BeanConfiguration, Map) is called.
      Returns:
      the type of service
    • setServiceClass

      public void setServiceClass(Class<?> serviceClass)
      Set the type of service to create when onBind(BeanConfiguration, Map) is called.
      Parameters:
      serviceClass - the type of service
    • getServiceInterfaces

      public String[] getServiceInterfaces()
      Get the array of interface names to register the OSGi service as.
      Returns:
      the interfaces to adopt
    • setServiceInterfaces

      public void setServiceInterfaces(String[] serviceInterfaces)
      Set the array of interface names to register the OSGi service as.
      Parameters:
      serviceInterfaces - the interfaces to adopt
    • getServiceProperties

      public Map<String,Object> getServiceProperties()
      Get an optional Map of properties to register the OSGi service with.
      Returns:
      the optional properties
    • setServiceProperties

      public void setServiceProperties(Map<String,Object> serviceProperties)
      Set an optional Map of properties to register the OSGi service with.
      Parameters:
      serviceProperties - the optional properties