Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect if running in osgi container

Tags:

java

eclipse

osgi

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.

like image 618
Konstantin Komissarchik Avatar asked May 04 '11 05:05

Konstantin Komissarchik


People also ask

How does OSGi container work?

OSGi facilitates creating and managing modular Java components (called bundles) that can be deployed in a container. As a developer, you use the OSGi specification and tools to create one or more bundles. OSGi defines the lifecycle for these bundles. It also hosts them and supports their interactions in a container.

What is OSGi Java container?

OSGi is a Java framework for developing and deploying modular software programs and libraries. Each bundle is a tightly coupled, dynamically loadable collection of classes, jars, and configuration files that explicitly declare their external dependencies (if any).

How do I run OSGi bundle?

Procedure. Install the plug-in bundle into the Eclipse Equinox OSGi framework with the OSGi console. Start the Eclipse Equinox framework with the console enabled. Install the plug-in bundle in the Equinox console.


2 Answers

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

like image 195
Brett Kail Avatar answered Sep 16 '22 14:09

Brett Kail


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.

like image 27
Paul Webster Avatar answered Sep 19 '22 14:09

Paul Webster