Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-osgi library usage in an osgi application

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?

like image 633
Izza Avatar asked Feb 08 '11 09:02

Izza


People also ask

What is an OSGi application?

The OSGi (Open Service Gateway Initiative) specification is a Java framework for developing and deploying modular software programs and libraries. The framework was originally managed by the OSGi Alliance, an open standards organization.

What are the benefits of using OSGi?

OSGi modularity provides standard mechanisms to address the issues faced by Java EE applications. The OSGi framework provides the following benefits: Applications are portable, easier to re-engineer, and adaptable to changing requirements.

What is difference between jar and OSGi bundle?

There is basically no difference. A JAR is a bundle and a bundle is a JAR, the formats are identical. However, a useful bundle requires OSGi metadata in its manifest so that an OSGi framework can manage the visibility of classes between bundles.

What are OSGi modules?

OSGi defines a dynamic module system for Java™. The OSGi service platform has a layered architecture, and is designed to run on various standard Java profiles. OSGi Applications deployed to WebSphere® Application Server run on an Enterprise Java profile that is provided as part of the server runtime environment.


2 Answers

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.

like image 113
Piotr Avatar answered Sep 22 '22 00:09

Piotr


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.

like image 31
Jan Uyttenhove Avatar answered Sep 23 '22 00:09

Jan Uyttenhove