Search code examples
javaosgiapache-felix

deactivate/disable OSGI component by name from external bundle


Is there a way to deactivate a given OSGI component by its name?

There is the componentContext.disableComponent(componentName) method - but it only works on components of the same bundle.

What is the best practice solution to do this without adding a new service to the given bundle to deactivate the component?

Solution:

When using e.g. Felix this would be :

import org.apache.felix.scr.ScrService;

@Reference
private ScrService serviceComponentRuntime;

  public void stopByName(final String componentName)
{
    final org.apache.felix.scr.Component[] components = serviceComponentRuntime.getComponents(componentName);

    for (final org.apache.felix.scr.Component component : components)
    {
        component.disable();
    }
}

Solution

  • You can use the ServiceComponentRuntime service. It allows to introspect and manage any component.