0

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.

Community
  • 1
  • 1
Shriram
  • 4,343
  • 8
  • 37
  • 64
  • 1
    Could you tell what you want to achieve in the end? Though there are rare cases where there is a need for the component factory, in general there are simpler solutions. The code you show sounds much more like something that can be done better with Configuration Admin and normal Components? – Peter Kriens Nov 24 '15 at 08:00
  • My intention is to create an instances based on the filtering factory and component name at runtime. Because i have to give the filters in static way instead dynamic. Anyway i can achieve it by registering componentfactory as a service. I will try with configuraiton Admin too. Thanks for the info! – Shriram Nov 24 '15 at 08:59

0 Answers0