How to get a content of config file for some OSGI Bundle

Tags: , , ,



I have a following structure:

-- bundles
---- nicebundle
------ src
-------- ...
-------- nicebundle.config
---------- NiceConfig.java
-------- ...
----  mybundle
------ ...
----  another bundles...

-- features
----  nicebundle.config.feature
------  rootifles
--------  config.cfg
------  ...
----  another features...

In config.cfg there are some constants, which I want to use in mybundle (in activate() method). What I have in NiceConfig.java for nicebundle is NiceConfig @interface, which just mirrorly lists all the content from .cfg file like String importantString();, and in .cfg file there is a corresponding record: importantString=Hello

For the nicebundle you can just load NiceConfig object, call the methods and receive the needed constants, like:

@Activate
public void activate(final NiceConfig niceConfig) {
    String compositeString = niceConfig.importantString() + " World"; // receive "Hello World"
    ...

What I want – to do the same but in mybundle. However when I do the same, niceConfig.importantString() returns null, not “Hello”. I think I miss something very simple and obvious, but my question is: how can I achieve a content of .cfg file, located how I specified above (I cannot change it) in mybundle?

Answer

Declarative Services (DS) will coerce component properties into an annotation type, like your NiceConfig type. But component properties come from the component description and configurations in Configuration Admin.

DS does not know anything about some config file in an Eclipse feature. And Eclipse features are not even installed bundles so are not visible at runtime anyway.

You could use an implementation of the Configurator specification to provision the properties from a resource in your bundle into the appropriate Configuration Admin configuration for your DS component. But this will require some structural changes to move the configuration file from the feature into your bundle.



Source: stackoverflow