public interface ConfigurationDependencyBuilder extends DependencyBuilder<org.apache.felix.dm.ConfigurationDependency>
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:
The return values supported are: primitive types (or their object wrappers), strings, enums, arrays of
primitives/strings, Collection
types, Map
types, Class
es 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:
Class
es and enum values yield null
;
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")));
}
}
Modifier and Type | Method and Description |
---|---|
ConfigurationDependencyBuilder |
optional()
Sets the dependency as optional.
|
ConfigurationDependencyBuilder |
pid(java.lang.String pid)
Sets the pid for this configuration dependency.
|
ConfigurationDependencyBuilder |
propagate()
Sets propagation of the configuration to the service properties (false by default).
|
ConfigurationDependencyBuilder |
propagate(boolean propagate)
Sets propagation of the configuration properties to the service properties (false by default).
|
ConfigurationDependencyBuilder |
required()
Sets the dependency as required.
|
ConfigurationDependencyBuilder |
required(boolean required)
Sets the required flag which determines if this configuration dependency is required or not.
|
<T> ConfigurationDependencyBuilder |
update(CbDictionary<T> callback)
Sets a reference to a "callback(Dictionary)" method from one of the component implementation classes.
|
<T> ConfigurationDependencyBuilder |
update(CbDictionaryComponent<T> callback)
Sets a reference to a "callback(Dictionary, Component)" method from one of the component implementation classes.
|
ConfigurationDependencyBuilder |
update(java.lang.Class<?> configType,
java.lang.Object callbackInstance,
java.lang.String updateMethod)
Sets a callback method to call on a given Object instance when the configuration is updated.
|
ConfigurationDependencyBuilder |
update(java.lang.Class<?> configType,
java.lang.String updateMethod)
Sets a callback method to call on the component implementation class(es) when the configuration is updated.
|
<T> ConfigurationDependencyBuilder |
update(java.lang.Class<T> configType,
InstanceCbConfiguration<T> updated)
Sets a reference to a "callback(Configuration)" method from an Object instance.
|
<T> ConfigurationDependencyBuilder |
update(java.lang.Class<T> configType,
InstanceCbConfigurationComponent<T> updated)
Sets a reference to a "callback(Configuration, Component)" method from an Object instance.
|
<T,U> ConfigurationDependencyBuilder |
update(java.lang.Class<U> configType,
CbConfiguration<T,U> callback)
Sets a reference to a "callback(Configuration)" method from one of the component implementation classes.
|
<T,U> ConfigurationDependencyBuilder |
update(java.lang.Class<U> configType,
CbConfigurationComponent<T,U> callback)
Sets a reference to a "callback(Configuration, Component)" method from one of the component implementation classes.
|
ConfigurationDependencyBuilder |
update(InstanceCbDictionary callback)
Sets a reference to a "callback(Dictionary)" method from an Object instance.
|
ConfigurationDependencyBuilder |
update(InstanceCbDictionaryComponent callback)
Sets a reference to a "callback(Dictionary, Component)" method from an Object instance.
|
ConfigurationDependencyBuilder |
update(java.lang.Object callbackInstance,
java.lang.String updateMethod)
Sets a callback method to call on a given Object instance when the configuration is updated.
|
ConfigurationDependencyBuilder |
update(java.lang.String updateMethod)
Sets a callback method to call on the component implementation class(es) when the configuration is updated.
|
build
ConfigurationDependencyBuilder required(boolean required)
required
- the required flagConfigurationDependencyBuilder required()
ConfigurationDependencyBuilder optional()
ConfigurationDependencyBuilder pid(java.lang.String pid)
pid
- the configuration dependency pid.ConfigurationDependencyBuilder propagate()
ConfigurationDependencyBuilder propagate(boolean propagate)
propagate
- true if all public configuration properties (not starting with a dot) must be propagated to the component service properties (false by default)ConfigurationDependencyBuilder update(java.lang.String updateMethod)
The following callback signatures are supported and searched in the following order:
updateMethod
- the name of the callbackConfigurationDependencyBuilder update(java.lang.Class<?> configType, java.lang.String updateMethod)
The following callback signatures are supported and searched in the following order:
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.ConfigurationDependencyBuilder update(java.lang.Object callbackInstance, java.lang.String updateMethod)
callbackInstance
- the object instance on which the updatedMethod is invokedupdateMethod
- the callback to call on the callbackInstance when the configuration is updated.ConfigurationDependencyBuilder update(java.lang.Class<?> configType, java.lang.Object callbackInstance, java.lang.String updateMethod)
The following callback signatures are supported and searched in the following order:
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.<T> ConfigurationDependencyBuilder update(CbDictionary<T> callback)
T
- The type of the target component implementation class on which the method is invokedcallback
- a reference to a method of one of the component implementation classes.<T> ConfigurationDependencyBuilder update(CbDictionaryComponent<T> callback)
T
- The type of the target component implementation class on which the method is invokedcallback
- a reference to a method callback defined in one of the the component implementation classes.<T,U> ConfigurationDependencyBuilder update(java.lang.Class<U> configType, CbConfiguration<T,U> callback)
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.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.<T,U> ConfigurationDependencyBuilder update(java.lang.Class<U> configType, CbConfigurationComponent<T,U> callback)
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.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.ConfigurationDependencyBuilder update(InstanceCbDictionary callback)
callback
- a reference to an Object instance which takes as argument a Dictionary (null if the configuration is lost).ConfigurationDependencyBuilder update(InstanceCbDictionaryComponent callback)
callback
- a reference to method from an Object instance which takes as argument a Dictionary and a Component<T> ConfigurationDependencyBuilder update(java.lang.Class<T> configType, InstanceCbConfiguration<T> updated)
T
- the type of the configuration interface accepted by the callback method.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<T> ConfigurationDependencyBuilder update(java.lang.Class<T> configType, InstanceCbConfigurationComponent<T> updated)
T
- the type of the configuration interface accepted by the callback method.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.