package org.jboss.proxy;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationHandler;
import java.util.ArrayList;
import org.jboss.invocation.Invocation;
import org.jboss.invocation.InvocationContext;
import org.jboss.invocation.InvocationKey;
import org.jboss.invocation.PayloadKey;
public class ClientContainer
implements Externalizable, InvocationHandler
{
private static final long serialVersionUID = -4061374432170701306L;
protected static final Object[] EMPTY_ARGS = {};
public InvocationContext context;
public Interceptor next;
public ClientContainer()
{
super();
}
public ClientContainer(final InvocationContext context)
{
this.context = context;
}
public Object invoke(final Object proxy,
final Method m,
Object[] args)
throws Throwable
{
if (args == null)
args = EMPTY_ARGS;
Invocation invocation = new Invocation();
invocation.setInvocationContext(context);
invocation.setId(context.getCacheId());
invocation.setObjectName(context.getObjectName());
invocation.setMethod(m);
invocation.setArguments(args);
invocation.setValue(InvocationKey.INVOKER_PROXY_BINDING,
context.getInvokerProxyBinding(),
PayloadKey.AS_IS);
Object obj = next.invoke(invocation);
return obj;
}
public InvocationContext getInvocationContext()
{
return this.context;
}
public ArrayList getInterceptors()
{
ArrayList tmp = new ArrayList();
Interceptor inext = next;
while( inext != null )
{
tmp.add(inext);
inext = inext.nextInterceptor;
}
return tmp;
}
public void setInterceptors(ArrayList interceptors)
{
if( interceptors.size() == 0 )
return;
next = (Interceptor) interceptors.get(0);
Interceptor i = next;
for(int n = 1; n < interceptors.size(); n ++)
{
Interceptor inext = (Interceptor) interceptors.get(n);
i.setNext(inext);
i = inext;
}
}
public Interceptor setNext(Interceptor interceptor)
{
next = interceptor;
return interceptor;
}
public void writeExternal(final ObjectOutput out)
throws IOException
{
out.writeObject(next);
out.writeObject(context);
}
public void readExternal(final ObjectInput in)
throws IOException, ClassNotFoundException
{
next = (Interceptor) in.readObject();
context = (InvocationContext) in.readObject();
}
}