如何检测是否在osgi容器中运行
我有一个 OSGi 包,也可以在纯 Java 进程中运行。我需要能够判断捆绑包是否已加载到 OSGi 系统中。我怎样才能做到这一点?如果没有 OSGi 标准方法来执行此操作,我将采用 Eclipse/Equinox 特定的方法。
I have an OSGi bundle that can also run in plain Java process. I need to be able to tell if the bundle was loaded in an OSGi system or not. How can I do that? If there is no OSGi-standard way of doing this, I will settle for Eclipse/Equinox-specific approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您没有激活器,您也可以尝试请求您的包:
如果它返回 null,则 OSGi 未加载您的类。
If you don't have an activator, you can also try asking for your bundle:
If it returns null, your class wasn't loaded by OSGi.
您可以检查是否为“this.getClass().getClassLoader() instanceof org.osgi.framework.BundleReference”,只有当您在 R4.2 OSGi 框架中运行时,该值才应为 true。
You can check whether "this.getClass().getClassLoader() instanceof org.osgi.framework.BundleReference", which should only be true if you are running in an R4.2 OSGi framework.
对 Richard S. Hall 的改进答案,不需要对 osgi 的编译和运行时依赖。它还检查类的类加载器是否实现 org.osgi.framework.BundleReference 接口。
An improved answer to Richard S. Hall that doesn't require a compile and runtime dependency to osgi. It also checks if the classloader of a class implements
org.osgi.framework.BundleReference
interface.将 Bundle-Activator 添加到您的 MANIFEST.MF 中。如果它被实例化,那么您的 JAR 正在 OSGi 容器中运行。
Add Bundle-Activator to your MANIFEST.MF. If it gets instantiated, then your JAR is running in an OSGi container.