1 22 package org.jboss.ejb3.stateful; 23 24 import java.lang.reflect.InvocationTargetException ; 25 26 import javax.naming.NamingException ; 27 28 import org.jboss.annotation.ejb.Clustered; 29 import org.jboss.annotation.ejb.RemoteBinding; 30 import org.jboss.aop.Advisor; 31 import org.jboss.aop.AspectManager; 32 import org.jboss.aop.Dispatcher; 33 import org.jboss.aop.advice.AdviceStack; 34 import org.jboss.aspects.remoting.FamilyWrapper; 35 import org.jboss.aspects.remoting.Remoting; 36 import org.jboss.ejb3.JBossProxy; 37 import org.jboss.ejb3.ProxyFactory; 38 import org.jboss.ejb3.ProxyFactoryHelper; 39 import org.jboss.ejb3.remoting.RemoteProxyFactory; 40 import org.jboss.ha.framework.interfaces.ClusteringTargetsRepository; 41 import org.jboss.ha.framework.interfaces.FirstAvailable; 42 import org.jboss.ha.framework.interfaces.HAPartition; 43 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 44 import org.jboss.ha.framework.server.HATarget; 45 import org.jboss.naming.Util; 46 import org.jboss.remoting.InvokerLocator; 47 48 49 55 public class StatefulClusterProxyFactory extends BaseStatefulProxyFactory implements RemoteProxyFactory 56 { 57 private RemoteBinding binding; 58 private InvokerLocator locator; 59 private HATarget hatarget; 60 private String proxyFamilyName; 61 private LoadBalancePolicy lbPolicy; 62 private FamilyWrapper wrapper; 63 64 public void setRemoteBinding(RemoteBinding binding) 65 { 66 this.binding = binding; 67 } 68 69 protected Class [] getInterfaces() 70 { 71 Class [] remoteInterfaces = ProxyFactoryHelper.getRemoteInterfaces(container); 72 Class [] interfaces = new Class [remoteInterfaces.length + 1]; 73 System.arraycopy(remoteInterfaces, 0, interfaces, 0, remoteInterfaces.length); 74 interfaces[remoteInterfaces.length] = JBossProxy.class; 75 return interfaces; 76 } 77 78 protected void initializeJndiName() 79 { 80 jndiName = ProxyFactoryHelper.getRemoteJndiName(container, binding); 81 } 82 83 public void start() throws Exception 84 { 85 String clientBindUrl = ProxyFactoryHelper.getClientBindUrl(binding); 86 locator = new InvokerLocator(clientBindUrl); 87 Clustered clustered = (Clustered) advisor.resolveAnnotation(Clustered.class); 88 if (clustered == null) throw new RuntimeException ("Could not find @Clustered annotation. Cannot deploy."); 89 String partitionName = clustered.partition(); 90 proxyFamilyName = container.getEjbName() + locator.getProtocol() + partitionName; 91 HAPartition partition = (HAPartition) container.getInitialContext().lookup("/HAPartition/" + partitionName); 92 hatarget = new HATarget(partition, proxyFamilyName, locator, HATarget.ENABLE_INVOCATIONS); 93 ClusteringTargetsRepository.initTarget(proxyFamilyName, hatarget.getReplicants()); 94 ((StatefulContainer) container).getClusterFamilies().put(proxyFamilyName, hatarget); 95 if (clustered.loadBalancePolicy() == null || clustered.loadBalancePolicy().equals(LoadBalancePolicy.class)) 96 { 97 lbPolicy = new FirstAvailable(); 98 } 99 else 100 { 101 lbPolicy = (LoadBalancePolicy) clustered.loadBalancePolicy().newInstance(); 102 } 103 wrapper = new FamilyWrapper(proxyFamilyName, hatarget.getReplicants()); 104 super.start(); 105 Class [] interfaces = {ProxyFactory.class}; 106 Object factoryProxy = Remoting.createPojiProxy(jndiName + PROXY_FACTORY_NAME, interfaces, ProxyFactoryHelper.getClientBindUrl(binding)); 107 try 108 { 109 Util.rebind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME, factoryProxy); 110 } catch (NamingException e) 111 { 112 NamingException namingException = new NamingException ("Could not bind stateful cluster proxy with ejb name " + container.getEjbName() + " into JNDI under jndiName: " + container.getInitialContext().getNameInNamespace() + "/" + jndiName + PROXY_FACTORY_NAME); 113 namingException.setRootCause(e); 114 throw namingException; 115 } 116 Dispatcher.singleton.registerTarget(jndiName + PROXY_FACTORY_NAME, this); 117 118 } 119 120 public Object createProxy() 121 { 122 try 123 { 124 Object containerId = container.getObjectName().getCanonicalName(); 125 String stackName = "ClusteredStatefulSessionClientInterceptors"; 126 if (binding.interceptorStack() != null && !binding.interceptorStack().equals("")) 127 { 128 stackName = binding.interceptorStack(); 129 } 130 AdviceStack stack = AspectManager.instance().getAdviceStack(stackName); 131 Object [] args = {new StatefulClusteredProxy(containerId, stack.createInterceptors((Advisor) container, null), wrapper, lbPolicy)}; 132 return proxyConstructor.newInstance(args); 133 } 134 catch (InstantiationException e) 135 { 136 throw new RuntimeException (e); } 138 catch (IllegalAccessException e) 139 { 140 throw new RuntimeException (e); } 142 catch (IllegalArgumentException e) 143 { 144 throw new RuntimeException (e); } 146 catch (InvocationTargetException e) 147 { 148 throw new RuntimeException (e.getTargetException()); } 150 } 151 152 public Object createProxy(Object id) 153 { 154 throw new RuntimeException ("NYI"); 155 } 156 157 public void stop() throws Exception 158 { 159 super.stop(); 160 hatarget.destroy(); 161 ((StatefulContainer) container).getClusterFamilies().remove(proxyFamilyName); 162 Util.unbind(container.getInitialContext(), jndiName + PROXY_FACTORY_NAME); 163 164 } 165 166 protected StatefulHandleImpl getHandle() 167 { 168 StatefulHandleImpl handle = new StatefulHandleImpl(); 169 RemoteBinding remoteBinding = (RemoteBinding)advisor.resolveAnnotation(RemoteBinding.class); 170 if (remoteBinding != null) 171 handle.jndiName = remoteBinding.jndiBinding(); 172 173 return handle; 174 } 175 176 } 177 | Popular Tags |