1 22 package org.jboss.test.cluster.ds; 23 24 import java.io.Serializable ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import javax.management.Notification ; 28 import javax.naming.InitialContext ; 29 30 import org.jboss.ha.framework.interfaces.DistributedState; 31 import org.jboss.ha.framework.interfaces.DistributedState.DSListenerEx; 32 import org.jboss.ha.framework.interfaces.HAPartition; 33 import org.jboss.logging.Logger; 34 import org.jboss.mx.util.JBossNotificationBroadcasterSupport; 35 36 41 public class DistributedStateUser extends JBossNotificationBroadcasterSupport 42 implements IDistributedState, DSListenerEx 43 { 44 protected static Logger log = Logger.getLogger(DistributedStateUser.class); 45 46 protected DistributedState entryMap; 47 protected String category; 48 protected String partitionName; 49 protected long sequence; 50 51 public String getPartitionName() 52 { 53 return partitionName; 54 } 55 public void setPartitionName(String partitionName) 56 { 57 this.partitionName = partitionName; 58 } 59 60 public String getLogger() 61 { 62 return category; 63 } 64 public void setLogger(String category) 65 { 66 this.category = category; 67 } 68 69 public void start() throws Exception 70 { 71 InitialContext ctx = new InitialContext (); 73 String jndiName = "/HAPartition/" + partitionName; 74 HAPartition partition = (HAPartition) ctx.lookup(jndiName); 75 this.entryMap = partition.getDistributedStateService(); 76 log.debug("Obtained DistributedState from partition="+partitionName); 77 entryMap.registerDSListenerEx(category, this); 78 } 79 public void stop() 80 { 81 entryMap.unregisterDSListenerEx(category, this); 82 flush(); 83 } 84 85 public Serializable get(Serializable key) 86 { 87 Serializable value = entryMap.get(category, key); 88 log.debug("Get: "+key+", value: "+value); 89 return value; 90 } 91 92 public void put(Serializable key, Serializable value) 93 throws Exception 94 { 95 entryMap.set(category, key, value, false); 96 log.debug("Put: "+key+", value: "+value); 97 } 98 99 public void remove(Serializable key) 100 throws Exception 101 { 102 Object value = entryMap.remove(category, key, false); 103 log.debug("Removed: "+key+", value: "+value); 104 } 105 106 public Collection listAllCategories() 107 { 108 Collection cats = entryMap.getAllCategories(); 110 if (cats == null || cats.isEmpty()) { 111 return new java.util.Vector (); 112 } 113 java.util.Vector vcats = new java.util.Vector (); 114 java.util.Iterator iter = cats.iterator(); 115 while (iter.hasNext()) 116 vcats.add(iter.next()); 117 return vcats; 118 } 119 120 public Collection listAllKeys(String category) 121 { 122 Collection keys = entryMap.getAllKeys(category); 124 if (keys == null || keys.isEmpty()) { 125 return new java.util.Vector (); 126 } 127 java.util.Vector vkeys = new java.util.Vector (); 128 java.util.Iterator iter = keys.iterator(); 129 while (iter.hasNext()) 130 vkeys.add(iter.next()); 131 return vkeys; 132 } 133 134 public Collection listAllValues(String category) 135 { 136 Collection vals = entryMap.getAllKeys(category); 138 if (vals == null || vals.isEmpty()) { 139 return new java.util.Vector (); 140 } 141 java.util.Vector vvals = new java.util.Vector (); 142 java.util.Iterator iter = vals.iterator(); 143 while (iter.hasNext()) 144 vvals.add(iter.next()); 145 return vvals; 146 } 147 148 150 public void flush() 151 { 152 Collection keys = entryMap.getAllKeys(category); 153 if(keys == null ) return; 154 Iterator iter = keys.iterator(); 156 while( iter.hasNext() ) 157 { 158 Serializable key = (Serializable ) iter.next(); 159 try 160 { 161 entryMap.remove(category, key); 162 } 163 catch(Exception e) 164 { 165 log.debug("Failed to remove: "+key, e); 166 } 167 } 168 } 169 170 public int size() 171 { 172 return entryMap.getAllKeys(category).size(); 173 } 174 175 public void valueHasChanged(String category, Serializable key, 176 Serializable value, boolean locallyModified) 177 { 178 NotifyData data = new NotifyData(); 179 data.category = category; 180 data.key = key; 181 data.value = value; 182 data.locallyModified = locallyModified; 183 String address = System.getProperty("jboss.bind.address"); 184 long id = nextSequence(); 185 Notification msg = new Notification ("valueHasChanged", this, id, address); 186 msg.setUserData(data); 187 log.debug("valueHasChanged, "+msg); 188 super.sendNotification(msg); 189 } 190 191 public void keyHasBeenRemoved(String category, Serializable key, 192 Serializable previousContent, boolean locallyModified) 193 { 194 NotifyData data = new NotifyData(); 195 data.category = category; 196 data.key = key; 197 data.value = previousContent; 198 data.locallyModified = locallyModified; 199 String address = System.getProperty("jboss.bind.address"); 200 long id = nextSequence(); 201 Notification msg = new Notification ("keyHasBeenRemoved", this, id, address); 202 msg.setUserData(data); 203 log.debug("keyHasBeenRemoved, "+msg); 204 super.sendNotification(msg); 205 } 206 207 private synchronized long nextSequence() 208 { 209 return sequence ++; 210 } 211 } 212 | Popular Tags |