package org.mockejb;
import java.io.Serializable;
import java.lang.reflect.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import org.mockejb.interceptor.*;
public class MockEjbObject implements InvocationHandler, EnterpriseBean, Serializable {
private InterceptorInvoker interceptorInvoker= new InterceptorInvoker();
private Object homeImpl;
private Object homeProxy;
private Class ifaceClass;
private Object bean;
private Object proxy;
private static MethodContainer standardMethods;
static {
standardMethods = new MethodContainer( MockEjbObject.class );
standardMethods.add("getEJBHome");
standardMethods.add("getEJBLocalHome");
standardMethods.add("getHandle");
standardMethods.add("getPrimaryKey");
standardMethods.add("remove");
Class objectArg[] = {Object.class};
standardMethods.add("isIdentical", objectArg );
standardMethods.add("equals", objectArg );
standardMethods.add("hashCode" );
standardMethods.add("toString" );
standardMethods.add("getBean");
standardMethods.add("getEjbContext");
}
MockEjbObject( Class ifaceClass ) {
this.ifaceClass = ifaceClass;
}
void setHomeImpl( final Object homeImpl ){
this.homeImpl = homeImpl;
}
void setHomeProxy ( final Object homeProxy ){
this.homeProxy = homeProxy;
}
public void addInterceptor( Interceptor interceptor ){
AspectSystemFactory.getAspectSystem().add(
new ClassPointcut( ifaceClass, false), interceptor);
}
public void setTransactionPolicy( TransactionPolicy policy ){
interceptorInvoker.setContext( TransactionManager.POLICY_CONTEXT_KEY, policy );
}
Object createProxy( Object bean, MockEjbContext ejbContext ) {
this.bean = bean;
proxy = Proxy.newProxyInstance( ifaceClass.getClassLoader(),
new Class[] { ifaceClass, EjbBeanAccess.class }, this );
ejbContext.setEjbObjectProxy( proxy );
interceptorInvoker.setContext( MockEjbContext.class.getName(), ejbContext );
return proxy;
}
public Object invoke( Object proxy, Method ifaceMethod, Object[] paramVals )
throws Throwable {
Method beanMethod = null;
Object returnValue = null;
Object beanToInvoke = null;
beanMethod = standardMethods.find( ifaceMethod );
if ( beanMethod != null ) {
beanToInvoke= this;
}
else {
beanToInvoke= this.bean;
beanMethod = bean.getClass().getMethod( ifaceMethod.getName(),
ifaceMethod.getParameterTypes() );
}
returnValue = interceptorInvoker.invoke( proxy, ifaceMethod, beanToInvoke, beanMethod, paramVals );
return returnValue;
}
Object getHomeImpl() {
return this.homeImpl;
}
public MockEjbContext getEjbContext(){
if ( interceptorInvoker.getContext( MockEjbContext.class.getName())== null )
throw new IllegalStateException( "Context does not exist. Most likely this EJB has not been created yet");
return (MockEjbContext) interceptorInvoker.getContext( MockEjbContext.class.getName());
}
public EJBHome getEJBHome() {
if ( homeProxy == null )
throw new IllegalStateException(
"Attempt to request a home for the Bean that does not have one, such as MDB");
return (EJBHome) homeProxy;
}
public EJBLocalHome getEJBLocalHome() {
if ( homeProxy == null )
throw new IllegalStateException(
"Attempt to request a home for the Bean that does not have one, such as MDB");
return (EJBLocalHome) homeProxy;
}
public Handle getHandle() throws RemoteException {
throwMethodNotImplemented( "getHandle");
return null;
}
public Object getPrimaryKey() {
return getEjbContext().getPrimaryKey();
}
public void remove() throws RemoveException {
}
public boolean isIdentical(Object object) {
return equals(object);
}
public boolean equals( Object obj ){
if (ifaceClass.isInstance( obj ) )
return (proxy==obj);
else
return super.equals( obj );
}
public int hashCode() {
return ifaceClass.hashCode();
}
public Object getBean(){
return bean;
}
public String toString(){
return "EJB object: " +
" BusinessIface: "+ifaceClass.getName() +"\nBean: "+bean.getClass().getName();
}
private void throwMethodNotImplemented( String methodName ){
throw new MethodNotImplementedException( methodName,
this.getClass().getName() );
}
}