在OSGi容器中查询功能的正确方法是什么

在OSGi容器中查询功能的正确方法是什么,osgi,Osgi,我曾经使用PackageAdmin.getBundles()查询由symbolicName和版本范围安装的捆绑包。但是这个类被弃用了 现在我知道我应该使用IdentityNamespace(在本例中)来使用功能和查询包 但是,查询容器中安装的所有捆绑包所提供的功能的正确和最快的方法是什么 我应该自己创建一个方法,从BundleContext.getBundles()获取所有bundle的数组吗。然后遍历这样的数组,为BundleVision调整每个bundle,然后尝试匹配其getDeclare

我曾经使用PackageAdmin.getBundles()查询由symbolicName和版本范围安装的捆绑包。但是这个类被弃用了

现在我知道我应该使用IdentityNamespace(在本例中)来使用功能和查询包

但是,查询容器中安装的所有捆绑包所提供的功能的正确和最快的方法是什么

我应该自己创建一个方法,从BundleContext.getBundles()获取所有bundle的数组吗。然后遍历这样的数组,为BundleVision调整每个bundle,然后尝试匹配其getDeclaredCapabilities()方法返回的功能?还是有其他简单的方法

我应该自己创建一个方法,从BundleContext.getBundles()获取所有bundle的数组吗。然后遍历这样的数组,为BundleVision调整每个bundle,然后尝试匹配其getDeclaredCapabilities()方法返回的功能?还是有其他简单的方法

差不多。你应该:

  • 从bundleContext获取可用的捆绑包
  • 使线束适应线束
  • 从bundlewiring中获取功能
e、 g:

Bundle[]bundles=context.getBundles();
foreach(Bundle:bundles){
BundleWiring BundleWiring=bundle.adapt(BundleWiring.class);
List capabilities=bundleWiring.getCapabilities(null);
myCapabilityBasedLogic(能力);
}
有关更多信息,请参见类

更新

如果捆绑包处于已安装状态,则没有捆绑包绑定。我编写了一段代码片段,以获取之前安装的捆绑包的功能:

请参阅函数getAllCapabilities(bundles,state)

相关代码段:

PlatformAdmin platformAdmin = systemBundleContext.getService(platformServiceSR);
State state = platformAdmin.getState();
Bundle[] bundles = systemBundleContext.getBundles();

List<BundleCapability> availableCapabilities = new ArrayList<BundleCapability>();
for (Bundle bundle : bundles) {
    BundleDescription bundleDescription = state.getBundle(bundle.getBundleId());
    List<BundleCapability> declaredCapabilities = bundleDescription.getDeclaredCapabilities(null);
    availableCapabilities.addAll(declaredCapabilities);
}
return availableCapabilities;
PlatformAdmin PlatformAdmin=systemBundleContext.getService(platformServiceSR);
State State=platformAdmin.getState();
Bundle[]bundles=systemBundleContext.getBundles();
List availableCapabilities=new ArrayList();
用于(捆绑包:捆绑包){
BundleDescription BundleDescription=state.getBundle(bundle.getBundleId());
List declaredCapabilities=bundleDescription.getDeclaredCapabilities(null);
可用能力。添加全部(申报能力);
}
返回可用能力;
我想菲利克斯也有类似的可能性。如果您了解了它的工作原理,请与我分享,我也将为Felix创建一个HackUtil实现;)

如果您想要一个通用的手工解决方案,您可以解析已安装包的提供功能和导出包头。您可以使用felix utils解析头。这个库嵌入到许多技术中,但我也对它进行了重新打包,并将其作为一个组件上传到maven central。您的代码可以类似于以下内容:

Dictionary<String, String> headers = installedBundle.getHeaders();
String header = headers.get("Provide-Capability");

if (header != null) {
    // Parse with felix-utils
    Clause[] clauses = Parse.parseHeader(header);
    for(Clause clause : clauses) {
        String nameSpace = clause.getName();
        Attribute[] attributes = clause.getAttributes();
        Directive[] directives = clause.getDirectives();

        processCapability(nameSpace, attributes, directives);
    }
}
Dictionary headers=installedBundle.getHeaders();
stringheader=headers.get(“提供功能”);
if(标题!=null){
//使用felixutils进行解析
子句[]子句=Parse.parseHeader(header);
(第条:条款){
字符串名称空间=子句.getName();
Attribute[]attributes=子句.getAttributes();
指令[]指令=子句。getDirectives();
处理能力(名称空间、属性、指令);
}
}

您必须根据需要实现processCapability。您还可以解析导出包头并将其转换为基于OSGi规范的功能。

我的理解是,捆绑包在安装状态时没有捆绑包环。是吗?你说得对。我以为你只需要可用的功能。用我目前知道的所有信息更新了答案:)。
Dictionary<String, String> headers = installedBundle.getHeaders();
String header = headers.get("Provide-Capability");

if (header != null) {
    // Parse with felix-utils
    Clause[] clauses = Parse.parseHeader(header);
    for(Clause clause : clauses) {
        String nameSpace = clause.getName();
        Attribute[] attributes = clause.getAttributes();
        Directive[] directives = clause.getDirectives();

        processCapability(nameSpace, attributes, directives);
    }
}