jrey
jrey

Reputation: 2183

OSGI - Find Bundle Reference By Service ID

I am using a reference listener, with Service,Dictionary parameters in my Listener Method.

The Dictionary has a service.id, then I need to have the Bundle of that Service.

My question is, having the Service, and the Service ID, how to I get the Bundle Object.

Example:

<reference-listener
            bind-method="bindFormProcessor"
            unbind-method="unbindFormProcessor"
            ref="mainSvc"
            >

    </reference-listener>

My Java Method

public void bindFormProcessor(IFormProcessor formProcessor,Dictionary dictionary) {
    try {
        Bundle bundle = OsgiUtil.getBundleByObject(dictionary.get("service.id"));
        logger.info("************************ GOOOAAL " + bundle.getSymbolicName());
        //Now, I have the bundle of my Service
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Regards,

Upvotes: 0

Views: 743

Answers (1)

Balazs Zsoldos
Balazs Zsoldos

Reputation: 6046

You can define ServiceReference as the parameter of the bind method. By using that, you can get the bundle that offers the service.

If you do not want to define ServiceReference as the parameter type (for whatever reason), you can get it with the following code:

ServiceReference serviceRef = bundleContext.getServiceReferences(IFormProcessor.class, "(service.id=" + serviceId + ")");

Upvotes: 2

Related Questions