Interface ConfigurationDependencyBuilder
- All Superinterfaces:
DependencyBuilder<org.apache.felix.dm.ConfigurationDependency>
- reflection based callbacks: you specify a callback method name
- method reference callbacks: you specify a java8 method reference
Callbacks may accept a Dictionary, a Component, or a user defined configuration type interface. If you only specify a pid, by default the callback method name is assumed to be "updated".
Configuration types are a new feature that allows you to specify an interface that is implemented
by DM and such interface is then injected to your callback instead of the actual Dictionary.
Using such configuration interface provides a way for creating type-safe configurations from a actual Dictionary that is
normally injected by Dependency Manager.
The callback accepts in argument an interface that you have to provide, and DM will inject a proxy that converts
method calls from your configuration-type to lookups in the actual map or dictionary. The results of these lookups are then
converted to the expected return type of the invoked configuration method.
As proxies are injected, no implementations of the desired configuration-type are necessary!
The lookups performed are based on the name of the method called on the configuration type. The method names are "mangled" to the following form: [lower case letter] [any valid character]*. Method names starting with get or is (JavaBean convention) are stripped from these prefixes. For example: given a dictionary with the key "foo" can be accessed from a configuration-type using the following method names: foo(), getFoo() and isFoo().
If the property contains a dot (which is invalid in java method names), then dots (".") can be converted using the following conventions:
- if the method name follows the javabean convention and/or kamel casing convention, then each capital letter is assumed to map to a "dot", followed by the same letter in lower case. This means only lower case properties are supported in this case. Example: getFooBar() or fooBar() will map to "foo.bar" property.
- else, if the method name follows the standard OSGi metatype specification, then dots are encoded as "_"; and "_" is encoded as "__". (see OSGi r6 compendium, chapter 105.9.2). Example: "foo_BAR()" is mapped to "foo.BAR" property; "foo__BAR_zoo()" is mapped to "foo_BAR.zoo" property.
The return values supported are: primitive types (or their object wrappers), strings, enums, arrays of
primitives/strings, Collection types, Map types, Classes and interfaces. When an interface is
returned, it is treated equally to a configuration type, that is, it is returned as a proxy.
Arrays can be represented either as comma-separated values, optionally enclosed in square brackets. For example: [ a, b, c ] and a, b,c are both considered an array of length 3 with the values "a", "b" and "c". Alternatively, you can append the array index to the key in the dictionary to obtain the same: a dictionary with "arr.0" => "a", "arr.1" => "b", "arr.2" => "c" would result in the same array as the earlier examples.
Maps can be represented as single string values similarly as arrays, each value consisting of both the key and value separated by a dot. Optionally, the value can be enclosed in curly brackets. Similar to array, you can use the same dot notation using the keys. For example, a dictionary with
"map" => "{key1.value1, key2.value2}"
and a dictionary with
"map.key1" => "value1", "map2.key2" => "value2"
result in the same map being returned.
Instead of a map, you could also define an interface with the methods getKey1() and getKey2 and use
that interface as return type instead of a Map.
In case a lookup does not yield a value from the underlying map or dictionary, the following rules are applied:
- primitive types yield their default value, as defined by the Java Specification;
- string,
Classes and enum values yieldnull; - for arrays, collections and maps, an empty array/collection/map is returned;
- for other interface types that are treated as configuration type a null-object is returned.
Code example with a component that defines a Configuration Dependency using a specific callback method reference, and the method accepts in argument a configuration type (the pid is assumed to be the fqdn of the configuration type):
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
component(comp -> comp
.impl(ServiceImpl.class)
.withCnf(conf -> conf.update(MyConfig.class, ServiceImpl::modified)));
}
}
Code example with a component that defines a Configuration Dependency using a specific callback method reference which accepts a Dictionary in argument:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
component(comp -> comp
.impl(ServiceImpl.class)
.withCnf(conf -> conf.pid("my.pid").update(ServiceImpl::modified)));
}
}
Code example which defines a configuration dependency injected in the "ServiceImpl.updated(Dictionary)" callback:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
component(comp -> comp.impl(ServiceImpl.class).withCnf("my.pid"));
}
}
Code example with a component that defines a Configuration Dependency using a specific callback method name:
public class Activator extends DependencyManagerActivator {
public void init(BundleContext ctx, DependencyManager dm) throws Exception {
component(comp -> comp.impl(ServiceImpl.class).withCnf(conf -> conf.pid("my.pid").update("modified")));
}
}
- Author:
- Felix Project Team
-
Method Summary
Modifier and TypeMethodDescriptionoptional()Sets the dependency as optional.Sets the pid for this configuration dependency.Sets propagation of the configuration to the service properties (false by default).propagate(boolean propagate) Sets propagation of the configuration properties to the service properties (false by default).required()Sets the dependency as required.required(boolean required) Sets the required flag which determines if this configuration dependency is required or not.Sets a callback method to call on a given Object instance when the configuration is updated.Sets a callback method to call on the component implementation class(es) when the configuration is updated.update(Class<T> configType, InstanceCbConfiguration<T> updated) Sets a reference to a "callback(Configuration)" method from an Object instance.update(Class<T> configType, InstanceCbConfigurationComponent<T> updated) Sets a reference to a "callback(Configuration, Component)" method from an Object instance.update(Class<U> configType, CbConfiguration<T, U> callback) Sets a reference to a "callback(Configuration)" method from one of the component implementation classes.update(Class<U> configType, CbConfigurationComponent<T, U> callback) Sets a reference to a "callback(Configuration, Component)" method from one of the component implementation classes.Sets a callback method to call on a given Object instance when the configuration is updated.Sets a callback method to call on the component implementation class(es) when the configuration is updated.update(CbDictionary<T> callback) Sets a reference to a "callback(Dictionary)" method from one of the component implementation classes.update(CbDictionaryComponent<T> callback) Sets a reference to a "callback(Dictionary, Component)" method from one of the component implementation classes.update(InstanceCbDictionary callback) Sets a reference to a "callback(Dictionary)" method from an Object instance.update(InstanceCbDictionaryComponent callback) Sets a reference to a "callback(Dictionary, Component)" method from an Object instance.Methods inherited from interface org.apache.felix.dm.lambda.DependencyBuilder
build
-
Method Details
-
required
Sets the required flag which determines if this configuration dependency is required or not. A configuration dependency is required by default.- Parameters:
required- the required flag- Returns:
- this service dependency
-
required
ConfigurationDependencyBuilder required()Sets the dependency as required. A configuration dependency is required by default.- Returns:
- this service dependency
-
optional
ConfigurationDependencyBuilder optional()Sets the dependency as optional. A configuration dependency is required by default.- Returns:
- this service dependency
-
pid
Sets the pid for this configuration dependency.- Parameters:
pid- the configuration dependency pid.- Returns:
- this builder
-
propagate
ConfigurationDependencyBuilder propagate()Sets propagation of the configuration to the service properties (false by default). All public configuration properties (not starting with a dot) will be propagated to the component service properties.- Returns:
- this builder
-
propagate
Sets propagation of the configuration properties to the service properties (false by default).- Parameters:
propagate- true if all public configuration properties (not starting with a dot) must be propagated to the component service properties (false by default)- Returns:
- this builder
-
update
Sets a callback method to call on the component implementation class(es) when the configuration is updated. When the configuration is lost, the callback is invoked with a null dictionary.The following callback signatures are supported and searched in the following order:
- method(Dictionary)
- method(Component, Dictionary)
- Parameters:
updateMethod- the name of the callback- Returns:
- this builder
-
update
Sets a callback method to call on the component implementation class(es) when the configuration is updated. The callback is invoked with a configuration type argument (null if the configuration is lost).The following callback signatures are supported and searched in the following order:
- method(Dictionary)
- method(Component, Dictionary)
- method(Configuration) // same type as the one specified in the "configType" argument
- method(Component, Configuration) // Configuration has the same type as the one specified in the "configType" argument
- Parameters:
configType- the type of a configuration that is passed as argument to the callbackupdateMethod- the callback to call on the component implementation class(es) when the configuration is updated.- Returns:
- this builder
-
update
Sets a callback method to call on a given Object instance when the configuration is updated. When the updated method is invoked, the Component implementation is not yet instantiated. This method can be typically used by a Factory object which needs the configuration before it can create the actual component implementation instance(s). When the configuration is lost, the callback is invoked with a null dictionary, and the following signatures are supported:- method(Dictionary)
- method(Component, Dictionary)
- Parameters:
callbackInstance- the object instance on which the updatedMethod is invokedupdateMethod- the callback to call on the callbackInstance when the configuration is updated.- Returns:
- this builder
-
update
ConfigurationDependencyBuilder update(Class<?> configType, Object callbackInstance, String updateMethod) Sets a callback method to call on a given Object instance when the configuration is updated. When the updated method is invoked, the Component implementation is not yet instantiated. This method can be typically used by a Factory object which needs the configuration before it can create the actual component implementation instance(s). The callback is invoked with a configuration type argument (null of the configuration is lost).The following callback signatures are supported and searched in the following order:
- method(Dictionary)
- method(Component, Dictionary)
- method(Configuration) // same type as the one specified in the "configType" argument
- method(Component, Configuration) // Configuration has the same type as the one specified in the "configType" argument
- Parameters:
configType- the type of a configuration that is passed as argument to the callbackcallbackInstance- the object instance on which the updatedMethod is invokedupdateMethod- the callback to call on the callbackInstance when the configuration is updated.- Returns:
- this builder
-
update
Sets a reference to a "callback(Dictionary)" method from one of the component implementation classes. The method is invoked with a Dictionary argument (which is null if the configuration is lost).- Type Parameters:
T- The type of the target component implementation class on which the method is invoked- Parameters:
callback- a reference to a method of one of the component implementation classes.- Returns:
- this builder
-
update
Sets a reference to a "callback(Dictionary, Component)" method from one of the component implementation classes. The method is invoked with Dictionary/Component arguments. When the configuration is lost, the Dictionary argument is null.- Type Parameters:
T- The type of the target component implementation class on which the method is invoked- Parameters:
callback- a reference to a method callback defined in one of the the component implementation classes.- Returns:
- this builder
-
update
Sets a reference to a "callback(Configuration)" method from one of the component implementation classes. The method is invoked with a configuration type argument (null if the configuration is lost).- Type Parameters:
T- The type of the target component implementation class on which the method is invokedU- the type of the configuration interface accepted by the callback method.- Parameters:
configType- the type of a configuration that is passed as argument to the callbackcallback- the callback method reference which must point to a method from one of the component implementation classes. The method takes as argument an interface which will be implemented by a dynamic proxy that wraps the actual configuration properties.- Returns:
- this builder
-
update
<T,U> ConfigurationDependencyBuilder update(Class<U> configType, CbConfigurationComponent<T, U> callback) Sets a reference to a "callback(Configuration, Component)" method from one of the component implementation classes. The method is invoked with two args: configuration type, Component. The configuration type argument is null if the configuration is lost.- Type Parameters:
T- The type of the target component implementation class on which the method is invokedU- the type of the configuration interface accepted by the callback method.- Parameters:
configType- the type of a configuration that is passed as argument to the callbackcallback- the reference to a method from one of the component implementation classes. The method takes as argument an interface which will be implemented by a dynamic proxy that wraps the actual configuration properties. It also takes as the second argument a Component object.- Returns:
- this builder
-
update
Sets a reference to a "callback(Dictionary)" method from an Object instance.- Parameters:
callback- a reference to an Object instance which takes as argument a Dictionary (null if the configuration is lost).- Returns:
- this builder
-
update
Sets a reference to a "callback(Dictionary, Component)" method from an Object instance. The method accepts a Dictionary and a Component object. The passed Dictionary is null in case the configuration is lost.- Parameters:
callback- a reference to method from an Object instance which takes as argument a Dictionary and a Component- Returns:
- this builder
-
update
Sets a reference to a "callback(Configuration)" method from an Object instance. The configuration type argument is null if the configuration is lost.- Type Parameters:
T- the type of the configuration interface accepted by the callback method.- Parameters:
configType- the class of the configuration that is passed as argument to the callbackupdated- a reference to an Object instance which takes as argument the given configuration type- Returns:
- this builder
-
update
<T> ConfigurationDependencyBuilder update(Class<T> configType, InstanceCbConfigurationComponent<T> updated) Sets a reference to a "callback(Configuration, Component)" method from an Object instance. The method accepts a configuration type and a Component object. The configuration type argument is null if the configuration is lost.- Type Parameters:
T- the type of the configuration interface accepted by the callback method.- Parameters:
configType- the class of the configuration that is passed as argument to the callbackupdated- a reference to an Object instance which takes as argument a the given configuration type, and a Component object.- Returns:
- this builder
-