Exporting API from implementation bundle - osgi

Can someone explain to me the rationale behind BND's exporting api packages that a bundle doesn't have. It seems just wrong to me, it always (AFAICT) results in ClassNotFoundException
Especifically
bundle A contains com.foo.api.MyInterface
bundle B contains com.foo.impl.InterfaceImpl which implements MyInterface
So if I tell bnd to export package com.foo in bundle A he will correctly export com.foo.api, but if i tell export package com.foo in bundle B it will incorrectly export com.foo.api, so consumers of MyInterface get wired to bundle B and then at runtime get a ClassNotFoundException
Why doesn't BND guarantee that when a bundle exports a package it actually contains it??
I know that if I export com.foo.api and com.foo.impl instead of just com.foo then the problem is solved in my trivial example, but in our real system this is not really a posibility. Also, the actual question is why BND does that, not how to fix the problem. i already know workarounds to solve the problem

This is just a feature of the bnd syntax. Because bnd has the option to embed imported packages, it considers the whole classpath when determining Export-Package and Private-Package headers. Bnd doesn't make any distinction between classes which are physically local to a folder being processed and others elsewhere on the classpath.
So if your Export-Package statement encompasses packages bnd knows about, bnd will assume you want to embed and export them. I'm surprised you're seeing exceptions, though, since bnd should also physically include the exported classes in the packaged bundle.

Did you look in bundle B to see that com.foo.api is not in there? If you tell bnd that bundle B should export com.foo.api, then bnd will actually put that package in bundle B and then export it. bnd has the "power" to include classes from anywhere in the -buildpath in the generated bundle. I think you really don't want bundle B to include and export com.foo.api, so you should modify your bnd file to remove the export statement.

I have not yet worked with bnd directly but through the maven bundle plugin which should be fairly similar. What I found is that if you export a package you do not have then bnd will embed the classes of this package inside your bundle jar.
So at least when using the maven bundle plugin bundle B should have the interface embedded when the export of com.foo.api is defined. Not sure what happens when you define only the parent package as export but I would assume the same happens.
This automatic embedding can be quite useful if you want to deploy the api together with each bundle which I do not do regularly but which seems to be a kind of best practice in OSGi (for example for bundles that implement OSGi apis).

Related

Maven bundle plugin, what is the difference between <Private-Package> and embedding dependency?

Here is the reference on Maven Bundle Plugin. I've read it, and concluded, that if I want to use some other package inside my bundle and I want to keep this as an implementation detail, I don't want to import or export this dependency, than I shall use Private-Package instruction. And, there is a possibility of embedding dependencies, which is described here. So, my question - isn't it the same, and if not - what is the difference?

Bndtools and org.osgi.util.tracker package

I'm using Bndtools in Eclipse IDE. It's great and works fine.
I'm able to add osgi.core package into the "Build Path" editing the bnd file (it uses the bndtools repositories).
Now I need the org.osgi.util.tracker package but I can't find it in the "Build Path" editing the bnd file.
How can I do it ?
Thanks,
Paolo
org.osgi.util.tracker is a package but in the Java compiler we need to put whole JARs on the build classpath.
This package happens to come from the bundle/JAR called osgi.cmpn, which represents the OSGi Compendium specifications. So just add osgi.cmpn to the -buildpath of your bnd.bnd file and the package you want will be visible.
If you don't know what JAR/bundle a package comes from, you can use the Advanced Search button in the Repositories View in Bndtools. If you can't find it there then you will have to go and get the JAR from the outside world and import it into your repository. Just googling for the package name will usually tell you the name of the library you need.
If you use [OSGi enRoute][1] then you'll find that you only need to compile against the base API JAR (all enRoute projects are so setup). This makes your life a lot easier. The base API contains all API (and ONLY API) for the most common tasks from the most humble 'Hello World' to a REST server with Web User Interface.
[1] http://enroute.osgi.org

OSGi:export a package without exporting a few Classes within the same package

I am working in a OSGi project where we need to export a package in bundle but we do not want to export a two specific class of this bundle. To give more background, we use a third party jar that is not osgi bundle so we add this as a part of OSGi bundle and in this third party jar we need to export a package "A" but this package A has two specific classes that we do not intend to be exported. Please give your suggestion on the way to achieve this.
Export-Package: some.package.name; version=1.0; exclude:="ClassA,ClassB"
See sections 3.6.5 and 3.7.9 in the OSGi Core specification.

OSGi - modify a class in a bundle without modifying the original bundle

