users@jersey.java.net

Re: [Jersey] Simplifying Jersey life-cycle and IoC integration

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 24 Nov 2008 17:33:41 +0100

On Nov 24, 2008, at 5:14 PM, Gili wrote:

> Paul Sandoz (via Nabble) wrote:
> > > Why is IoCComponentProviderFactory.getComponentProvider() being
> > > invoked on
> > > "internal" Jersey classes such as
> > > com.sun.jersey.multipart.impl.MultiPartConfigProvider?
> > >
> >
> > Cause i have yet to work out a way of filtering out the Jersey
> > components. I suppose i could do it by package name but that seems
> > wrong, i think i require a special annotation.
> [snip]
> > Null should be returned if the component provider cannot support
> the
> > class.
>
> When you finally implement this, wouldn't this mean the
> component
> provider must never return null?

No.


> My point is that if you're going to
> allow null anyway then it might not make a difference whether you pass
> it Jersey-specific classes or not because the ComponentProvider can
> just
> return null (filtering them beforehand might improve performance
> slightly).
>

Tis a good point. The main reason do to this is to reduce the noise.
But i am inclined to leave things as they are.


> > W.r.t. getInjectableInstance the parameter passed in should be from
> > the ComponentProvider .getInstance method, the return value should
> be
> > the underlying component instance that is proxied or the parameter
> > that is passed in otherwise.
>
> Paul, I think this method needs to return a Class, not an
> Object. CGLIB
> proxies are simply subclasses of the underlying class which means
> that I
> have an object of type Foo$$EnhancerByGuice$$45c70c66
> whose superclass is Foo which you want. There is no way for me to give
> you an unproxied instance; rather, I can point you to the class
> level to
> look at.
>

The important point here is that the returned instance can be injected
on by Jersey correctly. That is why the method is called
getInjectableInstance. The single use-case that has been driving this
so far has been Spring-AOP, where proxy by encapsulation can occur
rather than proxy by extension (see snippet of Spring-based code at
end of email).

Given an instance of Foo and an instance of Foo$$EnhancerByGuice$
$45c70c66 are the fields on the class Foo accessible and can those
fields be modified when given an instance of Foo$$EnhancerByGuice$
$45c70c66. If so then getInjectableInstance can just return the
instance that is passed in.

Paul.

     private static Object getInjectableInstance(Object o) {
         if (AopUtils.isAopProxy(o)) {
             final Advised aopResource = (Advised)o;
             try {
                 return aopResource.getTargetSource().getTarget();
             } catch (Exception e) {
                 LOGGER.log(Level.SEVERE, "Could not get target object
from proxy.", e);
                 throw new RuntimeException("Could not get target
object from proxy.", e);
             }
         } else {
             return o;
         }
     }