Question

I am looking for a simple way to control the instantiation of a declarative service.

What I want is something like a factory which is asked to return an instance of a component, but where I can control the instantiation.

Currently I just found the ComponentFactory where I just can call newInstance with a given dictionary. But what I really want, is something like this, assuming IComponent is the declarative service interface and MyComponent is the implementation class

public class MyComponentFactory implements ? {

   public IComponent newInstance() {
       return new MyComponent("firstParameter", "secondParameter");
   }
}

Is there something possible like this with declarative services, or do I need to use my own service registration in a bundle activation code?

Thanks in advance

Was it helpful?

Solution

DS does not provide for the level of instance creation indirection you are looking for. DS will always use the public no-args constructor and then call the specified activator method to complete the instance initialization.

OTHER TIPS

One alternative for such service instantiation control is using a combination of DS and CM.

You must set configuration-policy="require" in the DS xml and use CM to create a configuration instance that will be used to pass a Dictionary containing all properties that you need (only types supported by DS of course) to the created service instance. You can even use a propertie file plus Felix File Install for that connfiguration.

If that is not enough you have another alternative that is to track for new created service and call a setup(args) method just after you have added a configuration using CM.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top