JBoss Community

Re: Why application exceptions are wrapped in EJBException on AS 6.1?

created by goldm in EJB3 - View the full discussion

Hi,

 

yes, there is one. You can extend BMTTxInterceptorFactory and override method createPerJoinpoint. In returned instance of StatelessBMTInterceptor, override handleException method - check if ApplicationException annotation is present on exception class.

 

Something like this:

 

public class BMTTxInterceptorFactoryCustom extends BMTTxInterceptorFactory

{

   @SuppressWarnings("unused")

   private static final Logger log = Logger.getLogger(BMTTxInterceptorFactoryCustom.class);

 

   public Object createPerJoinpoint(Advisor advisor, Joinpoint jp)

   {

      // We have to do this until AOP supports matching based on annotation attributes

      TransactionManagementType type = TxUtil.getTransactionManagementType(advisor);

      if (type != TransactionManagementType.BEAN)

         return new NullInterceptor();

     

      TransactionManager tm = TxUtil.getTransactionManager();

      boolean stateful = advisor.resolveAnnotation(Stateful.class) != null;

      // Both MessageDriven and Stateless are stateless

      if(stateful)

         return new StatefulBMTInterceptor(tm);

      else

         return new StatelessBMTInterceptor(tm) {

             protected void handleException(Invocation invocation, Exception ex) throws Exception   {

                if (ex == null)

               {

                  return;

               }

               ApplicationException ae = (ApplicationException) invocation.getAdvisor().resolveAnnotation(ApplicationException.class);

               // it's an application exception, so just throw it back as-is

               if (ae != null || ex.getClass().isAnnotationPresent(ApplicationException.class))

               {

                  throw ex;

               }

               if (ex instanceof EJBException)

               {

                  throw (EJBException) ex;

               }

               else

               {

                  throw new EJBException(ex);

               }

             }

         };

   }

}

 

Then you have to use BMTTxInterceptorFactoryCustom instead of BMTTxInterceptorFactory (configured in ejb3-interceptors-aop.xml and singleton-container-aop.xml)

Reply to this message by going to Community

Start a new discussion in EJB3 at Community