01: package org.mockejb;
02:
03: import java.io.Serializable;
04:
05: import javax.ejb.*;
06: import org.apache.commons.logging.*;
07:
08: import org.mockejb.interceptor.*;
09:
10: /**
11: * Handles calls to findByPrimaryKey for CMP beans.
12: * Checks if the requested PK is in the EntityDatabase,
13: * if it is, gets the entity from the EntityDatabase and returns it to the client.
14: * Otherwise, proceeds to the next interceptor in the chain.
15: *
16: * @author Alexander Ananiev
17: */
18: public class CMPFindByPrimaryKeyHandler implements Aspect, Serializable {
19:
20: // logger for this class
21: private static Log logger = LogFactory
22: .getLog(CMPFindByPrimaryKeyHandler.class.getName());
23:
24: protected EntityDatabase entityDatabase;
25:
26: public CMPFindByPrimaryKeyHandler(
27: final EntityDatabase entityDatabase) {
28: this .entityDatabase = entityDatabase;
29: }
30:
31: public Pointcut getPointcut() {
32:
33: return PointcutPair.and(new MethodPatternPointcut(
34: "findByPrimaryKey"), PointcutPair.or(new ClassPointcut(
35: EJBHome.class, true), new ClassPointcut(
36: EJBLocalHome.class, true)));
37: }
38:
39: public void intercept(InvocationContext invocationContext)
40: throws Exception {
41:
42: // get the descriptor
43: BasicEjbDescriptor descriptor = (BasicEjbDescriptor) invocationContext
44: .getPropertyValue("descriptor");
45:
46: // handle only CMP entity beans
47: if (descriptor instanceof EntityBeanDescriptor
48: && ((EntityBeanDescriptor) descriptor).isCMP()) {
49:
50: //EntityBeanDescriptor descriptor = (EntityBeanDescriptor) ejbDescriptor;
51:
52: logger.debug("Intercepted "
53: + invocationContext.getProxyMethod());
54:
55: Object[] paramVals = invocationContext.getParamVals();
56: Object pk = paramVals[0];
57:
58: Object entity = entityDatabase.find(descriptor
59: .getHomeClass(), pk);
60: // if entity is in DB - return
61: if (entity != null) {
62: invocationContext.setReturnObject(entity);
63: } else {
64: logger
65: .info("Entity "
66: + descriptor.getIfaceClass().getName()
67: + " for PK "
68: + pk
69: + " is not found in the entity database. Proceeding to the next interceptor...");
70: // proceed to the next interceptor, may be some other interceptor implements "findByPrimaryKey"
71: invocationContext.proceed();
72: }
73: } else { // not a CMP entity bean
74: invocationContext.proceed();
75: }
76: }
77:
78: /**
79: * This class does not have state, so all instances of this class
80: * are considered equal
81: */
82: public boolean equals(Object obj) {
83:
84: if (obj instanceof CMPFindByPrimaryKeyHandler
85: && entityDatabase
86: .equals(((CMPFindByPrimaryKeyHandler) obj).entityDatabase))
87: return true;
88: else
89: return false;
90:
91: }
92:
93: public int hashCode() {
94: return this.getClass().hashCode() + entityDatabase.hashCode();
95: }
96:
97: }
|