1 22 package org.jboss.proxy.generic; 23 24 import java.util.List ; 25 26 import javax.management.AttributeChangeNotificationFilter ; 27 import javax.management.NotificationListener ; 28 import javax.management.AttributeChangeNotification ; 29 import javax.management.Notification ; 30 import javax.management.ObjectName ; 31 32 import org.jboss.invocation.Invoker; 33 import org.jboss.invocation.InvokerHA; 34 import org.jboss.invocation.InvokerProxyHA; 35 import org.jboss.invocation.jrmp.server.JRMPProxyFactory; 36 import org.jboss.ha.framework.interfaces.LoadBalancePolicy; 37 import org.jboss.ha.framework.interfaces.DistributedReplicantManager; 38 import org.jboss.ha.framework.interfaces.HAPartition; 39 import org.jboss.ha.framework.interfaces.RoundRobin; 40 import org.jboss.ha.framework.server.HATarget; 41 import org.jboss.logging.Logger; 42 import org.jboss.proxy.GenericProxyFactory; 43 import org.jboss.system.Registry; 44 import org.jboss.system.ServiceMBean; 45 46 52 public class ProxyFactoryHA 53 extends JRMPProxyFactory 54 implements ProxyFactoryHAMBean, DistributedReplicantManager.ReplicantListener 55 { 56 protected String replicantName = null; 57 protected InvokerHA invokerHA; 58 protected HATarget target; 59 protected Invoker invoker; 60 protected DistributedReplicantManager drm = null; 61 protected ObjectName partitionObjectName; 62 protected String loadBalancePolicy = RoundRobin.class.getName(); 63 protected NotificationListener listener; 64 protected int state = 0; 65 66 public ObjectName getPartitionObjectName() 67 { 68 return partitionObjectName; 69 } 70 71 public void setPartitionObjectName(ObjectName partitionObjectName) 72 { 73 this.partitionObjectName = partitionObjectName; 74 } 75 76 public String getLoadBalancePolicy() 77 { 78 return loadBalancePolicy; 79 } 80 81 public void setLoadBalancePolicy(String loadBalancePolicy) 82 { 83 this.loadBalancePolicy = loadBalancePolicy; 84 } 85 86 public void createService() throws Exception 87 { 88 super.createService(); 89 90 AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter (); 92 filter.enableAttribute("State"); 93 listener = new StateChangeListener(); 94 getServer().addNotificationListener(getTargetName(), listener, filter, null); 95 } 96 97 protected void startService() throws Exception 98 { 99 String partitionName = (String ) getServer().getAttribute(partitionObjectName, "PartitionName"); 100 HAPartition partition = (HAPartition) getServer().getAttribute(partitionObjectName, "HAPartition"); 101 if (partition == null) 102 throw new RuntimeException ("Partition is not registered: " + partitionObjectName); 103 this.drm = partition.getDistributedReplicantManager (); 104 105 replicantName = getTargetName().toString(); 106 107 invokerHA = (InvokerHA) Registry.lookup(getInvokerName()); 108 if (invokerHA == null) 109 throw new RuntimeException ("Invoker is not registered: " + getInvokerName()); 110 111 int mode = HATarget.MAKE_INVOCATIONS_WAIT; 112 if (state == ServiceMBean.STARTED) 113 mode = HATarget.ENABLE_INVOCATIONS; 114 target = new HATarget(partition, replicantName, invokerHA.getStub(), mode); 115 invokerHA.registerBean(getTargetName(), target); 116 117 String clusterFamilyName = partitionName + "/" + getTargetName() + "/"; 118 119 drm.registerListener (replicantName, this); 123 124 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 125 Class clazz; 126 LoadBalancePolicy policy; 127 128 clazz = cl.loadClass(loadBalancePolicy); 129 policy = (LoadBalancePolicy)clazz.newInstance(); 130 invoker = invokerHA.createProxy(getTargetName(), policy, clusterFamilyName + "H"); 131 132 134 super.startService(); 135 } 136 137 public void stopService() throws Exception 138 { 139 super.stopService(); 140 141 try 142 { 143 invokerHA.unregisterBean(getTargetName()); 145 target.destroy(); 146 } 147 catch (Exception ignored) 148 { 149 } 151 if (drm != null) 152 drm.unregisterListener(replicantName, this); 153 } 154 155 protected void destroyService() throws Exception 156 { 157 super.destroyService(); 158 getServer().removeNotificationListener(getTargetName(), listener); 159 } 160 161 protected void containerIsFullyStarted () 162 { 163 if (target != null) 164 target.setInvocationsAuthorization(HATarget.ENABLE_INVOCATIONS); 165 } 166 167 protected void containerIsAboutToStop() 168 { 169 if (target != null) 170 { 171 target.setInvocationsAuthorization(HATarget.DISABLE_INVOCATIONS); 172 target.disable(); 173 } 174 } 175 176 public synchronized void replicantsChanged(String key, 179 List newReplicants, 180 int newReplicantsViewId) 181 { 182 try 183 { 184 if (invoker instanceof InvokerProxyHA) 185 ((InvokerProxyHA) invoker).updateClusterInfo(target.getReplicants(), target.getCurrentViewId()); 186 187 log.debug ("Rebinding in JNDI... " + key); 188 rebind(); 189 } 190 catch (Exception none) 191 { 192 log.debug(none); 193 } 194 } 195 196 protected void createProxy 197 ( 198 Object cacheID, 199 String proxyBindingName, 200 ClassLoader loader, 201 Class [] ifaces 202 ) 203 { 204 GenericProxyFactory proxyFactory = new GenericProxyFactory(); 205 theProxy = proxyFactory.createProxy(cacheID, getTargetName(), invoker, 206 getJndiName(), proxyBindingName, getInterceptorClasses(), loader, ifaces); 207 } 208 209 211 class StateChangeListener implements NotificationListener 212 { 213 public void handleNotification (Notification notification, Object handback) 214 { 215 if (notification instanceof AttributeChangeNotification ) 216 { 217 AttributeChangeNotification notif = (AttributeChangeNotification ) notification; 218 state = ((Integer )notif.getNewValue()).intValue(); 219 220 if (state == ServiceMBean.STARTED) 221 { 222 log.debug ("Started: enabling remote access to mbean " + getTargetName()); 223 containerIsFullyStarted (); 224 } 225 else if (state == ServiceMBean.STOPPING) 226 { 227 log.debug ("About to stop: disabling remote access to mbean " + getTargetName()); 228 containerIsAboutToStop (); 229 } 230 } 231 } 232 233 } 234 } 235 | Popular Tags |