users@jersey.java.net

Re: [Jersey] Simplification of Injectable by using ThreadLocal

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 23 Oct 2008 09:03:17 +0200

On Oct 21, 2008, at 12:57 PM, Paul Sandoz wrote:

> Hi,
>
> The Injectable interface is as follows:
>
> public interface Injectable<T> {
> T getValue(HttpContext context);
> }
>
> it pulls in HttpContext thus a whole bunch of other stuff in the
> jersey-core that is not really required.
>
> It would be better if it was like this:
>
> public interface Injectable<T> {
> T getValue();
> }
>

I have modified the interface to the above. For those transitioning
from 1.0 to 1.0.1-SNAPSHOT just remove the HttpContext parameter. If
you need to access the context in your injectable then in your
injectable provider that returns such injectable instances do this:

   @Context private HttpContext hc;

or in a constructor:

   // No need to copy reference as this is final and can be reused by
   // anonymous inner classes
   final HttpContext hc;

   public MyInjectableProvider(@Context HttpContext hc) {
     this.hc = hc;
   }

The HttpContext instance injected will be a proxied thread local
instance. Any method calls to that instance out side the scope of a
request will result in an illegal state exception.

Paul.