Details

Description

If you just register a service in code, you can give the ServiceRegistration to the service and it can update its service properties to reflect what it can discover about its environment. This proposes that services registered through DS should be able to do this too, by calling an updateProperties(Dictionary) method on the ComponentContext. (Since we'd need a spec update to add the method to ComponentContext, I added a new interface that ComponentContextImpl implements).

Right now a service could get Config Admin and modify the properties there, but then (a) the update method is called even though the component itself initiated the changes and (b) the new property values are persisted which is presumably not desired.

According to the spec config admin properties override default property values specified in the component xml. I think that in order to reduce confusion, once a property has been set through config admin it should not be possible to update it through this update method. This also makes implementing this idea easy.

Thanks for the feedback. Actually all of the three component level methods – activate, modified, and deactivate – allow returning a Map. The bind and unbind methods currently are not supported to return a Map.

So for now I have applied the last patch. So we now have the following:

Activate, Modified, Deactivate methods may return Map to set service properties

Namespace "http://felix.apache.org/xmlns/scr/v1.2.0-felix" is required for component
declaration to support this feature

ExtComponentContext provides setServiceProperties method to explicitly set the
service registration properties at any time

I will resolve this issue for now. If we later come up with reasonable use cases to all Map return for bind and unbind we can create a new issue and easily extend the current implementation.

Felix Meschberger
added a comment - 09/May/12 19:27 Thanks for the feedback. Actually all of the three component level methods – activate, modified, and deactivate – allow returning a Map. The bind and unbind methods currently are not supported to return a Map.
So for now I have applied the last patch. So we now have the following:
Activate, Modified, Deactivate methods may return Map to set service properties
Namespace "http://felix.apache.org/xmlns/scr/v1.2.0-felix" is required for component
declaration to support this feature
ExtComponentContext provides setServiceProperties method to explicitly set the
service registration properties at any time
I will resolve this issue for now. If we later come up with reasonable use cases to all Map return for bind and unbind we can create a new issue and easily extend the current implementation.
Thank you very much for your input and patches.

I like your latest patch FELIX-3377-4-fmeschbe.patch. I'd be happy it you applied it. I'm going to look more into whether bind, updated, and deactivate methods can plausibly return a Map to affect service registration properties, I think there are some valid use cases.

David Jencks
added a comment - 09/May/12 07:19 I like your latest patch FELIX-3377 -4-fmeschbe.patch. I'd be happy it you applied it. I'm going to look more into whether bind, updated, and deactivate methods can plausibly return a Map to affect service registration properties, I think there are some valid use cases.

Felix Meschberger
added a comment - 08/May/12 18:43 - edited Updated patch based on FELIX-3377 -4.diff :
use MethodResult to convey the result of the methods
only support Map as a result (in line Map being used to convey configuration to activate)
Properly differentiate between null-result (reset service properties) and void (no action)
Created a new namespace required to support the new functionality
(components with older namespaces are not allowed to return Map)
updated the testcase for the new namespace
My own patch from May 5th is non-functional.

> Also, I can't see any reason the bind/unbind and updated methods (and deactivate) shouldn't be able
> to affect the service properties in the same way.

My patch allows the "updated" method (I call it the modified method) to return service properties.

Allowing the deactivate method to return service properties makes no sense to me. A service registered on behalf of a component is defined with default service registration properties. Those properties may be changed upon component activation. But they should probably be reverted to the original properties before component activation for symmetry reasons. I don't think the deactivate method should be able to define yet another set of properties.

I am not sure, whether the bind and unbind methods should be able to affect the service registration properties. Do you have concrete use cases in mind ?

In any case, I think my mechanism of defining a MethodResult class conveying the result is more flexible that extending the ActivationParameter which is specific to activate style methods (activate, deactivate, and modified).

> but I think that we need to distinguish between a method whose return type is Dictionary but returns null

This distinction may be important for a modified object, right. My patch does not currently support this, but this would be simple: The BaseMethod.invokeMethod currently checks for the result of invoking the method. This could be extended to be multi-state:

If the method is void, return MethodResult.NONE (maybe rename this to MethodResult.VOID)

Else return a MethodResult instance with the actual result, which may in fact be null, is returned

Consequently the callers (ImmediateComponentManager.createImplementationObject and ImmediateComponentManager.modify) should be changed to account for this disctinction.

Felix Meschberger
added a comment - 06/May/12 10:58 > Also, I can't see any reason the bind/unbind and updated methods (and deactivate) shouldn't be able
> to affect the service properties in the same way.
My patch allows the "updated" method (I call it the modified method) to return service properties.
Allowing the deactivate method to return service properties makes no sense to me. A service registered on behalf of a component is defined with default service registration properties. Those properties may be changed upon component activation. But they should probably be reverted to the original properties before component activation for symmetry reasons. I don't think the deactivate method should be able to define yet another set of properties.
I am not sure, whether the bind and unbind methods should be able to affect the service registration properties. Do you have concrete use cases in mind ?
In any case, I think my mechanism of defining a MethodResult class conveying the result is more flexible that extending the ActivationParameter which is specific to activate style methods (activate, deactivate, and modified).
> but I think that we need to distinguish between a method whose return type is Dictionary but returns null
This distinction may be important for a modified object, right. My patch does not currently support this, but this would be simple: The BaseMethod.invokeMethod currently checks for the result of invoking the method. This could be extended to be multi-state:
If the method is void, return MethodResult.NONE (maybe rename this to MethodResult.VOID)
Else return a MethodResult instance with the actual result, which may in fact be null, is returned
Consequently the callers (ImmediateComponentManager.createImplementationObject and ImmediateComponentManager.modify) should be changed to account for this disctinction.

I prefer returning the activate/whatever method result in a method object, but I think that we need to distinguish between a method whose return type is Dictionary but returns null (I think this means that the service properties should be reset to the base properties + config admin properties) and a method whose return type is void (meaning don't do anything to service properties).

Also, I can't see any reason the bind/unbind and updated methods (and deactivate) shouldn't be able to affect the service properties in the same way.

David Jencks
added a comment - 06/May/12 07:13 I prefer returning the activate/whatever method result in a method object, but I think that we need to distinguish between a method whose return type is Dictionary but returns null (I think this means that the service properties should be reset to the base properties + config admin properties) and a method whose return type is void (meaning don't do anything to service properties).
Also, I can't see any reason the bind/unbind and updated methods (and deactivate) shouldn't be able to affect the service properties in the same way.
I'll work on a new patch soon unless you beat me to it

Felix Meschberger
added a comment - 05/May/12 18:51 > I've identified the source of the 3 integration test failures. In rev 1298275 we added to
> ...
> The trivial patch is
You are of course right. Thanks. I applied that patch in Rev. 1334462

Felix Meschberger
added a comment - 05/May/12 18:36 Here is my proposal.
Instead of extending the ActivatorParameter this patch introduces a MethodResult conveying the result.
In addition a new namespace .../scr/v1.2.0-felix is introduced. The component must be declared with this namespace for the return functionality to be available.

David Jencks
added a comment - 05/May/12 04:04 This improves the previous patch by:
including tests of the new "return a map" functionality for activate, modify, and deactivate methods.
reuses the ActivationParameter class to return the results, avoiding the possible concurrency issues of the previous patch.

I've identified the source of the 3 integration test failures. In rev 1298275 we added to ImmediateComponentManager.deleteComponent a line setting m_configurationProperties to null. This is wrong. This field should only be set when confg admin provides an updates set of properties. In particular, for a component without a modify method, Config Admin calls reconfigured, which sets m_configurationProperties to the new properties. Then since there's no modify method the component has to be deleted..... wiping out the m_componentProperties we just set.... and then recreated.

David Jencks
added a comment - 10/Apr/12 05:59 I've identified the source of the 3 integration test failures. In rev 1298275 we added to ImmediateComponentManager.deleteComponent a line setting m_configurationProperties to null. This is wrong. This field should only be set when confg admin provides an updates set of properties. In particular, for a component without a modify method, Config Admin calls reconfigured, which sets m_configurationProperties to the new properties. Then since there's no modify method the component has to be deleted..... wiping out the m_componentProperties we just set.... and then recreated.
The trivial patch is
— a/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/ImmediateComponentManager.java
@@ -141,7 +141,6 @@ public class ImmediateComponentManager extends AbstractComponentManager
m_componentContext = null;
m_properties = null;
m_propertiesOverwrite = null;
m_configurationProperties = null;
}

This has 3 integration test failures for me that I haven't solved yet. On the other hand I'm getting these failures without the changes as well so it is not clear that I've caused them.

I've implemented this additional featue: if the setServiceProperties method is called with null or an empty dictionary, or the activate or modify method with a Map or Dictionary return type returns null or an empty map/dictionary, then the service properties override is turned off and the service properties are reset to the combination of the defaults from config.xml and the config admin settings.

I'm not thrilled with how the return value for activate and modify methods is returned to the caller, and what I've done might have concurrency issues.

David Jencks
added a comment - 13/Mar/12 00:09 Implements Felix's suggestions.
This has 3 integration test failures for me that I haven't solved yet. On the other hand I'm getting these failures without the changes as well so it is not clear that I've caused them.
I've implemented this additional featue: if the setServiceProperties method is called with null or an empty dictionary, or the activate or modify method with a Map or Dictionary return type returns null or an empty map/dictionary, then the service properties override is turned off and the service properties are reset to the combination of the defaults from config.xml and the config admin settings.
I'm not thrilled with how the return value for activate and modify methods is returned to the caller, and what I've done might have concurrency issues.

> ExtComponentContext.setServiceProperties and the return Dictionary value from activate and update
> replace the service registration properties rather than update the values calculated by some other method.

Yes

> calling ExtComponentContext.setServiceProperties from within an activate or modify method would
> have the same effect as returning the service properties from the method

Yes. If both are done – calling setServiceProperties and return Dictionary, the latter would overwrite/replace the former.

> In order to avoid "arguments" over whether config admin or the component sets the service properties,
> the update method would recalculate the service properties when config admin changes them and
> reset what it wants either through a return value or calling ExtComponentContext.setServiceProperties.

IMHO configuration admin properties are primarily to configure the component and are also (kind of secondarily) used to set the service properties. Both make sense, of course. But if we think a component should be able to set its own service properties, it should probably be able to do so completely but without "disturbing" its own configuration.

Thus:

#1 Component propertiesa are default configuration for the component
#2 Config Admin enhances, extends, overwrites default configuration for the component
#3 By default this set of properties is used as service properties (except private properties starting with ".")
#4 The component may overwrite the properties from #3 by returning a Dictionary from activate or modified or by calling setServiceProperties

In practice, I would assume a component to take the properties received from the DS runtime (either ComponentContext.getProperties or the Map passed to the activate or modified method) modify it and use the result to call setProperties or return from activate.

I might be conceivable, though to be slightly "strange" in the API:

(1) activate and modified may return Map<String, Object> for the service properties. This would be inline with the DS 1.1 option to get the configuration passed as a Map into the methods
(2) setServiceProperties would take a Dictionary<String, Object> for the service properties. This would be inline with the original ComponentContext.getProperties method.

Felix Meschberger
added a comment - 12/Mar/12 07:36 > ExtComponentContext.setServiceProperties and the return Dictionary value from activate and update
> replace the service registration properties rather than update the values calculated by some other method.
Yes
> calling ExtComponentContext.setServiceProperties from within an activate or modify method would
> have the same effect as returning the service properties from the method
Yes. If both are done – calling setServiceProperties and return Dictionary, the latter would overwrite/replace the former.
> In order to avoid "arguments" over whether config admin or the component sets the service properties,
> the update method would recalculate the service properties when config admin changes them and
> reset what it wants either through a return value or calling ExtComponentContext.setServiceProperties.
IMHO configuration admin properties are primarily to configure the component and are also (kind of secondarily) used to set the service properties. Both make sense, of course. But if we think a component should be able to set its own service properties, it should probably be able to do so completely but without "disturbing" its own configuration.
Thus:
#1 Component propertiesa are default configuration for the component
#2 Config Admin enhances, extends, overwrites default configuration for the component
#3 By default this set of properties is used as service properties (except private properties starting with ".")
#4 The component may overwrite the properties from #3 by returning a Dictionary from activate or modified or by calling setServiceProperties
In practice, I would assume a component to take the properties received from the DS runtime (either ComponentContext.getProperties or the Map passed to the activate or modified method) modify it and use the result to call setProperties or return from activate.
I might be conceivable, though to be slightly "strange" in the API:
(1) activate and modified may return Map<String, Object> for the service properties. This would be inline with the DS 1.1 option to get the configuration passed as a Map into the methods
(2) setServiceProperties would take a Dictionary<String, Object> for the service properties. This would be inline with the original ComponentContext.getProperties method.
WDYT ?

I think this is a good idea. I'd like to make sure I understand a couple details.

Both the ExtComponentContext.setServiceProperties and the return Dictionary value from activate and update replace the service registration properties rather than update the values calculated by some other method.

calling ExtComponentContext.setServiceProperties from within an activate or modify method would have the same effect as returning the service properties from the method.

In order to avoid "arguments" over whether config admin or the component sets the service properties, the update method would recalculate the service properties when config admin changes them and reset what it wants either through a return value or calling ExtComponentContext.setServiceProperties.

David Jencks
added a comment - 09/Mar/12 23:08 I think this is a good idea. I'd like to make sure I understand a couple details.
Both the ExtComponentContext.setServiceProperties and the return Dictionary value from activate and update replace the service registration properties rather than update the values calculated by some other method.
calling ExtComponentContext.setServiceProperties from within an activate or modify method would have the same effect as returning the service properties from the method.
In order to avoid "arguments" over whether config admin or the component sets the service properties, the update method would recalculate the service properties when config admin changes them and reset what it wants either through a return value or calling ExtComponentContext.setServiceProperties.

Thinking about it, I would like to refine your original proposal as follows:

(1) New ExtCompontentContext.setServiceProperties(Dictionary)
This replaces the set of properties derived from the component properties,
configuration admin properties, and from the ComponentFactory.newInstance
call and is only used to set the component's service registration property

(2) Allow the activate and modify methods to return Dictionary
If a Dictionary is returned, the respective properties are used for
subsequent service registration (Dictionary returned from activate for
an immediate component) or to update the service registration (Dictionary
returned from modify or from activate for a delayed or service factory
component).

The first method is inline with DS 1.0 tradition, where the ComponentContext provides access to framework related functionality (BundleContext etc.) The second method is inline with the DS 1.1 tradition to require as minimal OSGi API involvement as possible.

Any private properties (starting with a ".") are filtered out of the Dictionary in both cases before using the properties for service registration.

The ExtComponent method thus only influences the service registration but not an configuration supplied to the activate or modified method.

Felix Meschberger
added a comment - 09/Mar/12 15:37 Thinking about it, I would like to refine your original proposal as follows:
(1) New ExtCompontentContext.setServiceProperties(Dictionary)
This replaces the set of properties derived from the component properties,
configuration admin properties, and from the ComponentFactory.newInstance
call and is only used to set the component's service registration property
(2) Allow the activate and modify methods to return Dictionary
If a Dictionary is returned, the respective properties are used for
subsequent service registration (Dictionary returned from activate for
an immediate component) or to update the service registration (Dictionary
returned from modify or from activate for a delayed or service factory
component).
The first method is inline with DS 1.0 tradition, where the ComponentContext provides access to framework related functionality (BundleContext etc.) The second method is inline with the DS 1.1 tradition to require as minimal OSGi API involvement as possible.
Any private properties (starting with a ".") are filtered out of the Dictionary in both cases before using the properties for service registration.
The ExtComponent method thus only influences the service registration but not an configuration supplied to the activate or modified method.
Also updated OSGi Bug 2250 with this proposal.

David Jencks
added a comment - 08/Mar/12 22:34 I agree that having an additional map for the overrides is needed to avoid changing the defaults for multiple services based off the same component.
Many thanks!!

Internally tread property overwrites like supplied CM configuration
This allows for properly transient property modification instead
of overwriting the actual descriptor properties which would cause
the properties for all instances created from the same descriptor
to be subsequently modified.

Felix Meschberger
added a comment - 08/Mar/12 05:56 Thanks a lot for providing the patch.
I have applied it slightly modified in Rev. 1298275:
Added some more JavaDoc to ExtComponentContext
Have ExtComponentContext extend ComponentContext
Internally tread property overwrites like supplied CM configuration
This allows for properly transient property modification instead
of overwriting the actual descriptor properties which would cause
the properties for all instances created from the same descriptor
to be subsequently modified.
Does this sound correct ?

BTW the newer patch at least won't apply without first applying the build patch from FELIX-3371. I have the patches from FELIX-3345 applied also, if this causes problems let me know and I'll come up with something that only needs FELIX-3371.

David Jencks
added a comment - 08/Mar/12 00:06 BTW the newer patch at least won't apply without first applying the build patch from FELIX-3371 . I have the patches from FELIX-3345 applied also, if this causes problems let me know and I'll come up with something that only needs FELIX-3371 .

Some remarks:
(1) I would probably name the new package o.a.f.scr.component and export it with a mandatory attribute (as we do with other provisional API we export)
(2) I would rather call the MutablePropertiesComponentContext interface ExtComponentContext
(3) The JavaDoc for the updateProperties method should clearly indicate (as you propose) that the properties from the component descriptor are to be amended and/or updated and that these properties are still subject to being overwritten by configuration from Configuration Admin. This would imply that existing properties from the component descriptors cannot be removed, right ?

Felix Meschberger
added a comment - 07/Mar/12 19:26 - edited This makes sense to me.
Some remarks:
(1) I would probably name the new package o.a.f.scr.component and export it with a mandatory attribute (as we do with other provisional API we export)
(2) I would rather call the MutablePropertiesComponentContext interface ExtComponentContext
(3) The JavaDoc for the updateProperties method should clearly indicate (as you propose) that the properties from the component descriptor are to be amended and/or updated and that these properties are still subject to being overwritten by configuration from Configuration Admin. This would imply that existing properties from the component descriptors cannot be removed, right ?