Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - 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). 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();
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 -- i.e. it should not throw NPEs etc.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...