How to use OSGi getServiceReference() right

Posted by Jens on Stack Overflow See other posts from Stack Overflow or by Jens
Published on 2010-04-28T19:16:04Z Indexed on 2010/04/29 8:27 UTC
Read the original article Hit count: 510

Filed under:
|

Hello,

I am new to OSGi and came across several examples about OSGi services.

For example:

import org.osgi.framework.*;
import org.osgi.service.log.*;

public class MyActivator implements BundleActivator {
  public void start(BundleContext context) throws Exception {
    ServiceReference logRef = 
      context.getServiceReference(LogService.class.getName());
  }
}

My question is, why do you use

getServiceReference(LogService.class.getName())

instead of

getServiceReference("LogService")

If you use LogService.class.getName() you have to import the Interface. This also means that you have to import the package org.osgi.services.log in your MANIFEST.MF.

Isn't that completely counterproductive if you want to reduce dependencies to push loose coupling? As far as I know one advantage of services is that the service consumer doesn't have to know the service publisher. But if you have to import one specific Interface you clearly have to know who's providing it. By only using a string like "LogService" you would not have to know that the Interface is provided by org.osgi.services.log.LogService.

What am I missing here?

© Stack Overflow or respective owner

Related posts about osgi

Related posts about service