Don
Don

Reputation: 1458

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;
        }
    }   
}

Upvotes: 2

Views: 1257

Answers (3)

ankur_rajput
ankur_rajput

Reputation: 145

You can simply get the bundle of any class using FrameworkUtil#getBundle from which you can get BundleContext. Then from BundleContext you can get ServiceReference using which you can finally get the service instance from BundleContext API.

    import org.apache.sling.api.resource.ResourceResolverFactory;
    import org.osgi.framework.Bundle;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.FrameworkUtil;
    import org.osgi.framework.ServiceReference;

    public class GetServiceDemo {

        public static ResourceResolverFactory getResourceResolverFactory(){
            Bundle bundle = FrameworkUtil.getBundle(ResourceResolverFactory.class);
            BundleContext bundleContext = bundle.getBundleContext();
            ServiceReference<ResourceResolverFactory> resourceResolverFactoryServiceReference = bundleContext.getServiceReference(ResourceResolverFactory.class);
            ResourceResolverFactory resourceResolverFactory = bundleContext.getService(resourceResolverFactoryServiceReference);
            return resourceResolverFactory;
        }
    }

ResourceResolverFactory is a registered service.

Upvotes: 0

Peter Kriens
Peter Kriens

Reputation: 15372

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.

Upvotes: 3

Don
Don

Reputation: 1458

I found a proper working solution to the problem:

I just use the BundleContext, retrieved from the activate method. There, I use the method:

testServiceRef = context.getServiceReference(TestService.class);

where the TestService is retrieved from the bind method.

Upvotes: 0

Related Questions