Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 275   Methods: 12
NCLOC: 186   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractCacheLoader.java 75% 88.6% 100% 87.1%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 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.CacheException;
 12    import org.jboss.cache.CacheSPI;
 13    import org.jboss.cache.Fqn;
 14    import org.jboss.cache.Modification;
 15    import org.jboss.cache.RegionManager;
 16    import org.jboss.cache.buddyreplication.BuddyManager;
 17    import org.jboss.cache.marshall.Marshaller;
 18    import org.jboss.cache.marshall.NodeData;
 19    import org.jboss.cache.marshall.NodeDataExceptionMarker;
 20    import org.jboss.cache.marshall.NodeDataMarker;
 21    import org.jboss.cache.util.MapCopy;
 22   
 23    import java.io.ObjectInputStream;
 24    import java.io.ObjectOutputStream;
 25    import java.util.LinkedList;
 26    import java.util.List;
 27    import java.util.Map;
 28    import java.util.Set;
 29   
 30    /**
 31    * A convenience abstract implementation of a {@link CacheLoader}. Specific methods to note are methods like
 32    * {@link #storeState(org.jboss.cache.Fqn,java.io.ObjectInputStream)}, {@link #loadState(org.jboss.cache.Fqn,java.io.ObjectOutputStream)},
 33    * {@link #storeEntireState(java.io.ObjectInputStream)} and {@link #loadEntireState(java.io.ObjectOutputStream)} which have convenience
 34    * implementations here.
 35    * <p/>
 36    * Also useful to note is the implementation of {@link #put(java.util.List)}, used during the prepare phase of a transaction.
 37    * <p/>
 38    *
 39    * @author <a href="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
 40    * @author <a href="mailto:galder.zamarreno@jboss.com">Galder Zamarreno</a>
 41    * @since 2.0.0
 42    */
 43    public abstract class AbstractCacheLoader implements CacheLoader
 44    {
 45    protected CacheSPI cache;
 46    protected RegionManager regionManager;
 47    private static final Log log = LogFactory.getLog(AbstractCacheLoader.class);
 48   
 49  55 public void put(Fqn fqn, Map<Object, Object> attributes, boolean erase) throws Exception
 50    {
 51  55 if (erase)
 52    {
 53  55 removeData(fqn);
 54    }
 55   
 56    // JBCACHE-769 -- make a defensive copy
 57  55 Map<Object, Object> attrs = (attributes == null ? null : new MapCopy<Object, Object>(attributes));
 58  55 put(fqn, attrs);
 59    }
 60   
 61  60 public void storeEntireState(ObjectInputStream is) throws Exception
 62    {
 63  60 storeState(Fqn.ROOT, is);
 64    }
 65   
 66  128 public void storeState(Fqn subtree, ObjectInputStream in) throws Exception
 67    {
 68    // ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
 69    // try
 70    // {
 71    // // Set the TCCL to any classloader registered for subtree
 72    // setUnmarshallingClassLoader(subtree);
 73   
 74    // remove entire existing state
 75  128 this.remove(subtree);
 76   
 77  128 boolean moveToBuddy = subtree.isChildOf(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN) && subtree.size() > 1;
 78   
 79    // store new state
 80  128 Fqn fqn = null;
 81    //NodeData nd = null;
 82  128 Object objectFromStream = cache.getMarshaller().objectFromObjectStream(in);
 83  128 if (objectFromStream instanceof NodeDataMarker)
 84    {
 85    // no persistent state sent across; return?
 86  41 return;
 87    }
 88  87 List nodeData = (List) objectFromStream;
 89   
 90    //for (nd = (NodeData) in.readObject(); nd != null && !nd.isMarker(); nd = (NodeData) in.readObject())
 91  85 for (Object o : nodeData)
 92    {
 93  240 NodeData nd = (NodeData) o;
 94  0 if (nd.isMarker()) break;
 95   
 96  240 if (nd.isExceptionMarker())
 97    {
 98  0 NodeDataExceptionMarker ndem = (NodeDataExceptionMarker) nd;
 99  0 throw new CacheException("State provider cacheloader at node " + ndem.getCacheNodeIdentity()
 100    + " threw exception during loadState (see Caused by)", ndem.getCause());
 101    }
 102   
 103  240 if (moveToBuddy)
 104    {
 105  26 fqn = BuddyManager.getBackupFqn(subtree, nd.getFqn());
 106    }
 107    else
 108    {
 109  214 fqn = nd.getFqn();
 110    }
 111   
 112  240 if (nd.getAttributes() != null)
 113    {
 114  141 this.put(fqn, nd.getAttributes(), true);// creates a node with 0 or more attributes
 115    }
 116    else
 117    {
 118  99 this.put(fqn, null);// creates a node with null attributes
 119    }
 120    }
 121   
 122    // read marker off stack
 123    // cache.getMarshaller().objectFromObjectStream(in);
 124    // }
 125    // finally
 126    // {
 127    // Thread.currentThread().setContextClassLoader(currentCL);
 128    // }
 129    }
 130   
 131  94 public void loadEntireState(ObjectOutputStream os) throws Exception
 132    {
 133  94 loadState(Fqn.ROOT, os);
 134    }
 135   
 136  125 public void loadState(Fqn subtree, ObjectOutputStream os) throws Exception
 137    {
 138    // ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
 139    // try
 140    // {
 141    // Set the TCCL to any classloader registered for subtree
 142    // setUnmarshallingClassLoader(subtree);
 143  125 loadStateHelper(subtree, os);
 144    // }
 145    // finally
 146    // {
 147    // Thread.currentThread().setContextClassLoader(currentCL);
 148    // }
 149    }
 150   
 151   
 152  1191 public void setCache(CacheSPI c)
 153    {
 154  1191 this.cache = c;
 155    }
 156   
 157  17 public void setRegionManager(RegionManager regionManager)
 158    {
 159  17 this.regionManager = regionManager;
 160    }
 161   
 162    /**
 163    * Do a preorder traversal: visit the node first, then the node's children
 164    *
 165    * @param fqn Start node
 166    * @param out
 167    * @throws Exception
 168    */
 169  125 protected void loadStateHelper(Fqn fqn, ObjectOutputStream out) throws Exception
 170    {
 171  125 List<NodeData> list = new LinkedList<NodeData>();
 172  125 getNodeDataList(fqn, list);
 173  0 if (log.isTraceEnabled()) log.trace("Loading state of " + list.size() + " nodes into stream");
 174  125 cache.getMarshaller().objectToObjectStream(list, out, fqn);
 175    }
 176   
 177  237 protected void getNodeDataList(Fqn fqn, List<NodeData> list) throws Exception
 178    {
 179  237 Map<Object, Object> attrs;
 180  237 Set<? extends Object> children_names;
 181  237 String child_name;
 182  237 Fqn tmp_fqn;
 183  237 NodeData nd;
 184   
 185    // first handle the current node
 186  237 attrs = get(fqn);
 187  237 if (attrs == null || attrs.size() == 0)
 188    {
 189  118 nd = new NodeData(fqn);
 190    }
 191    else
 192    {
 193  119 nd = new NodeData(fqn, attrs);
 194    }
 195    //out.writeObject(nd);
 196  237 list.add(nd);
 197   
 198    // then visit the children
 199  237 children_names = getChildrenNames(fqn);
 200  237 if (children_names == null)
 201    {
 202  152 return;
 203    }
 204  85 for (Object children_name : children_names)
 205    {
 206  136 child_name = (String) children_name;
 207  136 tmp_fqn = new Fqn(fqn, child_name);
 208    //loadStateHelper(tmp_fqn, out);
 209  136 getNodeDataList(tmp_fqn, list);
 210    }
 211    }
 212   
 213  801 public void put(List<Modification> modifications) throws Exception
 214    {
 215  801 for (Modification m : modifications)
 216    {
 217  1135 switch (m.getType())
 218    {
 219  607 case PUT_DATA:
 220  607 put(m.getFqn(), m.getData());
 221  607 break;
 222  0 case PUT_DATA_ERASE:
 223  0 removeData(m.getFqn());
 224  0 put(m.getFqn(), m.getData());
 225  0 break;
 226  414 case PUT_KEY_VALUE:
 227  414 put(m.getFqn(), m.getKey(), m.getValue());
 228  414 break;
 229  24 case REMOVE_DATA:
 230  24 removeData(m.getFqn());
 231  24 break;
 232  25 case REMOVE_KEY_VALUE:
 233  25 remove(m.getFqn(), m.getKey());
 234  25 break;
 235  59 case REMOVE_NODE:
 236  59 remove(m.getFqn());
 237  59 break;
 238  6 case MOVE:
 239    // involve moving all children too,
 240  6 _move(m.getFqn(), m.getFqn2());
 241  6 break;
 242  0 default:
 243  0 throw new CacheException("Unknown modificatiobn " + m.getType());
 244    }
 245    }
 246    }
 247   
 248  12 private void _move(Fqn fqn, Fqn parent) throws Exception
 249    {
 250  12 Object name = fqn.getLastElement();
 251  12 Fqn newFqn = new Fqn(parent, name);
 252   
 253    // start deep.
 254  12 Set childrenNames = getChildrenNames(fqn);
 255  12 if (childrenNames != null)
 256    {
 257  6 for (Object c : childrenNames)
 258    {
 259  6 _move(new Fqn(fqn, c), newFqn);
 260    }
 261    }
 262    // get data for node.
 263  12 Map<Object, Object> data = get(fqn);
 264  12 if (data != null)// if null, then the node never existed. Don't bother removing?
 265    {
 266  12 remove(fqn);
 267  12 put(newFqn, data);
 268    }
 269    }
 270   
 271  68989 protected Marshaller getMarshaller()
 272    {
 273  68989 return cache.getMarshaller();
 274    }
 275    }