concept Import - Package in category osgi

appears as: Import-Package, Import-Package
OSGi in Action: Creating Modular Applications in Java

This is an excerpt from Manning's book OSGi in Action: Creating Modular Applications in Java.

Your bundles can import any number of packages by listing them on Import-Package and separating them using commas. It’s not uncommon in larger projects for the Import-Package declaration to grow large (although you should strive to minimize this).

In chapter 2, you learned how Export-Package exposes internal bundle classes and how Import-Package makes external classes visible to internal bundle classes. This seems to be a nice division of labor between the two. You may even assume the two are mutually exclusive. In other words, you may assume a bundle exporting a given package can’t import it also and vice versa. In many module systems, this would be a reasonable assumption, but for OSGi it’s incorrect. A bundle importing a package it exports is a special case in OSGi, but what exactly does it mean? The answer to this question is both philosophical and technical.

The original vision of the OSGi service platform was to create a lightweight execution environment where dynamically downloaded bundles collaborate. In an effort to meet the “lightweight” aspect of this vision, these bundles collaborated by sharing direct references to service objects. Using direct references means that bundles collaborate via normal method calls, which is lightweight. As a byproduct of using direct references, bundles must share the Java class associated with shared service objects. As you’ve learned, OSGi has code sharing covered in spades with Export-Package and Import-Package. Still, there’s an issue lurking here, so let’s examine a collaboration scenario more closely.

Handling Split Packages with Import-Package

If another bundle wants to use Import-Package to access the portion of the package contained in bundle B, it can do something like this: