1 22 package org.jboss.ejb3.stateless; 23 24 import java.io.Serializable ; 25 import java.lang.reflect.Constructor ; 26 import java.lang.reflect.InvocationHandler ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.lang.reflect.Method ; 29 import java.lang.reflect.Proxy ; 30 31 import javassist.util.proxy.MethodHandler; 32 import javassist.util.proxy.ProxyObject; 33 34 import javax.naming.Context ; 35 import javax.naming.NamingException ; 36 37 import org.jboss.aop.Advisor; 38 import org.jboss.ejb3.Container; 39 import org.jboss.ejb3.ProxyFactory; 40 import org.jboss.logging.Logger; 41 import org.jboss.naming.Util; 42 43 49 public abstract class BaseStatelessProxyFactory extends org.jboss.ejb3.session.BaseSessionProxyFactory implements ProxyFactory 50 { 51 private static final Logger log = Logger.getLogger(BaseStatelessProxyFactory.class); 52 53 protected Context proxyFactoryContext; 56 protected String jndiName; 57 58 private javassist.util.proxy.ProxyFactory proxyFactory; 59 private Class proxyClass; 60 private Constructor proxyConstructor; 61 62 67 88 89 94 private static class MethodHandlerAdapter implements MethodHandler, Serializable 95 { 96 private static final long serialVersionUID = 1L; 97 98 private InvocationHandler delegate; 99 100 private MethodHandlerAdapter(InvocationHandler delegate) 101 { 102 if(delegate == null) 103 throw new IllegalArgumentException ("delegate must not be null"); 104 this.delegate = delegate; 105 } 106 107 public Object invoke(Object self, Method thisMethod, Method process, Object [] args) throws Throwable 108 { 109 return delegate.invoke(self, thisMethod, args); 110 } 111 } 112 113 119 protected Object constructProxy(final InvocationHandler handler) 120 { 121 try 122 { 123 124 Object args[] = { handler }; 125 Object proxy = proxyConstructor.newInstance(args); 126 127 128 136 137 138 142 143 return proxy; 144 } 145 catch (IllegalArgumentException e) 146 { 147 throw new RuntimeException (e); 148 } 149 catch (InstantiationException e) 150 { 151 throw new RuntimeException (e); 152 } 153 catch (IllegalAccessException e) 154 { 155 throw new RuntimeException (e); 156 } 157 catch (InvocationTargetException e) 158 { 159 throw new RuntimeException (e.getTargetException()); 160 } 161 } 162 163 public final Object createProxy(Object id) 164 { 165 assert id == null : "stateless bean must not have an id"; 166 return createProxy(); 167 } 168 169 public void init() throws Exception 170 { 171 initializeJndiName(); 172 Class [] interfaces = getInterfaces(); 173 174 175 Class proxyClass = java.lang.reflect.Proxy.getProxyClass(container.getBeanClass().getClassLoader(), interfaces); 176 final Class [] constructorParams = 177 {InvocationHandler .class}; 178 proxyConstructor = proxyClass.getConstructor(constructorParams); 179 180 181 195 196 197 202 } 203 204 218 219 public void start() throws Exception 220 { 221 init(); 222 223 Object proxy = createProxy(); 224 try 226 { 227 Util.rebind(container.getInitialContext(), jndiName, proxy); 228 } catch (NamingException e) 229 { 230 NamingException namingException = new NamingException ("Could not bind stateless proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName); 231 namingException.setRootCause(e); 232 throw namingException; 233 } 234 } 235 236 public void stop() throws Exception 237 { 238 Util.unbind(container.getInitialContext(), jndiName); 239 } 240 241 protected abstract Class [] getInterfaces(); 242 243 protected abstract void initializeJndiName(); 244 245 public void setContainer(Container container) 246 { 247 this.container = container; 248 this.advisor = (Advisor) container; 249 } 250 251 } 252 | Popular Tags |