org.apache.felix.dm.runtime
Class ServiceLifecycleHandler

java.lang.Object
  extended by org.apache.felix.dm.runtime.ServiceLifecycleHandler

public class ServiceLifecycleHandler
extends Object

Allow Services to configure dynamically their dependency filters from their init() method. Basically, this class acts as a service implementation lifecycle handler. When we detect that the Service is called in its init() method, and if init() returns a Map, then the Map is assumed to contain dependency filters, which will be applied to all named dependencies. The Map optionally returned by Service's init method may contain the following keys:

Dependencies which provide a name attribute will be activated after the init method returns. Other dependencies are injected before the init method.

Example of a Service whose dependency filter is configured from ConfigAdmin:

  /**
    * A Service whose service dependency filter/require attribute may be configured from ConfigAdmin
    */
  @Service
  class X {
      private Dictionary m_config;
      
      @ConfigurationDependency(pid="MyPid")
      void configure(Dictionary conf) {
           // Initialize our service from config ...
           
           // And store the config for later usage (from our init method)
           m_config = config;
      }
      
      @ServiceDependency(name="dependency1") 
      void bindOtherService(OtherService other) {
         // the filter and required flag will be configured from our init method.
      }

      // The returned Map will be used to configure our "dependency1" Dependency.
      @Init
      Map init() {
          return new HashMap() {{
              put("dependency1.filter", m_config.get("filter"));
              put("dependency1.required", m_config.get("required"));
          }};
      } 
  }
  


Constructor Summary
ServiceLifecycleHandler(Component srv, Bundle srvBundle, DependencyManager dm, MetaData srvMeta,  depMeta)
          Makes a new ServiceLifecycleHandler object.
 
Method Summary
 void destroy(Component service)
          Handles the Service's destroy lifecycle callback.
 void init(Component service)
          Handles an "init" lifecycle service callback.
 void start(Component service)
          Handles the Service's start lifecycle callback.
 void stop(Component service)
          Handles the Service's stop lifecycle callback.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ServiceLifecycleHandler

public ServiceLifecycleHandler(Component srv,
                               Bundle srvBundle,
                               DependencyManager dm,
                               MetaData srvMeta,
                                depMeta)
Makes a new ServiceLifecycleHandler object. This objects allows to decorate the "init" service callback, in order to see if "init" callback returns a dependency customization map.

Parameters:
srv - The Service for the annotated class
srvBundle - the Service bundle
dm - The DependencyManager that was used to create the service
srvMeta - The Service MetaData
depMeta - The Dependencies MetaData
Method Detail

init

public void init(Component service)
          throws Exception
Handles an "init" lifecycle service callback. We just catch the "init" method, and callback the actual Service' init method, to see if a dependency customization map is returned. We also check if a Lifecycle Controller is used. In this case, we add a hidden custom dependency, allowing to take control of when the component is actually started/stopped.

Parameters:
service - The Annotated Service
Throws:
Exception

start

public void start(Component service)
           throws IllegalArgumentException,
                  IllegalAccessException,
                  InvocationTargetException
Handles the Service's start lifecycle callback. We just invoke the service "start" service callback on the service instance, as well as on all eventual service composites. We take care to check if a start callback returns a Map, which is meant to contain some additional properties which must be appended to existing service properties. Such extra properties takes precedence over existing service properties.

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException

stop

public void stop(Component service)
          throws IllegalArgumentException,
                 IllegalAccessException,
                 InvocationTargetException
Handles the Service's stop lifecycle callback. We just invoke the service "stop" callback on the service instance, as well as on all eventual service composites.

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException

destroy

public void destroy(Component service)
             throws IllegalArgumentException,
                    IllegalAccessException,
                    InvocationTargetException
Handles the Service's destroy lifecycle callback. We just invoke the service "destroy" callback on the service instance, as well as on all eventual service composites.

Throws:
IllegalArgumentException
IllegalAccessException
InvocationTargetException


Copyright © 2011 Apache Software Foundation. All Rights Reserved.