Interface FactoryComponent

All Superinterfaces:
Component

public interface FactoryComponent extends Component
Interface used to configure the various parameters needed when defining a Dependency Manager factory component. A factory component is a component which can be instantiated multiple times using standard OSGi Factory Configurations.

When a factory configuration is created, an instance of the component is created and the configuration is injected by default in the "updated" callback, which supports the following signatures:

  • callback(Dictionary)
  • callback(Component, Dictionary)
  • callback(Component, Configuration ... configTypes) // type safe configuration interface(s)
  • callback(Configuration ... configTypes) // type safe configuration interface(s)
  • callback(Dictionary, Configuration ... configTypes) // type safe configuration interfaces(s)
  • callback(Component, Dictionary, Configuration ... configTypes) // type safe configuration interfaces(s)

Usage Examples

Here is a sample showing a Hello component, which can be instantiated multiple times using a factory configuration:
 
 public class Activator extends DependencyActivatorBase {
     &Override
     public void init(BundleContext context, DependencyManager dm) throws Exception {
         FactoryComponent factoryComponent = createFactoryComponent()
             .setFactoryPid("my.factory.pid")
             .setInterface(MySevice.class.getName(), null)
             .setImplementation(MyComponent.class)
             .setConfigType(MyConfig.class);
         dm.add(factoryComponent);
     }
 }
 
 public interface MyConfig {
     int getPort();
     String getAddress();
 }
 
 public class MyComponent implements MyService {
     void updated(MyConfig cnf) {
         int port = cnf.getPort();
         String addr = cnf.getAddress();
         ...
     }
 }
  
