OSGI - 按服务 ID 查找捆绑包参考
OSGI - Find Bundle Reference By Service ID
我正在使用参考侦听器,在我的侦听器方法中使用 Service、Dictionary 参数。
Dictionary 有一个 service.id,那么我需要那个服务的 Bundle。
我的问题是,有了服务和服务 ID,如何获取捆绑对象。
示例:
<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作为绑定方法的参数。通过使用它,您可以获得提供该服务的捆绑包。
如果不想将ServiceReference定义为参数类型(无论出于何种原因),可以通过以下代码获取:
ServiceReference serviceRef = bundleContext.getServiceReferences(IFormProcessor.class, "(service.id=" + serviceId + ")");
我正在使用参考侦听器,在我的侦听器方法中使用 Service、Dictionary 参数。
Dictionary 有一个 service.id,那么我需要那个服务的 Bundle。
我的问题是,有了服务和服务 ID,如何获取捆绑对象。
示例:
<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作为绑定方法的参数。通过使用它,您可以获得提供该服务的捆绑包。
如果不想将ServiceReference定义为参数类型(无论出于何种原因),可以通过以下代码获取:
ServiceReference serviceRef = bundleContext.getServiceReferences(IFormProcessor.class, "(service.id=" + serviceId + ")");