0

I am running some tests in Apache Felix and Arquillian, and the Framework fails to load the class org.osgi.service.log.LogService using:

BundleContext context = FrameworkUtil.getBundle(ProviderLoader.class).getBundleContext();
ServiceReference<LogService> logRef = context.getServiceReference(LogService.class);
logService = context.getService(logRef);

This service comes from org.osgi.compendium-5.0.0.jar which is installed and started in Felix too, but the logRef returns null all the time. My question is, is there any command to query what Services are currently registered in Felix? or to trace why is this reference always returning null.

Joe Almore
  • 4,036
  • 9
  • 52
  • 77

1 Answers1

1

Whilst you are right that org.osgi.compendium-5.0.0.jar contains the LogService class, that does not mean it also provides the service. In fact, that bundle only provides the APIs for all compendium services, but no implementations.

You need to deploy an implementation of LogService to get a service, for example the Apache Felix Log (that can be downloaded here: http://felix.apache.org/downloads.cgi).

That explains why you're getting null when querying the service.

If you want to query all services in the framework, you can invoke the getAllServiceReferences(null, null) method on the BundleContext. That will give you a full list of services. Take care though, without also importing all the relevant API packages, you won't be able to easily use the services (as you cannot cast the reference to the appropriate API class/interface).

As a piece of general advise, I would recommend not to directly interact with the BundleContext to lookup services though. It works in simple cases, and it's good to know how things work under the covers, but in any reasonably sized application, you will want to use some higher level (declarative) API to deal with services and dependencies. I'm biased, as I wrote on of those, so I obviously recommend the Apache Felix Dependency Manager, but there are lots of alternatives, such as Declarative Services, iPOJO and Blueprint. The good news is that you do not have to pick one, they all interoperate nicely.

Marcel Offermans
  • 3,313
  • 1
  • 15
  • 23
  • +1 for recommending frameworks. I recently read about Apache Felix Dependency Manager but I didn't get its advantages over Declarative Services apart from that the project started before Declarative Services existed. Is there any documentation how these two compare? I couldn't find any... – Puce Mar 25 '15 at 11:52
  • Hi, thanks for your answer. Yes, I could finally trace the `LogService` in the compendium bundle and it is only an interface, no implementation. And about the query, it was more about a console command to get a sort of lists of services registered under Felix Shell rather than from the bundle code. – Joe Almore Mar 25 '15 at 14:32
  • @Puce there are no documents that directly compare the different solutions, but two colleagues of mine did do a presentation comparing the different solutions: http://www.slideshare.net/SanderMak/the-ultimate-dependency-manager-shootout-qcon-ny-2014 with code at https://github.com/sandermak/osgi-dm-shootout – Marcel Offermans Mar 25 '15 at 17:21