users@jersey.java.net

Re: [Jersey] Use spring's _at_Scope annotation for jersey lifecycle [WAS: Reuse of Spring's Autowired]

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 28 Aug 2008 08:16:15 +0200

Hi Martin,

I will take a close look at this on Friday. From what you describe
and from what i understand so far option 3 does look like the best
choice.

Paul.

On Aug 27, 2008, at 8:26 PM, Martin Grotzke wrote:

> Hi Paul,
>
> On Wed, 2008-08-27 at 18:32 +0200, Paul Sandoz wrote:
>> Martin Grotzke wrote:
>>>
>>> On Wed, 2008-08-27 at 16:02 +0200, Paul Sandoz wrote:
>>>> Martin Grotzke wrote:
>>>>> On Wed, 2008-08-27 at 14:52 +0200, Paul Sandoz wrote:
>>>>>> Martin Grotzke wrote:
> [snip]
>>>>>> can be set to declare a default ResourceProvider when one is not
>>>>>> declared on a resource class. Thus the SpringServlet can set this
>>>>>> property (if not already set) and the SpringServlet
>>>>>> ResourceProvider
>>>>>> implementation can look at the @Component annotation and
>>>>>> choose to defer
>>>>>> to the singleton or per-request ResourceProvider implementations.
>>>>> Ok. I'll do a basic implementation (depending on spring 2.5)
>>>>> and send
>>>>> you a diff.
>>>>>
>>>> OK!
>>>
>>> Here it is. It's codified what I thought how it should work, and
>>> after
>>> having a look at ResourceProviderFactory.createProvider.
> I just had a second look at what's happening there: The
> ResourceProviderFactory.createProvider uses the ComponentProvider
> "provider" for
> ResourceProvider r = (ResourceProvider)provider.
> getInstance(ComponentProvider.Scope.PerRequest, providerClass);
>
> whereas the SpringResourceProvider uses the ComponentProvider
> "resourceProvider" that is passes to it in
> r.init(resourceProvider, resource);
> (also within ResourceProviderFactory.createProvider).
>
> The former one is the WebApplicationImpl.AdaptingComponentProvider
> that
> does injection, the latter one is the
> WebApplicationImpl.AdaptingResourceComponentProvider that does _no_
> injection.
>
> That's the reason for the NPE: when the SpringResourceProvider invokes
> ResourceProvider r = (ResourceProvider)provider.
> getInstance(ComponentProvider.Scope.PerRequest, providerClass);
> on the WebApplicationImpl.AdaptingResourceComponentProvider, it
> does not
> inject the
> @Context InjectableProviderContext ipc;
> on e.g. the SingletonProvider.
>
> I have no clue why the ResourceComponentProviders don't perform
> injection but the ComponentProviders do, but I see two possible
> changes/fixes for this:
> 1) Let the ResourceComponentProviders perform injection
> 2) In ResourceProviderFactory.createProvider pass the
> ComponentProvider
> ("provider") to the ResourceProvider in r.init
> 3) Extend the ResourceProvider.init and additionally provide the
> ComponentProvider ("provider") so that it can be used by the
> SpringResourceProvider
>
> For now I chose option 3) as it does not change any functionality. I
> also attach the according diff.
>
> What do you think?
>
> Cheers,
> Martin
>
>
>>>
>>> Unfortunately it does not work, when running the
>>> SpringScopedResourceTest (or any other spring test) I get this
>>> exception:
>>>
>>> Aug 27, 2008 6:09:14 PM
>>> com.sun.jersey.api.core.PackagesResourceConfig init
>>> INFO: Provider classes found:
>>> [INFO] SpringServlet - -The spring Component annotation is
>>> present, we're using spring >= 2.5
>>> [ERROR] SpringResourceProvider - -Could not initialize resource
>>> provider for resource class com.sun.jersey.impl.wadl.WadlResource
>>> java.lang.NullPointerException
>>> at com.sun.jersey.spi.resource.ResourceConstructor.getConstructor
>>> (ResourceConstructor.java:78)
>>> at com.sun.jersey.impl.resource.PerRequestProvider.init
>>> (PerRequestProvider.java:79)
>>> at
>>> com.sun.jersey.spi.spring.container.servlet.SpringResourceProvider.i
>>> nit(SpringResourceProvider.java:156)
>>> at
>>> com.sun.jersey.spi.resource.ResourceProviderFactory.createProvider
>>> (ResourceProviderFactory.java:132)
>>> at com.sun.jersey.impl.model.ResourceClass.init
>>> (ResourceClass.java:182)
>>> at
>>> com.sun.jersey.impl.application.WebApplicationImpl.getResourceClass(
>>> WebApplicationImpl.java:258)
>>> at
>>> com.sun.jersey.impl.application.WebApplicationImpl.createWadlResourc
>>> e(WebApplicationImpl.java:866)
>>> at
>>> com.sun.jersey.impl.application.WebApplicationImpl.processRootResour
>>> ces(WebApplicationImpl.java:848)
>>> at com.sun.jersey.impl.application.WebApplicationImpl.initiate
>>> (WebApplicationImpl.java:675)
>>> at
>>> com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate
>>> (SpringServlet.java:169)
>>>
>>> I think it's random that it complains when creating the
>>> WadlResource,
>>> before the exception was thrown when creating the
>>> SpringScopedResource.
>>>
>>> Do you have any idea what might be the reason?
>>>
>>
>> Not sure, i will take a look later this week. I am a bit busy at the
>> moment getting Jersey in line with the final 311 API.
>>
>> Thanks,
>> Paul.
>>
>>> Cheers,
>>> Martin
>>>
>>>
>>>
>>>> Paul.
>
> <springscopes.patch>