简体   繁体   English

如何创建具有可选软件包分辨率的有效OSGi捆绑包?

[英]How to create a working OSGi bundle with optional package resolution?

I would like my bundle to be either configurable via System properties or via the OSGi Compendium Config Admin. 我希望可以通过系统属性或通过OSGi Compendium Config Admin来配置我的软件包。

I am compiling my bundle against the org.osgi:osgi.cmpn:6.0.0 bundle, which as made clear in the OSGi Alliance blog is meant to be used at compile-time only, with the framework providing the actual implementations at runtime. 我正在针对org.osgi:osgi.cmpn:6.0.0捆绑包编译我的捆绑包,正如OSGi Alliance博客中明确指出的那样 ,该捆绑包仅在编译时使用,该框架在运行时提供了实际的实现。

My code obviously needs to use the ConfigAdmin package (to handle the case in which the ConfigAdmin Service is present)... which means that if the runtime does not export the ConfigAdmin package, my bundle will not resolve properly. 我的代码显然需要使用ConfigAdmin程序包(以处理存在ConfigAdmin服务的情况)...这意味着,如果运行时未导出ConfigAdmin程序包,则我的捆绑包将无法正确解析。

But I wanted this resolution to be optional... so I added this to the manifest: 但是我希望该分辨率是可选的...因此我将其添加到清单中:

org.osgi.service.cm;resolution:=optional;version="[1.5,2)"

Now, the bundle will resolve but will crash at runtime with a java.lang.NoClassDefFoundError: org/osgi/service/cm/ManagedService even if the user will not actually use ConfigAdmin for configuration. 现在,该捆绑包将解决,但在运行时会出现java.lang.NoClassDefFoundError: org/osgi/service/cm/ManagedService崩溃java.lang.NoClassDefFoundError: org/osgi/service/cm/ManagedService即使用户实际上不会使用ConfigAdmin进行配置。 So this forces the user to install the config-admin bundle just to make my bundle work. 因此,这迫使用户安装config-admin软件包只是为了使我的软件包正常工作。

I guess the secret here is to not instantiate any classes that force the JVM to load a class that uses the org.osgi.service.cm package... but I can't see how I can achieve that without ugly hacks with reflection... 我想这里的秘诀是不要实例化任何迫使JVM加载使用org.osgi.service.cm包的类...但是我看不到没有丑陋的反射怎么实现。 ..

Does anyone know how I can check if the package is available at runtime, and if not, avoid the java.lang.NoClassDefFoundError at runtime, making this package dependency truly optional? 有谁知道我如何检查该程序包在运行时是否可用,否则,请避免在运行时避免java.lang.NoClassDefFoundError ,从而使此程序包依赖项真正成为可选项?

The way to tell if the package is available at runtime is to attempt to load a class from it and be prepared for the NoClassDefFoundError. 判断程序包是否在运行时可用的方法是尝试从程序包中加载类并为NoClassDefFoundError做准备。 You could do this in a central place and then decide to avoid code paths which require the optional but absent package. 您可以在中心位置执行此操作,然后决定避开需要可选但不存在软件包的代码路径。

The PackageAdmin can be used to inspect package metadata of the system. PackageAdmin可用于检查系统的软件包元数据。 It has been deprecated and you are expected to use the BundleWiring instead. 它已被弃用,您应该改用BundleWiring。 So, in your DS, you would have a dependency to the PackageAdmin/BundleWiring and check if the cm package is exported... 因此,在您的DS中,您将依赖于PackageAdmin / BundleWiring并检查cm包是否已导出...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM