Tanvir,
Not a trivial issue as one needs to combine two injectors in a single
object.
You did not mention how you declare your REST resource class to Jersey
in your environment.
The process will probably at some point publish a servlet using new
ServletContainer(ResourceConfig.forApplication(application)),
where application is an instance of a subclass of
javax.ws.rs.core.Application.
If you control how this subclass of javax.ws.rs.core.application is
created, there are two strategies:
- Either you add your REST resource DS component instance to the
application.getSingletons() set. This has the disadvantage of changing
the default JAX RS model of one resource instance per request to one
shared resource instance for all requests potentially causing threading
issues
- Do not define your REST resource classes as DS components, but add
them to your application.getClasses(). Pass the needed
OSGI services to Jersey's HK2 injector by adding a HK2 AbstractModule to
your application's getSingletons() set,
and use @inject in your REST resource.
To simplify this process, a whiteboard approach is recommended, where
registering an javax.ws.rs.core.Application subclass as an OSGI service
will automatically
register a Jersey ServletContainer with the http service.
See
https://github.com/Amplifino/nestor/tree/master/com.amplifino.nestor.rest for
a sample whiteboard implementation
and
https://github.com/Amplifino/nestor/tree/master/com.amplifino.nestor.useradmin.rest
for a sample rest application using the whiteboard.
regards,
Karel
I have a REST resource class, say Employees, and I need access to
another service using DS. Hence I have to make this call a component.
Now objects instantiated by @component and JAX-RS Servlet are not same.
I do not want to use static reference to the service as shown below. How
this can be handled?
-----------------------------------------------
@Component
@Path("employees")
public class EmployeeResource {
static MyService service;
@Reference
void setMyService(MyService s) {
service = s;
}
@GET
@Produces("text/plain")
public String getEmployees() {
return service.get();
}
}
_______________________________________________
OSGi Developer Mail List
osgi-dev@mail.osgi.org
https://mail.osgi.org/mailman/listinfo/osgi-dev