2010-04-08 16 views
0

在我的一个实现库中,我想知道哪些用户库请求来自哪个用户?如何获得使用我的导出包之一的Osgi包的符号名称?

捆绑 ClientCode - > serviceInterface等

捆B ClientCode - > serviceInterface等

捆绑Ç serviceInterface等 ServiceImpl。

这些接口由impl中的一个来解决。捆绑(捆绑C)。在这个包里面,我想知道哪个bundle请求来自(A或B)?

谢谢。

+0

你能有点更specifc?标题是关于包裹,关于服务的文字。你想知道谁在使用(即导入)导出的软件包,或者谁在调用你的软件包中注册的服务? – akr 2010-04-21 13:14:55

回答

2

您可以将BundleContext的参数添加到您的接口方法中。然后,当客户端代码调用您的服务时,传递其捆绑上下文,您可以调用context.getBundle().getSymbolicName()或其他方法来获取有关调用来自的包的信息。

1

正确的做法是使用ServiceFactory,如OSGi规范中所述。如果将服务注册为服务工厂,则可以为每个“客户端”(其中“客户端”定义为捆绑包,调用服务)提供实现。这可以让你知道谁在调用你,而不需要客户端指定任何东西,因为它显然不是好的设计来添加一个名为BundleContext的参数(除非没有别的办法)。

一些 “伪” 代码:

class Bundle_C_Activator implements BundleActivator { 
    public void start(BundleContext c) { 
    c.registerService(ServiceInterface.class.getName(), 
     new ServiceFactory() { 
     Object getService(Bundle b, ServiceRegistration r) { 
      return new ServiceImpl(b); // <- here you hold on to the invoking bundle 
     } 
     public void ungetService(Bundle b, ServiceRegistration r, Object s) {} 
     }, null); 
    } 
} 

class ServiceImpl implements ServiceInterface { 
    ServiceImpl(Bundle b) { 
    this.b = b; // <- so we know who is invoking us later 
    } 
    // proceed here with the implementation... 
} 
相关问题