Search code examples
osgifelix-dependency-manager

Stopping an OSGi Bundle created using Felix Dependency Manager at startup time


We use Felix as our OSGi framework and Felix Dependency Manager for managing our OSGi Components. We have a situation where we load and validate an XML in the start method of the component. If the validation of the XML against the schema fails, we would like to stop the bundle. We have

  • attempted to throw an exception in the start method but the bundle becomes active still
  • called the stop method of the Bundle in the start method , but this sets the bundle in an unstable ( stopping ) state.

                FrameworkUtil.getBundle(this.getClass()).stop();
    

What is the right way to achieve this requirement viz if the start method has a fatal issue the bundle should remain stopped ?


Solution

  • If you want a bundle not to become active after someone tried to start it, you have to throw an exception from the start method of the BundleActivator. That is the best way to tell the framework that there is something wrong and your bundle can't start.

    You try to throw an exception from the start method of a component. At that point the bundle has already become active, so you're too late.

    I would recommend in this case that you override the start method from the DependencyActivatorBase and:

    1. Load and validate your XML (and throw an exception if that fails).
    2. Invoke the start method of the superclass, so the DM can do its work.

    That implicitly means that you cannot use the annotation based version of DM for this bundle, so if you were using that, you have to convert it to a bundle that uses code to declare its components and dependencies.