package org.mockejb;
import java.io.Serializable;
import javax.ejb.*;
import org.apache.commons.logging.*;
import org.mockejb.interceptor.*;
public class CMPFindByPrimaryKeyHandler implements Aspect, Serializable {
private static Log logger = LogFactory.getLog( CMPFindByPrimaryKeyHandler.class.getName() );
protected EntityDatabase entityDatabase;
public CMPFindByPrimaryKeyHandler( final EntityDatabase entityDatabase ){
this.entityDatabase = entityDatabase;
}
public Pointcut getPointcut(){
return PointcutPair.and( new MethodPatternPointcut( "findByPrimaryKey" ),
PointcutPair.or( new ClassPointcut( EJBHome.class, true),
new ClassPointcut( EJBLocalHome.class, true) ) );
}
public void intercept( InvocationContext invocationContext ) throws Exception {
BasicEjbDescriptor descriptor= (BasicEjbDescriptor) invocationContext.getPropertyValue("descriptor");
if ( descriptor instanceof EntityBeanDescriptor &&
((EntityBeanDescriptor) descriptor).isCMP()) {
logger.debug("Intercepted "+invocationContext.getProxyMethod());
Object [] paramVals = invocationContext.getParamVals();
Object pk = paramVals[0];
Object entity = entityDatabase.find( descriptor.getHomeClass(), pk);
if (entity!=null) {
invocationContext.setReturnObject( entity );
}
else {
logger.info( "Entity "+descriptor.getIfaceClass().getName()+
" for PK "+pk+" is not found in the entity database. Proceeding to the next interceptor...");
invocationContext.proceed();
}
}
else { invocationContext.proceed();
}
}
public boolean equals( Object obj ){
if ( obj instanceof CMPFindByPrimaryKeyHandler &&
entityDatabase.equals( ((CMPFindByPrimaryKeyHandler)obj).entityDatabase ) )
return true;
else
return false;
}
public int hashCode() {
return this.getClass().hashCode()+entityDatabase.hashCode();
}
}