Disclaimer: I'm just getting started with OSGI so please bear with me and my lack of knowledge...
For the sake of this exercise, suppose I have a Jersey (2.17.0) based REST application running under Jetty (9.2.10) in an OSGI environment, which provides the user with some statistics that are gathered from a separate server via SOAP.
I'm now trying to reuse some of the existing code in a different application which is supposed to retrieve statistics via JMS (or something else).
My intention is to introduce an abstraction layer for the client API in a bundle, implement a bundle for each application with the appropriate communication channel and use CDI to inject the client at runtime in my REST resources. For example:
The REST resource (bundle 1, common for both apps)
@Path("statistics")
public class StatisticsResource {
@Inject
private StatisticsClient client;
@GET
@Path("users")
public List<User> getActiveUsers(){
return client.getActiveUsers();
}
}
Common API (bundle 2, common for both apps)
public interface StatisticsClient {
List<User> getActiveUsers();
}
SOAP implementation (bundle 3 for app 1)
@ApplicationScoped
public class SOAPClient implements StatisticsClient {
@Override
public List<User> getActiveUsers() {
// connect to server via SOAP
}
}
JMS implementation (bundle 3 for app 2)
@ApplicationScoped
public class JMSClient implements StatisticsClient {
@Override
public List<User> getActiveUsers() {
// connect to server via JMS
}
}
I've been reading and searching for information on how to use injection with Jersey, HK2 (2.4.0) and OSGI but I so far I haven't found something relevant to match the above idea.
Most of the Jersey CDI injection examples I've seen so far, define bindings using concrete classes, such as bind(MyService.class).to(MyService.class);
whereas I'd like to be able to switch implementation at runtime and use either SOAPClient
or JMSClient
depending on the application where the code is currently running. Ideally the implementation would be deduced/detected by the framework from the available OSGI services (or perhaps classpath or something similar)...
Is that doable, and if so, what am I missing? Alternatively what basic concept did I perhaps misunderstood or missed?