我正在使用引用监听器,并在监听器方法中使用 Service、Dictionary 参数。
字典有一个service.id,那么我需要拥有该服务的Bundle。
我的问题是,有了服务和服务 ID,如何获取 Bundle 对象。
示例:
<reference-listener
bind-method="bindFormProcessor"
unbind-method="unbindFormProcessor"
ref="mainSvc"
>
</reference-listener>
我的Java方法
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();
}
}
问候,
最佳答案
您可以定义 ServiceReference作为bind方法的参数。通过使用它,您可以获得提供该服务的 bundle 。
如果您不想将 ServiceReference 定义为参数类型(无论出于何种原因),您可以通过以下代码获取它:
ServiceReference serviceRef = bundleContext.getServiceReferences(IFormProcessor.class, "(service.id=" + serviceId + ")");
关于java - OSGI - 通过服务 ID 查找 bundle 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28615820/