1 22 package org.jboss.proxy.ejb; 23 24 import java.util.List ; 25 import javax.management.ObjectName ; 26 import javax.naming.InitialContext ; 27 import org.jboss.system.Registry; 28 import org.jboss.logging.Logger; 29 import org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxyHA; 30 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 31 import org.jboss.ha.framework.interfaces.DistributedReplicantManager; 32 import org.jboss.ha.framework.interfaces.HAPartition; 33 import org.jboss.ha.framework.server.HATarget; 34 35 import javax.management.AttributeChangeNotificationFilter ; 36 import javax.management.NotificationListener ; 37 import javax.management.AttributeChangeNotification ; 38 import javax.management.Notification ; 39 import org.jboss.invocation.InvokerProxyHA; 40 import org.jboss.invocation.InvokerHA; 41 import org.jboss.system.ServiceMBean; 42 import org.jboss.naming.Util; 43 44 63 public class ProxyFactoryHA 64 extends ProxyFactory 65 implements DistributedReplicantManager.ReplicantListener, ClusterProxyFactory 66 { 67 68 protected static Logger log = Logger.getLogger(ProxyFactory.class); 69 protected String replicantName = null; 70 protected InvokerHA jrmp; 71 protected HATarget target; 72 73 protected DistributedReplicantManager drm = null; 74 75 public void create () throws Exception 76 { 77 super.create (); 78 79 AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter (); 82 filter.enableAttribute ("State"); 83 84 this.container.getServer (). 92 addNotificationListener (this.container.getEjbModule ().getServiceName (), 93 new ProxyFactoryHA.StateChangeListener (), 94 filter, 95 null); 96 } 97 98 public void start () throws Exception 99 { 100 super.start (); 101 } 102 103 protected void setupInvokers() throws Exception 104 { 105 String partitionName = container.getBeanMetaData().getClusterConfigMetaData().getPartitionName(); 106 InitialContext ctx = new InitialContext (); 107 HAPartition partition = (HAPartition) ctx.lookup("/HAPartition/" + partitionName); 108 this.drm = partition.getDistributedReplicantManager (); 109 110 replicantName = jmxName.toString (); 111 112 ObjectName oname = new ObjectName (invokerMetaData.getInvokerMBean()); 113 jrmp = (InvokerHA)Registry.lookup(oname); 114 if (jrmp == null) 115 throw new RuntimeException ("home JRMPInvokerHA is null: " + oname); 116 117 118 target = new HATarget(partition, replicantName, jrmp.getStub (), HATarget.MAKE_INVOCATIONS_WAIT); 119 jrmp.registerBean(jmxName, target); 120 121 String clusterFamilyName = partitionName + "/" + jmxName + "/"; 122 123 drm.registerListener (replicantName, this); 128 129 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 130 Class clazz; 131 LoadBalancePolicy policy; 132 133 clazz = cl.loadClass(container.getBeanMetaData().getClusterConfigMetaData().getHomeLoadBalancePolicy()); 134 policy = (LoadBalancePolicy)clazz.newInstance(); 135 homeInvoker = jrmp.createProxy(jmxName, policy, clusterFamilyName + "H"); 136 String homeName = jndiBinding + "-HomeInvoker"; 138 log.debug("(re-)Binding Home invoker under: " + homeName); 139 Util.rebind(ctx, 140 homeName, 142 homeInvoker 144 ); 145 146 clazz = cl.loadClass(container.getBeanMetaData().getClusterConfigMetaData().getBeanLoadBalancePolicy()); 147 policy = (LoadBalancePolicy)clazz.newInstance(); 148 beanInvoker = jrmp.createProxy(jmxName, policy, clusterFamilyName + "R"); 149 String beanName = jndiBinding + "-RemoteInvoker"; 151 log.debug("(re-)Binding Remote invoker under: " + beanName); 152 Util.rebind(ctx, 153 beanName, 155 beanInvoker 157 ); 158 159 JRMPInvokerProxyHA.colocation.add(new Integer (jmxNameHash)); 160 } 161 162 public void destroy () 163 { 164 super.destroy (); 165 166 try 167 { 168 JRMPInvokerProxyHA.colocation.remove(new Integer (jmxNameHash)); 169 jrmp.unregisterBean(jmxName); 170 target.destroy(); 171 } 172 catch (Exception ignored) 173 { 174 } 176 try 177 { 178 InitialContext ctx = new InitialContext (); 179 String homeInvokerName = jndiBinding + "-HomeInvoker"; 180 ctx.unbind(homeInvokerName); 181 } 182 catch(Exception ignored) 183 { 184 } 185 try 186 { 187 InitialContext ctx = new InitialContext (); 188 String beanInvokerName = jndiBinding + "-RemoteInvoker"; 189 ctx.unbind(beanInvokerName); 190 } 191 catch(Exception ignored) 192 { 193 } 194 195 if( drm != null ) 196 drm.unregisterListener (replicantName, this); 197 } 198 199 protected void containerIsFullyStarted () 200 { 201 if( target != null ) 202 target.setInvocationsAuthorization (HATarget.ENABLE_INVOCATIONS); 203 } 204 205 protected void containerIsAboutToStop () 206 { 207 if( target != null ) 208 { 209 target.setInvocationsAuthorization (HATarget.DISABLE_INVOCATIONS); 210 target.disable (); 211 } 212 } 213 214 public synchronized void replicantsChanged (String key, 217 List newReplicants, 218 int newReplicantsViewId) 219 { 220 try 221 { 222 if (homeInvoker instanceof InvokerProxyHA) 223 { 224 ((InvokerProxyHA)homeInvoker).updateClusterInfo (target.getReplicants(), target.getCurrentViewId ()); 225 } 226 if (beanInvoker instanceof InvokerProxyHA) 227 { 228 ((InvokerProxyHA)beanInvoker).updateClusterInfo (target.getReplicants(), target.getCurrentViewId ()); 229 } 230 231 log.debug ("Rebinding in JNDI... " + key); 232 rebindHomeProxy (); 233 } 234 catch (Exception none) 235 { 236 log.debug (none); 237 } 238 } 239 240 242 class StateChangeListener implements NotificationListener 243 { 244 245 public void handleNotification (Notification notification, java.lang.Object handback) 246 { 247 if (notification instanceof AttributeChangeNotification ) 248 { 249 AttributeChangeNotification notif = (AttributeChangeNotification ) notification; 250 int value = ((Integer )notif.getNewValue()).intValue (); 251 252 if (value == ServiceMBean.STARTED) 253 { 254 log.debug ("Container fully started: enabling HA-RMI access to bean"); 255 containerIsFullyStarted (); 256 } 257 else if (value == ServiceMBean.STOPPING) 258 { 259 log.debug ("Container about to stop: disabling HA-RMI access to bean"); 260 containerIsAboutToStop (); 261 } 262 } 263 } 264 265 } 266 } 267 | Popular Tags |