OSGi:将参数传递给特定的包 [英] OSGi: passing parameters to a specific bundle

查看:134
本文介绍了OSGi:将参数传递给特定的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义Main类的自定义jar,它启动OSGi Framework并安装/启动bundle。这个主jar还包含一个属性文件。

I have a custom jar with a custom Main class, which starts the OSGi Framework and installs/starts the bundles. This main jar also includes a properties file.

目标:我有一个包A应该以某种方式获取主jar的属性文件的属性。

Goal: I have a bundle A which should pick up the properties of this properties file of the main jar somehow.

我的第一次尝试是在bundle A中定义一个组件(使用Apache Felix SCR / Declarative Services)并在其activate-method中检索属性。到目前为止,我得到了@ Property-annotation中指定的默认值。

My first attempt was to define a component in bundle A (using Apache Felix SCR/ Declarative Services) and retrieve the properties in its activate-method. This works so far that I'm getting the default value specified in the @Property-annotation.

但是我现在如何将属性文件的属性传递给此组件?

But how can I now pass the properties of the properties file to this component?

将参数传递给OSGi应用程序
提到使用Config Admin,但我如何在Main类中使用它?

Passing arguments to OSGi application mentions to use the Config Admin, but how can I use this in the Main class?


  • 配置管理

  • Main类对它安装的软件包一无所知,更不用说特定的了,所以捆绑软件包不在主jar中,并且没有按任何特定顺序安装软件包service.pid。

我现在正在尝试@vizier建议的方法(不使用Config Admin,因此没有提到的问题):

I'm trying now an approach suggested by @vizier (which doesn't use Config Admin and thus doesn't have the mentioned issues):


  • 定义服务接口在主jar(系统包)中

  • 提供一个实现,它读取属性文件(属性文件在同一个jar中)

  • 导出包含此服务接口的包X

  • bundle A然后可以导入包装X和例如使用声明性服务引用服务

  • define a service interface in the main jar (system bundle)
  • provide an implementation, which reads the property file (the properties file is in the same jar)
  • export the package X containing this service interface
  • bundle A then can import the package X and e.g. reference the service using Declarative Services

但是在我的包A中我得到了:

But in my bundle A I'm getting:

org.osgi.framework.BundleException: Unresolved constraint in bundle <bundle A> [14]: Unable to resolve 14.0: missing requirement [14.0] osgi.wiring.package; (&(osgi.wiring.package=<package X>)(version>=0.1.0)(!(version>=1.0.0)))
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3826)
    at org.apache.felix.framework.Felix.startBundle(Felix.java:1868)
    at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1191)
    at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:295)
    at java.lang.Thread.run(Thread.java:722)

即使在Manifest文件中将一些自定义包添加到Export-Package,系统捆绑包是否只导出osgi包?或者出了什么问题?

Does the system bundle only export osgi packages even if some custom packages were added to Export-Package in the Manifest file? Or what is going wrong?

推荐答案

一如既往,注册服务......接收包(以及任何其他包)可以搜索服务并阅读属性。由于您是框架启动器,您可以通过从启动API获得的Framework对象注册您想要的任何服务。

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.

在bnd(工具)中我有一个启动器。它使用OSGi Launcher API创建捆绑包,类似于您描述的内容。我将命令行参数作为属性传递给Launcher服务。请查看 https:// github例如.com / bndtools / bnd / blob / master / biz.aQute.launcher / src / aQute / launcher / Launcher.java

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.

你当然也可以通过Framework对象获取Configuration Admin服务并使用它来注册Configurations。用JSON(或者说,我敢说,XML)文件定义配置然后读取它并更新配置管理员是非常容易的(实际上已经完成了)。

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.

这篇关于OSGi:将参数传递给特定的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