Best technique for getting the OSGi bundle context

2019-02-01 05:55发布

Each bundle in my OSGi project has it's own BundleActivator, which I think is normal. This gets passed the current BundleContext, which is useful to have around for getting service references and whatnot.

However, from classes in my bundle, how can I get the BundleContext? Assigning it to a public static field in the BundleActivator sucks and passing it around as an argument also sucks. Is there a more intelligent way?

标签: java osgi
4条回答
爷、活的狠高调
2楼-- · 2019-02-01 05:58

There is no magic here. You need some way to provide the information to the other classes. So it is either available via the call stack or in some well known place (e.g. static).

查看更多
疯言疯语
3楼-- · 2019-02-01 06:01

Another alternative is to use Declarative Services, which allows you to receive the BundleContext into your activator method. For example, assuming you use the Bnd Annotations for DS:

@Activate
public void activate(BundleContext context) {
    // ...
}

However as RaduK said, it's much better if you can write the majority of your code in POJO style without using OSGi APIs such as BundleContext.

查看更多
爷的心禁止访问
4楼-- · 2019-02-01 06:10

You can use FrameworkUtil.getBundle(ClassFromBundle).getBundleContext().

See FrameworkUtil JavaDoc.

查看更多
放我归山
5楼-- · 2019-02-01 06:25

A good practice when developing OSGi bundles in my opinion is to try to write the OSGi related code as centralized as possible. This way, if you want to use your code in a non-OSGi environment, the migration effort is minimum.

Therefore, using static references or FrameworkUtil all over the place is not a good idea imho. Neither is using plain OSGi. Try to look at iPOJO or Declarative Services.

查看更多
登录 后发表回答