OSGi: passing parameters to a specific bundle

北城余情 提交于 2019-12-05 21:34:16

As always, register a service ... The receive bundle (and any other bundle) can search for the service and read the properties. Since you're the framework launcher you can register any service you want through the Framework object you get from the Launching API.

In bnd(tools) I have a launcher. It uses the OSGi Launcher API to create the bundle, similar to what you describe. I pass the command line arguments as properties on a Launcher service. Look at https://github.com/bndtools/bnd/blob/master/biz.aQute.launcher/src/aQute/launcher/Launcher.java for an example.

You can of course also just get the Configuration Admin service through the Framework object and use it to register Configurations. It is quite easy (just actually done it) to define the configurations in JSON (or, dare I say it, XML) file and then read it and update Configuration Admin.

You can make the jar containing the main class an OSGi bundle as well, and during startup you install this bundle together with the other bundles. Then you can let other bundles access the properties contained in your jar using a number of techniques, such as the followings:

  • provide a class for accessing the properties and integrate using module dependancy (Export-package/Import-package)
  • provide as service for accessing the properties
  • use Config Admin

The simplest may be to make the jar with the properties file a fragment, with bundle A as the host.

Then the properties will be on the classpath of the bundle and can be easily read.

We have a similar case in Apache Karaf. There we simply set the properties as java System properties and also give them to the framework. These are then available as properties in the OSGi bundle context. So this is nice for properties that are for the whole platform. See the project of the karaf starter: http://svn.apache.org/viewvc/karaf/trunk/main/

If you need properties in user bundles then using the config admin service is much nicer. It decouples the user bundle form the mechanism of retrieving the properties and also allows updates at runtime. There is a also the metatype service which allows to define the structure and meaning of the properties which allows nice forms for editing the properties.

I describe in two tutorials on my website how to use the config admin service:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!