[jboss-user] [EJB 3.0] - Re: RuntimeExceptions caught, wrapped in EJBException

jc7442 do-not-reply at jboss.com
Wed Jan 24 04:04:51 EST 2007


For me it looks like this:

  | package fr.toto.interceptor;
  | 
  | import java.lang.reflect.Method;
  | import java.lang.reflect.UndeclaredThrowableException;
  | import java.util.logging.Level;
  | import java.util.logging.Logger;
  | 
  | import org.jboss.aop.advice.Interceptor;
  | import org.jboss.aop.joinpoint.Invocation;
  | import org.jboss.aop.joinpoint.MethodInvocation;
  | 
  | /**
  |  * Interceptor in charge to log unexpected exception.
  |  * 
  |  * @author jean-cedricwalmetz
  |  * 
  |  */
  | public class ExceptionHanlderInterceptor implements Interceptor {
  |   /**
  |    * Version number
  |    */
  |   private final Logger LOGGER = Logger
  |       .getLogger(ExceptionHanlderInterceptor.class.getName());
  | 
  |   /**
  |    * Log unexpected exception and thows an UndeclaredThrowableException
  |    */
  |   public Object invoke(Invocation invocation) throws Throwable {
  |     try {
  |       return invocation.invokeNext();
  |     } catch (Throwable t) {
  |       MethodInvocation methodInvocation = (MethodInvocation) invocation;
  |       Method m = methodInvocation.getMethod();
  |       Class<?>[] exceptions = m.getExceptionTypes();
  |       // Exception is an EJBException.
  |       Class<?> clazz = (t.getCause() == null ? t.getClass() : t.getCause()
  |           .getClass());// /*.getCause()*/.getClass();
  |       for (Class<?> c : exceptions) {
  |         if (c.isAssignableFrom(clazz)) {
  |           throw t;
  |         }
  |       }
  |       long time = System.currentTimeMillis();
  |       LOGGER.log(Level.SEVERE, "Unexpected exception " + time + ":", t);
  |       // UndeclaredThrowableException is not perfect since I need to
  |       // provide null as parameter to the constructor. But excpetion
  |       // thrown has to be a runtime and must be in classpath on client
  |       // side.
  |       Throwable newException = new UndeclaredThrowableException(null,
  |           "Chained exception has been logged on server with reference " + time
  |               + " !\nMessage was " + t.getMessage());
  |       throw newException;
  |     }
  |   }
  | 
  |   /**
  |    * Return the name of the interceptor
  |    */
  |   public String getName() {
  |     return ExceptionHanlderInterceptor.class.getName();
  |   }
  | }
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4005704#4005704

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4005704



More information about the jboss-user mailing list