how to detect if running in osgi container

2019-06-27 05:24发布

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.

3条回答
Viruses.
2楼-- · 2019-06-27 05:35

Add Bundle-Activator to your MANIFEST.MF. If it gets instantiated, then your JAR is running in an OSGi container.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-27 05:43

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.

查看更多
戒情不戒烟
4楼-- · 2019-06-27 05:45

If you don't have an activator, you can also try asking for your bundle:

Bundle b = org.osgi.framework.FrameworkUtil.getBundle(MyClass.this);

If it returns null, your class wasn't loaded by OSGi.

查看更多
登录 后发表回答