James Baker
James Baker

Reputation: 1256

Creating an extensible Java library (JAR)

I'm looking to create a Java 'library' (JAR) that can be used within other projects, but I'd like my library to be extensible. I'm after something OSGi-esque, where my library has 'extension points' that other JARs can hook into. The thinking is that my library will have a core set of methods that will be called by the projects it's used in, but the exact implementation of these methods might vary based on the project/context.

I've looked into OSGi (e.g. Equinox), but I'm not sure it can be used in the way I'm after. It seems to be geared towards standalone apps rather than creating a library.

What would be the best way of achieving this? Can OSGi be used in this way, and if not are there frameworks out there that will do this?

I hope all that's clear - I have a clear idea of what I want, but it's hard to articulate.

Upvotes: 3

Views: 351

Answers (3)

Björn Pollex
Björn Pollex

Reputation: 76778

As others have mentioned, you don't need OSGi or any framework for this. You can do this my employing patterns like the template method pattern or the strategy pattern. There are several other patterns for dynamically modifying/extending functionality, but these seem to fit your description most. They do not require any framework.

The benefit you would get from a framework like OSGi would be that it would manage the wiring for you. Normally, you'll have to write some code that glues your libraries and the extensions together - with a framework like OSGi, this will not be automated with minimal overhead (in case of OSGi, the overhead is some entries in the JAR-manifest).

Upvotes: 0

thobens
thobens

Reputation: 1731

OSGi is great, but I don't think that this is what you need. By using OSGi (-Services), you force the user of your library to use an OSGi environment, too.

I think as @Peter stated, you can do this by simply extending classes of your library in the specific project / context.

But in case you want to use OSGi, there is a simple way to achieve this. It's called Bundle Fragments. This way you can create a bundle and extend a so-called Host-Bundle", i.e. your library, without altering the original library. A popular use case for this is if you have platform specific code in your bundles.

Upvotes: 2

Rohit Jain
Rohit Jain

Reputation: 213193

What you are naming a Java library is named "Bundle" in OSGi context.

OSGi Bundle is a JAR file with some special Meta-Information in its MANIFEST.MF file. Now, every OSGi Bundle have either Exported-Packages or Imported-Packages.

Through Export-Packages Manifest header, you can show what all packages you are exporting.. And your other project can simply add the package it wants to use from them to its Import-Packages.. Here's an example: -

Bundle A Manifest: -

Export-Packages: com.demo.exported;

Bundle B Manifest: -

Import-Packages: com.demo.exported;version=(1.0.0, 2.0.0]

This way your bundle B (A different project) can call the methods from the class in the package that it imported from Bundle A.. Now, the version you see in the import-package, is just to show what all package version can it accept.. You can have 2 bundles with two different implementation of some interfaces and provide this package in two different version.. Both will be available..

Till now, I was talking about Static data-types..

You can also have your services exposed dynamically through Declarative Service.. In this case you will have to define one XML file (Component Definition) where you show what all services your Bundle will expose.. And in the other bundle, you can again define another XML, to show what all services it requires.. These are called, Provided Services and Referenced Services..

I think this will give you a little idea about what can be done. And if I am wrong somewhere in interpreting your problem please specify the same..

*NOTE: - And of course OSGi is used for creating independent Bundles, that can be re-used in other projects.. They bring Modularity to your project..

Upvotes: 1

Related Questions