You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stanbol.apache.org by Fabian Christ <ch...@googlemail.com> on 2011/03/28 13:08:36 UTC

Why don't JAXRS resources use @Reference instead of getting references through ServletContext?

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?

Best,
 - Fabian

Re: Why don't JAXRS resources use @Reference instead of getting references through ServletContext?

Posted by Olivier Grisel <ol...@ensta.org>.
2011/3/28 Fabian Christ <ch...@googlemail.com>:
> 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