简体   繁体   English

什么时候OSGi片段附加到主机?

[英]When is an OSGi fragment attached to host?

I have an OSGi bundle with persistence service (using hibernate ) and a fragment, which contains configuration (xml file). 我有一个带有持久性服务的OSGi包(使用hibernate )和一个包含配置(xml文件)的片段。 In bundle's activator, I'm loading the configuration using: 在bundle的激活器中,我使用以下命令加载配置:

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

but sometimes, the URL is null. 但有时, URL为空。 When I've tried to list all available URL s (using findEntries method), it appeared that the bundle's own ones are available always, but the fragment ones only sometimes. 当我试图列出所有可用的URL (使用findEntries方法)时,看起来捆绑包自己的URL总是可用,但有时只是片段。 I'm using Felix 4.0.2, the bundle and the fragment is started at the same Felix . 我正在使用Felix 4.0.2,捆绑包和片段是在同一个Felix启动的。 auto.start level. auto.start级别。

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. 通常,只要在主机解析之前安装了片段,它就会被连接。

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 -- ie it should not throw NPEs etc. 因此,通常你应该写你的主机,以便它可以处理不存在的片段 - 即它不应该抛出NPE等。

Since OSGi R4.3 you can introduce a dependency from the host onto its fragment using the Require-Capability and Provide-Capability headers. 从OSGi R4.3开始,您可以使用Require-CapabilityProvide-Capability标头从主机向其片段引入依赖关系。 By inventing your own namespace for the dependency you can make your fragment provide it with Provide-Capability . 通过为依赖项创建自己的命名空间,您可以使您的片段为其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. 然后你的主机可以要求它具有Require-Capability ....现在OSGi框架将确保片段在解析主机之前必须可用。

The fragment is attached to the host during the resolving process of the fragment bundle. 在片段束的解析过程期间,片段附着到主机。 The host is resolved and can start successfully even if the fragment is not there; 主机已解析,即使片段不存在,也可以成功启动; but the fragment is dependent on the host - it can be resolved and afterwards started only after it is attached to the host. 但是片段依赖于主机 - 它可以被解析,然后只有在它连接到主机后才能启动。

By having both bundles with the same start level, it seems that you have created race conditions for these two bundles. 通过使两个捆绑包具有相同的起始级别,您似乎已经为这两个捆绑包创建了竞争条件。 The framework starts resolving and starting both bundles at the same time. 该框架开始同时解析和启动两个包。 Sometimes it manages to start the host bundle before the resolving process of the fragment has been finished -> then the start method of the host bundle behaves as if no fragment is available. 有时它会在片段的解析过程完成之前设法启动主机包 - >然后主机包的start方法就像没有片段可用一样。

What you can do is eg to give the fragment an earlier start level than that of the host bundle. 您可以做的是例如为片段提供比宿主束更早的开始级别。 The fragment should resolve and start successfully even if the host bundle is not started yet. 即使主机包尚未启动,片段也应该解析并成功启动。 It only needs the host bundle to be resolved. 它只需要解析主机包。

You can also test this behavior on other OSGi frameworks - eg on ProSyst's mBedded Server (mBS) - I know that it is fully compliant with OSGI spec 4.2 where the above fragment resolving is specified. 您还可以在其他OSGi框架上测试此行为 - 例如在ProSyst的mBedded Server(mBS)上 - 我知道它完全符合OSGI规范4.2,其中指定了上述片段解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM