You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@openwebbeans.apache.org by thiago soares <th...@gmail.com> on 2013/01/28 14:31:43 UTC

InvocationContextImp cleans target field if occurs an exception

Hi all,

I'm doing some tests using ApacheOpenWebBeans 1.1.7 as the CDI implementation.
I am using a Framework (frameworkdemoiselle.gov.br) that offers an
interceptor that handles exceptions thrown by the target bean.

A summary of this interceptor is written bellow:

=========================
@AroundInvoke
public Object manage(final InvocationContext ic) throws Exception {
        Object result = null;

        try {
                result = ic.proceed();

        } catch (Exception cause) {
                // Handle Exception here
                target = ic.getTarget();
                ...

        }
        return result;
}
=========================

The "Handle Exception" logic uses the target bean. The problem is that
the "ic.getTarget()" method returns null and i get a
NullPointerException.
I've done the same test using WELD and ic.getTarget() does not returns null.

Analyzing the source code of the class
"org.apache.webbeans.intercept.InvocationContextImpl" I see that it
destroys the target instance when it catches an exception.

=========================
public Object proceed() throws Exception
{
        try
        {
            if (type.equals(InterceptionType.AROUND_INVOKE))
            {
                return proceedAroundInvokes(interceptorDatas);
            }
            else if (type.equals(InterceptionType.AROUND_TIMEOUT))
            {
                return proceedAroundTimeouts(interceptorDatas);
            }
            return proceedCommonAnnots(interceptorDatas, type);

        }
        catch (InvocationTargetException ite)
        {
            target = null; // destroy target instance    <<=================

            // Try to provide the original exception to the interceptor stack,
            // not the InvocationTargetException from Method.invoke
            Throwable t = ite.getCause();
            if (t instanceof Exception)
            {
                throw (Exception) t;
            }
            throw ite;
        }
        catch (Exception e)
        {
            target = null; // destroy target instance <<=================

            throw e;
        }
}
=========================

My question is: Why we need to destroy the target instance when occurs
an Exception? Doing it so, the interceptor cannot get the target after
invoking proceed() method.

Thanks,

Thiago

Re: InvocationContextImp cleans target field if occurs an exception

Posted by Mark Struberg <st...@yahoo.de>.
Hi Thiago!

Thanks for the catch, we will address this. 
Can you please create a jira ticket [1] so we do not forget?

txs and LieGrue,
strub



[1] https://issues.apache.org/jira/browse/OWB



----- Original Message -----
> From: thiago soares <th...@gmail.com>
> To: user@openwebbeans.apache.org
> Cc: 
> Sent: Monday, January 28, 2013 2:31 PM
> Subject: InvocationContextImp cleans target field if occurs an exception
> 
> Hi all,
> 
> I'm doing some tests using ApacheOpenWebBeans 1.1.7 as the CDI 
> implementation.
> I am using a Framework (frameworkdemoiselle.gov.br) that offers an
> interceptor that handles exceptions thrown by the target bean.
> 
> A summary of this interceptor is written bellow:
> 
> =========================
> @AroundInvoke
> public Object manage(final InvocationContext ic) throws Exception {
>         Object result = null;
> 
>         try {
>                 result = ic.proceed();
> 
>         } catch (Exception cause) {
>                 // Handle Exception here
>                 target = ic.getTarget();
>                 ...
> 
>         }
>         return result;
> }
> =========================
> 
> The "Handle Exception" logic uses the target bean. The problem is that
> the "ic.getTarget()" method returns null and i get a
> NullPointerException.
> I've done the same test using WELD and ic.getTarget() does not returns null.
> 
> Analyzing the source code of the class
> "org.apache.webbeans.intercept.InvocationContextImpl" I see that it
> destroys the target instance when it catches an exception.
> 
> =========================
> public Object proceed() throws Exception
> {
>         try
>         {
>             if (type.equals(InterceptionType.AROUND_INVOKE))
>             {
>                 return proceedAroundInvokes(interceptorDatas);
>             }
>             else if (type.equals(InterceptionType.AROUND_TIMEOUT))
>             {
>                 return proceedAroundTimeouts(interceptorDatas);
>             }
>             return proceedCommonAnnots(interceptorDatas, type);
> 
>         }
>         catch (InvocationTargetException ite)
>         {
>             target = null; // destroy target instance    
> <<=================
> 
>             // Try to provide the original exception to the interceptor stack,
>             // not the InvocationTargetException from Method.invoke
>             Throwable t = ite.getCause();
>             if (t instanceof Exception)
>             {
>                 throw (Exception) t;
>             }
>             throw ite;
>         }
>         catch (Exception e)
>         {
>             target = null; // destroy target instance <<=================
> 
>             throw e;
>         }
> }
> =========================
> 
> My question is: Why we need to destroy the target instance when occurs
> an Exception? Doing it so, the interceptor cannot get the target after
> invoking proceed() method.
> 
> Thanks,
> 
> Thiago
>