01: package org.mockejb;
02:
03: import java.io.Serializable;
04: import java.rmi.RemoteException;
05:
06: import javax.ejb.*;
07: import org.apache.commons.logging.*;
08:
09: import org.mockejb.interceptor.*;
10:
11: /**
12: * Performs exception logging and chaining according to the
13: * EJB spec. Wraps system exceptions in RemoteException or EJBException.
14: * All runtime and transaction-related exceptions are considered system exceptions.
15: *
16: * @see MockContainer#isSystemException(Throwable)
17: *
18: * @author Alexander Ananiev
19: */
20: public class EjbExceptionHandler implements Aspect, Serializable {
21:
22: // logger for this class
23: private static Log logger = LogFactory
24: .getLog(EjbExceptionHandler.class.getName());
25:
26: /**
27: * Intercepts all EJB methods.
28: * @see org.mockejb.interceptor.Aspect#getPointcut()
29: */
30: public Pointcut getPointcut() {
31:
32: return PointcutPair.or(
33: new ClassPointcut(EJBObject.class, true),
34: new ClassPointcut(EJBLocalObject.class, true));
35: }
36:
37: /**
38: * Performs exception translation according to the ejb spec rules.
39: */
40: public void intercept(InvocationContext invocationContext)
41: throws Exception {
42:
43: try {
44: invocationContext.proceed();
45: } catch (Exception exception) {
46:
47: if (MockContainer.isSystemException(exception)) {
48: // According to the spec we must log the exception
49: logger.error("\nException during invocation of "
50: + invocationContext.getTargetMethod(),
51: exception);
52: //exception.printStackTrace();
53:
54: // All system exceptions must be wrapped in remote exceptions for remote objects
55: // here we can use the service provided by MockEjbContext to determine if the bean is remote or local
56: MockEjbContext ejbContext = (MockEjbContext) invocationContext
57: .getPropertyValue(MockEjbContext.class
58: .getName());
59:
60: if (ejbContext.isRemote()
61: && !(exception instanceof RemoteException)) {
62:
63: throw new RemoteException("System Error", exception);
64: }
65: if (!ejbContext.isRemote()
66: && !(exception instanceof EJBException)) {
67: throw new EJBException(exception);
68: }
69: }
70:
71: throw exception;
72:
73: }
74:
75: }
76:
77: /**
78: * This class does not have state, so all instances of this class
79: * are considered equal
80: */
81: public boolean equals(Object obj) {
82:
83: if (obj instanceof EjbExceptionHandler)
84: return true;
85: else
86: return false;
87:
88: }
89:
90: public int hashCode() {
91: return this.getClass().hashCode();
92: }
93:
94: }
|