package org.mockejb;
import java.lang.reflect.*;
import javax.ejb.*;
class EntityBeanHome extends BasicEjbHome {
private DummyCMPBean dummyCmpBean = new DummyCMPBean();
private EntityDatabase entityDatabase;
EntityBeanHome( BasicEjbDescriptor descriptor, final EntityDatabase entityDatabase ){
super( descriptor );
this.entityDatabase = entityDatabase;
}
public Object create( BasicEjbDescriptor basicDescriptor, MockEjbObject ejbObject,
Method createMethod, Object[] paramVals ) throws Exception {
EntityBeanDescriptor descriptor = (EntityBeanDescriptor)basicDescriptor;
MockEjbContext ejbContext = new MockEjbContext( getHomeProxy() );
Object bean = createBeanInstance(descriptor, createMethod, ejbContext );
Object pk = null;
if ( ! createMethod.getDeclaringClass().equals( GenericHome.class )) {
pk = invokeBeanCreateMethod( bean, createMethod, paramVals );
ejbContext.setPrimaryKey( pk );
invokeBeanMethodWithPrefix( "ejbPost", bean, createMethod, paramVals );
}
Object ejbObjectProxy = ejbObject.createProxy( bean, ejbContext );
if (pk != null ) {
entityDatabase.add( descriptor.getHomeClass(), pk, ejbObjectProxy);
}
return ejbObjectProxy;
}
private Object createBeanInstance( EntityBeanDescriptor descriptor,
Method method, MockEjbContext ejbContext ) throws Exception {
Class beanClass = descriptor.getBeanClass();
Object bean;
if (beanClass != null && descriptor.isCMP() ) {
EntityBeanSubclass subclassFactory = EntityBeanSubclass.newInstance(beanClass);
bean = subclassFactory.create();
}
else {
bean = createBean( descriptor );
}
if ( bean instanceof EntityBean ) {
Class paramTypes[]={ EntityContext.class };
Object args[]={ ejbContext };
invokeBeanMethod(bean, null,
"setEntityContext", paramTypes, args );
}
return bean;
}
public Object invokeHomeMethod( BasicEjbDescriptor basicDescriptor,
Method homeMethod, Object[] paramVals ) throws Exception{
Object returnObj = null;
EntityBeanDescriptor descriptor = (EntityBeanDescriptor)basicDescriptor;
if ( homeMethod.getName().startsWith("find") )
returnObj = invokeFinder(descriptor, homeMethod, paramVals);
else {
Object bean = createBeanInstance( descriptor, homeMethod,
getMockEjbContext( ) );
returnObj = invokeBeanMethodWithPrefix( "ejbHome", bean, homeMethod, paramVals );
}
return returnObj;
}
protected Object invokeFinder(EntityBeanDescriptor descriptor, Method finderMethod,
Object[] paramVals ) throws Exception{
Object returnObj = null;
if ( descriptor.isCMP() ) {
getMockEjbContext();
try {
returnObj = interceptorInvoker.invoke( getHomeProxy(), finderMethod,
dummyCmpBean, dummyCmpBean.getTargetMethod(), paramVals );
}
catch ( MustBeInterceptedException mbie ){
throw new MustBeInterceptedException( finderMethod );
}
}
else {
Object bean = createBeanInstance( descriptor, finderMethod,
getMockEjbContext() );
returnObj = invokeBeanMethodWithPrefix("ejb", bean, finderMethod,
paramVals );
}
return returnObj;
}
private MockEjbContext getMockEjbContext( ) {
MockEjbContext context = new MockEjbContext( getHomeProxy() );
interceptorInvoker.setContext( MockEjbContext.class.getName(), context );
return context;
}
}