How to package and use an existing Java library with OSGI

After requesting help on dependency management in different versions of the same Java libraries, it was suggested to take a look at the OSGI implementation. Being under pressure from the deadline, I really could use some help that would save me from digging through endless OSGI documents. I have a working application that will use the new structure. The structure uses different versions of jars, which I already use, so I want to package the new infrastructure as an OSGI package. Can I leave my application as it is and use the OSGI package only as a container in the JVM? This would mean that I would use the OSGI package only to isolate the set of classes from the rest of the JVM in order to avoid conflicts between different versions of the classes. in other words, I want to use OSGI without transferring all my code to an OSGI-based installation.

Regards Seref

+7
java legacy osgi
source share
2 answers

I do not have a complete answer for you here, I just wanted to meet what the deterbert said:

First, you must have your entire OSGi application.

This is not true. Another approach would be to embed the OSGi container in the host application.

The hard part here is the interaction inside OSGi and outside, because the code lives on separate class loaders.

You can make your host classes visible to the OSGi part by using the path to the OSGi system. On the other hand, more complicated.

One of the ways host code interacts with packages is through an interface that is visible to both the host application and the package, that is, to the host part. Another way is to use reflection.

If you have a clear separation between the host and OSGi, for example, the plugin system, then this can work very well.

The future is under pressure

This can be a problem. OSGi has a lot to learn, and since it is about to hit the mainstream, it still lacks community knowledge, tool support, library compatibility, etc.

The biggest question you should ask at this point is: Do I really need to manage different versions of the dependencies at runtime? If not, that is, you can understand what happens during the deployment (for example, by configuration), then there may be a simpler solution for you.

+3
source share

First, you must have your entire OSGi application. However, I don’t think it would be difficult to configure it so that you only have to work with a few packages (maybe only two, depending on your setup.

The way with the least problems that I see is to take the framework and "wrap" it and all its dependencies in one package. Make packages for dependents dependent. Do the same for your main project.

Another way is to pack all the libraries separately using the appropriate wrap commands. These are significantly more potential problems if you are not interested in upgrading to the full version of OSGi.

Depending on your build, I would look at maven-bundle-plugin and / or Bnd . The maven-dependency plugin makes it pretty simple, since all you have to do is tell which artifact objects to embed, and it will do it. You will need to make sure that you specify all the built-in jar packages as private packages.

This should work if the infrastructure does not load the classes / AOP, which makes OSGify more difficult.

Finally, if you are interested in a quick non-osgi solution, create a framework with maven-shade-plugin and simply rename all packages for conflicting libraries. This would probably be the easiest since you just repackaged the shaded library framework once, and then used it as your dependency.

0
source share

All Articles