有没有一种方法可以让一个捆绑包从 ServiceReference 获取另一个捆绑包的 Bundle 实例?
我正在尝试创建一个包来监视服务注册,并根据服务接口的 API 包中嵌入的某些元数据执行一些附加任务。元数据主要由一个或多个属性文件组成,因此我的想法是使用 Bundle.findEntries()
但由于元数据嵌入在 API 包中,我不能只做类似 >ServiceReference.getBundle().findEntries()
因为这会尝试在服务实现包中查找属性,而不是在 API 包中。
我考虑过从 ServiceReference
ObjectClass 属性获取服务 API 类名,然后使用 Package Admin 服务或 FrameworkUtil.getBundle()
,但这两者都需要 < code>Class——但是如何获取服务接口的Class
呢?执行此工作的包可能尚未导入该类的包,因此 Class.forName() 将不起作用。
我的另一个选择是监视捆绑包和服务事件:第一个创建包含元数据的捆绑包注册表,第二个选择在注册服务时使用第一个。在走这条路之前,我想看看是否有更简单的方法。
I'm trying to create a bundle that watches service registrations and, depending on certain metadata embedded in the API bundle for the service interface, performs some additional tasks. The metadata consists primarily of one or more properties files, so my thought was to use Bundle.findEntries()
but since the metadata is embedded in the API bundle, I can't just do something like ServiceReference.getBundle().findEntries()
as this would try to find the properties in the service implementation bundle, not in the API bundle.
I thought about getting the service API class name from the ServiceReference
ObjectClass property and then using either the Package Admin service or FrameworkUtil.getBundle()
, but both of these require a Class
--but how do I get the Class
of the service interface? The bundle that's doing this work probably hasn't imported the Class's package, so Class.forName()
won't work.
My other option is to watch for both bundle and service events: the first creates a registry of bundles that contain the metadata, the second using the first when a service is registered. Before going down that path I'm looking to see if there's an easier way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:我还没有尝试过这个,但我有理由确定它应该可以完成这项工作。
您可以从
ServiceReference
的ObjectClass
获取包名称,所以现在我们有了它,我们可以在框架中找到该包。给定一个 PackageAdmin packageAdmin ,您可以执行类似的操作,我们在这里所做的就是查找具有给定包名称的所有包(可能有多个),找到注册服务的包导入,并获取导出该包的包。你或许可以让这个方法变得更好一点。
Disclaimer: I haven't tried this, but I'm reasonably sure it should do the job.
You can get the packagename from the
ServiceReference
'sObjectClass
, so now we have that, we can find the package in the framework. Given aPackageAdmin packageAdmin
, you can do something likeWhat we're doing here, is find all packages with the given package name (there might be multiple), find the one that the bundle that registered the service imports, and get the bundle that exported that package. You can probably make the method a little nicer.