users@jersey.java.net

[Fwd: Re: Comment: Integrating Jersey and Spring: Take 2]

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 18 Mar 2008 11:27:24 +0100

Hi Martin,

I am replying to your comment on the users, easier to track :-)

You wrote:
> Just one improvement could be made to support PerRequest scoped
> resources that have constructor arguments: the getInstance(Scope,
> Constructor, Object[]) method (which returns null in your example)
> should be changed to return getInstance( scope,
> constructor.getDeclaringClass() ).

The intended design was that if the ComponentProvider implementation
returned null Jersey would instantiate.

The WebAppicationImpl provides two implements of ComponentProvider, an
adapting and default (see end of email). If a ComponentProvider is
supplied by the application then it is adapted using
AdaptingComponentProvider implementation, otherwise the
DefaultComponentProvider is used.

The WebApplicationImpl.initiate method does this:

     public void initiate(ResourceConfig resourceConfig,
             ComponentProvider provider) {
         ...
         // Set up the component provider
         this.provider = (provider == null)
             ? new DefaultComponentProvider()
             : new AdaptingComponentProvider(provider);

Sort of a keep on trucking approach...

Paul.

     private final class AdaptingComponentProvider implements
ComponentProvider {
         private final ComponentProvider cp;

         AdaptingComponentProvider(ComponentProvider cp) {
             this.cp = cp;
         }

         public <T> T getInstance(Scope scope, Class<T> c)
                 throws InstantiationException, IllegalAccessException {
             T o = cp.getInstance(scope,c);
             if (o == null)
                 o = c.newInstance();
             injectResources(o);
             return o;
         }

         public <T> T getInstance(Scope scope, Constructor<T>
contructor, Object[] parameters)
                 throws InstantiationException, IllegalArgumentException,
                 IllegalAccessException, InvocationTargetException {
             T o = cp.getInstance(scope, contructor, parameters);
             if (o == null)
                 o = contructor.newInstance(parameters);
             injectResources(o);
             return o;
         }

         public void inject(Object instance) {
             cp.inject(instance);
             injectResources(instance);
         }
     }

     private final class DefaultComponentProvider implements
ComponentProvider {
         public <T> T getInstance(Scope scope, Class<T> c)
                 throws InstantiationException, IllegalAccessException {
             final T o = c.newInstance();
             injectResources(o);
             return o;
         }

         public <T> T getInstance(Scope scope, Constructor<T>
contructor, Object[] parameters)
                 throws InstantiationException, IllegalArgumentException,
                 IllegalAccessException, InvocationTargetException {
             final T o = contructor.newInstance(parameters);
             injectResources(o);
             return o;
         }

         public void inject(Object instance) {
             injectResources(instance);
         }
     }

-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109

attached mail follows:



Martin Grotzke wrote:

Hi,

very helpful posting, this should go into the documentation :)

Just one improvement could be made to support PerRequest scoped resources that have constructor arguments:
the getInstance(Scope, Constructor, Object[]) method (which returns null in your example) should be changed to return getInstance( scope, constructor.getDeclaringClass() ).

This seems to be a difference of the PerRequestProvider and SingletonProvider.

Cheers,
Martin

----
Respond to this comment at: 
http://blogs.sun.com/sandoz/entry/integrating_jersey_and_spring_take#comments
----
Link to comment management page:
http://blogs.sun.com/roller-ui/authoring/comments.rol?weblog=sandoz&bean.entryId=ca14c33a-4a77-4f39-8997-7dea8ae8dfc8