package org.jboss.proxy;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import javax.management.ObjectName;
import org.jboss.invocation.InvocationContext;
import org.jboss.invocation.InvocationKey;
import org.jboss.invocation.Invoker;
import org.jboss.system.Registry;
import org.jboss.util.NestedRuntimeException;
public class GenericProxyFactory
{
public Object createProxy(Object id, ObjectName targetName,
Invoker invoker, String jndiName, String proxyBindingName,
ArrayList interceptorClasses, ClassLoader loader, Class[] ifaces)
{
return createProxy(id, targetName, invoker, jndiName, proxyBindingName, interceptorClasses, loader, ifaces, null);
}
public Object createProxy(Object id, ObjectName targetName, ObjectName invokerName,
String jndiName, String proxyBindingName,
ArrayList interceptorClasses, ClassLoader loader, Class[] ifaces)
{
Invoker invoker = (Invoker) Registry.lookup(invokerName);
if (invoker == null)
throw new RuntimeException("Failed to find invoker for name: " + invokerName);
return createProxy(id, targetName, invoker, jndiName, proxyBindingName, interceptorClasses, loader, ifaces, null);
}
public Object createProxy(Object id, ObjectName targetName,
Invoker invoker, String jndiName, String proxyBindingName,
ArrayList interceptorClasses, ClassLoader loader, Class[] ifaces, HashMap ctx)
{
InvocationContext context;
if (ctx != null)
context = new InvocationContext(ctx);
else
context = new InvocationContext();
Integer nameHash = new Integer(targetName.hashCode());
context.setObjectName(nameHash);
context.setCacheId(id);
if( jndiName != null )
context.setValue(InvocationKey.JNDI_NAME, jndiName);
if( invoker == null )
throw new RuntimeException("Null invoker given for name: " + targetName);
context.setInvoker(invoker);
if( proxyBindingName != null )
context.setInvokerProxyBinding(proxyBindingName);
boolean wantIClientAccess = false;
for(int n = 0; wantIClientAccess == false && n < interceptorClasses.size(); n ++)
{
Class type = (Class) interceptorClasses.get(n);
wantIClientAccess = type.isAssignableFrom(IClientContainer.class);
}
ClientContainer client;
if( wantIClientAccess )
{
client = new ClientContainerEx(context);
}
else
{
client = new ClientContainer(context);
}
try
{
loadInterceptorChain(interceptorClasses, client);
}
catch(Exception e)
{
throw new NestedRuntimeException("Failed to load interceptor chain", e);
}
ArrayList tmp = new ArrayList(Arrays.asList(ifaces));
Class[] ifaces2 = new Class[tmp.size()];
tmp.toArray(ifaces2);
return Proxy.newProxyInstance(
loader,
ifaces2,
client);
}
protected void loadInterceptorChain(ArrayList chain, ClientContainer client)
throws Exception
{
Interceptor last = null;
for (int i = 0; i < chain.size(); i++)
{
Class clazz = (Class)chain.get(i);
Interceptor interceptor = (Interceptor) clazz.newInstance();
if (last == null)
{
last = interceptor;
client.setNext(interceptor);
}
else
{
last.setNext(interceptor);
last = interceptor;
}
}
}
}