2011/3/28 Fabian Christ <[email protected]>:
> Hi,
>
> when looking at the current implementation of JAXRS resources in
> enhancer/jersey, I see that the JerseyEndpoint class for instance uses
>
> @Reference
> EnhancementJobManager jobManager;
>
> On the other hand we have JAXRS resources like EnginesRootResource
> which don't use @Reference. Instead they use the ServletContext to get
> references.
>
> public EnginesRootResource(@Context ServletContext context) {
> tcManager = (TcManager) context.getAttribute(TcManager.class.getName());
> }
>
> What is the reason behind this construct? Wouldn't it be possible to
> use @Reference in the JAXRS resource classes?
JAX-RS resources are instanciated by the jax rs runtime and are
therefore are not registered as OSGi components (hence I don't think
you can use the OSGi dependency injection on non-OSGi POJO classes).
With JAX-RS, typically there is one class instantiated for each
resource per HTTP request using that resource. That makes it trivial
to make thread safe code.
It should be possible to put the OSGi bundle context inside the
ServletContext so that JAX-RS resource can use the bundleContext API
to lookup any OSGi service instead of preloading individual services
in advance, for instance:
ServiceReference tcManagerReference =
bundleContext.getServiceReference(TcManager.class.getCanonicalName());
TcManager tcManager = (TcManager)
bundleContext.getService(tcManagerReference);
--
Olivier
http://twitter.com/ogrisel - http://github.com/ogrisel