Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get property values from bundle manifest using BundleContext?

With my level of proficiency in OSGi, I can get property strings from:

  1. BundleContext.getProperty(key) (stored in 'conf/config.properties')
  2. ComponentContext.getProperties().get(key) (stored in bundle's 'MANIFEST.MF')

    Service-Component: \ foo.bar.impl.FixServer;application="quickfix.Application";properties:="acceptor.resourcename=acceptor.cfg"

I want to get properties in the bundle manifest, accessible at a bundle level (i.e. BundleContext), which is higher than 'Service-Component' (i.e. ComponentContext).

Can anyone show me how can this be done?


ADDENDUM

From the answers from AValchev and Neil Bartlett,

java.util.Dictionary headers = Bundle.getHeaders();

is a good approach.

Edited 2011-12-10

However, JAR Manifest syntax (requiring first char in the key to be uppercase, and '.' char disallowed) breaks my app key constants unless I do some refactoring.

If I do that, the app will break again should I use .properties file in the future.

To overcome the (IMO) 'limitation' of JAR Manifest Syntax, I have come up with this single manifest entry:

Bundle-Properties: \
foo.bar.prefix=MS,\
foo.bar.hostname=127.0.0.1,\
foo.bar.port=8106,\
foo.bar.homepath=/foo/bar/E3,\
foo.bar.secure=false,\

, and the code to digest the string into Properties:

java.util.Properties properties = new java.util.Properties();
java.util.Dictionary headers = bcontext.getBundle().getHeaders();
String manifest_key = "Bundle-Properties";
String manifest_value = (String) headers.get(manifest_key);
if (manifest_value != null) {
    String[] t = manifest_value.split(",");
    for (int i = 0; i < t.length; i++) {
        String[] u = t[i].split("=");
        if (1 < u.length) {
            String key = u[0];
            String value = u[1];
            properties.setProperty(key, value);
        }
    }
}
like image 655
Augustus Thoo Avatar asked Feb 23 '23 04:02

Augustus Thoo


1 Answers

I'm not completely sure what is your goal, but why you don't take a look at ConfigurationAdmin service, which is defined as OSGi standard.

One way of setting properties available to all bundles is with -Dproperty=value parameter in the command line. If you are using equinox, you can put your properties into config.ini.

What about the Bundle.getHeader(...) method? Just specify in your MANIFEST.MF file something like MY-Property: proper-value and then you can access it via Bundle.getHeader(). The problem with this approach is the naming convention required in MANIFEST.MF.

like image 117
Valchev Avatar answered Apr 07 '23 13:04

Valchev