1 22 package org.jboss.ha.framework.server; 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import org.jboss.ha.framework.interfaces.DistributedReplicantManager; 27 import org.jboss.ha.framework.interfaces.DistributedReplicantManager.ReplicantListener; 28 import org.jboss.ha.framework.interfaces.HAPartition; 29 import java.io.Serializable ; 30 import EDU.oswego.cs.dl.util.concurrent.Latch; 31 32 46 public class HATarget 47 implements ReplicantListener 48 { 49 public static final int DISABLE_INVOCATIONS = 0; 50 public static final int MAKE_INVOCATIONS_WAIT = 1; 51 public static final int ENABLE_INVOCATIONS = 2; 52 53 protected String replicantName; 54 protected ArrayList replicants = new ArrayList (); 55 protected HAPartition partition = null; 56 protected org.jboss.logging.Logger log; 57 protected int clusterViewId = 0; 58 protected Serializable target; 59 protected int allowInvocationsStatus = 0; 60 protected Latch latch = null; 61 62 public HATarget(HAPartition partition, 63 String replicantName, 64 Serializable target, 65 int allowInvocations) 66 throws Exception 67 { 68 this.replicantName = replicantName; 69 this.target = target; 70 init (); 71 setInvocationsAuthorization (allowInvocations); 72 updateHAPartition(partition); 73 } 74 75 public void init() throws Exception 76 { 77 this.log = org.jboss.logging.Logger.getLogger(this.getClass()); 78 } 79 80 public String toString() 81 { 82 StringBuffer buffer = new StringBuffer (super.toString()); 83 buffer.append('{'); 84 buffer.append("replicantName="+replicantName); 85 buffer.append("partition="+partition.getPartitionName()); 86 buffer.append("clusterViewId="+clusterViewId); 87 buffer.append("allowInvocationsStatus="+allowInvocationsStatus); 88 buffer.append("replicants="+replicants); 89 buffer.append('}'); 90 return buffer.toString(); 91 } 92 93 public long getCurrentViewId() 94 { 95 return (long)clusterViewId; 96 } 97 98 public void destroy() 99 { 100 try 101 { 102 this.cleanExistenceInCurrentHAPartition(); 103 104 setInvocationsAuthorization (HATarget.DISABLE_INVOCATIONS); 107 } 108 catch (Exception e) 109 { 110 log.error("failed to destroy", e); 111 } 112 } 113 114 public void disable() 118 { 119 try 120 { 121 if (this.partition != null) 122 { 123 log.debug ("Disabled called on HATarget"); 124 this.partition.getDistributedReplicantManager().remove (this.replicantName); 125 } 126 } 127 catch (Exception e) 128 { 129 log.error("failed to disable", e); 130 } 131 } 132 133 public ArrayList getReplicants() 134 { 135 return replicants; 136 } 137 138 public void updateHAPartition(HAPartition partition) throws Exception 139 { 140 cleanExistenceInCurrentHAPartition(); 141 142 this.partition = partition; 143 DistributedReplicantManager drm = partition.getDistributedReplicantManager(); 144 drm.registerListener(this.replicantName, this); 145 drm.add(this.replicantName, this.target); 146 } 147 148 public synchronized void setInvocationsAuthorization (int status) 149 { 150 if (this.allowInvocationsStatus == status) 151 { 152 log.debug ("Invocation authorization called with no-op"); 155 } 156 else 157 { 158 if (status == MAKE_INVOCATIONS_WAIT) 161 { 162 log.debug ("Invocation authorization called: MAKE_INVOCATIONS_WAIT"); 163 latch = new Latch(); 164 this.allowInvocationsStatus = status; 165 } 166 else 167 { 168 log.debug ("Invocation authorization called: " + 169 ((status==ENABLE_INVOCATIONS)?"ENABLE_INVOCATIONS":"DISABLE_INVOCATIONS") ); 170 this.allowInvocationsStatus = status; 171 if (latch != null) 172 latch.release(); 173 } 174 } 175 } 176 177 public boolean invocationsAllowed () throws InterruptedException 178 { 179 if (this.allowInvocationsStatus == ENABLE_INVOCATIONS) 180 return true; 181 else if (this.allowInvocationsStatus == DISABLE_INVOCATIONS) 182 return false; 183 else if (this.allowInvocationsStatus == MAKE_INVOCATIONS_WAIT) 184 { 185 latch.acquire (); 186 187 if (this.allowInvocationsStatus == ENABLE_INVOCATIONS) 191 return true; 192 else 193 return false; 194 } 195 else 196 return false; 197 } 198 199 protected void releaseCurrentLatch () 200 { 201 latch.release (); 202 latch = null; 203 } 204 205 public HAPartition getAssociatedPartition () 206 { 207 return this.partition; 208 } 209 210 212 public void replicantsChanged(String key, List newReplicants, int newReplicantsViewId) 213 { 214 if (log.isDebugEnabled()) 215 log.debug("replicantsChanged '" + replicantName + 216 "' to " + (newReplicants==null? "0 (null)" : Integer.toString (newReplicants.size() ) ) + 217 " (intra-view id: " + newReplicantsViewId + ")"); 218 219 synchronized(replicants) 220 { 221 replicants.clear(); 224 if (newReplicants != null) 225 replicants.addAll(newReplicants); 226 227 this.clusterViewId = newReplicantsViewId; 228 } 229 230 } 231 232 protected void cleanExistenceInCurrentHAPartition() 233 { 234 if (this.partition != null) 235 { 236 try 237 { 238 DistributedReplicantManager drm = partition.getDistributedReplicantManager(); 239 drm.unregisterListener(this.replicantName, this); 240 drm.remove(this.replicantName); 241 } 242 catch (Exception e) 243 { 244 log.error("failed to clean existence in current ha partition", e); 245 } 246 } 247 } 248 } 249 | Popular Tags |