Search code examples
javaeclipseserviceosgideclarative

OSGi Declarative Service (ds) usage


Right now I try to implement the services of my OSGi-application as ds.

Unfortunately I can't figure out how to access consume the service.

My Service looks like this:

public interface IService {
    public void foo(<T> bar);
}
public class ServiceImpl implemets IService {
    public void foo( bar){
        ...
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="iservice">
   <implementation class="ServiceImpl"/>
   <service>
      <provide interface="IService"/>
   </service>
</scr:component>

That's as far as I am right now.

But how can I access the service?

  1. I tried the following solution: http://it-republik.de/jaxenter/artikel/OSGi-in-kleinen-Dosen-Services-auf-deklarative-Weise-2340.html

    But eclipse won't find the import for

    ComponentContext h**p://www.osgi.org/javadoc/r4v42/org/osgi/service/component/ComponentContext.html

  2. I also found this solution: h**p://www.eclipsezone.com/eclipse/forums/t97690.rhtml

    But I'm a bit disappointed I'd have to wrap every single method and I'd have to use Eclipse specific apis

    There are the same problems with this solution: https://stackoverflow.com/a/11034485/1737519 although the example uses the apache felix api and not the Eclipse api.

All I want to do is access/reference the service like this:

Iservice s = ???;
s.foo(<T> bar);

Thx for your help in advance!

P.S. sry for masking the links, but I can't include more than 2!


Solution

  • Here's a way to consume your service. I have invented a fictional Billing component that needs to call your IService. Instead of using the XML I am using bnd annotations, which are much more convenient:

    @Component
    public class Billing {
    
        private IService service;
    
        @Reference
        public void setService(IService service) {
            this.service = service;
        }
    
        public void billCustomer() {
             // Do some stuff related to billing, whatever.
    
             // Blah blah blah
    
             // Now call the service, even though it wasn't real Java because
             // the <T> type parameter was unbound, but who cares...
             service.foo(bar);
    
             // Yay.
        }
    

    }