简体   繁体   中英

Trouble with dependencies in an OSGI bundle

I have an OSGI bundle that is working perfectly, I added, as a maven dependency, unirest a lightweight HTTP library, when deploying to serviceMix I get a missing requirement :

filter:="(osgi.wiring.package=com.mashape.unirest.http)"

Of course I am using that package in my bundle, but as far as serviceMix is concerned, that library is just classes in my classpath like my own classes

I guess I'am missing something Here

I know it is possible to embed a library, but I don't understand why any additional manipulation is needed ? how is that different from just adding that library as a maven dependency

Any answers and pointers to articles/documentation is really appreciated

There is a difference between the way dependencies are treated in maven and osgi. Maven treats the dependencies as a single classpath/classloader where the classes from every jar can access the classes from every other jar. This is historical from main apps, and its the way maven runs unit tests.

In the osgi run-time environment each bundle has its own classloader, and by default, only has access to the classes and jars embedded in its own bundle. The classes in each bundle are isolated unless the package is exported from one bundle and imported to another.

The maven bundle plugin attempts to bridge the gap. Just adding a dependency to maven is not enough, one must also tell the maven bundle plugin how to package and deploy it. The options are 1) embed the dependency in your bundle, or 2) deploy the dependency as a separate bundle. The maven bundle plugin defaults to option 2: import everything.

To understand the exports from your dependency better, I suggest looking at the manifest file in the bundle jar. (META-INF/manifest.mf) I just looked at the unirest jar on maven central and found no packages exported in the manifest. In fact unirest has no bundle headers, so its not an osgi bundle.

The error message missing requirement : filter:="(osgi.wiring.package=com.mashape.unirest.http)" means your bundle is attempting to import a package, but servicemix has no bundle exporting it. To understand this better, I suggest looking at the manifest file in your bundle jar. (META-INF/manifest.mf) I expect it will contain an import of com.mashape.unirest.http. Also look at servicemix to see if anything exports it: From the command from try: exports | grep com.mashape.unirest.http I expect you will find no bundle exporting it.

I suggest you configure the maven bundle plugin to embed unirest instead of importing it. like this:

<Embed-Dependency>unirest-java</Embed-Dependency>

(if you already have an Embed-Dependency configuration you will need to merge them.)

Ref. http://felix.apache.org/documentation/subprojects/apache-felix-maven-bundle-plugin-bnd.html#detailed-how-to

Another way is deploy unirest-java as a bundle by wrapping it. That could be as simple as copying it to the deploy folder: https://karaf.apache.org/manual/latest/#_wrap_deployer That would have the advantage of sharing unirest-java.

Lastly, if unirest does its own class loading, then that may turn out to be incompatible with osgi.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM