Class DependencyManagerActivator

java.lang.Object
org.apache.felix.dm.lambda.DependencyManagerActivator
All Implemented Interfaces:
org.osgi.framework.BundleActivator

public abstract class DependencyManagerActivator extends Object implements org.osgi.framework.BundleActivator
Defines a base for Activators in order to build DependencyManager Components using a java8 style.

Code example using auto configured fields:

 
 
 import org.apache.felix.dm.lambda.DependencyManagerActivator;

 public class Activator extends DependencyManagerActivator {    
     public void init(BundleContext ctx, DependencyManager dm) throws Exception {
         component(comp -> comp
             .provides(Service.class, property -> "value")
             .impl(ServiceImpl.class)            
             .withSvc(LogService.class, ConfigurationAdmni.class) // both services are required and injected in class fields with compatible types.           
     }
 }
 
Code example using reflection callbacks:
 
 import org.apache.felix.dm.lambda.DependencyManagerActivator;

 public class Activator extends DependencyManagerActivator {    
     public void init(BundleContext ctx, DependencyManager dm) throws Exception {
         component(comp -> comp
             .provides(Service.class, property -> "value")
             .impl(ServiceImpl.class)            
             .withSvc(LogService.class, svc -> svc.add("setLog"))              
             .withSvc(ConfigurationAdmni.class, svc -> svc.add("setConfigAdmin")))                
     }
 }
 
Code example using method references:
 
 import org.apache.felix.dm.lambda.DependencyManagerActivator;

 public class Activator extends DependencyManagerActivator {    
     public void init(BundleContext ctx, DependencyManager dm) throws Exception {
         component(comp -> comp
             .provides(Service.class, property -> "value")
             .impl(ServiceImpl.class)            
             .withSvc(LogService.class, svc -> svc.add(ServiceImpl::setLog))              
             .withSvc(ConfigurationAdmni.class, svc -> svc.add(ServiceImpl::setConfigAdmin)))                
     }
 }
 
When a dependency is not explicitly defined as "required" or "optional", then it is assumed to be optional by default, like it is the case with the original DM API. You can change the default mode using the "org.apache.felix.dependencymanager.lambda.defaultRequiredDependency system property" (see Felix dm-lambda online documentation).
  • Constructor Details

    • DependencyManagerActivator

      public DependencyManagerActivator()
  • Method Details

    • start

      public void start(org.osgi.framework.BundleContext context) throws Exception
      Our Activator is starting.
      Specified by:
      start in interface org.osgi.framework.BundleActivator
      Throws:
      Exception
    • stop

      public void stop(org.osgi.framework.BundleContext context) throws Exception
      Our Activator is stopped.
      Specified by:
      stop in interface org.osgi.framework.BundleActivator
      Throws:
      Exception
    • init

      protected abstract void init(org.osgi.framework.BundleContext ctx, org.apache.felix.dm.DependencyManager dm) throws Exception
      Sub classes must override this method in order to build some DM components.
      Parameters:
      ctx - the context associated to the bundle
      dm - the DependencyManager assocaited to this activator
      Throws:
      Exception - if the activation fails
    • destroy

      protected void destroy() throws Exception
      Sub classes may override this method that is called when the Activator is stopped.
      Throws:
      Exception - if the deactivation fails
    • getDM

      public org.apache.felix.dm.DependencyManager getDM()
      Returns the DependencyManager used to create/managed DM Components.
      Returns:
      the DependencyManager associated to this Activator
    • getBC

      public org.osgi.framework.BundleContext getBC()
      Returns the bundle context that is associated with this bundle.
      Returns:
      the bundle context
    • component

      protected ComponentBuilder<?> component()
      Creates a Component builder that can be used to create a DM Component.
      Returns:
      a Component builder that can be used to create a DM Component.
    • aspect

      protected <T> ServiceAspectBuilder<T> aspect(Class<T> aspectType)
      Creates a service Aspect builder that can be used to create a DM Aspect Component.
      Type Parameters:
      T - the aspect service type
      Parameters:
      aspectType - the aspect service
      Returns:
      a service Aspect builder.
    • adapter

      protected <T> ServiceAdapterBuilder<T> adapter(Class<T> adaptee)
      Creates a service Adapter builder that can be used to create a DM Adapter Component.
      Type Parameters:
      T - the adapted service type.
      Parameters:
      adaptee - the adapted service
      Returns:
      a service Adapter builder.
    • component

      protected org.apache.felix.dm.Component component(Consumer<ComponentBuilder<?>> consumer)
      Builds a DM Component using a Java8 style ComponentBuilder.
      Parameters:
      consumer - the lambda that will use the ComponentBuilder for building the DM component. The component is auto-added to the DependencyManager, unless the lambda calls the ComponentBuilder.autoAdd(false) method.
      Returns:
      a newly built DM component.
    • aspect

      protected <T> org.apache.felix.dm.Component aspect(Class<T> aspect, Consumer<ServiceAspectBuilder<T>> consumer)
      Builds a DM Aspect Component using a Java8 style AspectBuilder. The component is auto-added to the DependencyManager, unless the lambda calls the AspectBuilder.autoAdd(false) method.
      Type Parameters:
      T - the aspect service type
      Parameters:
      aspect - the aspect service
      consumer - the lambda that will use the AspectBuilder for building the DM aspect component.
      Returns:
      the DM component build by the consumer of the aspect builder
    • adapter

      protected <T> org.apache.felix.dm.Component adapter(Class<T> adaptee, Consumer<ServiceAdapterBuilder<T>> consumer)
      Builds a DM Adapter Component using a Java8 style AdapterBuilder. The component is auto-added to the DependencyManager, unless the lambda calls the AdapterBuilder.autoAdd(false) method.
      Type Parameters:
      T - the adapted service type
      Parameters:
      adaptee - the adapted service
      consumer - the lambda that will use the AdapterBuilder for building the DM adapter component.
      Returns:
      a newly built DM component.
    • factoryPidAdapter

      protected org.apache.felix.dm.Component factoryPidAdapter(Consumer<FactoryPidAdapterBuilder> consumer)
      Builds a DM Factory Configuration Adapter Component using a Java8 style FactoryPidAdapterBuilder. The component is auto-added to the DependencyManager, unless the lambda calls the FactoryPidAdapterBuilder.autoAdd(false) method.
      Parameters:
      consumer - the lambda that will use the FactoryPidAdapterBuilder for building the DM factory configuration adapter component.
      Returns:
      a newly built DM component.
    • bundleAdapter

      protected org.apache.felix.dm.Component bundleAdapter(Consumer<BundleAdapterBuilder> consumer)
      Builds a DM Bundle Adapter Component.
      Parameters:
      consumer - the lambda used to build the actual bundle adapter. The component is auto-added to the DependencyManager, unless the lambda calls the BundleAdapter.autoAdd(false) method.
      Returns:
      a newly built DM component.
    • component

      public static ComponentBuilder<?> component(org.apache.felix.dm.DependencyManager dm)
      Creates a Component builder that can be used to create a Component.
      Parameters:
      dm - the DependencyManager object used to create the component builder
      Returns:
      a Component builder that can be used to create a Component.
    • aspect

      public static <T> ServiceAspectBuilder<T> aspect(org.apache.felix.dm.DependencyManager dm, Class<T> aspect)
      Creates a service Aspect builder that can be used to create an Aspect Component.
      Type Parameters:
      T - the aspect service type
      Parameters:
      dm - the DependencyManager object used to register the built component
      aspect - the type of the aspect service
      Returns:
      a service Aspect builder that can be used to create an Aspect Component.
    • adapter

      public static <T> ServiceAdapterBuilder<T> adapter(org.apache.felix.dm.DependencyManager dm, Class<T> adaptee)
      Creates a service Adapter builder that can be used to create an Adapter Component.
      Type Parameters:
      T - the adapted service type
      Parameters:
      dm - the DependencyManager object used to register the built component
      adaptee - the type of the adaptee service
      Returns:
      a service Adapter builder that can be used to create an Adapter Component.
    • factoryPidAdapter

      public static FactoryPidAdapterBuilder factoryPidAdapter(org.apache.felix.dm.DependencyManager dm)
      Creates a factory pid adapter that can be used to create a factory adapter Component.
      Parameters:
      dm - the DependencyManager object used to register the built component
      Returns:
      a factory pid adapter that can be used to create a factory adapter Component.
    • bundleAdapter

      public static BundleAdapterBuilder bundleAdapter(org.apache.felix.dm.DependencyManager dm)
      Creates a bundle adapter builder that can be used to create a DM bundle adapter Component.
      Parameters:
      dm - the DependencyManager object used to create the bundle adapter builder.
      Returns:
      a bundle adapter builder that can be used to create a DM bundle adapter Component.
    • serviceDependency

      public static <T> ServiceDependencyBuilder<T> serviceDependency(org.apache.felix.dm.Component component, Class<T> service)
      Creates a DM ServiceDependency builder.
      Type Parameters:
      T - the service dependency type
      Parameters:
      component - the component on which you want to build a new service dependency using the returned builder
      service - the service dependency type.
      Returns:
      a DM ServiceDependency builder.
    • confDependency

      public static ConfigurationDependencyBuilder confDependency(org.apache.felix.dm.Component component)
      Creates a DM Configuration Dependency builder.
      Parameters:
      component - the component on which you want to build a new configuration dependency using the returned builder
      Returns:
      a DM Configuration Dependency builder.
    • bundleDependency

      public static BundleDependencyBuilder bundleDependency(org.apache.felix.dm.Component component)
      Creates a DM Bundle Dependency builder.
      Parameters:
      component - the component on which you want to build a new bundle dependency using the returned builder
      Returns:
      a DM Configuration Dependency builder.
    • futureDependency

      public static <F> FutureDependencyBuilder<F> futureDependency(org.apache.felix.dm.Component component, CompletableFuture<F> future)
      Creates a DM CompletableFuture Dependency builder.
      Type Parameters:
      F - the type of the CompletableFuture result.
      Parameters:
      component - the component on which you want to build a new completable future dependency using the returned builder.
      future - the future the dependency built using the returned builder will depend on.
      Returns:
      a CompletableFuture dependency builder.
    • component

      public static org.apache.felix.dm.Component component(org.apache.felix.dm.DependencyManager dm, Consumer<ComponentBuilder<?>> consumer)
      Builds a component using a lambda and a component builder
      Parameters:
      dm - the DependencyManager where the component is auto-added (unless the component.autoAdd(false) is called)
      consumer - a lambda that is called to build the component. When the lambda is called, it will be provided with a ComponentBuilder object that is used to build the actual DM component.
      Returns:
      the built DM component.
    • component

      public static void component(org.apache.felix.dm.Component comp, Consumer<ComponentBuilder<?>> consumer)
      Update an existing component. Typically, this method can be used from a Component.init method, where more dependencies has to be added.
      Parameters:
      comp - an existing DM component
      consumer - the lambda that will be used to update the component
    • aspect

      public static <T> org.apache.felix.dm.Component aspect(org.apache.felix.dm.DependencyManager dm, Class<T> aspect, Consumer<ServiceAspectBuilder<T>> consumer)
      Builds an aspect DM Component.
      Type Parameters:
      T - the aspect service type
      Parameters:
      dm - the DependencyManager object used to register the built component
      aspect - the type of the aspect service
      consumer - a lambda used to build the DM aspect component
      Returns:
      a new DM aspect component. The aspect component is auto-added into the dm object, unless the lambda calls the AspectBuilder.autoAdd(false) method.
    • adapter

      public static <T> org.apache.felix.dm.Component adapter(org.apache.felix.dm.DependencyManager dm, Class<T> adaptee, Consumer<ServiceAdapterBuilder<T>> consumer)
      Builds an adapter DM Component.
      Type Parameters:
      T - the adapted service type
      Parameters:
      dm - the DependencyManager object used to register the built component
      adaptee - the type of the adapted service
      consumer - a lambda used to build the DM adapter component
      Returns:
      a new DM adapter component. The adapter component is auto-added into the dm object, unless the lambda calls the AspectBuilder.autoAdd(false) method is called.
    • bundleAdapter

      public static org.apache.felix.dm.Component bundleAdapter(org.apache.felix.dm.DependencyManager dm, Consumer<BundleAdapterBuilder> consumer)
      Builds a bundle adapter DM Component.
      Parameters:
      dm - the DependencyManager object used to register the built component
      consumer - a lambda used to build the bundle adapter component
      Returns:
      a new bundle adapter component. The adapter component is auto-added into the dm object, unless the lambda calls the AspectBuilder.autoAdd(false) method is called.
    • factoryPidAdapter

      public static org.apache.felix.dm.Component factoryPidAdapter(org.apache.felix.dm.DependencyManager dm, Consumer<FactoryPidAdapterBuilder> consumer)
      Builds a DM factory configuration adapter.
      Parameters:
      dm - the DependencyManager object used to create DM components.
      consumer - a lambda used to build the DM factory configuration adapter component
      Returns:
      a new DM factory configuration adapter component. The adapter component is auto-added into the dm object, unless the lambda calls the FactoryPidAdapterBuilder.autoAdd(false) method is called