Java Reference
In-Depth Information
installed and another bundle comes along with the above import declaration, the
framework treats A and B as both being candidates to resolve dependency. It chooses
one following the normal rules of priority for multiple matching candidates. Clearly,
no matter which candidate it chooses, the resulting solution will be incorrect.
To avoid such situations, you need to ensure that your split package portions aren't
accidentally used by the framework to resolve an import for the entire package. But
how? Mandatory attributes can help. You can rewrite bundle A's metadata like so:
Bundle-ManifestVersion: 2
Bundle-SymbolicName: A
Bundle-Version: 2.0.0
Export-Package: org.foo.bar; version="2.0.0"; split="part1";
mandatory:="split"
Likewise for bundle B, but with split equal to part2 . Now for a bundle to import
either part of the split package, they must explicitly mention the part they wish to use.
But what about an existing client bundle wanting to import the whole package?
Because its import doesn't specify the mandatory attribute, it can't be resolved. You
need some way to reconstruct the whole package and make it available for importing;
OSG i allows you to create a facade bundle for such a purpose. To make bundle C a
facade bundle, you change its metadata to be
Bundle-ManifestVersion: 2
Bundle-SymbolicName: C
Bundle-Version: 1.0.0
Require-Bundle: A; version="[2.0.0,2.1.0)", B; version="[2.0.0,2.1.0)"
Export-Package: org.foo.bar; version="2.0.0"
The only change is the last line where bundle C exports org.foo.bar , which is
another form of re-exporting a package. In this case, it aggregates the split package by
requiring the bundles containing the different parts, and it re-exports the package
without the mandatory attribute. Now any client importing org.foo.bar will be able
to resolve to bundle C and have access to the whole package.
Summarizing split package best practices
In short, if you must use a split package, make sure you follow these steps:
Always export split packages with a mandatory attribute to avoid unsuspecting
bundles from using them.
Use either Require-Bundle or Import-Package plus the mandatory attribute to
access parts of the split packages.
To provide access to the whole package, create a facade bundle that requires
the bundles containing the package parts and exports the package in question.
Admittedly, this isn't the most intuitive or straightforward way to deal with split pack-
ages. This approach wasn't intended to make them easy to use, because they're best
avoided; but it does make it possible in those situations where you have no choice.
 
Search WWH ::




Custom Search