1 22 package org.jboss.proxy; 23 24 import java.lang.reflect.Proxy ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.HashMap ; 28 import javax.management.ObjectName ; 29 30 import org.jboss.invocation.InvocationContext; 31 import org.jboss.invocation.InvocationKey; 32 import org.jboss.invocation.Invoker; 33 import org.jboss.system.Registry; 34 import org.jboss.util.NestedRuntimeException; 35 36 37 44 public class GenericProxyFactory 45 { 46 47 57 public Object createProxy(Object id, ObjectName targetName, 58 Invoker invoker, String jndiName, String proxyBindingName, 59 ArrayList interceptorClasses, ClassLoader loader, Class [] ifaces) 60 { 61 return createProxy(id, targetName, invoker, jndiName, proxyBindingName, interceptorClasses, loader, ifaces, null); 62 } 63 64 74 public Object createProxy(Object id, ObjectName targetName, ObjectName invokerName, 75 String jndiName, String proxyBindingName, 76 ArrayList interceptorClasses, ClassLoader loader, Class [] ifaces) 77 { 78 Invoker invoker = (Invoker) Registry.lookup(invokerName); 79 if (invoker == null) 80 throw new RuntimeException ("Failed to find invoker for name: " + invokerName); 81 return createProxy(id, targetName, invoker, jndiName, proxyBindingName, interceptorClasses, loader, ifaces, null); 82 } 83 84 95 public Object createProxy(Object id, ObjectName targetName, 96 Invoker invoker, String jndiName, String proxyBindingName, 97 ArrayList interceptorClasses, ClassLoader loader, Class [] ifaces, HashMap ctx) 98 { 99 InvocationContext context; 100 if (ctx != null) 101 context = new InvocationContext(ctx); 102 else 103 context = new InvocationContext(); 104 Integer nameHash = new Integer (targetName.hashCode()); 105 context.setObjectName(nameHash); 106 context.setCacheId(id); 107 if( jndiName != null ) 108 context.setValue(InvocationKey.JNDI_NAME, jndiName); 109 110 if( invoker == null ) 111 throw new RuntimeException ("Null invoker given for name: " + targetName); 112 context.setInvoker(invoker); 113 if( proxyBindingName != null ) 114 context.setInvokerProxyBinding(proxyBindingName); 115 116 boolean wantIClientAccess = false; 118 for(int n = 0; wantIClientAccess == false && n < interceptorClasses.size(); n ++) 119 { 120 Class type = (Class ) interceptorClasses.get(n); 121 wantIClientAccess = type.isAssignableFrom(IClientContainer.class); 122 } 123 ClientContainer client; 124 if( wantIClientAccess ) 125 { 126 client = new ClientContainerEx(context); 127 } 128 else 129 { 130 client = new ClientContainer(context); 131 } 132 133 try 134 { 135 loadInterceptorChain(interceptorClasses, client); 136 } 137 catch(Exception e) 138 { 139 throw new NestedRuntimeException("Failed to load interceptor chain", e); 140 } 141 142 ArrayList tmp = new ArrayList (Arrays.asList(ifaces)); 143 Class [] ifaces2 = new Class [tmp.size()]; 144 tmp.toArray(ifaces2); 145 return Proxy.newProxyInstance( 146 loader, 148 ifaces2, 150 client); 152 } 153 154 159 protected void loadInterceptorChain(ArrayList chain, ClientContainer client) 160 throws Exception 161 { 162 Interceptor last = null; 163 for (int i = 0; i < chain.size(); i++) 164 { 165 Class clazz = (Class )chain.get(i); 166 Interceptor interceptor = (Interceptor) clazz.newInstance(); 167 if (last == null) 168 { 169 last = interceptor; 170 client.setNext(interceptor); 171 } 172 else 173 { 174 last.setNext(interceptor); 175 last = interceptor; 176 } 177 } 178 } 179 } 180 | Popular Tags |