How to get use of OSGi bundle services on-the-fly
i have a little problem here finding out how to use registered
services in OSGi.
what i am doing is the followig:
- register/provide services with
context.registerService(... , ... , null);
in each of the "service providing" Activator classes (means: the
- set up a "managing" bundle with a ServiceListener in the Activator
that (un)registers provided services on-the-fly.
now i have implemented my serviceChanged method like below
public void serviceChanged(ServiceEvent event)
String objectClass = (String)
if (event.getType() == ServiceEvent.REGISTERED)
System.out.println("Service " + objectClass + " registered.");
else if (event.getType() == ServiceEvent.UNREGISTERING)
System.out.println("Service " + objectClass + "
else if (event.getType() == ServiceEvent.MODIFIED)
System.out.println("Ex1: Service of type " + objectClass + "
System.out.println("RuntimeException :: " + re.getMessage());
here is a rather nice tutorial (even though i do not use felix/
knopflerfish - i am using Eclipse/Equinox) that shows an example on
how to get use of a registered service from another bundle:
now my question is: how do i dynamically "use" my services? right now
it seems to me that on the one hand i can (un)register services on-the-
fly but on the other i always have to make sure that the service i
want to use is currently registered. this sounds like i have to use a
hell of a lot if-clauses or switch-case... at least if i am following
that tutorial from above.
for example I have a bundle providing a xml validation service and a
xml parsing service. another bundle is just for logging purpose and so
how would you implement a project like this? is it even useful to have
a "managing bundle" or should i add a ServiceListener to each of my
providing bundles to share funcionality under each other?
thank you so much in advance! i hope you can help me out with this.