Dynamic target for declarative service in OSGI
Given the user who is using the service, how can this consumer dynamically select a specific provider using a declarative service?
Example
Service.java
public interface Service {
public void do();
}
Provider1.java
public class Provider1 implements Service {
@Override
public void do(){
//a way
}
}
Provider2.java
public class Provider2 implements Service {
@Override
public void do(){
//another way
}
}
Consumer.java
public class Consumer {
private Service myService;
protected void bindService(Service s){ // Actually it Provider1
myService = s;
}
protected void unbindService(Service s){
myService = null;
}
public void useThisKindOfService(String s){
// Do something crazy
}
}
So, what I would like, instead of "Do Something Crazy", is to find a way to reconfigure the user to free Provider1 and request Provider2.
Is it possible?
Update related to "Duplicate question"
OSGI / Felix Declarative Services: How to Filter Related Services
In my context, I cannot use a declarative target because the value of the target needs to be known at build time, in my case the target can be user defined at runtime.
source to share
Declarative service components can be configured via ConfigurationAdmin. However, the configuration of the component can be changed at runtime.
You can also change the myService.target configuration via ConfigurationAdmin at runtime. If you do this, another link will bind to your component.
If your component's link policy is dynamic, the new link will be linked without reactivating your component.
For more information, see the Declarative Services chapter of the OSGi Compendium specification.
source to share