Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在运行时通过导入包获得我的捆绑包所依赖的所有OSGi捆绑包?_Osgi_Osgi Bundle - Fatal编程技术网

如何在运行时通过导入包获得我的捆绑包所依赖的所有OSGi捆绑包?

如何在运行时通过导入包获得我的捆绑包所依赖的所有OSGi捆绑包?,osgi,osgi-bundle,Osgi,Osgi Bundle,我知道我可以(理论上)为我的包获取Import-Package头,解析它,并检查哪些包导出该包,但这似乎很容易出错(甚至可能返回错误的结果)。没有比获取当前捆绑包(通过FrameworkUtil.getBundle(ClassFromMyBundle.class))更好的方法来找到捆绑包的依赖关系了吗?检查导入包头(通过调用bundle.getHeaders().get(“导入包”)),并将导入的包与其他包导出的包进行比较(再次调用Bundle.getHeaders().get(“导出包”))?

我知道我可以(理论上)为我的包获取
Import-Package
头,解析它,并检查哪些包导出该包,但这似乎很容易出错(甚至可能返回错误的结果)。没有比获取当前捆绑包(通过
FrameworkUtil.getBundle(ClassFromMyBundle.class)
)更好的方法来找到捆绑包的依赖关系了吗?检查
导入包
头(通过调用
bundle.getHeaders().get(“导入包”)
),并将导入的包与其他包导出的包进行比较(再次调用
Bundle.getHeaders().get(“导出包”)
)?

不要尝试分析和比较头。相反,您可以使用捆绑包的
捆绑包配线
,获取捆绑包所需的导线(具体基于所需的包)。 然后,您可以通过
Import Package
获取提供这些导线的捆绑包,以获取捆绑包所依赖的所有捆绑包的列表

将把提供包的所有必需的返回包。然后,您可以调用每个
BundleWire
,以获取提供一个或多个包的包。但是,
getRequiredWires(BundleRevision.PACKAGE_命名空间)
不区分通过
Import-PACKAGE
头导入的包和
DynamicImport-PACKAGE
头导入的包。由于您只想查找
Import-Package
依赖项,因此需要检查
BundleWire
是否是动态依赖项。您可以通过检查以下命令的指令来执行此操作:


除了最后一部分之外,答案不错……使用
FrameworkUtil.getBundle
不是获取您自己的捆绑包的首选方法,只有在无法以其他方式获取捆绑包时才应使用(例如,通过实现DS组件或
BundleActivator
)。
BundleRequirement requirement = bundleWire.getRequirement();

if (requirement != null) {

    Map<String, String> directives = requirement.getDirectives();
    String resolution = directives.get("resolution");

    if ("dynamic".equalsIgnoreCase(resolution)) {
        // The dependency was obtained via DynamicImport-Package.
    }
}
public static Set<Bundle> getBundlePackageImportDependencies(Bundle bundle) {

    BundleWiring bundleWiring = bundle.adapt(BundleWiring.class);

    if (bundleWiring == null) {
        return Collections.emptySet();
    }

    List<BundleWire> bundleWires =
        bundleWiring.getRequiredWires(BundleRevision.PACKAGE_NAMESPACE);

    if (bundleWires == null) {
        return Collections.emptySet();
    }

    Set<Bundle> bundleDependencies = new HashSet<Bundle>();

    for (BundleWire bundleWire : bundleWires) {

        BundleRevision provider = bundleWire.getProvider();

        if (provider == null) {
            continue;
        }

        Bundle providerBundle = provider.getBundle();
        BundleRequirement requirement = bundleWire.getRequirement();

        if (requirement != null) {

            Map<String, String> directives = requirement.getDirectives();
            String resolution = directives.get("resolution");

            if ("dynamic".equalsIgnoreCase(resolution)) {
                continue;
            }
        }

        bundleDependencies.add(providerBundle);
    }

    return Collections.unmodifiableSet(bundleDependencies);
}
getBundlePackageImportDependencies(
    FrameworkUtil.getBundle(ClassFromYourBundle.class))