2016-12-01 75 views
0

有沒有辦法通過名稱禁用給定的OSGI組件?通過外部軟件包的名稱禁用/禁用OSGI組件

componentContext.disableComponent(componentName)方法 - 但它只適用於相同捆綁的組件。

什麼是最佳做法的解決方案做到這一點,而無需向給定的包添加新的服務來停用組件?

解決方案:

當使用例如菲利克斯這將是:

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(); 
    } 
} 

回答

0

您可以啓用/禁用通過組件上下文:

@Component(service=ComponentEnabler.class) 
public class ComponentEnabler { 

    ComponentContext context; 

    @Activate 
    void activate(ComponentContext context) { 
    this.context = context; 
    } 

    public void enable(String name) { 
    this.context.enableComponent(name); 
    } 
    public void disable(String name) { 
    this.context.disableComponent(name); 
    } 
}