Well, let's take an example:  we want to publish a transaction manager
implementing javax.transaction.TransactionManager

To reduce class fragmentation, we want to import / export the
javax.transaction.xa package.  This way, if another bundle provides the same
package, our bundle will be wired to that one instead of having the same
package exported twice.   For the implementation (in case we want to export
it), we don't really want to import our own package, else we may be wired to
another version of the same package, which does not make sense, because our
bundle is really supposed to provide this package in the given version.

If we don't do that, this leads to very undesirable behavior:  let's say our
implementation package is foo.bar

If we deploy our bundle in version 1.2, we will end up with the following
wirings for our bundle:
   export javax.transaction.xa ; version = 1.1
   export foo.bar ; version = 1.2

Later, if we deploy the same bundle in version 1.1, we will end up with the
following wirings
   import javax.transaction.xa ; version = 1.1  (from bundle v1.2)
   import foo.bar ; version = 1.2 (from bundle v1.2)

Now let's say that between foo.bar v1.2 and v1.1, one class has been
refactored as an inner class, then we could easily end up in
NoClassDefFoundError in bundle v1.1, because the package comes from v1.2 and
this class does not exists ...

So while importing exported packages is fine to reduce class fragmentation,
this should only be done for substituable packages, such as specifications,
etc...    For jars that don't provide any services, such as libraries
transformed into OSGi bundles, this should be really avoided.

See
http://felix.apache.org/site/apache-felix-osgi-faq.html#ApacheFelixOSGiFAQ-Shouldabundleimportitsownexportedpackages?
also

On Tue, Sep 8, 2009 at 12:56, Oisin Hurley <oisin.hur...@gmail.com> wrote:

> > #1. bundles should not import the packages they export
>
> This is kindof contrary to what OSGi recommends - that you should
> always import what you export to prevent class fragmentation in
> consumer bundles.
>
> Full gory details here
>
> http://www.osgi.org/blog/2007/04/importance-of-exporting-nd-importing.html
>
>
>  --oh
>



-- 
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
------------------------
Open Source SOA
http://fusesource.com

Reply via email to