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 URL
s (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.
An OSGi fragment is a Java™ archive file with specific manifest headers that enable it to attach to a specified host bundle or specified host bundles to function. Fragments are treated as part of the host bundles.
OSGi facilitates creating and managing modular Java components (called bundles) that can be deployed in a container. As a developer, you use the OSGi specification and tools to create one or more bundles. OSGi defines the lifecycle for these bundles. It also hosts them and supports their interactions in a container.
The OSGi (Open Service Gateway Initiative) specification is a Java framework for developing and deploying modular software programs and libraries. The framework was originally managed by the OSGi Alliance, an open standards organization.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With