karaf-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Timothy Ward <tim.w...@paremus.com>
Subject Re: What's the recommended way of reading configuration?
Date Sun, 03 Dec 2017 09:25:07 GMT
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?
> 


Mime
View raw message