implementing component factory for multiple class with same names and retrieving through property/filter values

泪湿孤枕 提交于 2019-12-25 00:38:09

问题


I am working on OSGi(Rev 4) project in which i have to use karaf registry. Either i can declare my implementation as Component or Service. ie) I can register my component/service in karaf with component factory/service Factory to produce objects of Component/Service. I am having multiple class implementations, so with Service factory i hope it is achievable with single class ie) by registering all the services with the same name with different property/filter values.While retrieving service i can query with the properties so that only one retrieval class is enough. Project structure

 1. Service(interface)
         - implementation 1
         - implementation 2  (different implemenations)
         - ....
 2. Factory(register as a component factory)
 3. FactoryManager(which produces objects)

I have raise an another question in SO with my same project structure. Can anybody please tell me whether this kind of scenario is achievable through component factory ie) registering with same factory name(factory="com.java.test.ClassImpl") with different properties and retrieving through the property values(dynamic invocation of target attribute). In more precise manner

first factory

 @Component(name = "ExampleComponentFactoryServiceProvider", factory = "example.factory.provider")
    public class ExampleComponentFactoryServiceProvider implements ExampleFactoryService {

second factory

@Component(name = "ExampleComponentFactoryServiceProvider1", factory = "example.factory.provider")
public class ExampleComponentFactoryServiceProvider1 implements ExampleFactoryService {

The above are two factories registered with same factory name. My expectation registering with different property values. Retrieval part in activate()method

System.out.println("activate in manager !!!!");
        Dictionary<String, String> hashMap = new Hashtable<String, String>();
        hashMap.put("component.name", "ExampleComponentFactoryServiceProvider");
        instance = factory.newInstance(hashMap);
        service = (ExampleFactoryService) instance.getInstance();
        System.out.println("service  = " + service.toString());

        Dictionary<String, String> hashMap1 = new Hashtable<String, String>();
        hashMap1.put("component.name", "ExampleComponentFactoryServiceProvider1");
        instance = factory.newInstance(hashMap1);
        service = (ExampleFactoryService) instance.getInstance();
        System.out.println("Service = " + service.toString());

In the above code , component factory tries to create two different instances by filtering through property. But it creates the first one and creates the second one if first one is stopped. how to create objects through same factory id with different target attribute generated at runtime.

来源:https://stackoverflow.com/questions/33603037/implementing-component-factory-for-multiple-class-with-same-names-and-retrieving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!