1 22 package org.jboss.ejb3.stateful; 23 24 import java.lang.reflect.InvocationTargetException ; 25 import javax.naming.NamingException ; 26 import javax.ejb.RemoteHome ; 27 import org.jboss.annotation.ejb.RemoteBinding; 28 import org.jboss.aop.Advisor; 29 import org.jboss.aop.AspectManager; 30 import org.jboss.aop.Dispatcher; 31 import org.jboss.aop.advice.AdviceStack; 32 import org.jboss.aspects.remoting.Remoting; 33 import org.jboss.ejb3.JBossProxy; 34 import org.jboss.ejb3.ProxyFactory; 35 import org.jboss.ejb3.ProxyFactoryHelper; 36 import org.jboss.ejb3.remoting.RemoteProxyFactory; 37 import org.jboss.logging.Logger; 38 import org.jboss.naming.Util; 39 import org.jboss.remoting.InvokerLocator; 40 41 47 public class StatefulRemoteProxyFactory extends BaseStatefulProxyFactory implements RemoteProxyFactory 48 { 49 private static final Logger log = Logger.getLogger(StatefulRemoteProxyFactory.class); 50 51 private RemoteBinding binding; 52 private InvokerLocator locator; 53 54 public void setRemoteBinding(RemoteBinding binding) 55 { 56 this.binding = binding; 57 } 58 59 protected Class [] getInterfaces() 60 { 61 Class [] interfaces; 62 63 Class [] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container); 64 interfaces = new Class [remoteInterfaces.length + 2]; 65 66 System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length); 67 interfaces[remoteInterfaces.length] = JBossProxy.class; 68 interfaces[remoteInterfaces.length + 1] = javax.ejb.EJBObject .class; 69 return interfaces; 70 } 71 72 protected void initializeJndiName() 73 { 74 jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding); 75 } 76 77 public void init() throws Exception 78 { 79 super.init(); 80 String clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding); 81 locator = new InvokerLocator(clientBindUrl); 82 } 83 84 public void start() throws Exception 85 { 86 init(); 87 88 super.start(); 89 Class [] interfaces = {ProxyFactory.class}; 90 Object factoryProxy = Remoting.createPojiProxy(jndiName + PROXY_FACTORY_NAME, interfaces, ProxyFactoryHelper.getClientBindUrl(binding)); 91 try 92 { 93 Util.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, factoryProxy); 94 } 95 catch (NamingException e) 96 { 97 NamingException namingException = new NamingException ("Could not bind stateful remote proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + PROXY_FACTORY_NAME); 98 namingException.setRootCause(e); 99 throw namingException; 100 } 101 Dispatcher.singleton.registerTarget(jndiName + PROXY_FACTORY_NAME, this); 102 103 StatefulContainer statefulContainer = (StatefulContainer) container; 104 RemoteHome remoteHome = (RemoteHome ) statefulContainer.resolveAnnotation(RemoteHome .class); 105 if (remoteHome != null) 106 { 107 Object homeProxy = createHomeProxy(remoteHome.value()); 108 Util.rebind(container.getInitialContext(), jndiName + "Home", homeProxy); 109 } 110 } 111 112 public void stop() throws Exception 113 { 114 super.stop(); 115 Util.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME); 116 Dispatcher.singleton.unregisterTarget(jndiName + PROXY_FACTORY_NAME); 117 StatefulContainer statefulContainer = (StatefulContainer) container; 118 RemoteHome remoteHome = (RemoteHome ) statefulContainer.resolveAnnotation(RemoteHome .class); 119 if (remoteHome != null) 120 { 121 Util.unbind(container.getInitialContext(), jndiName + "Home"); 122 } 123 } 124 125 126 public Object createHomeProxy(Class homeInterface) 127 { 128 try 129 { 130 Object containerId = container.getObjectName().getCanonicalName(); 131 String stackName = "StatefulSessionClientInterceptors"; 132 if (binding.interceptorStack() != null && !binding.interceptorStack().equals("")) 133 { 134 stackName = binding.interceptorStack(); 135 } 136 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 137 if (stack == null) throw new RuntimeException ("unable to find interceptor stack: " + stackName); 138 StatefulHomeRemoteProxy proxy = new StatefulHomeRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator); 139 140 141 setEjb21Objects(proxy); 142 Class [] intfs = {homeInterface}; 143 return java.lang.reflect.Proxy.newProxyInstance(container.getBeanClass().getClassLoader(), intfs, proxy); 144 } 145 catch (IllegalArgumentException e) 146 { 147 throw new RuntimeException (e); } 149 } 150 public Object createProxy() 151 { 152 try 153 { 154 Object containerId = container.getObjectName().getCanonicalName(); 155 String stackName = "StatefulSessionClientInterceptors"; 156 if (binding.interceptorStack() != null && !binding.interceptorStack().equals("")) 157 { 158 stackName = binding.interceptorStack(); 159 } 160 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 161 if (stack == null) throw new RuntimeException ("unable to find interceptor stack: " + stackName); 162 StatefulRemoteProxy proxy = new StatefulRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator); 163 164 165 setEjb21Objects(proxy); 166 Object [] args = {proxy}; 167 return proxyConstructor.newInstance(args); 168 } 169 catch (InstantiationException e) 170 { 171 throw new RuntimeException (e); } 173 catch (IllegalAccessException e) 174 { 175 throw new RuntimeException (e); } 177 catch (IllegalArgumentException e) 178 { 179 throw new RuntimeException (e); } 181 catch (InvocationTargetException e) 182 { 183 throw new RuntimeException (e.getTargetException()); } 185 } 186 187 protected StatefulHandleImpl getHandle() 188 { 189 StatefulHandleImpl handle = new StatefulHandleImpl(); 190 RemoteBinding remoteBinding = (RemoteBinding) advisor.resolveAnnotation(RemoteBinding.class); 191 if (remoteBinding != null) 192 handle.jndiName = remoteBinding.jndiBinding(); 193 194 return handle; 195 } 196 197 public Object createProxy(Object id) 198 { 199 try 200 { 201 Object containerId = container.getObjectName().getCanonicalName(); 202 String stackName = "StatefulSessionClientInterceptors"; 203 if (binding.interceptorStack() != null && !binding.interceptorStack().equals("")) 204 { 205 stackName = binding.interceptorStack(); 206 } 207 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 208 StatefulRemoteProxy proxy = new StatefulRemoteProxy(containerId, stack.createInterceptors((Advisor) container, null), locator, id); 209 setEjb21Objects(proxy); 210 Object [] args = {proxy}; 211 return proxyConstructor.newInstance(args); 212 } 213 catch (InstantiationException e) 214 { 215 throw new RuntimeException (e); } 217 catch (IllegalAccessException e) 218 { 219 throw new RuntimeException (e); } 221 catch (IllegalArgumentException e) 222 { 223 throw new RuntimeException (e); } 225 catch (InvocationTargetException e) 226 { 227 throw new RuntimeException (e.getTargetException()); } 229 } 230 231 232 } 233 | Popular Tags |