users@jersey.java.net

Re: [Jersey] Possible bug injecting (trying to) custom _at_Context object, using jersey 1.0.

From: Samuel Le Berrigaud <samuel.lb_at_gmail.com>
Date: Thu, 2 Apr 2009 08:59:18 +1100

Thank you Paul.

Works perfectly. It took me a while to reply as I was busy with something else.
As always you've been very helpful.

SaM

On Tue, Mar 10, 2009 at 9:38 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
> Hi Samuel,
>
> ContextResolver is not designed as a mechanism of inject of a type using
> @Context.
>
> If you declare:
>
>    @Provider
>    public class MyContextResolver implements ContextResolver<MyContext>
>
> then you can inject:
>
>    @Context ContextResolver<MyContext> cr;
>
>
> If you want to support:
>
>    @Context MyContext c;
>
> then you need to register a Jersey injectable provider, for example:
>
>
>  https://jersey.dev.java.net/source/browse/*checkout*/jersey/tags/jersey-1.0.2/api/jersey/com/sun/jersey/spi/inject/SingletonTypeInjectableProvider.html
>
>    @Provider
>    public class MyContextProvider extends
> SingletonTypeInjectableProvider<Context, MyContext> {
>        public MyContextProvider() {
>            super(new MyContext());
>        }
>    }
>
> SingletonTypeInjectableProvider is a helper class that supports injection of
> a singleton instance.
>
> Paul.
>
> On Mar 10, 2009, at 12:58 AM, Samuel Le Berrigaud wrote:
>
>> So I want to inject my own @Context object.
>>
>> I have the the interface of the type to be injected and the associated
>> resolver. Something like:
>>
>> public interface MyContext
>> {
>> }
>>
>> @Provider
>> public class MyContextResolver implements ContextResolver<MyContext>
>> {
>>  public MyContext getContext(Class type)
>>  {
>>      return MyContext.class.equals(type) ? new MyContext() {} : null;
>>  }
>> }
>>
>> Unfortunately "somewhere" this doesn't work for me. And I think I have
>> located the somewhere. The ContextResolver does get picked up
>> correctly and gets registered as an InjectableProvider<Context, Type>.
>> I believe there could be an issue in the following piece of code,
>> found at lines 134 to 151 of the
>> com.sun.jersey.core.spi.factory.ContextResolverFactory:
>>
>> public Injectable getInjectable(ComponentContext ic, Context ac, Type c) {
>>              if (!(c instanceof ParameterizedType))
>>                  return null;
>>              ParameterizedType pType = (ParameterizedType)c;
>>              if (pType.getRawType() != ContextResolver.class)
>>                  return null;
>>              Type type = pType.getActualTypeArguments()[0];
>>              // TODO check if concrete type
>>
>>              final ContextResolver cr = getResolver(ic, type);
>>              if (cr == null) return null;
>>
>>              return new Injectable() {
>>                  public Object getValue() {
>>                      return cr;
>>                  }
>>              };
>>          }
>>
>> Here the "Type c" parameter is assumed to be of type ContextResolver.
>> But what I get when debugging is different. The Type is the actual
>> type of object to be injected, so in my case not MyContextResolver but
>> MyContext. I believe this is correct according to the javadocs. This
>> would mean that this implementation doesn't do at all what it is
>> supposed to be doing. Moreover the getInjectable should return
>> MyContext not a ContextResolver AFAIK.
>>
>> So currently the code doesn't get passed the first if statement for
>> me, as MyContext is not a ParameterizedType.
>> Later in this method, if I change the type in this method call
>> "getResolver(ic, type)" to MyContextResolver I can access the
>> correctly registered MyContextResolver and from there access
>> MyContext.
>>
>> So is that a bug? Am I missing something obvious? Anyone has had the
>> same issue? I could find anything in bugzilla…
>>
>> Hope this all makes sense,
>> Any help appreciated,
>> SaM
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>