Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 373   Methods: 38
NCLOC: 259   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InternalHelper.java 54.8% 52.6% 50% 52.6%
coverage coverage
 1    /*
 2    * JBoss, the OpenSource J2EE webOS
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache.pojo.impl;
 8   
 9    import java.util.Map;
 10   
 11    import org.apache.commons.logging.Log;
 12    import org.apache.commons.logging.LogFactory;
 13    import org.jboss.cache.Cache;
 14    import org.jboss.cache.CacheException;
 15    import org.jboss.cache.Fqn;
 16    import org.jboss.cache.pojo.PojoCacheException;
 17    import org.jboss.cache.pojo.util.ObjectUtil;
 18   
 19    /**
 20    * Internal helper class to handle internal cache sotre, that is, the portion that is not part of
 21    * user's data.
 22    *
 23    * @author Ben Wang
 24    */
 25    public class InternalHelper
 26    {
 27    private static Log log = LogFactory.getLog(InternalHelper.class.getName());
 28   
 29    private Cache cache;
 30   
 31  499 InternalHelper(Cache cache)
 32    {
 33  499 this.cache = cache;
 34    }
 35   
 36  44496 PojoInstance getPojoInstance(Fqn fqn) throws CacheException
 37    {
 38  44496 return (PojoInstance) get(fqn, PojoInstance.KEY, true);
 39    }
 40   
 41  61418 PojoReference getPojoReference(Fqn fqn, String field) throws CacheException
 42    {
 43  61418 if (field == null)
 44  38070 field = PojoReference.KEY;
 45   
 46  61418 return (PojoReference) get(fqn, field, true);
 47    }
 48   
 49  0 PojoReference getPojoReference(Fqn fqn) throws CacheException
 50    {
 51  0 return getPojoReference(fqn, null);
 52    }
 53   
 54   
 55  3942 static PojoInstance initializeAopInstance(Fqn sourceFqn)
 56    {
 57  3942 PojoInstance pojoInstance = new PojoInstance();
 58   
 59  3942 pojoInstance.incrementRefCount(sourceFqn);
 60  3942 return pojoInstance;
 61    }
 62   
 63    /**
 64    * Increment reference count for the pojo. Note that this is not thread safe or atomic.
 65    */
 66  192 int incrementRefCount(Fqn originalFqn, Fqn referencingFqn) throws CacheException
 67    {
 68  192 PojoInstance pojoInstance = getPojoInstance(originalFqn);
 69  192 if (pojoInstance == null)
 70  0 throw new PojoCacheException("InternalDelegate.incrementRefCount(): null pojoReference for fqn: " + originalFqn);
 71   
 72  192 int count = pojoInstance.incrementRefCount(referencingFqn);
 73    // need to update it.
 74  192 put(originalFqn, PojoInstance.KEY, pojoInstance);
 75  192 return count;
 76    }
 77   
 78    /**
 79    * Has a delegate method so we can use the switch.
 80    */
 81   
 82  61 Object get(Fqn fqn, Object key) throws CacheException
 83    {
 84  61 return get(fqn, key, false);
 85    }
 86   
 87  105975 private Object get(Fqn fqn, Object key, boolean gravitate) throws CacheException
 88    {
 89  105975 if (gravitate)
 90    {
 91  105914 cache.getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
 92  105914 Object obj = cache.get(fqn, key);
 93  105909 cache.getInvocationContext().getOptionOverrides().setForceDataGravitation(false);
 94  105909 return obj;
 95    }
 96   
 97  61 return cache.get(fqn, key);
 98    }
 99   
 100  7860 private void put(Fqn fqn, Object key, Object value) throws CacheException
 101    {
 102  7860 cache.put(fqn, key, value);
 103    }
 104   
 105  3497 void put(Fqn fqn, Map map) throws CacheException
 106    {
 107  3497 cache.put(fqn, map);
 108    }
 109   
 110   
 111    /**
 112    * decrement reference count for the pojo. Note that this is not thread safe or atomic.
 113    */
 114  38 int decrementRefCount(Fqn originalFqn, Fqn referencingFqn) throws CacheException
 115    {
 116  38 PojoInstance pojoInstance = getPojoInstance(originalFqn);
 117  38 if (pojoInstance == null)
 118  0 throw new PojoCacheException("InternalDelegate.decrementRefCount(): null pojoReference.");
 119   
 120  38 int count = pojoInstance.decrementRefCount(referencingFqn);
 121   
 122  38 if (count < -1) // can't dip below -1
 123  0 throw new PojoCacheException("InternalDelegate.decrementRefCount(): null pojoReference.");
 124   
 125    // need to update it.
 126  38 put(originalFqn, PojoInstance.KEY, pojoInstance);
 127  38 return count;
 128    }
 129   
 130  0 static boolean isReferenced(PojoInstance pojoInstance)
 131    {
 132    // If ref counter is greater than 0, we fqn is being referenced.
 133  0 return (pojoInstance.getRefCount() > 0);
 134    }
 135   
 136  0 int getRefCount(Fqn fqn) throws CacheException
 137    {
 138  0 return getPojoInstance(fqn).getRefCount();
 139    }
 140   
 141  0 String XgetRefFqn(Fqn fqn) throws CacheException
 142    {
 143  0 PojoInstance pojoInstance = getPojoInstance(fqn);
 144  0 return getRefFqn(pojoInstance);
 145    }
 146   
 147  0 String getRefFqn(PojoInstance pojoInstance) throws CacheException
 148    {
 149  0 if (pojoInstance == null)
 150  0 return null;
 151   
 152  0 String aliasFqn = pojoInstance.getInternalFqn();
 153   
 154  0 if (aliasFqn == null || aliasFqn.length() == 0) return null;
 155   
 156  0 return getRefFqnFromAlias(aliasFqn);
 157    }
 158   
 159  0 void setRefFqn(Fqn fqn, String internalFqn) throws CacheException
 160    {
 161  0 PojoInstance pojoInstance = getPojoInstance(fqn);
 162  0 if (pojoInstance == null)
 163  0 pojoInstance = new PojoInstance();
 164   
 165  0 pojoInstance.setInternalFqn(internalFqn);
 166  0 put(fqn, PojoInstance.KEY, pojoInstance);
 167    }
 168   
 169  0 void removeRefFqn(Fqn fqn) throws CacheException
 170    {
 171  0 PojoInstance pojoInstance = getPojoInstance(fqn);
 172  0 if (pojoInstance == null)
 173  0 throw new PojoCacheException("InternalDelegate.getInternalFqn(): null pojoReference.");
 174   
 175  0 pojoInstance.removeInternalFqn();
 176  0 put(fqn, PojoInstance.KEY, pojoInstance);
 177    }
 178   
 179  43301 Object getPojo(Fqn fqn, String field) throws CacheException
 180    {
 181  43301 PojoReference pojoReference = getPojoReference(fqn, field);
 182  43301 Fqn realFqn = null;
 183  43301 if (pojoReference != null)
 184    {
 185    // This is outward facing node
 186  10948 realFqn = pojoReference.getFqn();
 187    }
 188    else
 189    {
 190    // If we are looking for a field then there must be a reference
 191  32353 if (field != null)
 192  10935 return null;
 193   
 194    // This is the internal node.
 195  21418 realFqn = fqn;
 196    }
 197   
 198  32366 PojoInstance pojoInstance = getPojoInstance(realFqn);
 199  32361 if (pojoInstance == null)
 200  14929 return null;
 201   
 202  17432 return pojoInstance.get();
 203    }
 204   
 205  0 void setPojo(Fqn fqn, Object pojo) throws CacheException
 206    {
 207  0 PojoInstance pojoInstance = getPojoInstance(fqn);
 208  0 if (pojoInstance == null)
 209    {
 210  0 pojoInstance = new PojoInstance();
 211  0 put(fqn, PojoInstance.KEY, pojoInstance);
 212    }
 213   
 214  0 pojoInstance.set(pojo);
 215    // No need to do a cache put since pojo is transient anyway.
 216    }
 217   
 218  6316 static boolean isMultipleReferenced(PojoInstance pojoInstance)
 219    {
 220  38 if (pojoInstance.getRefCount() > (PojoInstance.INITIAL_COUNTER_VALUE + 1)) return true;
 221   
 222  6278 return false;
 223    }
 224   
 225  2590 static void setPojo(PojoInstance pojoInstance, Object pojo)
 226    {
 227    // No need to do a cache put since pojo is transient anyway.
 228  2590 pojoInstance.set(pojo);
 229    }
 230   
 231  0 void setPojo(Fqn fqn, Object pojo, PojoInstance pojoInstance) throws CacheException
 232    {
 233  0 if (pojoInstance == null)
 234    {
 235  0 pojoInstance = new PojoInstance();
 236  0 put(fqn, PojoInstance.KEY, pojoInstance);
 237    }
 238   
 239  0 pojoInstance.set(pojo);
 240    // No need to do a cache put since pojo is transient anyway.
 241    }
 242   
 243  0 void putPojoReference(Fqn fqn, PojoReference pojoReference) throws CacheException
 244    {
 245  0 putPojoReference(fqn, pojoReference, PojoReference.KEY);
 246    }
 247   
 248  7630 void putPojoReference(Fqn fqn, PojoReference pojoReference, String field) throws CacheException
 249    {
 250  7630 if (field == null)
 251  5256 field = PojoReference.KEY;
 252   
 253  7630 put(fqn, field, pojoReference);
 254    }
 255   
 256  0 void putAopClazz(Fqn fqn, Class clazz) throws CacheException
 257    {
 258  0 put(fqn, InternalConstant.CLASS_INTERNAL, clazz);
 259    }
 260   
 261    /**
 262    * We store the class name in string and put it in map instead of directly putting
 263    * it into cache for optimization.
 264    */
 265  0 static void putAopClazz(Class clazz, Map map)
 266    {
 267  0 map.put(InternalConstant.CLASS_INTERNAL, clazz);
 268    }
 269   
 270  0 Class peekAopClazz(Fqn fqn) throws CacheException
 271    {
 272  0 return (Class) get(fqn, InternalConstant.CLASS_INTERNAL);
 273    }
 274   
 275  0 void removeInternalAttributes(Fqn fqn) throws CacheException
 276    {
 277  0 cache.remove(fqn, PojoInstance.KEY);
 278  0 cache.remove(fqn, InternalConstant.CLASS_INTERNAL);
 279    }
 280   
 281  12594 void cleanUp(Fqn fqn, String field) throws CacheException
 282    {
 283  12594 if (field != null)
 284    {
 285  2032 cache.remove(fqn, field);
 286  2032 return;
 287    }
 288   
 289    // We can't do a brute force remove anymore?
 290  10562 if (cache.getRoot().getChild(fqn).getChildren().size() == 0)
 291    {
 292    // remove everything
 293  10562 cache.removeNode(fqn);
 294    // cache_.getRoot().getChild(fqn).clearData();
 295    // removeNodeWithoutInterceptor(fqn);
 296    }
 297    else
 298    {
 299    // Assume everything here is all PojoCache data for optimization
 300  0 cache.getRoot().getChild(fqn).clearData();
 301  0 if (log.isTraceEnabled())
 302    {
 303  0 log.trace("cleanup(): fqn: " + fqn + " is not empty. That means it has sub-pojos. Will not remove node");
 304    }
 305    }
 306    }
 307   
 308  0 String createIndirectFqn(String fqn) throws CacheException
 309    {
 310  0 String indirectFqn = getIndirectFqn(fqn);
 311  0 Fqn internalFqn = getInternalFqn(fqn);
 312  0 put(internalFqn, indirectFqn, fqn);
 313  0 return indirectFqn;
 314    }
 315   
 316  34 private Fqn getInternalFqn(String fqn)
 317    {
 318  34 if (fqn == null || fqn.length() == 0)
 319  0 throw new IllegalStateException("InternalDelegate.getInternalFqn(). fqn is either null or empty!");
 320   
 321  34 String indirectFqn = getIndirectFqn(fqn);
 322  34 return new Fqn(InternalConstant.JBOSS_INTERNAL_MAP, indirectFqn);
 323    // return JBOSS_INTERNAL_MAP;
 324    }
 325   
 326  68 static String getIndirectFqn(String fqn)
 327    {
 328    // TODO This is not unique. Will need to come up with a better one in the future.
 329  68 return ObjectUtil.getIndirectFqn(fqn);
 330    }
 331   
 332  34 void removeIndirectFqn(String oldFqn) throws CacheException
 333    {
 334  34 String indirectFqn = getIndirectFqn(oldFqn);
 335  34 cache.remove(getInternalFqn(oldFqn), indirectFqn);
 336    }
 337   
 338  0 void setIndirectFqn(String oldFqn, String newFqn) throws CacheException
 339    {
 340  0 String indirectFqn = getIndirectFqn(oldFqn);
 341  0 Fqn tmpFqn = getInternalFqn(oldFqn);
 342  0 put(tmpFqn, indirectFqn, newFqn);
 343    }
 344   
 345  0 void updateIndirectFqn(Fqn originalFqn, Fqn newFqn) throws CacheException
 346    {
 347  0 put(getInternalFqn(originalFqn.toString()), getIndirectFqn(originalFqn.toString()), newFqn.toString());
 348    }
 349   
 350  0 private String getRefFqnFromAlias(String aliasFqn) throws CacheException
 351    {
 352  0 return (String) get(getInternalFqn(aliasFqn), aliasFqn, true);
 353    }
 354   
 355  0 Fqn getNextFqnInLine(Fqn currentFqn) throws CacheException
 356    {
 357  0 PojoInstance ai = getPojoInstance(currentFqn);
 358  0 return ai.getAndRemoveFirstFqnInList();
 359    }
 360   
 361    /**
 362    * Test if this internal node.
 363    *
 364    * @param fqn
 365    */
 366  36 public static boolean isInternalNode(Fqn fqn)
 367    {
 368    // we ignore all the node events corresponding to JBOSS_INTERNAL
 369  5 if (fqn.isChildOrEquals(InternalConstant.JBOSS_INTERNAL)) return true;
 370   
 371  31 return false;
 372    }
 373    }