You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Steinar Bang <sb...@dod.no> on 2017/12/03 08:41:26 UTC
What's the recommended way of reading configuration?
What's the recommended way for an OSGi bundle running inside karaf to
access its configuration?
Get an OSGi service of type ConfigMBean[1]?
Get an OSGi servie of type ConfigRepository[2]?
Or maybe avoid karaf dependencies, and get an OSGi service of type
ConfigurationAdmin[3]...?
Does anyone know of code examples of any of the three approaches?
Preferrably DS w/annotation based code examples?
Thanks!
- Steinar
References:
[1] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java>
[2] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigRepository.java>
[3] <https://osgi.org/javadoc/r4v42/org/osgi/service/cm/ConfigurationAdmin.html>
Re: What's the recommended way of reading configuration?
Posted by Steinar Bang <sb...@dod.no>.
>>>>> Timothy Ward <ti...@paremus.com>:
> None of the above. When it comes to configuration you should always be
> using Configuration Admin to inject you, not getting a service. If
> you’re using DS then this configuration can be directly injected into
> your activate method, either as a Map or a Component Property Type.
Ok.
>> Does anyone know of code examples of any of the three approaches?
>> Preferrably DS w/annotation based code examples?
> There are examples out there, for example:
> I recently answered this question on Stack Overflow <https://stackoverflow.com/questions/47550999/cant-add-the-plus-button-in-osgi-apache-felix-web-console-to-create-new-configu/47557470#47557470>, which includes metatype as well.
> Paremus have example applications using DS <https://github.com/paremus/hello-examples/blob/37c9b2b1ac430227f0d92bf336be6cf5a59c079a/helloworld/helloworld-ds/src/main/java/com/example/hello/impl/GreetingImpl.java>
> From OSGi enRoute <http://enroute.osgi.org/services/org.osgi.service.component.html#configuration> (this link may change as enRoute content is updated/moved for OSGi R7).
Thanks! Great examples! :-)
This looks extremely simple, and a way to get dependencies without
adding additional runtime dependencies to my bundle.
Re: What's the recommended way of reading configuration?
Posted by Timothy Ward <ti...@paremus.com>.
Hi Steinar,
> What's the recommended way for an OSGi bundle running inside karaf to
> access its configuration?
>
> Get an OSGi service of type ConfigMBean[1]?
> Get an OSGi servie of type ConfigRepository[2]?
>
> Or maybe avoid karaf dependencies, and get an OSGi service of type
> ConfigurationAdmin[3]…?
None of the above. When it comes to configuration you should always be using Configuration Admin to inject you, not getting a service. If you’re using DS then this configuration can be directly injected into your activate method, either as a Map or a Component Property Type.
>
> Does anyone know of code examples of any of the three approaches?
> Preferrably DS w/annotation based code examples?
There are examples out there, for example:
I recently answered this question on Stack Overflow <https://stackoverflow.com/questions/47550999/cant-add-the-plus-button-in-osgi-apache-felix-web-console-to-create-new-configu/47557470#47557470>, which includes metatype as well.
Paremus have example applications using DS <https://github.com/paremus/hello-examples/blob/37c9b2b1ac430227f0d92bf336be6cf5a59c079a/helloworld/helloworld-ds/src/main/java/com/example/hello/impl/GreetingImpl.java>
From OSGi enRoute <http://enroute.osgi.org/services/org.osgi.service.component.html#configuration> (this link may change as enRoute content is updated/moved for OSGi R7).
> To be more specific: I'm looking for the best and simplest way to
> retrieve the configuration that in a plain Java application typically
> would be put in .properties files, like eg. JDBC URL, JDBC username and
> JDBC password?
If you’re planning to do JDBC type things (which it looks like you are) then you should probably also look at the OSGi R7 Transaction Control Service <https://osgi.org/download/osgi.enterprise-7.0.0-early-draft-2017-03.pdf> (chapter 147) This will give you automatic lifecycle management for your database connections, standardised properties for connection pooling, and local/XA transaction support, and tends to be much easier to configure correctly (and harder to misconfigure) than other approaches to transactions in OSGi. I’ve seen plenty of “transactional” OSGi applications which due to configuration/deployment issues don’t actually have transactions running, which is one of the main reasons for this new spec… If you are interested then the prototype RI is complete and available from Apache Aries <https://github.com/apache/aries/tree/trunk/tx-control> with snapshots available from https://repository.apache.org/content/groups/snapshots/ <https://repository.apache.org/content/groups/snapshots/>
Regards,
Tim
> On 3 Dec 2017, at 08:50, Steinar Bang <sb...@dod.no> wrote:
>
>>>>>> Steinar Bang <sb...@dod.no>:
>
>> What's the recommended way for an OSGi bundle running inside karaf to
>> access its configuration?
>
> To be more specific: I'm looking for the best and simplest way to
> retrieve the configuration that in a plain Java application typically
> would be put in .properties files, like eg. JDBC URL, JDBC username and
> JDBC password?
>
Re: What's the recommended way of reading configuration?
Posted by Steinar Bang <sb...@dod.no>.
>>>>> Steinar Bang <sb...@dod.no>:
> What's the recommended way for an OSGi bundle running inside karaf to
> access its configuration?
To be more specific: I'm looking for the best and simplest way to
retrieve the configuration that in a plain Java application typically
would be put in .properties files, like eg. JDBC URL, JDBC username and
JDBC password?
Re: What's the recommended way of reading configuration?
Posted by Timothy Ward <ti...@paremus.com>.
> I didn’t mean ManagedService using DS, I meant using DS configuration (which is basically a managed service.
Ok - but you can see the source of confusion right? Your post does not reference DS at all, and ManagedService is an interface defined by ConfigurationAdmin. If someone looks up ManagedService on Google then they won’d find the DS solution.
> I disagree about @Referece to ConfigurationAdmin: in some use cases, it makes sense. Look in Karaf Cellar for instance.
I have taken a look - in most of the cases here Cellar is acting as a management agent (which I explicitly mentioned in my first post) and is making configuration updates on behalf of other people, or synchronising configurations, not getting its own configuration. In some other cases it appears as though the code could be simplified and improved by not using ConfigurationAdmin to retrieve its own configuration, but I’m not a cellar expert, and it’s possible that these components are still acting on other bundle’s configurations not their own. The main thing is not to confuse middleware/infrastructure with application code. The number of people who are writing things like Cellar is very small, and the OSGi advice/best practice is pretty clear that using ConfigurationAdmin to get your own configuration is an anti-pattern.
Regards,
Tim
> On 3 Dec 2017, at 13:55, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>
> I didn't mean ManagedService using DS, I meant using DS configuration (which is basically a managed service.
>
> When using @Component, we implicitly have a configuration link to the component injected via activate and the ComponentContext (getProperties).
>
> I disagree about @Referece to ConfigurationAdmin: in some use cases, it makes sense. Look in Karaf Cellar for instance.
>
> Regards
> JB
>
> On 12/03/2017 01:10 PM, Timothy Ward wrote:
>>> I would more recommend a ManagedService or inject the ConfigAdmin service.
>> This is really bad advice, and Steinar should definitely not do either of these things (nor should other readers of this mail chain).
>> * Steinar has already indicated that he wants to use Declarative Services, so
>> implementing ManagedService is both unnecessary and ties him to an API that
>> he has no need to implement. It also prevents him from using Component
>> Property Types, defaults, annotations, and forces him to use Dictionary for
>> his configuration. Finally, it also makes his component’s lifecycle
>> management harder, as he would have to manage all the reconfiguration
>> dynamics himself.
>> * Injecting the Config Admin service is inevitably the wrong way for any user
>> to read configuration in OSGi. The only people who should ever access the
>> ConfigurationAdmin service directly are management agents like Felix File
>> Install and Felix Web Console (and whatever component in Karaf does the same
>> job).
>> The correct way to consume configuration in DS is to receive it via your activate method - if you think that you need to do something else then you have most likely got the wrong design.
>> If you’re not using DS then most component frameworks still provide you with configuration in a similar way (injection of a map). If you’re determined to do things the hard way (without a framework) then, and only then, should you implement ManagedService or ManagedServiceFactory, and you should absolutely never try to consume your configuration directly from the Configuration Admin service (it’s simply not designed to be used this way).
>> Regards,
>> Tim
>>> On 3 Dec 2017, at 10:41, Jean-Baptiste Onofré <jb@nanthrax.net <ma...@nanthrax.net> <mailto:jb@nanthrax.net <ma...@nanthrax.net>>> wrote:
>>>
>>> I would more recommend a ManagedService or inject the ConfigAdmin service.
>>>
>>> Regards
>>> JB
>>>
>>> On 12/03/2017 09:41 AM, Steinar Bang wrote:
>>>> What's the recommended way for an OSGi bundle running inside karaf to
>>>> access its configuration?
>>>> Get an OSGi service of type ConfigMBean[1]?
>>>> Get an OSGi servie of type ConfigRepository[2]?
>>>> Or maybe avoid karaf dependencies, and get an OSGi service of type
>>>> ConfigurationAdmin[3]...?
>>>> Does anyone know of code examples of any of the three approaches?
>>>> Preferrably DS w/annotation based code examples?
>>>> Thanks!
>>>> - Steinar
>>>> References:
>>>> [1] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java>
>>>> [2] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigRepository.java>
>>>> [3] <https://osgi.org/javadoc/r4v42/org/osgi/service/cm/ConfigurationAdmin.html>
>>>
>>> --
>>> Jean-Baptiste Onofré
>>> jbonofre@apache.org <ma...@apache.org> <mailto:jbonofre@apache.org <ma...@apache.org>>
>>> http://blog.nanthrax.net <http://blog.nanthrax.net/>
>>> Talend - http://www.talend.com <http://www.talend.com/>
>
> --
> Jean-Baptiste Onofré
> jbonofre@apache.org <ma...@apache.org>
> http://blog.nanthrax.net <http://blog.nanthrax.net/>
> Talend - http://www.talend.com <http://www.talend.com/>
Re: What's the recommended way of reading configuration?
Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
I didn't mean ManagedService using DS, I meant using DS configuration (which is
basically a managed service.
When using @Component, we implicitly have a configuration link to the component
injected via activate and the ComponentContext (getProperties).
I disagree about @Referece to ConfigurationAdmin: in some use cases, it makes
sense. Look in Karaf Cellar for instance.
Regards
JB
On 12/03/2017 01:10 PM, Timothy Ward wrote:
>> I would more recommend a ManagedService or inject the ConfigAdmin service.
>
> This is really bad advice, and Steinar should definitely not do either of these
> things (nor should other readers of this mail chain).
>
> * Steinar has already indicated that he wants to use Declarative Services, so
> implementing ManagedService is both unnecessary and ties him to an API that
> he has no need to implement. It also prevents him from using Component
> Property Types, defaults, annotations, and forces him to use Dictionary for
> his configuration. Finally, it also makes his component’s lifecycle
> management harder, as he would have to manage all the reconfiguration
> dynamics himself.
> * Injecting the Config Admin service is inevitably the wrong way for any user
> to read configuration in OSGi. The only people who should ever access the
> ConfigurationAdmin service directly are management agents like Felix File
> Install and Felix Web Console (and whatever component in Karaf does the same
> job).
>
>
> The correct way to consume configuration in DS is to receive it via your
> activate method - if you think that you need to do something else then you have
> most likely got the wrong design.
>
> If you’re not using DS then most component frameworks still provide you with
> configuration in a similar way (injection of a map). If you’re determined to do
> things the hard way (without a framework) then, and only then, should you
> implement ManagedService or ManagedServiceFactory, and you should absolutely
> never try to consume your configuration directly from the Configuration Admin
> service (it’s simply not designed to be used this way).
>
> Regards,
>
> Tim
>
>> On 3 Dec 2017, at 10:41, Jean-Baptiste Onofré <jb@nanthrax.net
>> <ma...@nanthrax.net>> wrote:
>>
>> I would more recommend a ManagedService or inject the ConfigAdmin service.
>>
>> Regards
>> JB
>>
>> On 12/03/2017 09:41 AM, Steinar Bang wrote:
>>> What's the recommended way for an OSGi bundle running inside karaf to
>>> access its configuration?
>>> Get an OSGi service of type ConfigMBean[1]?
>>> Get an OSGi servie of type ConfigRepository[2]?
>>> Or maybe avoid karaf dependencies, and get an OSGi service of type
>>> ConfigurationAdmin[3]...?
>>> Does anyone know of code examples of any of the three approaches?
>>> Preferrably DS w/annotation based code examples?
>>> Thanks!
>>> - Steinar
>>> References:
>>> [1]
>>> <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java>
>>> [2]
>>> <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigRepository.java>
>>> [3] <https://osgi.org/javadoc/r4v42/org/osgi/service/cm/ConfigurationAdmin.html>
>>
>> --
>> Jean-Baptiste Onofré
>> jbonofre@apache.org <ma...@apache.org>
>> http://blog.nanthrax.net
>> Talend - http://www.talend.com
>
--
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com
Re: What's the recommended way of reading configuration?
Posted by Timothy Ward <ti...@paremus.com>.
> I would more recommend a ManagedService or inject the ConfigAdmin service.
This is really bad advice, and Steinar should definitely not do either of these things (nor should other readers of this mail chain).
Steinar has already indicated that he wants to use Declarative Services, so implementing ManagedService is both unnecessary and ties him to an API that he has no need to implement. It also prevents him from using Component Property Types, defaults, annotations, and forces him to use Dictionary for his configuration. Finally, it also makes his component’s lifecycle management harder, as he would have to manage all the reconfiguration dynamics himself.
Injecting the Config Admin service is inevitably the wrong way for any user to read configuration in OSGi. The only people who should ever access the ConfigurationAdmin service directly are management agents like Felix File Install and Felix Web Console (and whatever component in Karaf does the same job).
The correct way to consume configuration in DS is to receive it via your activate method - if you think that you need to do something else then you have most likely got the wrong design.
If you’re not using DS then most component frameworks still provide you with configuration in a similar way (injection of a map). If you’re determined to do things the hard way (without a framework) then, and only then, should you implement ManagedService or ManagedServiceFactory, and you should absolutely never try to consume your configuration directly from the Configuration Admin service (it’s simply not designed to be used this way).
Regards,
Tim
> On 3 Dec 2017, at 10:41, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
>
> I would more recommend a ManagedService or inject the ConfigAdmin service.
>
> Regards
> JB
>
> On 12/03/2017 09:41 AM, Steinar Bang wrote:
>> What's the recommended way for an OSGi bundle running inside karaf to
>> access its configuration?
>> Get an OSGi service of type ConfigMBean[1]?
>> Get an OSGi servie of type ConfigRepository[2]?
>> Or maybe avoid karaf dependencies, and get an OSGi service of type
>> ConfigurationAdmin[3]...?
>> Does anyone know of code examples of any of the three approaches?
>> Preferrably DS w/annotation based code examples?
>> Thanks!
>> - Steinar
>> References:
>> [1] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java>
>> [2] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigRepository.java>
>> [3] <https://osgi.org/javadoc/r4v42/org/osgi/service/cm/ConfigurationAdmin.html>
>
> --
> Jean-Baptiste Onofré
> jbonofre@apache.org
> http://blog.nanthrax.net
> Talend - http://www.talend.com
Re: What's the recommended way of reading configuration?
Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
I would more recommend a ManagedService or inject the ConfigAdmin service.
Regards
JB
On 12/03/2017 09:41 AM, Steinar Bang wrote:
> What's the recommended way for an OSGi bundle running inside karaf to
> access its configuration?
>
> Get an OSGi service of type ConfigMBean[1]?
> Get an OSGi servie of type ConfigRepository[2]?
>
> Or maybe avoid karaf dependencies, and get an OSGi service of type
> ConfigurationAdmin[3]...?
>
> Does anyone know of code examples of any of the three approaches?
> Preferrably DS w/annotation based code examples?
>
>
> Thanks!
>
>
> - Steinar
>
> References:
> [1] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigMBean.java>
> [2] <https://github.com/apache/karaf/blob/master/config/src/main/java/org/apache/karaf/config/core/ConfigRepository.java>
> [3] <https://osgi.org/javadoc/r4v42/org/osgi/service/cm/ConfigurationAdmin.html>
>
--
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com