Interface ComponentBuilder<B extends ComponentBuilder<B>>
- Type Parameters:
B- the type of a builder that may extends this builder interface (aspect/adapter).
- All Known Subinterfaces:
BundleAdapterBuilder,FactoryPidAdapterBuilder,ServiceAdapterBuilder<T>,ServiceAspectBuilder<T>
Components are the main building blocks for OSGi applications. They can publish themselves as a service, and they can have dependencies. These dependencies will influence their life cycle as component will only be activated when all required dependencies are available. This interface is also the base interface for extended components like aspects, adapters, etc ...
Example of a component that depends on a LogServce service. The dependency is injected by reflection on fields having a compatible type with the LogService interface:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
component(comp -> comp.impl(Pojo.class).withSvc(LogService.class));
}
}
- Author:
- Felix Project Team
-
Method Summary
Modifier and TypeMethodDescriptionautoAdd(boolean autoAdd) Automatically adds this component to its DependencyManager object.autoConfig(Class<?> clazz, boolean autoConfig) Configures OSGi object (BundleContext, Component, etc ...) that will be injected in any field having the same OSGi object type.autoConfig(Class<?> clazz, String field) Configures OSGi object (BundleContext, Component, etc ...) that will be injected in a given field.org.apache.felix.dm.Componentbuild()Builds the real DependencyManager Component.composition(Object instance, String getCompositionMethod) Sets the instance and method to invoke to get back all instances that are part of a composition and need dependencies injected.composition(String getCompositionMethod) Sets the method to invoke on the service implementation to get back all instances that are part of a composition and need dependencies injected.composition(Supplier<Object[]> getCompositionMethod) Sets a java8 method reference to a Supplier that returns all instances that are part of a composition and need dependencies injected.Activates debug modeSets a callback instance and the name of the method used as the "destroy" callback.Sets the name of the method used as the "destroy" callback.destroy(InstanceCb callback) Sets an Object instance method reference used as the "destroy" callback.destroy(InstanceCbComponent callback) Sets an Object instance method reference used as the "destroy" callback.Sets the factory to use when creating the implementation.Configures a factory that can be used to create this component implementation.Configures a factory used to create this component implementation using a Factory object and a "getComposition" factory method.<U> BConfigures a factory that also returns a composition of objects for this component implemenation.<U,V> B Configures a factory used to create this component implementation using a Factory object and a method in the Factory object.Configures the component implementation.Sets a callback instance and the name of the method used as the "init" callback.Sets the name of the method used as the "init" callback.init(InstanceCb callback) Sets an Object instance method reference used as the "init" callback.init(InstanceCbComponent callback) Sets an Object instance method reference used as the "init" callback.listener(org.apache.felix.dm.ComponentStateListener listener) Adds a component state listener to this component.properties(String name, Object value, Object... rest) Sets the components's service properties using varargs.properties(Dictionary<?, ?> properties) Sets the component's service propertiesproperties(FluentProperty... properties) Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry.Sets the public interfaces under which this component should be registered in the OSGi service registry.Sets the public interfaces under which this component should be registered in the OSGi service registry.provides(Class<?>[] ifaces, Dictionary<?, ?> properties) Sets the public interfaces under which this component should be registered in the OSGi service registry.provides(Class<?>[] ifaces, FluentProperty... properties) Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry.provides(Class<?> iface, Dictionary<?, ?> properties) Sets the public interface under which this component should be registered in the OSGi service registry.provides(Class<?> iface, FluentProperty... properties) Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry.Sets the public interfaces under which this component should be registered in the OSGi service registry.Sets the public interfaces under which this component should be registered in the OSGi service registry.provides(String[] ifaces, Dictionary<?, ?> properties) Sets the public interfaces under which this component should be registered in the OSGi service registry.provides(String[] ifaces, FluentProperty... properties) Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry.provides(String iface, Dictionary<?, ?> properties) Sets the public interface under which this component should be registered in the OSGi service registry.provides(String iface, FluentProperty... properties) Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releasescope(org.apache.felix.dm.Component.ServiceScope scope) Configures the component scope.Sets the name of the method used as the "start" callback.Sets a callback instance and the name of the method used as the "start" callback.start(InstanceCb callback) Sets an Object instance method reference used as the "start" callback.start(InstanceCbComponent callback) Sets an Object instance method reference used as the "start" callback.Sets a callback instance and the name of the method used as the "stop" callback.Sets the name of the method used as the "stop" callback.stop(InstanceCb callback) Sets an Object instance method reference used as the "stop" callback.stop(InstanceCbComponent callback) Sets an Object instance method reference used as the "stop" callback.withBundle(Consumer<BundleDependencyBuilder> consumer) Adds a bundle dependency.default BAdds a configuration dependency using a configuration type.default BAdds multiple configuration dependencies in one single call.withCnf(Consumer<ConfigurationDependencyBuilder> consumer) Adds a configuration dependency.withDep(org.apache.felix.dm.Dependency dependency) Adds a generic Dependency Manager dependency.<U> BwithFuture(CompletableFuture<U> future, Consumer<FutureDependencyBuilder<U>> consumer) Adds a CompletableFuture dependency.default BAdds in one shot multiple service dependencies injected in compatible class fields.default BAdds in one shot multiple service dependencies injected in compatible class fields.default BAdds a service dependency injected in compatible class fields.default <T> BAdds a service dependency injected in compatible class fields.default <T> BAdds a service dependency injected in a given compatible class field.<U> BwithSvc(Class<U> service, Consumer<ServiceDependencyBuilder<U>> consumer) Adds a service dependency built using a Consumer lambda that is provided with a ServiceDependencyBuilder.
-
Method Details
-
scope
Configures the component scope. -
impl
Configures the component implementation. Can be a class name, or a component implementation object.- Parameters:
impl- the component implementation (a class, or an Object).- Returns:
- this builder
-
factory
Sets the factory to use when creating 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.- Parameters:
factory- the factory instance, or the factory class.createMethod- the create method called on the factory in order to instantiate the component.- Returns:
- this builder
-
factory
Configures a factory that can be used to create this component implementation. Example:factory(ComponentImpl::new)", or "factory(() -> new ComponentImpl())- Parameters:
create- the factory used to create the component implementation.- Returns:
- this builder
-
factory
Configures a factory used to create this component implementation using a Factory object and a method in the Factory object. Example:factory(Factory::new, Factory::create)- Type Parameters:
U- the type of the factory returned by the supplierV- the type of the object that is returned by the factory create method.- Parameters:
factory- the function used to create the Factory itselfcreate- the method reference on the Factory method that is used to create the Component implementation- Returns:
- this builder
-
factory
Configures a factory used to create this component implementation using a Factory object and a "getComposition" factory method. the Factory method may then return multiple objects that will be part of this component implementation, and all of them will be searched when injecting any of the dependencies. Example:CompositionManager mngr = new CompositionManager(); ... factory(mngr::create, mngr::getComposition)- Parameters:
factory- the supplier used to return the main component implementation instancegetComposition- the supplier that returns the list of instances that are part of the component implementation classes.- Returns:
- this builder
-
factory
Configures a factory that also returns a composition of objects for this component implemenation. Example:
Here, the CompositionManager will act as a factory (the create method will return the component implementation object), the CompositionManager.getComposition() method will return all the objects that are also part of the component implementation, and all of them will be searched for injecting any of the dependencies.factory(CompositionManager::new, CompositionManager::create, CompositionManager::getComposition).- Type Parameters:
U- the type of the object returned by the supplier factory- Parameters:
factory- the function used to create the Factory itselfcreate- the Factory method used to create the main component implementation objectgetComposition- the Factory method used to return the list of objects that are also part of the component implementation.- Returns:
- this builder
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the public interface to register in the OSGI service registry.- Returns:
- this builder
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the public interface to register in the OSGI service registry.name- a property name for the provided servicevalue- a property value for the provided servicerest- the rest of property name/value pairs.- Returns:
- this builder.
-
provides
Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry. Warning: you can only use this method if you compile your application using the "-parameters" javac option. code example:provides(MyService.class, property1 -> "value1", property2 -> 123);- Parameters:
iface- the public interface to register in the OSGI service registry.properties- a list of fluent service properties for the provided service. You can specify a list of lambda expression, each one implementing theFluentPropertyinterface that allows to define a property name using a lambda parameter.- Returns:
- this builder.
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the public interface to register in the OSGI service registry.properties- the properties for the provided service- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- list of services provided by the component.- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.name- a property name for the provided servicevalue- a property value for the provided servicerest- the rest of property name/value pairs.- Returns:
- this builder.
-
provides
Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interfaces under which this component should be registered in the OSGi service registry. Warning: you can only use this method if you compile your application using the "-parameters" javac option. code example:provides(new Class[] { MyService.class, MyService2.class }, property1 -> "value1", property2 -> 123);- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.properties- a list of fluent service properties for the provided service. You can specify a list of lambda expression, each one implementing theFluentPropertyinterface that allows to define a property name using a lambda parameter.- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.properties- the properties for the provided service- Returns:
- this builder.
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the service provided by this component.- Returns:
- this builder.
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the public interface to register in the OSGI service registry.name- a property name for the provided servicevalue- a property value for the provided servicerest- the rest of property name/value pairs.- Returns:
- this builder.
-
provides
Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interface under which this component should be registered in the OSGi service registry. Warning: you can only use this method if you compile your application using the "-parameters" javac option. code example:provides(MyService.class, property1 -> "value1", property2 -> 123);- Parameters:
iface- the public interface to register in the OSGI service registry.properties- a list of fluent service properties for the provided service. You can specify a list of lambda expression, each one implementing theFluentPropertyinterface that allows to define a property name using a lambda parameter.- Returns:
- this builder.
-
provides
Sets the public interface under which this component should be registered in the OSGi service registry.- Parameters:
iface- the public interface to register in the OSGI service registry.properties- the properties for the provided service- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- the list of services provided by the component.- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.name- a property name for the provided servicevalue- a property value for the provided servicerest- the rest of property name/value pairs.- Returns:
- this builder.
-
provides
Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the public interfaces under which this component should be registered in the OSGi service registry. Warning: you can only use this method if you compile your application using the "-parameters" javac option. code example:provides(new Class[] { MyService.class, MyService2.class }, property1 -> "value1", property2 -> 123);- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.properties- a list of fluent service properties for the provided service. You can specify a list of lambda expression, each one implementing theFluentPropertyinterface that allows to define a property name using a lambda parameter.- Returns:
- this builder.
-
provides
Sets the public interfaces under which this component should be registered in the OSGi service registry.- Parameters:
ifaces- the public interfaces to register in the OSGI service registry.properties- the properties for the provided service- Returns:
- this builder.
-
properties
Sets the component's service properties- Parameters:
properties- the component's service properties- Returns:
- this builder
-
properties
Sets the components's service properties using varargs. The number of parameters must be even, representing a list of pair property key-value.Example: properties("param1", "value1", "service.ranking", 3)- Parameters:
name- the first property namevalue- the first property valuerest- the rest of properties key/value pairs.- Returns:
- this builder
-
properties
Deprecated.Fluent properties are only supported using java8 and this method will be removed in next DM releaseSets the components's service properties using List of lamda properties. Example:
When you use this method, you must compile your source code using the "-parameters" option, and the "arg0" parameter name is now allowed.properties(param1 -> "value1, param2 -> 2);- Parameters:
properties- the fluent properties- Returns:
- this builder
-
withSvc
Adds a service dependency built using a Consumer lambda that is provided with a ServiceDependencyBuilder.- Type Parameters:
U- the type of the dependency service- Parameters:
service- the serviceconsumer- the lambda used to build the service dependency- Returns:
- this builder.
-
withSvc
Adds in one shot multiple service dependencies injected in compatible class fields.- Parameters:
services- some dependencies to inject in compatible class fields.- Returns:
- this builder
-
withSvc
Adds in one shot multiple service dependencies injected in compatible class fields.- Parameters:
required- true if the dependency is required, false if notservices- some dependencies to inject in compatible class fields.- Returns:
- this builder
-
withSvc
Adds a service dependency injected in compatible class fields.- Parameters:
service- a service dependencyrequired- true if the dependency is required, false if not- Returns:
- this builder
-
withSvc
Adds a service dependency injected in compatible class fields.- Type Parameters:
T- the service dependency type- Parameters:
service- the service dependency.filter- the service filterrequired- true if the dependency is required, false if not- Returns:
- this builder
-
withSvc
Adds a service dependency injected in a given compatible class field.- Type Parameters:
T- the service dependency type- Parameters:
service- the service dependencyfilter- the service filterfield- the class field when the dependency has to be injectedrequired- true if the dependency is required, false if not- Returns:
- this builder
-
withCnf
Adds a configuration dependency.- Parameters:
consumer- the lambda used to build the configuration dependency.- Returns:
- this builder.
-
withCnf
Adds multiple configuration dependencies in one single call. All configurations are injected by default in the "updated" callback.- Parameters:
pids- list of configuration pids.- Returns:
- this builder
-
withCnf
Adds a configuration dependency using a configuration type. The configuration is injected in an updated callback which takes in argument an implementation of the specified configuration type.- Parameters:
configType- the configuration type that will be injected to the "updated" callback- Returns:
- this builder
- See Also:
-
withBundle
Adds a bundle dependency.- Parameters:
consumer- the lambda used to build the bundle dependency.- Returns:
- this builder.
-
withFuture
Adds a CompletableFuture dependency.- Type Parameters:
U- the type of the result of the CompletableFuture.- Parameters:
future- a CompletableFuture on which the dependency will wait forconsumer- the builder used to build the dependency- Returns:
- this builder.
-
withDep
Adds a generic Dependency Manager dependency. You can use this method if you want to add a dependency that you have built using the Dependency Manager API, or a specific custom DM dependency (like toggles, etc ...). -
init
Sets the name of the method used as the "init" callback. This method, when found, is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked.The dependency manager will look for a method of this name with the following signatures, in this order:
- method(Component component)
- method()
- Parameters:
callback- the callback name- Returns:
- this builder.
-
init
Sets a callback instance and the name of the method used as the "init" callback. This method, when found, is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked.The dependency manager will look for a method of this name with the following signatures, in this order:
- method(Component component)
- method()
- Parameters:
callbackInstance- a callback instance object the callback is invoked oncallback- the callback name- Returns:
- this builder.
-
init
Sets an Object instance method reference used as the "init" callback. It is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked. The method does not take any parameters.- Parameters:
callback- an Object instance method reference. The method does not take any parameters.- Returns:
- this builder
-
init
Sets an Object instance method reference used as the "init" callback. It is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked. The method takes as argument a Component parameter.- Parameters:
callback- an Object instance method reference. The method takes as argument a Component parameter.- Returns:
- this builder
-
start
Sets a callback instance and the name of the method used as the "start" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callback- the callback name- Returns:
- this builder.
-
start
Sets the name of the method used as the "start" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callbackInstance- a callback instance object the callback is invoked oncallback- the callback name- Returns:
- this builder.
-
start
Sets an Object instance method reference used as the "start" callback. This method is invoked as part of the life cycle management of the component implementation. The method does not take any parameters.- Parameters:
callback- an Object instance method reference. The method does not take any parameters.- Returns:
- this builder.
-
start
Sets an Object instance method reference used as the "start" callback. This method is invoked as part of the life cycle management of the component implementation. The method takes as argument a Component parameter.- Parameters:
callback- an Object instance method reference. The method takes as argument a Component parameter.- Returns:
- this builder.
-
stop
Sets the name of the method used as the "stop" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callback- the callback name- Returns:
- this builder.
-
stop
Sets a callback instance and the name of the method used as the "stop" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callbackInstance- a callback instance object the callback is invoked oncallback- the callback name- Returns:
- this builder.
-
stop
Sets an Object instance method reference used as the "stop" callback. It is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked. The method does not take any parameters.- Parameters:
callback- an Object instance method reference. The method does not take any parameters.- Returns:
- this builder
-
stop
Sets an Object instance method reference used as the "stop" callback. This method is invoked as part of the life cycle management of the component implementation. The method takes as argument a Component parameter.- Parameters:
callback- an Object instance method reference. The method takes as argument a Component parameter.- Returns:
- this builder.
-
destroy
Sets the name of the method used as the "destroy" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callback- the callback name- Returns:
- this builder.
-
destroy
Sets a callback instance and the name of the method used as the "destroy" callback. This method, when found, is 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:
- method(Component component)
- method()
- Parameters:
callbackInstance- a callback instance object the callback is invoked oncallback- the callback name- Returns:
- this builder.
-
destroy
Sets an Object instance method reference used as the "destroy" callback. It is invoked as part of the life cycle management of the component implementation. This method is useful because when it is invoked, all required dependencies defines in the Activator are already injected, and you can then add more extra dependencies from the init() method. And once all extra dependencies will be available and injected, then the "start" callback will be invoked. The method does not take any parameters.- Parameters:
callback- an Object instance method reference. The method does not take any parameters.- Returns:
- this builder
-
destroy
Sets an Object instance method reference used as the "destroy" callback. This method is invoked as part of the life cycle management of the component implementation. The method takes as argument a Component parameter.- Parameters:
callback- an Object instance method reference. The method takes as argument a Component parameter.- Returns:
- this builder.
-
autoConfig
Configures OSGi object (BundleContext, Component, etc ...) that will be injected in any field having the same OSGi object type.- Parameters:
clazz- the OSGi object type (BundleContext, Component, DependencyManager).autoConfig- true if the OSGi object has to be injected, false if not- Returns:
- this builder
-
autoConfig
Configures OSGi object (BundleContext, Component, etc ...) that will be injected in a given field.- Parameters:
clazz- the OSGi object type (BundleContext, Component, DependencyManager).field- the field that will be injected with the OSGI object- Returns:
- this builder
-
debug
Activates debug mode- Parameters:
label- the debug label- Returns:
- this builder
-
autoAdd
Automatically adds this component to its DependencyManager object. When a lambda builds a Component using this builder, by default the built component is auto added to its DependencyManager object, unless you invoke autoAdd(false).- Parameters:
autoAdd- true for automatically adding this component to the DependencyManager object, false if not- Returns:
- this builder
-
composition
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 to inject any of the dependencies. The method that is invoked must return anObject[].- Parameters:
getCompositionMethod- the method to invoke- Returns:
- this builder
-
composition
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 to inject any of the dependencies. The method that is invoked must return anObject[].- Parameters:
instance- the instance that has the methodgetCompositionMethod- the method to invoke- Returns:
- this builder
-
composition
Sets a java8 method reference to a Supplier that returns 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 anObject[].- Parameters:
getCompositionMethod- the method to invoke- Returns:
- this builder
-
listener
Adds a component state listener to this component.- Parameters:
listener- the state listener
-
build
org.apache.felix.dm.Component build()Builds the real DependencyManager Component.- Returns:
- the real DependencyManager Component.
-