package org.jboss.proxy.ejb;
import java.lang.reflect.Method;
import javax.ejb.EJBHome;
import javax.ejb.EJBMetaData;
import javax.ejb.RemoveException;
import javax.ejb.Handle;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.ejb.HomeHandle;
import org.jboss.proxy.ejb.handle.HomeHandleImpl;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.InvocationContext;
import org.jboss.invocation.InvocationKey;
import org.jboss.invocation.InvocationType;
public class HomeInterceptor
extends GenericEJBInterceptor
{
private static final long serialVersionUID = 1333656107035759718L;
protected static final Object[] EMPTY_ARGS = {};
protected static final Method GET_EJB_META_DATA;
protected static final Method GET_HOME_HANDLE;
protected static final Method REMOVE_BY_HANDLE;
protected static final Method REMOVE_BY_PRIMARY_KEY;
protected static final Method REMOVE_OBJECT;
static
{
try
{
final Class empty[] = {};
final Class type = EJBHome.class;
GET_EJB_META_DATA = type.getMethod("getEJBMetaData", empty);
GET_HOME_HANDLE = type.getMethod("getHomeHandle", empty);
REMOVE_BY_HANDLE = type.getMethod("remove", new Class[] {
Handle.class
});
REMOVE_BY_PRIMARY_KEY = type.getMethod("remove", new Class[] {
Object.class
});
REMOVE_OBJECT = EJBObject.class.getMethod("remove", empty);
}
catch (Exception e) {
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public HomeInterceptor() {}
public Object invoke(Invocation invocation)
throws Throwable
{
InvocationContext ctx = invocation.getInvocationContext();
Method m = invocation.getMethod();
if (m.equals(TO_STRING))
{
return ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home";
}
else if (m.equals(EQUALS))
{
Object[] args = invocation.getArguments();
String argsString = args[0] != null ? args[0].toString() : "";
String thisString = ctx.getValue(InvocationKey.JNDI_NAME).toString() + "Home";
return new Boolean(thisString.equals(argsString));
}
else if (m.equals(HASH_CODE))
{
return new Integer(this.hashCode());
}
else if (m.equals(GET_HOME_HANDLE))
{
return new HomeHandleImpl(
(String)ctx.getValue(InvocationKey.JNDI_NAME));
}
else if (m.equals(GET_EJB_META_DATA))
{
return ctx.getValue(InvocationKey.EJB_METADATA);
}
else if (m.equals(REMOVE_BY_HANDLE))
{
EJBObject object =
((Handle) invocation.getArguments()[0]).getEJBObject();
object.remove();
return Void.TYPE;
}
else if (m.equals(REMOVE_BY_PRIMARY_KEY))
{
if(((EJBMetaData)ctx.getValue(InvocationKey.EJB_METADATA)).isSession())
throw new RemoveException("Session beans cannot be removed " +
"by primary key.");
Object id = invocation.getArguments()[0];
invocation.setId(id);
invocation.setType(InvocationType.REMOTE);
invocation.setMethod(REMOVE_OBJECT);
invocation.setArguments(EMPTY_ARGS);
return getNext().invoke(invocation);
}
else
{
invocation.setType(InvocationType.HOME);
return getNext().invoke(invocation);
}
}
}