Apache Felix SCR的OSGi DS bundle范围组件配置不符合规范问题
规范要求
根据《OSGi DS规范》:
当服务的
scope属性设为bundle时,SCR必须为每个请求该服务的bundle创建并激活唯一的组件配置。即当两个不同bundle同时请求该bundle范围的服务时,调用org.osgi.service.component.runtime.ServiceComponentRuntime.getComponentConfigurationDTOs(ComponentDescriptionDTO)方法应返回两个不同的ComponentConfigurationDTO实例。
规范同时明确了组件实例与配置的对应关系:
组件实例(Component Instance):组件实现类的实例。组件实例在组件配置激活时创建,在配置停用后销毁。一个组件实例仅关联一个组件配置。
问题现象
Apache Felix SCR 2.2.6版本(对应jar包org.apache.felix.scr_2.2.6.jar)的实现不符合上述规范:当两个不同bundle请求bundle scope的服务时,调用getComponentConfigurationDTOs方法只能获取到一个ComponentConfigurationDTO实例。
源码分析
查看该版本源码可知,bundle scope的组件由org.apache.felix.scr.impl.manager.ServiceFactoryComponentManager类管理,它通过serviceContexts字段维护该组件的所有实例:
// maintain the map of ComponentContext objects created for the // service instances private IdentityHashMap<S, ComponentContextImpl<S>> serviceContexts = new IdentityHashMap<>();
这种实现方式是单个组件配置关联多个组件实例,直接违背了规范中“组件实例与组件配置一一对应”的要求。
内容的提问来源于stack exchange,提问作者Bourbon_7

