tags:

views:

129

answers:

2

I am interested in using OSGI as a means of managing plugins for a project. That is there can be many implemenators of my interface, each appearing in its own / separate OSGI bundle with the implementation class exported...

+1  A: 

Declarative Service should be the way to go.

You can declare your interface as a service

<service>
    <provide interface="my.Interface"/>
    <property name="foo" value="bar"
</service>

Each implementation of that interface can define Bundle activation and de-activation methods.
But what is really neat is their nature: if you are using the latest SCR (the "Service Component Runtime" which is an "extender bundle" implementing the new and improved OSGi R4.2 DS - Declarative Service - specification), your classes will not import anything from the OSGI model. They remain pure POJO.

Then define another service which depends on your first service:

<reference name="myInterfaceServiceName"
    interface="my.Interface"
    bind="myActivationMethod" unbind="myDeactivationMethod"
    cardinality="0..n"/>

That service will detect and list all your concrete instances of your first service and deal with them as you intent to.

See the Eclipse Extensions and Declarative Services question for more details.

The presentation: Component Oriented Development in OSGi with Declarative Services, Spring Dynamic Modules and Apache iPOJO, from EclipseCON2009, will provide you with a concrete example.

VonC
This model has one fault - in that it seems very passive that is you can only find a bundle exists once it is activated. I was hoping to be able to get a directory of available bundles and their exported types before they are activated. Is this possible / how ?
mP
From what i can tell this is not entirely possible because in order to learn about a bundle one must be a tracker but that means its too late, and i cant control what budnles are activate/inactive.
mP
A: 

This can be done declaratively (like VonC) has detailed, or dynamically at runtime via the standard service registry.

Any implementer can simply register their implementations as a service and consumers can get them from the registry, which is pretty basic OSGi stuff. The services can also be registered with properties, so consumers can use these properties to distinguish between implementations when looking up the service.

Robin
Yes i get that - but i want to read the meta data like wht classes are exported from a bundle without "installing" or "activating" them...Is this possible.
mP