Non-osgi library usage in an osgi application
Asked Answered
N

4

14

Is it possible to use a non-osgi library with an OSGi application?

For an example, I'm developing a semantic based search engine, and I am using a third party Natural Language Processing library for it (http://wiki.opencog.org/w/RelEx_Dependency_Relationship_Extractor).

Is it possible to interface such a library which doesn't suport OSGi, as a couple of jar files, with my OSGi application?

Nevile answered 8/2, 2011 at 9:18 Comment(0)
W
23

As it was written in previous answers you have two options if you want to use additional libraries in your bundles:

  1. embedding library jars in a bundle in which it will be used,
  2. creating a valid OSGi bundle from the library.

The first approach is simpler because you only need to copy library jars (and all its dependencies) to a bundle (e.g. to a root directory) and then add them to Bundle-Classpath element in MANIFEST.MF (see here). However, while doing this you must remember that this added library will be visible only in a bundle in which it is embedded (so library reuse is limited). You could always add packages from this library to Export-package element in MANIFEST.MF to make it visible for other bundles but this is far from elegant solution (however it will work).

In order to make it visible to other bundles you should use the second approach, i.e. create an OSGi bundle from the library (there are tools which can help you in doing that, also in Eclipse). However, for more complicated libraries this approach may be harder (because of dependencies and specific class loading approach in OSGi).

So if you want to use the library only in one bundle I suggest using the first approach (it is easier to implement). If you want to use this library in many bundles in your application you should consider the second approach.

Wysocki answered 8/2, 2011 at 10:15 Comment(1)
The first approach has a problem. Embedding transitive optional dependencies will be a problem. I faced this problem. If you have a solution for this, please add it.Whitehorse
A
5

Yes, you can either embed the external library in your bundle or wrap ("OSGIfy") the library as an OSGi bundle. For both options, Pax Construct (http://www.ops4j.org/projects/pax/construct) is a good tool.

If your external library itself has dependencies, embed all of these in a single bundle or use Pax Construct to wrap them transitive.

If have to to choose between wrapping or embedding, consider dependency management and versioning of bundles. If you need to upgrade the external library and it's embedded in your own application bundle, you always release both the library and your own code. It is e.g also not possible to have 2 version of the library active without having 2 version of your application bundle active. And also... if you are not working in an OSGi environment, would you like to add third-party classes in your application jars? So why do it in an OSGi environment?

I personally prefer in such a case to see the external lib as a black box, and wrap the library and it's dependencies in a single bundle.

Aquanaut answered 8/2, 2011 at 10:2 Comment(0)
L
3

Yes, it is possible. You have two options:

Firstly, you can include all packages from external library into private-package section of your bundle. It will include all this packages into jar with your application. The second option is to make a valid osgi bundle from external library.

Lysias answered 8/2, 2011 at 9:32 Comment(0)
M
0

For Eclipse IDE;

  1. Create libs folder under your bundle.
  2. Add your jar file which is not OSGi bundle to libs folder.
  3. Right click to jar file -> click "Build Path" -> click "Add to Build Path"
Mongolia answered 28/2, 2020 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.