dev@jsr311.java.net

default EJBExceptionMapper in spec...

From: Bill Burke <bburke_at_redhat.com>
Date: Wed, 19 Aug 2009 13:33:43 -0400

I'm doing an EJB example in my book and came across the scenario of
EJBException. Maybe the spec should define a default ExceptionMapper
for it? Here's a simple one I did:

@Provider
public class EJBExceptionMapper implements ExceptionMapper<EJBException>
{
    @Context
    private Providers providers;

    public Response toResponse(EJBException exception)
    {
       if (exception.getCausedByException() == null)
       {
          return Response.serverError().build();
       }
       Class cause = exception.getCausedByException().getClass();
       ExceptionMapper mapper = providers.getExceptionMapper(cause);
       if (mapper == null)
       {
          return Response.serverError().build();
       }
       else
       {
          return mapper.toResponse(exception.getCausedByException());
       }
    }
}

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com