Search code examples
javaosgi

How do I get both Service Object and ServiceReference, when using declarative services


I need both ServiceReference and Service Object when I'm using my Bundle in osgi. However, when I do it as showed by the code below, my bundle can no longer find the bind method. what am I doing wrong?

public class AServiceImpl implements AService{

    TestService t;
    JournalService journalService;
    BundleContext context;
    ServiceReference testServiceRef;

    public void bindTestService(TestService testService, ServiceReference sv) {
        t = testService;
            testServiceRef = sv;
        //this.testServiceRef = testServiceRef;
        test.api.TestStaticVariable.getUniqueInstance().add("TEST A");
        System.out.println(test.api.TestStaticVariable.getUniqueInstance().toString());
    }

    public void unbindTestService(TestService testService){
        System.out.println("");
        if(t.equals(testService)){
            t = null;
        }
    }

    public void bindJournalService(JournalService journalService){
        this.journalService = journalService;  
    }

    public void unbindJournalService(JournalService journalService){
        if(this.journalService.equals(journalService)){
            this.journalService = null;
        }
    }   
}

Solution

  • See section 112.3.2 of OSGi compendium 4.3 at https://osgi.org/download/r4v43/osgi.cmpn-4.3.0.pdf.

    So just use the ServiceReference and get the object through the component context as described in 112.3.2.