如何获取使用我导出的包之一的 Osgi 包的符号名称?
在我的一个实现库中,我想知道哪个用户库请求来自哪个?
捆绑包 A 客户端代码 -->服务接口
包B 客户端代码 -->服务接口
包C 服务接口 服务实现
这些接口由 impl 之一解析。捆绑包(捆绑包 C)。在该捆绑包内,我想知道哪个捆绑包请求来自(A 或 B)?
谢谢。
Inside one of my implementation libraries I want to know from which user library request is coming from?
Bundle A
ClientCode -- > ServiceInterface
Bundle B
ClientCode -- > ServiceInterface
Bundle C
ServiceInterface
ServiceImpl.
And those interfaces are resolved by one of impl. bundles (Bundle C). Inside that bundle I want to know from which bundle request is coming from (A or B)?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
BundleContext
的参数添加到您的接口方法中。然后,当客户端代码调用您的服务并传入其捆绑包上下文时,您可以调用 context.getBundle().getSymbolicName() 或其他方法来获取有关调用来源的捆绑包的信息。You could add a parameter for the
BundleContext
to your interface methods. Then, when the client code calls into your service, passing in its bundle context, you can callcontext.getBundle().getSymbolicName()
or other methods to get information about the bundle from which the call came.正确的方法是使用 ServiceFactory,如 OSGi 规范中所述。如果您将服务注册为服务工厂,则可以为每个“客户端”提供一个实现(其中“客户端”被定义为捆绑包,调用您的服务)。这使您可以知道谁在调用您,而客户端无需指定任何内容,因为添加名为 BundleContext 的参数显然不是一个好的设计(除非没有其他方法)。
一些“伪”代码:
The correct way to do this is to use a ServiceFactory, as explained in the OSGi specification. If you register your service as a service factory, you can supply an implementation for each "client" (where "client" is defined as bundle, invoking your service). This allows you to know who is invoking you, without the client having to specify anything as it's clearly not good design to add a parameter called BundleContext (unless there is no other way).
Some "pseudo" code: