Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSGi modules/bundles granularity

Tags:

java

osgi

When I first started looking at OSGi I was under the impression that you could just build a JAR and as-long as it had a manifest file, you could deploy it in a OSGi container. I imagined building my modules in a classic way (maven), and maybe use some plugin or something of the sort to write the manifest, I could then have my module that would basically be a standalone application communicating with other modules through OSGi.

Further reading about OSGi, I'm beginning to see more examples of it being used at a more low-level and basically replacing dependency injection and providing cross-cutting concern services like logging. And seems that using things like hibernate or others, is a problem... (or maybe I'm just missing something).

At least for me, I don't really see the point of having a such fine-gained level of modularity and integration to OSGi, I would much rather have a separate modules, each one of them having its own set of technologies and frameworks, and possibly a web resources and persistence layer. Is this achievable with OSGi? If yes, can you point me in the right direction, examples etc.?

edit, added some more details of how I'm trying to use OSGi:

I'm just envisioning the possibility of having a more than one-class module, that might have a more higher-level responsibility.

Like say agenda module. In this case I want to have things like, persistence of the events, add events, list events with filters, etc... This agenda might have several internal classes, and might even need a persistence layer. So I would like to use something like Guice to DI those classes, and some JPA to persist my data.

I can understand that some X-cutting concerns like server or logging can have a bundle, but the data model is specific to the agenda bundle. So I think my question was at the end What is and what is not possible to do inside a bundle? And what should and shouldn't be done inside as a general practice?

Thanks! Mauricio

like image 631
Mauricio Avatar asked Mar 04 '13 14:03

Mauricio


2 Answers

You can use OSGi without forcing any dependencies on OSGi on the application code. However, since OSGi provides modularity, the middleware (your layers) need to have some knowledge of OSGi. The problem is that in a modular world you want to hide implementation details, that is the whole purpose. However, things like Spring and Hibernate tend to assume the classpath has no boundaries and they run head on into the fences. Fortunately, more and more middleware is becoming prepared for this, I heard Hibernate now has an effort and JPA is also available in OSGi.

like image 127
Peter Kriens Avatar answered Sep 21 '22 06:09

Peter Kriens


OSGi is many things to many people, and you can almost pick and choose what parts of it you want to use:

  1. Do you have a plain library that doesn't use any other dependencies? Sweet, just put up a minimal MANIFEST.MF listing the public packages, use maven to build your JAR and you're done.
  2. Do you have dependencies? Same as (1), you just add the imported packages in your manifest.
  3. Do you need to perform some initialization? Write an Activator, and mention it in your manifest.
  4. Services? Just put the dependencies and descriptions of those in XML files and add them in the manifest.

And so on - just use the level where you are comfortable.

On the other hand, if you want to do web applications you really need to consider the architectural interplay between OSGi, the libraries you use, your application manager and the servlet/jee/whatever container. At what level will OSGi reside? In a general sense, there are OSGi->container->app, container->OSGi->app and container->app->OSGi solutions, and each has their own idiosyncrasies.

like image 33
Tassos Bassoukos Avatar answered Sep 18 '22 06:09

Tassos Bassoukos