I have a need to modify a class in a OSGi bundle. But this bundle comes from an upstream project hence I cannot(/won't) touch it. But I have to modify a class in it to match my project's requirements. So, I'm looking for a solution for this.
So, I created my own bundle which has the patched classes. But there is a complication since the upstream OSGi bundle, and the patch OSGi bundle I created has the same packages. I want to over-ride the classes in the upstream OSGi bundle, without touching the it.
I looked at the osgi.org Fragment page. It suggests a way to patch a host bundle by using a fragment bundle, but for that I have to modify the upstream project bundle's MANIFEST.MF to set Bundle-ClassPath: patch.jar,.
Any suggestions?
You can implement a WeavingHook and catch the class loading of that bundle.
However, do it only if you do not have any other chance to solve your problem. If you have any chance to contribute to that project that contains the jar, send a patch for them and have this only as a temporary solution. With hacks like this, your system will be chaotic for others.

What is the intended use case for Bundle-Classpath in OSGI bundles

I am trying to understand the intended use case for Bundle-Classpath in OSGI bundles.
Here is my understanding, please help me understand if this is correct.
Let's say I am working on creating an OSGI bundle which will be deployed in an ecosystem of other bundles. The bundle I am working on needs some other bundles, but they are not loaded/exported in this ecosystem, and I do not have control on what the ecosystem exports. In such a scenario, I can put these bundles inside some directory (say 'lib') which becomes part of my bundle. These bundles should also be referenced from the Bundle-Classpath, so they can be loaded.
Is this a correct use case for Bundle-Classpath ?
Will these additional bundles also be loaded in the OSGI container and will packages exported by them be available to other bundles ?
Bundle-ClassPath is intended for including dependencies in our bundle, so that our bundle can be deployed standalone.
Let's take an example. Suppose the code in my bundle uses a library, e.g. Google Guava. I have two choices for packaging my bundle:
Simply create my bundle with only my own code inside it. The bundle will now have the Import-Package statements that declare a dependency on Guava, and anybody who wants to deploy my bundle into his application will also have to deploy Guava.
Alternatively I can include a copy of Guava inside my bundle and reference it from my Bundle-ClassPath. Whoever deploys my bundle can deploy just my bundle, and doesn't need to worry about where to get Guava from. In fact, the existence of Guava inside my bundle is an implementation detail, and the deployer doesn't even need to know that I am using it.
The choice between these two options is a trade-off. Option 2 has the advantage that my bundle is easier to deploy because it is standalone -- everything it needs is right there inside it. On the other hand my bundle is much bigger than it needs to be, which could become a problem if lots of other bundles also embed their own copy of Guava.
A more severe problem with option 2 is that all of the dependencies of the library now become my dependencies as well. Actually Guava is a rare example of a Java library with no dependencies of its own... but many other Java libraries drag in a huge tree of transitive dependencies. If you use this approach with, say, Hibernate then your own bundle will also have that large dependency set. This gets very ugly, very quickly.
So, you should be cautious not to overuse Bundle-ClassPath/Embed-Dependency. You should only consider using it if the dependency is (a) small, and with no transitive dependencies, and (b) your bundle uses the library as an internal implementation detail, i.e. it is not part of your public API.
UPDATE
I forgot to answer your second question about the exports. The answer is NO, the exports of any "bundles" you put on your Bundle-ClassPath will NOT become exports of your own bundle. In fact the JARs we put on Bundle-ClassPath are not treated as bundles at all, they are just JARs.
You can choose to export packages that come from within the JARs on your Bundle-ClassPath but you have to do this in the MANIFEST.MF of your own bundle.
The most common use case for this header is the packaging of external libraries. Let's say you have some library foo.jar, and want to use its classes in your bundle.
You put the jar into your bundle like so,
/
com/company/Activator.class
foo.jar
META-INF/MANIFEST.MF
In you manifest, you can now use
Bundle-ClassPath: foo.jar,.
Remember to include the . on the classpath, or you will not be able to find the classes in your bundle.
When classes are on the Bundle-ClassPath, you can use them like any other class: use them in your code, or export them.
I think you may be a bit off here.
A Bundle-Classpath is an ordered, comma-separated list of relative
bundle JAR file locations to be searched for class and resource
requests.
What this means is that when some bundle class needs another class in
the same bundle, the entire bundle class path of the containing bundle
is searched to find the class.
From OSGI in Action.
Let's consider a concrete case. Imagine a bundle (JAR file) with the following structure:
src/a/A.class
src2/b/B.class
src3/c/C.class
if you wanted a.A, b.B and c.C to be available to each other, you'd have to define src, src2 and src3 as pertaining to the bundle classpath. That would mean you'd have to add to your manifest file the following line:
Bundle-ClassPath: src,src2,src3

Resources