什么时候OSGi片段附加到主机? [英] When is an OSGi fragment attached to host?

查看:134
本文介绍了什么时候OSGi片段附加到主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 OSGi 包含持久性服务(使用 hibernate )和一个包含配置的片段(xml)文件)。在bundle的激活器中,我正在使用以下命令加载配置:

I have an OSGi bundle with persistence service (using hibernate) and a fragment, which contains configuration (xml file). In bundle's activator, I'm loading the configuration using:

@Override
public void start(BundleContext ctx) {
   URL url = ctx.getBundle().getResource("hibernate.cfg.xml");
   SessionFactory sessionFactory = new AnnotationConfiguration().configure(url).buildSessionFactory();
}

但有时候,网址为null。当我试图列出所有可用的 URL s(使用 findEntries 方法)时,看起来捆绑包是自己的始终可用,但有时只是片段。我正在使用 Felix 4.0.2,捆绑包和片段是在相同的 Felix 中启动的。 auto.start level。

but sometimes, the URL is null. When I've tried to list all available URLs (using findEntries method), it appeared that the bundle's own ones are available always, but the fragment ones only sometimes. I'm using Felix 4.0.2, the bundle and the fragment is started at the same Felix. auto.start level.

推荐答案

片段在主机解析时附加到主机。通常,只要在主机解析之前安装了片段,它就会被连接。

Fragments attach to the host at the time that the host is resolved. Normally the fragment will be attached so long as it is installed before the host resolves.

但是主机总是有可能在没有片段的情况下解析,因为主机做了不依赖于他们的碎片。因此,通常你应该写你的主机,以便它可以处理不存在的片段 - 即它不应该抛出NPE等。

However there is always the possibility for the host to resolve without the fragment, because hosts do not depend on their fragments. Therefore ordinarily you should write your host so that it can cope with the fragment not being present -- i.e. it should not throw NPEs etc.

因为OSGi R4.3你可以使用 Require-Capability Provide-Capability 标头将主机的依赖项引入其片段。通过为依赖项创建自己的命名空间,您可以使您的片段为其提供 Provide-Capability 。然后你的主机可以要求 Require-Capability ....现在OSGi框架将确保片段在解析主机之前必须可用。

Since OSGi R4.3 you can introduce a dependency from the host onto its fragment using the Require-Capability and Provide-Capability headers. By inventing your own namespace for the dependency you can make your fragment provide it with Provide-Capability. Then your host can require it with Require-Capability.... now the OSGi framework will ensure that the fragment must be available before it resolves the host.

这篇关于什么时候OSGi片段附加到主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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