Felix
  1. Felix
  2. FELIX-3377

Allow a component to update its own service properties

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: scr-1.6.0
    • Fix Version/s: scr-1.6.2
    • Labels:
      None

      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.

      IIUC this idea does not make sense for component factories.

      This idea was originally suggested by Erin Schnabel in OSGI bug 2250.

      1. FELIX-3377-returnDictionary.patch
        29 kB
        Felix Meschberger
      2. FELIX-3377-4-fmeschbe.patch
        52 kB
        Felix Meschberger
      3. FELIX-3377-4.diff
        32 kB
        David Jencks
      4. FELIX-3377-3.diff
        18 kB
        David Jencks
      5. FELIX-3377-2.diff
        17 kB
        David Jencks
      6. FELIX-3377.diff
        16 kB
        David Jencks

        Activity

        Hide
        Felix Meschberger added a comment -

        Close after release

        Show
        Felix Meschberger added a comment - Close after release
        Hide
        Felix Meschberger added a comment -

        Resolving with the latest changes in Rev. 1336331

        Show
        Felix Meschberger added a comment - Resolving with the latest changes in Rev. 1336331
        Hide
        Felix Meschberger added a comment -

        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.

        Show
        Felix Meschberger added a comment - 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.
        Hide
        David Jencks added a comment -

        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.

        Show
        David Jencks added a comment - 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.
        Hide
        Felix Meschberger added a comment - - 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.

        Show
        Felix Meschberger added a comment - - 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.
        Hide
        Felix Meschberger added a comment -

        > 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.

        Show
        Felix Meschberger added a comment - > 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.
        Hide
        David Jencks added a comment -

        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

        Show
        David Jencks added a comment - 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
        Hide
        Felix Meschberger added a comment -

        > 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

        Show
        Felix Meschberger added a comment - > 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
        Hide
        Felix Meschberger added a comment -

        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.

        Show
        Felix Meschberger added a comment - 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.
        Hide
        David Jencks added a comment -

        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.
        Show
        David Jencks added a comment - 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.
        Hide
        David Jencks added a comment -

        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;
          }
        Show
        David Jencks added a comment - 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; }
        Hide
        David Jencks added a comment -

        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.

        Show
        David Jencks added a comment - 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.
        Hide
        Felix Meschberger added a comment -

        > 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 ?

        Show
        Felix Meschberger added a comment - > 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 ?
        Hide
        David Jencks added a comment -

        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.
        Show
        David Jencks added a comment - 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.
        Hide
        Felix Meschberger added a comment -

        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.

        Show
        Felix Meschberger added a comment - 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.
        Hide
        David Jencks added a comment -

        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!!

        Show
        David Jencks added a comment - 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!!
        Hide
        Felix Meschberger added a comment -

        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 ?

        Show
        Felix Meschberger added a comment - 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 ?
        Hide
        David Jencks added a comment -

        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.

        Show
        David Jencks added a comment - 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 .
        Hide
        David Jencks added a comment -

        Implement Felix M's suggestions

        Show
        David Jencks added a comment - Implement Felix M's suggestions
        Hide
        David Jencks added a comment -

        After checking that it's OK I added attribution to the osgi bug that inspired me. I'll look into your suggestions shortly.

        Show
        David Jencks added a comment - After checking that it's OK I added attribution to the osgi bug that inspired me. I'll look into your suggestions shortly.
        Hide
        Felix Meschberger added a comment - - 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 ?

        Show
        Felix Meschberger added a comment - - 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 ?
        Hide
        David Jencks added a comment -

        Possible implementation of allowing a component to update its own service properties

        Show
        David Jencks added a comment - Possible implementation of allowing a component to update its own service properties

          People

          • Assignee:
            Felix Meschberger
            Reporter:
            David Jencks
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development