package org.jboss.proxy.ejb;
import java.io.Externalizable;
import java.lang.reflect.Method;
import javax.ejb.EJBHome;
import javax.ejb.EJBObject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.InvocationKey;
import org.jboss.invocation.InvocationContext;
import org.jboss.proxy.Interceptor;
public abstract class GenericEJBInterceptor
extends Interceptor
implements Externalizable
{
private static final long serialVersionUID = 3844706474734439975L;
protected static final Method TO_STRING;
protected static final Method HASH_CODE;
protected static final Method EQUALS;
protected static final Method GET_PRIMARY_KEY;
protected static final Method GET_HANDLE;
protected static final Method GET_EJB_HOME;
protected static final Method IS_IDENTICAL;
static
{
try
{
Class[] empty = {};
Class type = Object.class;
TO_STRING = type.getMethod("toString", empty);
HASH_CODE = type.getMethod("hashCode", empty);
EQUALS = type.getMethod("equals", new Class[] { type });
type = EJBObject.class;
GET_PRIMARY_KEY = type.getMethod("getPrimaryKey", empty);
GET_HANDLE = type.getMethod("getHandle", empty);
GET_EJB_HOME = type.getMethod("getEJBHome", empty);
IS_IDENTICAL = type.getMethod("isIdentical", new Class[] { type });
}
catch (Exception e)
{
e.printStackTrace();
throw new ExceptionInInitializerError(e);
}
}
public GenericEJBInterceptor()
{
}
protected EJBHome getEJBHome(Invocation invocation) throws NamingException
{
InvocationContext ctx = invocation.getInvocationContext();
EJBHome home = (EJBHome) ctx.getValue(InvocationKey.EJB_HOME);
if( home == null )
{
String jndiName = (String) ctx.getValue(InvocationKey.JNDI_NAME);
InitialContext iniCtx = new InitialContext();
home = (EJBHome) iniCtx.lookup(jndiName);
}
return home;
}
}