See Also:
  • Method Details

    • setScope

      Sets the component scope.
      Specified by:
      setScope in interface Component
      Parameters:
      scope - the component scope (default=SINGLETON)
      Returns:
      this component
    • setImplementation

      FactoryComponent setImplementation(Object implementation)
      Sets the implementation for this component. You can actually specify an instance you have instantiated manually, or a Class that will be instantiated using its default constructor when the required dependencies are resolved, effectively giving you a lazy instantiation mechanism. There are four special methods that are called when found through reflection to give you life cycle management options:
      1. init() is invoked after the instance has been created and dependencies have been resolved, and can be used to initialize the internal state of the instance or even to add more dependencies based on runtime state
      2. start() is invoked right before the service is registered
      3. stop() is invoked right after the service is unregistered
      4. destroy() is invoked after all dependencies are removed
      In short, this allows you to initialize your instance before it is registered, perform some post-initialization and pre-destruction code as well as final cleanup. If a method is not defined, it simply is not called, so you can decide which one(s) you need. If you need even more fine-grained control, you can register as a service state listener too.
      Specified by:
      setImplementation in interface Component
      Parameters:
      implementation - the implementation
      Returns:
      this component
      See Also:
    • add

      FactoryComponent add(Dependency... dependencies)
      Adds dependency(ies) to this component, atomically. If the component is already active or if you add dependencies from the init method, then you should add all the dependencies in one single add method call (using the varargs argument), because this method may trigger component activation (like the ServiceTracker.open() method does).
      Specified by:
      add in interface Component
      Parameters:
      dependencies - the dependencies to add.
      Returns:
      this component
    • remove

      Removes a dependency from the component.
      Specified by:
      remove in interface Component
      Parameters:
      d - the dependency to remove
      Returns:
      this component
    • add

      Adds a component state listener to this component.
      Specified by:
      add in interface Component
      Parameters:
      listener - the state listener
    • remove

      Removes a component state listener from this component.
      Specified by:
      remove in interface Component
      Parameters:
      listener - the state listener
    • setInterface

      FactoryComponent setInterface(String serviceName, Dictionary<?,?> properties)
      Sets the public interface under which this component should be registered in the OSGi service registry.
      Specified by:
      setInterface in interface Component
      Parameters:
      serviceName - the name of the service interface
      properties - the properties for this service
      Returns:
      this component
    • setInterface

      FactoryComponent setInterface(String[] serviceNames, Dictionary<?,?> properties)
      Sets the public interfaces under which this component should be registered in the OSGi service registry.
      Specified by:
      setInterface in interface Component
      Parameters:
      serviceNames - the names of the service interface
      properties - the properties for these services
      Returns:
      this component
    • setInterface

      FactoryComponent setInterface(Class<?> serviceName, Dictionary<?,?> properties)
      Sets the public interface under which this component should be registered in the OSGi service registry.
      Specified by:
      setInterface in interface Component
      Parameters:
      serviceName - the name of the service interface
      properties - the properties for this service
      Returns:
      this component
    • setInterface

      FactoryComponent setInterface(Class<?>[] serviceNames, Dictionary<?,?> properties)
      Sets the public interfaces under which this component should be registered in the OSGi service registry.
      Specified by:
      setInterface in interface Component
      Parameters:
      serviceNames - the names of the service interface
      properties - the properties for these services
      Returns:
      this component
    • setAutoConfig

      FactoryComponent setAutoConfig(Class<?> clazz, boolean autoConfig)
      Configures auto configuration of injected classes in the component instance. The following injections are currently performed, unless you explicitly turn them off:
      BundleContext
      the bundle context of the bundle
      ServiceRegistration
      the service registration used to register your service
      DependencyManager
      the dependency manager instance
      Component
      the component instance of the dependency manager
      Specified by:
      setAutoConfig in interface Component
      Parameters:
      clazz - the class (from the list above)
      autoConfig - false to turn off auto configuration
    • setAutoConfig

      FactoryComponent setAutoConfig(Class<?> clazz, String instanceName)
      Configures auto configuration of injected classes in the component instance.
      Specified by:
      setAutoConfig in interface Component
      Parameters:
      clazz - the class (from the list above)
      instanceName - the name of the instance to inject the class into
      See Also:
    • setServiceProperties

      FactoryComponent setServiceProperties(Dictionary<?,?> serviceProperties)
      Sets the service properties associated with the component. If the service was already registered, it will be updated.
      Specified by:
      setServiceProperties in interface Component
      Parameters:
      serviceProperties - the properties
    • setCallbacks

      FactoryComponent setCallbacks(String init, String start, String stop, String destroy)
      Sets the names of the methods used as callbacks. These methods, when found, are invoked as part of the life cycle management of the component implementation. The dependency manager will look for a method of this name with the following signatures, in this order:
      1. method(Component component)
      2. method()
      Specified by:
      setCallbacks in interface Component
      Parameters:
      init - the name of the init method
      start - the name of the start method
      stop - the name of the stop method
      destroy - the name of the destroy method
      Returns:
      the component
    • setCallbacks

      FactoryComponent setCallbacks(Object instance, String init, String start, String stop, String destroy)
      Sets the names of the methods used as callbacks. These methods, when found, are invoked on the specified instance as part of the life cycle management of the component implementation.

      See setCallbacks(String init, String start, String stop, String destroy) for more information on the signatures. Specifying an instance means you can create a manager that will be invoked whenever the life cycle of a component changes and this manager can then decide how to expose this life cycle to the actual component, offering an important indirection when developing your own component models.

      Specified by:
      setCallbacks in interface Component
      Returns:
      this component
    • setFactory

      FactoryComponent setFactory(Object factory, String createMethod)
      Sets the factory to use to create the implementation. You can specify both the factory class and method to invoke. The method should return the implementation, and can use any method to create it. Actually, this can be used together with setComposition to create a composition of instances that work together to implement a component. The factory itself can also be instantiated lazily by not specifying an instance, but a Class.
      Specified by:
      setFactory in interface Component
      Parameters:
      factory - the factory instance or class
      createMethod - the name of the create method
      Returns:
      this component
    • setFactory

      FactoryComponent setFactory(String createMethod)
      Sets the factory to use to create the implementation. You specify the method to invoke. The method should return the implementation, and can use any method to create it. Actually, this can be used together with setComposition to create a composition of instances that work together to implement a component.

      Note that currently, there is no default for the factory, so please use setFactory(factory, createMethod) instead.

      Specified by:
      setFactory in interface Component
      Parameters:
      createMethod - the name of the create method
      Returns:
      this component
    • setComposition

      FactoryComponent setComposition(Object instance, String getMethod)
      Sets the instance and method to invoke to get back all instances that are part of a composition and need dependencies injected. All of them will be searched for any of the dependencies. The method that is invoked must return an Object[].
      Specified by:
      setComposition in interface Component
      Parameters:
      instance - the instance that has the method
      getMethod - the method to invoke
      Returns:
      this component
    • setComposition

      FactoryComponent setComposition(String getMethod)
      Sets the method to invoke on the service implementation to get back all instances that are part of a composition and need dependencies injected. All of them will be searched for any of the dependencies. The method that is invoked must return an Object[].
      Specified by:
      setComposition in interface Component
      Parameters:
      getMethod - the method to invoke
      Returns:
      this component
    • setDebug

      FactoryComponent setDebug(String label)
      Activate debug for this component. Informations related to dependency processing will be displayed using osgi log service, our to standard output if no log service is currently available.
      Specified by:
      setDebug in interface Component
      Parameters:
      label -
    • setFactoryPid

      FactoryComponent setFactoryPid(String factoryPid)
      Sets the pid matching the factory configuration
      Parameters:
      factoryPid - the pid matching the factory configuration
    • setFactoryPid

      FactoryComponent setFactoryPid(Class<?> clazz)
      Sets the pid matching the factory configuration using the specified class. The FQDN of the specified class will be used as the factory pid.
      Parameters:
      clazz - the class whose FQDN name will be used for the factory pid
    • setUpdated

      FactoryComponent setUpdated(String update)
      Sets the method name that will be notified when the factory configuration is created/updated. By default, the callback name used is updated TODO describe supported signatures
      Parameters:
      update - the method name that will be notified when the factory configuration is created/updated.
    • setUpdateInstance

      FactoryComponent setUpdateInstance(Object updatedCallbackInstance)
      Sets the object on which the updated callback will be invoked. By default, the callback is invoked on the component instance.
      Parameters:
      updatedCallbackInstance - the object on which the updated callback will be invoked.
    • setPropagate

      FactoryComponent setPropagate(boolean propagate)
      Sets the propagate flag (true means all public configuration properties are propagated to service properties). By default, public configurations are not propagated.
      Parameters:
      propagate - the propagate flag (false, by default; true means all public configuration properties are propagated to service properties).
    • setConfigType

      FactoryComponent setConfigType(Class<?>... configTypes)
      Sets the configuration type to use instead of a dictionary. The updated callback is assumed to take as arguments the specified configuration types in the same order they are provided in this method. Optionally, the callback may define a Dictionary as the first argument, in order to also get the raw configuration.
      Parameters:
      configTypes - the configuration type to use instead of a dictionary
      See Also:
    • setHeading

      FactoryComponent setHeading(String heading)
      Sets the metatype label used to display the tab name (or section) where the properties are displayed. Example: "Printer Service"
      Parameters:
      heading - the label used to display the tab name (or section) where the properties are displayed.
    • setDesc

      FactoryComponent setDesc(String desc)
      A metatype human readable description of the factory PID this configuration is associated with.
      Parameters:
      desc -
    • setLocalization

      FactoryComponent setLocalization(String localization)
      Points to the metatype basename of the Properties file that can localize the Meta Type informations. The default localization base name for the properties is OSGI-INF/l10n/bundle, but can be overridden by the manifest Bundle-Localization header (see core specification, in section Localization on page 68). You can specify a specific localization basename file using this parameter (e.g. "person" will match person_du_NL.properties in the root bundle directory).
      Parameters:
      localization -
    • add

      Sets metatype MetaData regarding configuration properties.
      Parameters:
      metaData - the metadata regarding configuration properties