1 7 package org.jboss.cache.loader; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.Modification; 13 import org.jboss.cache.config.CacheLoaderConfig.IndividualCacheLoaderConfig; 14 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 26 public class DummyInMemoryCacheLoader extends AbstractCacheLoader 27 { 28 private Map <Fqn, Node> nodes = new HashMap <Fqn, Node>(); 29 private Log log = LogFactory.getLog(DummyInMemoryCacheLoader.class); 30 private Map <Object , List <Modification>> transactions = new HashMap <Object , List <Modification>>(); 31 32 public void setConfig(IndividualCacheLoaderConfig config) 33 { 34 } 35 36 public IndividualCacheLoaderConfig getConfig() 37 { 38 return null; 39 } 40 41 public Set getChildrenNames(Fqn fqn) throws Exception 42 { 43 log.debug("Calling getChildrenNames on Fqn " + fqn); 44 if (!nodes.containsKey(fqn)) 45 { 46 log.debug("node not in loader"); 47 return null; 48 } 49 50 Set children = findChildren(fqn); 51 log.debug("Fqn " + fqn + " has children " + children); 52 return children; 53 } 54 55 private Set findChildren(Fqn p) 56 { 57 Set c = new HashSet (); 58 for (Fqn f : nodes.keySet()) 59 { 60 if (f.getParent().equals(p)) 61 { 62 c.add(f.getLastElement()); 63 } 64 } 65 return c; 66 } 67 68 public Map get(Fqn name) throws Exception 69 { 70 Map d = nodes.containsKey(name) ? nodes.get(name).data : null; 71 log.debug("Getting data for fqn " + name + " = " + d); 72 return d; 73 } 74 75 public boolean exists(Fqn name) throws Exception 76 { 77 return nodes.containsKey(name); 78 } 79 80 public Object put(Fqn name, Object key, Object value) throws Exception 81 { 82 Node n = nodes.get(name); 83 if (n == null) 84 { 85 n = new Node(); 86 n.fqn = name; 87 } 88 Object old = n.data.put(key, value); 89 nodes.put(name, n); 90 log.debug("Did a put on " + name + ", data is " + n.data); 91 return old; 92 } 93 94 public void put(Fqn name, Map attributes) throws Exception 95 { 96 Node n = nodes.get(name); 97 if (n == null) 98 { 99 n = new Node(); 100 n.fqn = name; 101 } 102 if (attributes != null) n.data.putAll(attributes); 103 nodes.put(name, n); 104 log.debug("Did a put on " + name + ", data is " + n.data); 105 } 106 107 public Object remove(Fqn fqn, Object key) throws Exception 108 { 109 log.debug("Removing data from " + fqn); 110 Node n = nodes.get(fqn); 111 if (n == null) n = new Node(); 112 n.fqn = fqn; 113 Object old = n.data.remove(key); 114 nodes.put(fqn, n); 115 return old; 116 } 117 118 public void remove(Fqn fqn) throws Exception 119 { 120 log.debug("Removing fqn " + fqn); 121 nodes.remove(fqn); 122 } 123 124 public void removeData(Fqn fqn) throws Exception 125 { 126 log.debug("Removing data from " + fqn); 127 Node n = nodes.get(fqn); 128 if (n == null) n = new Node(); 129 n.fqn = fqn; 130 n.data.clear(); 131 nodes.put(fqn, n); 132 } 133 134 135 public void prepare(Object tx, List <Modification> modifications, boolean one_phase) throws Exception 136 { 137 if (one_phase) 138 { 139 put(modifications); 140 } 141 else 142 { 143 transactions.put(tx, modifications); 144 } 145 } 146 147 public void commit(Object tx) throws Exception 148 { 149 List <Modification> modifications = transactions.remove(tx); 150 if (modifications == null) 151 { 152 throw new Exception ("transaction " + tx + " not found in transaction table"); 153 } 154 put(modifications); 155 } 156 157 public void rollback(Object tx) 158 { 159 transactions.remove(tx); 160 } 161 162 public void create() throws Exception 163 { 164 } 165 166 public void start() throws Exception 167 { 168 } 169 170 public void stop() 171 { 172 } 173 174 public void destroy() 175 { 176 } 177 178 179 public class Node 180 { 181 Map data = new HashMap (); 182 Fqn fqn; 183 184 185 public String toString() 186 { 187 return "Node{" + 188 "data=" + data + 189 ", fqn=" + fqn + 190 '}'; 191 } 192 } 193 194 195 public String toString() 196 { 197 return "DummyInMemoryCacheLoader{" + 198 "nodes=" + nodes + 199 '}'; 200 } 201 } 202 | Popular Tags |