1 7 package org.jboss.cache.loader; 8 9 import org.jboss.cache.CacheException; 10 import org.jboss.cache.CacheSPI; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.Modification; 13 import org.jboss.cache.RegionManager; 14 import org.jboss.cache.buddyreplication.BuddyManager; 15 import org.jboss.cache.util.MapCopy; 16 17 import java.io.ObjectInputStream ; 18 import java.io.ObjectOutputStream ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 29 public abstract class AbstractCacheLoader implements CacheLoader 30 { 31 protected CacheSPI cache; 32 protected RegionManager regionManager; 33 34 public void put(Fqn fqn, Map <Object , Object > attributes, boolean erase) throws Exception 35 { 36 if (erase) 37 { 38 removeData(fqn); 39 } 40 41 Map <Object , Object > attrs = (attributes == null ? null : new MapCopy<Object , Object >(attributes)); 43 put(fqn, attrs); 44 } 45 46 public void storeEntireState(ObjectInputStream is) throws Exception 47 { 48 storeState(Fqn.ROOT, is); 49 } 50 51 public void storeState(Fqn subtree, ObjectInputStream in) throws Exception 52 { 53 ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); 54 try 55 { 56 setUnmarshallingClassLoader(subtree); 58 59 this.remove(subtree); 61 62 boolean moveToBuddy = subtree.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN) && subtree.size() > 1; 63 64 Fqn fqn = null; 66 NodeData nd = null; 67 68 for (nd = (NodeData) in.readObject(); nd != null && !nd.isMarker(); nd = (NodeData) in.readObject()) 69 { 70 if (nd.isExceptionMarker()) 71 { 72 NodeDataExceptionMarker ndem = (NodeDataExceptionMarker) nd; 73 throw new CacheException("State provider cacheloader at node " + ndem.getCacheNodeIdentity() 74 + " threw exception during loadState (see Caused by)", ndem.getCause()); 75 } 76 77 if (moveToBuddy) 78 { 79 fqn = BuddyManager.getBackupFqn(subtree, nd.fqn); 80 } 81 else 82 { 83 fqn = nd.fqn; 84 } 85 86 if (nd.attrs != null) 87 { 88 this.put(fqn, nd.attrs, true); } 90 else 91 { 92 this.put(fqn, null); } 94 } 95 } 96 finally 97 { 98 Thread.currentThread().setContextClassLoader(currentCL); 99 } 100 } 101 102 public void loadEntireState(ObjectOutputStream os) throws Exception 103 { 104 loadState(Fqn.ROOT, os); 105 } 106 107 public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception 108 { 109 ClassLoader currentCL = Thread.currentThread().getContextClassLoader(); 110 try 111 { 112 setUnmarshallingClassLoader(subtree); 114 loadStateHelper(subtree, os); 115 } 116 finally 117 { 118 Thread.currentThread().setContextClassLoader(currentCL); 119 } 120 } 121 122 123 public void setCache(CacheSPI c) 124 { 125 this.cache = c; 126 } 127 128 public void setRegionManager(RegionManager regionManager) 129 { 130 this.regionManager = regionManager; 131 } 132 133 139 protected void setUnmarshallingClassLoader(Fqn subtree) 140 { 141 if (regionManager != null) 142 { 143 regionManager.setContextClassLoaderAsCurrent(subtree); 144 } 145 } 146 147 154 protected void loadStateHelper(Fqn fqn, ObjectOutputStream out) throws Exception 155 { 156 Map <Object , Object > attrs; 157 Set <? extends Object > children_names; 158 String child_name; 159 Fqn tmp_fqn; 160 NodeData nd; 161 162 attrs = get(fqn); 164 if (attrs == null || attrs.size() == 0) 165 { 166 nd = new NodeData(fqn); 167 } 168 else 169 { 170 nd = new NodeData(fqn, attrs); 171 } 172 out.writeObject(nd); 173 174 children_names = getChildrenNames(fqn); 176 if (children_names == null) 177 { 178 return; 179 } 180 for (Object children_name : children_names) 181 { 182 child_name = (String ) children_name; 183 tmp_fqn = new Fqn(fqn, child_name); 184 loadStateHelper(tmp_fqn, out); 185 } 186 } 187 188 public void put(List <Modification> modifications) throws Exception 189 { 190 for (Modification m : modifications) 191 { 192 switch (m.getType()) 193 { 194 case PUT_DATA: 195 put(m.getFqn(), m.getData()); 196 break; 197 case PUT_DATA_ERASE: 198 removeData(m.getFqn()); 199 put(m.getFqn(), m.getData()); 200 break; 201 case PUT_KEY_VALUE: 202 put(m.getFqn(), m.getKey(), m.getValue()); 203 break; 204 case REMOVE_DATA: 205 removeData(m.getFqn()); 206 break; 207 case REMOVE_KEY_VALUE: 208 remove(m.getFqn(), m.getKey()); 209 break; 210 case REMOVE_NODE: 211 remove(m.getFqn()); 212 break; 213 case MOVE: 214 _move(m.getFqn(), m.getFqn2()); 216 break; 217 default: 218 throw new CacheException("Unknown modificatiobn " + m.getType()); 219 } 220 } 221 } 222 223 private void _move(Fqn fqn, Fqn parent) throws Exception 224 { 225 Object name = fqn.getLastElement(); 226 Fqn newFqn = new Fqn(parent, name); 227 228 for (Object c : getChildrenNames(fqn)) 230 { 231 _move(new Fqn(fqn, c), newFqn); 232 } 233 Map <Object , Object > data = get(fqn); 235 if (data != null) { 237 remove(fqn); 238 put(newFqn, data); 239 } 240 } 241 242 } 243 | Popular Tags |