How to OSGIfy a library
Asked Answered
C

1

7

I'm working on a project, it's integration project, we are using Apache Camel and Apache Karaf. In the project, I need to use the Jira REST Java client library.

So I've read quite a lot of various articles and threads about how to wrap non-OSGI library to OSGI bundle, but I'm really not sure if I got it right.

So, I've created a POM file with a dependency to the needed library. Made a package and tried to deploy it to Karaf, of course, Karaf complained for missing packages.

So, I've found corresponding maven dependency, added it, package goes into <Import-Package> and dependency into <Embed-Dependency>.

Another round, deploy, find dependency, add, ... and again, and again, until Karaf is fine with the bundle.

Is that really correct? It seems to me like quite crazy, so I guess I don't got it as usualy :)

Finally, the package get to stable that was on my work computer, I checked it quickly and went home, there I continued but, strange, the same POM / package, compiled on my personal computer is not working, again complaining about missing package, but this time, this package is for sure in the POM file and for sure it is embeded in the package, I can see it there.

This missing package is this time org.apache.commons.codec.

org.osgi.framework.BundleException: Unresolved constraint in bundle jiraclient.bundle [134]: Unable to     resolve 134.0: missing requirement [134.0] osgi.wiring.package; (osgi.wiring.package=org.apache.commons.codec)
    at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:3826)[org.apache.felix.framework-4.0.3.jar:]
    at org.apache.felix.framework.Felix.startBundle(Felix.java:1868)[org.apache.felix.framework-4.0.3.jar:]
    at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:944)[org.apache.felix.framework-4.0.3.jar:]
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundle(DirectoryWatcher.java:1247)[6:org.apache.felix.fileinstall:3.2.6]
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.startBundles(DirectoryWatcher.java:1219)[6:org.apache.felix.fileinstall:3.2.6]
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.startAllBundles(DirectoryWatcher.java:1208)[6:org.apache.felix.fileinstall:3.2.6]
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.process(DirectoryWatcher.java:503)[6:org.apache.felix.fileinstall:3.2.6]
    at org.apache.felix.fileinstall.internal.DirectoryWatcher.run(DirectoryWatcher.java:291)[6:org.apache.felix.fileinstall:3.2.6]

So, now I'm totally confused, what is wrong :(

Pretty please, guys, help me. Thanks!

The POM file is long, so I guess link is better: http://pastebin.com/j5cmWveG

Callen answered 25/10, 2012 at 20:56 Comment(4)
And do you have installed commons-codec in Karaf prior to installing your bundle? The exception tells you it need to import a package that it cannot find. Yes IMHO the exceptions from Felix should be better explained to end user, what it means.Checkerboard
Hello Claus, nice to meet you again (btw, thanks for the "Camel in Action" book :)). What it does mean, "installed commons-codec" ? It seems that in my Karaf instace, there is at least the bundle installed and active: karaf@root> bundle:list | grep codec [ 246] [Installed ] [ ] [ ] [ 80] mvn:commons-codec/commons-codec/1.2 Is that enough? Does this bundle expose needed classes? There is I guess a way how to find out bundle exports, but I don't know right now :( Btw, I have the commons-codec package embedded in the library bundle, why it doesn't work?Callen
Maybe the bundle have to explicitely export the "org.apache.commons.codec" so the embedded jira rest library can use it in the same bundle? Does that make sense?Callen
I'm quite talkative here, anyway…I tried to add these packages what Karaf complained about into <Export-Package> and the Karaf get satisfied with the bundle, or at least, no error in the log. Now there is another problem, completely new for me and that is: Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/UriBuilder. :DCallen
C
9

Yes OSGi is IMHO "far from easy to use" in terms of its deployment model, requiring 100% bundles with osgi metadata in MANIFEST.MF files. And you need a PhD in mathematics to understand the BND tool. And unfortunately many JARs are not OSGi bundles.

Looking at your pom.xml file with all the imports|exports, and that "not easy to understand" syntax, would just take 5-sec for any average engineer to understand that this "something is wrotten in the state of Denmark" ; eg OSGi != the world we live in. This must and should be easier IMHO.

You can install a plain JAR in Karaf using the wrap url handler: http://karaf.apache.org/manual/latest/developers-guide/creating-bundles.html

Another trick is to create an uber JAR, eg to put it all in a single JAR file and then you can deploy that.

There is also FAB (Fuse Bundles) which makes OSGi deployment easier, as it handles much of this craziness for you at deploy time, instead of you having to deal with the OSGi MANIFEST.MF madness: http://www.davsclaus.com/2012/08/osgi-deployment-made-easy-with-fab.html

Checkerboard answered 26/10, 2012 at 6:46 Comment(3)
You can also try with dynamic import * in your bundle. This can be configured on the Felix Bundle Plugin. felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.htmlCheckerboard
<DynamicImport-Package>*</DynamicImport-Package>Checkerboard
This must and should be easier IMHO, too!Snips

© 2022 - 2024 — McMap. All rights reserved.