-1

I am trying to create a map object using osgi @reference ie) registering the below class as a factory with dependency as Hashmap objects. My intention is create an object using the factory which should create a hashmap. When i am trying to register it as factory state is unsatisfied. Is it possible to create a map object by the below approach? if not, can any one please let me know what i am doing wrong? or why we should not do this? Because Map is a interface.

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

    @Reference(name = "MapObject", bind = "createMap", unbind = "disolveMapObject", referenceInterface = Map.class)
    private Map<String, String> testMap = null;

    @Activate
    public void activate(Map<String, String> props) {
        System.out.println("Activated 1 !!!!!");
    }

    public void createMap(Map<String, String> aMap) {
        this.testMap = aMap;
        System.out.println("Map created !! " + testMap);
    }

    public void disolveMapObject(Map<String, String> aMap) {
        this.testMap = null;
    }

    @Override
    public void start() {
        System.out.println("Started 1 !!!!");
    }

    @Override
    public void stop() {
        System.out.println("Stopped 1 !!!!");
    }
}
Shriram
  • 4,343
  • 8
  • 37
  • 64

1 Answers1

1

I don't think this works. @Reference is used to bind OSGi services. So this would only work if someone has published a service of type Map.

Where do you expect the Map contents to come from?

Christian Schneider
  • 19,420
  • 2
  • 39
  • 64
  • got it! thanks Chirstian. for reference something should be exposed as a service eventhough it is internal java objects(map,list,set). – Shriram Nov 24 '15 at 07:56
  • Yes .. though I would be careful with a Map as a service as it is too generic. Chances are high that you might get the wrong one. So again the question. Where would your map contents come from? – Christian Schneider Nov 24 '15 at 08:19
  • My attempt is to create a java objects directly instead of registering as a service. Because my assumption is that default objects don't need to register it as a service as everything runs on a vm. But from your explanation i understood that everything should be exposed as a service to get it back via reference. – Shriram Nov 24 '15 at 09:01
  • Yes. In DS this is the only way. The question is why do you need @Reference at all. Why not simply create the object yourself? – Christian Schneider Nov 24 '15 at 11:25